 # LM35 temp sensor controlling LEDs Help!

Hi everyone! Im new to the forum, arduino and coding! And this is my first post I am trying to use arduino uno and an LM35 temperature sensor to control three LEDs (blue, green and red). What i want to happen is when the temp is between 0 and 35 degrees Celsius the blue led will glow (all other leds will be off) , when the temp is between 35 and 40 degrees the green led will glow (all other leds will be off) and when above 40 degrees the red led will glow(all other leds will be off).

Help would be much appreciated!
Here is my code and ill attach an image of my circuit (on the image just pretend that P thingy is the LM35).

``````/*
temp lights LM35 and leds
*/

int tempPin = A0;
int temp;

void setup() {

pinMode (13, OUTPUT); //Blue LED symbolising Cold and Not Fit for Consumption
pinMode (8, OUTPUT); //Green LED symbolising Lukewarm and Read for Consumption
pinMode (9, OUTPUT); //Red LED symbolising Hot and Not Fit for Consumption
pinMode (tempPin, INPUT);
}

void loop() {
temp = temp * 0.48828125;

delay(1000);
if (0 < 35) //when temp is between 0-35 degrees
{
digitalWrite(13, HIGH); //blue led is on
digitalWrite(8, LOW); //red led is off
digitalWrite(9, LOW); //green led is off
}
else if (0 > 40) //when temp is above 40 degrees
{
digitalWrite(13, LOW); //blue led is off
digitalWrite(8, HIGH); //red led is on
digitalWrite(9, LOW); //green led is off
}
else //when between 35-40 degrees
{
digitalWrite(13, LOW); //blue led is off
digitalWrite(8, LOW); //red led is off
digitalWrite(9, HIGH); //green led is on
}
}
``````

Cheers, Help would be much appreciated!

What sort of help? Is there a problem?

Do you know that the gap in the middle of the bread board is an electrical gap as well, so those LEDs have one end not connected to anything?

Grumpy_Mike:

Help would be much appreciated!

What sort of help?
Is there a problem?

Do you know that the gap in the middle of the bread board is an electrical gap as well, so those LEDs have one end not connected to anything?

Hi, thanks for the quick reply.
When i upload the code to arduino only the blue led stays on even if i change the temperature to like 3 degrees Celsius, room temp at my house is 28 degrees celcius. In regards to the breadboard, i had no idea about that and thanks for telling me, but my breadboard is one of those small ones anyway so that mistake was only made on the graphical image and not in reality.

I attached another pic of the actual circuit that led is actually blue i dont know why it looks green.

but my breadboard is one of those small ones anyway so that mistake was only made on the graphical image and not in reality.

``````if (0 > 40) //when temp is above 40 degrees
``````

That if statement is saying if the value zero is greater than the value forty. Do you think zero is ever going to be greater that zero?

Maybe it needs to be

``````if (temp > 40) //when temp is above 40 degrees
``````

Maybe it needs to be

``````if (temp > 40) //when temp is above 40 degrees
``````

Yes, it works! Thank you! That was a very silly mistake from my part but the coding just blinds me. I’m sure ill get to learn coding eventually. Here is the new code if anyone wants it. I tested it by changing the temperature variables.

``````/*
temp lights LM35 and leds
*/

int tempPin = A0;
int temp;

void setup() {

pinMode (13, OUTPUT); //Blue LED symbolising Cold and Not Fit for Consumption
pinMode (8, OUTPUT); //Red LED symbolising Hot and Not Fit for Consumption
pinMode (9, OUTPUT); //Green LED symbolising Lukewarm and Read for Consumption
pinMode (tempPin, INPUT);
}

void loop() {
temp = temp * 0.48828125;

delay(1000);
if (temp < 35) //when temp is below 35 degrees
{
digitalWrite(13, HIGH); //blue led is on
digitalWrite(8, LOW); //red led is off
digitalWrite(9, LOW); //green led is off
}
else if (temp > 40) //when temp is above 40 degrees
{
digitalWrite(13, LOW); //blue led is off
digitalWrite(8, HIGH); //red led is on
digitalWrite(9, LOW); //green led is off
}
else //when between 35-40 degrees
{
digitalWrite(13, LOW); //blue led is off
digitalWrite(8, LOW); //red led is off
digitalWrite(9, HIGH); //green led is on
}
}
``````

Well done. A better way of doing that would be this:-

``````/*
temp lights LM35 and leds
*/

int tempPin = A0;
int temp;
byte redPin=8, greenPin=9, bluePin=13; // swap numbers for words

