Go Down

Topic: Arduino Uno Temperature Sensor (Read 6398 times) previous topic - next topic

jasonski

Nov 15, 2011, 11:04 pm Last Edit: Nov 17, 2011, 01:12 am by jasonski Reason: 1
Hi, I have very little experience in programming. I am attempting to use an Arduino Uno with an LM34 Temperature Sensor to control a solenoid valve. When the sensor reaches a set temperature I want the Arduino to open the valve to release water. So far I have a program that reads temperature (originally created to show the values on an LCD screen). So, all I really need to know is how I can modify this program to read the temperature and open the valve when the temperature reaches a set value. The program I have so far is below, I would really appreciate any help you may offer. Thanks!

Code: [Select]
int pin = 0; // analog pin
int tempc = 0,tempf=0; // temperature variables
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;

void setup()
{
 Serial.begin(9600); // start serial communication
}

void loop()
{


for(i = 0;i< =7;i++){ // gets 8 samples of temperature

 samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
 tempc = tempc + samples[i];
 delay(1000);

}

tempc = tempc/8.0; // better precision
tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit

if(tempc > maxi) {maxi = tempc;} // set max temperature
if(tempc < mini) {mini = tempc;} // set min temperature

Serial.print(tempc,DEC);
Serial.print(" Celsius, ");

Serial.print(tempf,DEC);
Serial.print(" fahrenheit -> ");

Serial.print(maxi,DEC);
Serial.print(" Max, ");
Serial.print(mini,DEC);
Serial.println(" Min");

tempc = 0;

delay(1000); // delay before loop
}

wildbill

At its simplest, all you need to do is to use digitalwrite so you can say somewhere in loop:
Code: [Select]

if(tempc > 25)
  {
  digitalwrite(solenoidpin,HIGH);
  }
else
  {
  digitalwrite(solenoidpin,LOW);
  }


Look at the blink example to see use of digitalwrite and the necessary predecessor pinmode. Don't forget though that your solenoid will likely require more power than an arduino pin can provide - you will need additional circuitry and a power supply to control it. You can test your code though by using a led - don't forget the current limiting resisitor.

robtillaart

@jasonski,

Please modify your post, select the code and press the # button to get a nice formatted look ...

Thank you,
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

jasonski

Thanks for the help, WildBill, that helped a lot. I modified and inserted that code into my own. Now, when I get the sensor below 67o F, the diode lights up and turns off when the temperature is raised. However, when I attempted to connect it to the solenoid circuit, it does not open the solenoid. I believe that the solenoid is just not receiving enough current to power it. I am going to try a few things tomorrow to correct this problem. Thanks for all your help, it was much appreciated, any more advice from anyone else would also be appreciated. Also, robtillaart I modified my post, I apologize as I am knew to this site and didn't know, thanks for informing me of this method.

wildbill

As you observe, the Arduino can't source enough current to drive your solenoid, indeed trying it may damage the arduino. There is an example in the playground that shows how such a thing can be attached: http://arduino.cc/playground/uploads/Learning/solenoid_driver.pdf

cantrade

You might need an external power source and an interposing relay to switch the proper voltage that your solenoid requires.   :)

Go Up