Im trying to read the room temperature using DFR0023 LM35 Analog Linear Temperature Sensor via arduino. I found this code in the internet to display temperature on the serial monitor:
void setup()
{
Serial.begin(9600);//Set Baud Rate to 9600 bps
}
void loop()
{
int val;
int dat;
val=analogRead(0);//Connect LM35 on Analog 0
dat=(500 * val) /1024;;
Serial.print("Temp:"); //Display the temperature on Serial monitor
Serial.print(dat);
Serial.println("C");
delay(500);
}
I would like to find out if this code is correct? On the serial monitor i see 4C however this did not match with the current room temperature which was 14 degrees celcius at the time. Can anyone confirm if this conversion used in the code is correct.
I've used a few of those and if you're getting a stable reading that is that low, I'd suspect you may have it wired it back to front. Does it get warm when connected?
My readings are definately not correct. I ran the same initial code - but i keep getting a wrong read. This cannot be correct. Please see attached output :~
Its 11:30pm now here now, a reading of 27 is just not correct
Any reason why I could get wrong reading from sensor, what can i check to investigate this? I'm really confused now
void setup()
{
Serial.begin(9600);//Set Baud Rate to 9600 bps
}
void loop()
{
int val;
int dat;
val=analogRead(0);//Connect LM35 on Analog 0
dat=(500 * val) /1024;;
Serial.print("Temp:"); //Display the temperature on Serial monitor
Serial.print(dat);
Serial.println("C");
delay(500);
}
I uploaded this code of yours on arduino, the readings are somewhat strange :~. Please see output from my code
...
What I see is that the values might be wrong but are quite consistent, given the normal noise of the Arduino ADC.
That can easily be checked by printing the raw analogRead value.
void setup()
{
Serial.begin(9600);//Set Baud Rate to 9600 bps
}
void loop()
{
int val;
int dat;
val=analogRead(0);//Connect LM35 on Analog 0
Serial.println(val);
dat=(500 * val) /1024;;
Serial.print("Temp:"); //Display the temperature on Serial monitor
Serial.print(dat);
Serial.println("C");
delay(500);
}
The sensor is NOT heating up. I think i have connected the sensor correctly on the arduino. please also see attached.
BLACK - GND
RED - 5V
BLUE - A0
@ Rob, when you say "That can easily be checked by printing the raw analogRead value", do you mean i must do a Serial.print(val); How will this help me? any ideas on how to make the reading accurate?
I saw this page on google and connected it as per the NEW version. You will see that the BLUE wire from the sensor connects to the BLACK wire which goes to A0, The RED wire from the sensor connects to the WHITE wire goes to 5V, the BLACK wire from the sensor connects to BLUE which goes to GND.
I believe its connected according to the new version. Please advise otherwise
Please also note that I'm powering my arduino board using the USBcable and not the external adaptor at the moment. Could this have an impact to my readings?
thandana:
Please also note that I'm powering my arduino board using the USBcable and not the external adaptor at the moment. Could this have an impact to my readings?
That's exactly how I had mine hooked up when I tested your code.
Thanks for the feedback. I now want to send the temperature that is read from the sensor via an SMS, i have this code below. Please have a look at it and advise:
#include <SoftwareSerial.h>
SoftwareSerial SIM900(2,3); // RX, TX
void setup()
{
Serial.println("lm35 test 0.1.00");
Serial.begin(19200); // set the baud rate
SIM900.begin(19200); // for GSM shield
delay(20000); // give time to log on to network.
SIM900.print("ATE0\r");
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(1000);
SIM900.println("AT+CNMI=2,2,0,0,0\r");
delay(1000);
Serial.println("Finished Setup Section");
}
void sendSMS(temperature)
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(1000);
SIM900.println("AT + CMGS = \"+27XXXXXXXXXXX\""); // recipient's mobile number, in international format
delay(1000);
SIM900.println( " WARNING TEMPERATURE IS: temperature");
delay(1000);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(1000);
SIM900.println();
Serial.println("Message sent");
delay(5000); // give module time to send SMS
}
void loop()
{
int val = analogRead(0);
float voltage = val * (4.0 /1023); // compiler can optimize the constant part
float temperature = voltage * 100; // 0.01V = 1C
Serial.print("Current Temp: ");
Serial.print(temperature, 2); // Reading the temperature to 1 decimal point
Serial.println("C"); // ALT-0176 => °
delay(500);
if (temperature >= 18)
{
sendSMS(temperature);
delay(5000); // wait for a second
}
}
Can you confirm if what I'm doing here in the code is correct though:
#include <SoftwareSerial.h>
SoftwareSerial SIM900(2,3); // RX, TX
void setup()
{
Serial.println("lm35 test 0.1.00");
Serial.begin(19200); // set the baud rate
SIM900.begin(19200); // for GSM shield
delay(20000); // give time to log on to network.
SIM900.print("ATE0\r");
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(1000);
SIM900.println("AT+CNMI=2,2,0,0,0\r");
delay(1000);
Serial.println("Finished Setup Section");
}
void sendSMS(temperature)
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(1000);
SIM900.println("AT + CMGS = \"+27XXXXXXXXXXX\""); // recipient's mobile number, in international format
delay(1000);
SIM900.println( " WARNING TEMPERATURE IS: temperature");
delay(1000);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(1000);
SIM900.println();
Serial.println("Message sent");
delay(5000); // give module time to send SMS
}
void loop()
{
int val = analogRead(0);
float voltage = val * (4.0 /1023); // compiler can optimize the constant part
float temperature = voltage * 100; // 0.01V = 1C
Serial.print("Current Temp: ");
Serial.print(temperature, 2); // Reading the temperature to 1 decimal point
Serial.println("C"); // ALT-0176 => °
delay(500);
if (temperature >= 18)
{
sendSMS(temperature);
delay(5000); // wait for 5 second
}
}
I tried to verify this code and i kept getting this error:
Temperature:5: error: variable or field 'sendSMS' declared void
Temperature:5: error: 'temperature' was not declared in this scope
Temperature:18: error: variable or field 'sendSMS' declared void
Temperature:18: error: 'temperature' was not declared in this scope
Temperature.ino: In function 'void loop()':
Temperature:47: error: 'sendSMS' was not declared in this scope
I think you want to send a temperature via SMS, but your code as shown below..
SIM900.println( " WARNING TEMPERATURE IS: temperature");
delay(1000);
I have done some trial to send SMS using arduino uno R3... Here is my code:
mySerial.print("This is a sending message, read input: "); //the content of the message
mySerial.println(read_input); //variable sent
mySerial.println(" -Al Toex Labs- "); //the content of the message
delay(100);
I'm sorry if out of topic...
My comment about thandana problem, I think you have to compare your temperature reading with a calibrated temperature sensors.. Then you have a table to repair the true value of temperature...