void setup() {

pinMode (bluePin, OUTPUT); //Blue LED symbolising Cold and Not Fit for Consumption
pinMode (redPin, OUTPUT); //Red LED symbolising Hot and Not Fit for Consumption
pinMode (greenPin, OUTPUT); //Green LED symbolising Lukewarm and Read for Consumption
// pinMode (tempPin, INPUT); no need for this
}

void loop() {
temp = temp * 0.48828125;

delay(1000);
if (temp < 35) lightLED(bluePin);
if (temp >= 35 && temp <= 40) lightLED(greenPin);
if (temp > 40) lightLED(redPin);
}

void lightLED(byte whatLED){
// first turn off all the LEDs
digitalWrite(bluePin, LOW); //blue led is off
digitalWrite(redPin, LOW); //red led is off
digitalWrite(greenPin, LOW); //green led is off
// now turn on the requested LED
digitalWrite(whatLED, HIGH);
}
``````

Note that numbers have been turned to names and that all the repetition has been gathered into one function.
I think you will find it reads easier as well.

Hi ishy2217,
good project but If you want to get better results you can use the INTERNAL ANALOG REFERENCE that is 1.1 v to increase the precision of your results. You can read something else about analog referece called also as AREF at this link : http://arduino.cc/en/Reference/AnalogReference?from=Reference.AREF and if you want other informations about the LM35 sensor you can read here : http://playground.arduino.cc/Main/LM35HigherResolution
I have rewritten your code in this way using the internal Aref :

``````/*
temp lights LM35 and leds
*/
const float aref = 1.1;  // internal reference is 1.1 V
int tempPin = A0;
float value; // used to read the value from LM35 sensor
float voltage;  // used to calculate the voltage
float temp;  // varable used to save the value of temperature in Celsius degrees

void setup() {

pinMode (13, OUTPUT); //Blue LED symbolising Cold and Not Fit for Consumption
pinMode (8, OUTPUT); //Red LED symbolising Hot and Not Fit for Consumption
pinMode (9, OUTPUT); //Green LED symbolising Lukewarm and Read for Consumption
pinMode (tempPin, INPUT);  // you can also live out this line of code because
// Arduino pins are configured as INPUT by default
analogReference(INTERNAL); // set the internal reference
}

void loop() {
voltage = (value * aref) / 1024.0; // in this way you calculate the voltage
temp = voltage * 100.0; // in this way you calculate the temperature in Celsius degrees

delay(1000);
if (temp < 35) //when temp is below 35 degrees
{
digitalWrite(13, HIGH); //blue led is on
digitalWrite(8, LOW); //red led is off
digitalWrite(9, LOW); //green led is off
}
else if (temp > 40) //when temp is above 40 degrees
{
digitalWrite(13, LOW); //blue led is off
digitalWrite(8, HIGH); //red led is on
digitalWrite(9, LOW); //green led is off
}
else //when between 35-40 degrees
{
digitalWrite(13, LOW); //blue led is off
digitalWrite(8, LOW); //red led is off
digitalWrite(9, HIGH); //green led is on
}
}
``````

if you find some errors inform me and i willtry to correct them.
I have find another way to calculate the temperature in Celsius degrees. You can also try this code and check which one works better :

``````/*
temp lights LM35 and leds
*/
const float aref = 1.1;  // internal reference is 1.1 V
int tempPin = A0;
float value; // used to read the value from LM35 sensor
float constant;  // used to calculate the constant to get the temperature value
float temp;  // varable used to save the value of temperature in Celsius degrees

void setup() {

pinMode (13, OUTPUT); //Blue LED symbolising Cold and Not Fit for Consumption
pinMode (8, OUTPUT); //Red LED symbolising Hot and Not Fit for Consumption
pinMode (9, OUTPUT); //Green LED symbolising Lukewarm and Read for Consumption
pinMode (tempPin, INPUT);  // you can also live out this line of code because
// Arduino pins are configured as INPUT by default
analogReference(INTERNAL); // set the internal reference
}

void loop() {

constant = aref / 1024;
constant = 1 / (constant * 100);  // constant is 9.309
temp = value / constant ; // in this way you calculate the temperature in Celsius

delay(1000);
if (temp < 35) //when temp is below 35 degrees
{
digitalWrite(13, HIGH); //blue led is on
digitalWrite(8, LOW); //red led is off
digitalWrite(9, LOW); //green led is off
}
else if (temp > 40) //when temp is above 40 degrees
{
digitalWrite(13, LOW); //blue led is off
digitalWrite(8, HIGH); //red led is on
digitalWrite(9, LOW); //green led is off
}
else //when between 35-40 degrees
{
digitalWrite(13, LOW); //blue led is off
digitalWrite(8, LOW); //red led is off
digitalWrite(9, HIGH); //green led is on
}
}
``````