Arduino Uno Temperature Sensor

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!

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];


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(" Celsius, ");

Serial.print(" fahrenheit -> ");

Serial.print(" Max, ");
Serial.println(" Min");

tempc = 0;

delay(1000); // delay before loop

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

if(tempc > 25)

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.


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

Thank you,

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.

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:

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