Hello!
I came up with an idea to use temperature sensor and leds to indicate the temperature value. I found LadyAda-s tutorial Temperature sensor tutorial - Using the TMP36 / LM35, and now i want to add leds, so that when the temperature is under 0 degrees the blue led is on, when the temp is over 0 degrees the green led is on and when it is over 20 degrees the red led is on. Actually I have no idea, how to integrate leds to the temperature code. Maybe someone here can help..
//TMP36 Pin Variables
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures
#define BANDGAPREF 14 // special indicator that we want to measure the bandgap
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup()
{
Serial.begin(9600); //Start the serial connection with the computer
//to view the result open the serial monitor
delay(500);
}
void loop() // run over and over again
{
// get voltage reading from the secret internal 1.05V reference
int refReading = analogRead(BANDGAPREF);
Serial.println(refReading);
// now calculate our power supply voltage from the known 1.05 volt reading
float supplyvoltage = (1.05 * 1024) / refReading;
Serial.print(supplyvoltage); Serial.println("V power supply");
//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorPin);
// converting that reading to voltage
float voltage = reading * supplyvoltage / 1024;
// print out the voltage
Serial.print(voltage); Serial.println(" volts");
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((volatge - 500mV) times 100)
Serial.print(temperatureC); Serial.println(" degress C");
// now convert to Fahrenheight
float temperatureF = (temperatureC * 9 / 5) + 32;
Serial.print(temperatureF); Serial.println(" degress F");
delay(1000); //waiting a second
}
Is it really that simple?
Part of the code taken from my first post:
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ;
if (temperatureC < 0){
digitalWrite(LEDpin1, HIGH);
digitalWrite(LEDpin2, LOW);
digitalWrite(LEDpin2, LOW);
}
if (temperatureC > 0){
digitalWrite(LEDpin1, LOW);
digitalWrite(LEDpin2, HIGH);
digitalWrite(LEDpin2, LOW);
}
if (temperatureC > 20){
digitalWrite(LEDpin1, LOW);
digitalWrite(LEDpin2, LOW);
digitalWrite(LEDpin2, HIGH);
}
Is this correct, when i write temperatureC > 0 and temperatureC > 20 - does it change the led or both are going to be ON ? I want that only one led is on..
// Under 0 degrees blue led is on
if (temperatureC < 0){
digitalWrite(LEDpin1, HIGH);
digitalWrite(LEDpin2, LOW);
digitalWrite(LEDpin2, LOW);
}
// Over 20 degrees red led is on
else if (temperatureC > 20)
{
digitalWrite(LEDpin1, LOW);
digitalWrite(LEDpin2, LOW);
digitalWrite(LEDpin2, HIGH);
}
// All that is left (0 - 20 degrees), the green led is on
else
{
digitalWrite(LEDpin1, LOW);
digitalWrite(LEDpin2, HIGH);
digitalWrite(LEDpin2, LOW);
}
Just to avoid grief, whenever you're dealing with "float"s or "double"s, particularly if you're mixing them with "integer types, always try to include the decimal point on constants.
e.g.float supplyvoltage = (1.05 * 1024[glow].[/glow]0) / refReading;
Sometimes, they're not necessary, but get into the habit and you'll have a lot less hair-tearing.
As I bought temperature sensor LM35CZ, there is actually better script:
Code*****
/*
An open-source LM35DZ Temperature Sensor for Arduino. (cc) by Daniel Spillere Andrade
*/
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
}
END OF CODE*****
You don't use the values in the samples array again. Why bother storing the values in an array?
Since multiplication and division are not fast on the Arduino, you'd be better off just adding up the values read, in the loop, and then performing the multiplication and division on the average reading, after the loop.
Why are you storing the results in an array?
You're not using the results afterwards, or doing a rolling average or a median filter or anything that requires a discrete sample history, so there's no need for the array.
tempc += analogRead (pin);
Just don't forget to zero "tempc" before the loop.
How do I make Arduino to display decimal places? When i open the serial monitor, it only shows 22 degrees, but I want 22,5 degrees. In data-sheet it is said that the temperature sensor LM35 has precision of 0,1 degrees.