DFR0023 LM35 Analog Linear Temperature Sensor

Hi,

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.

Im using an UNO REV3: ARD UNO REV3 - Communica [Part No: ARD UNO REV3]
LM35 sensor: Product Discontinued – Communica South Africa

Please help as your assistance will be highly appreciated as im currently working on this to subject on campus as part of my industrail project.

regards,
Thandanani

that code looks good but you should use float to get better results

void setup()
{
  Serial.begin(9600);
  Serial.println("lm35 test 0.1.00");
}
 
void loop()
{ 
  int val = analogRead(0);
  float voltage = val * (5.0  /1023);  // compiler can optimize the constant part
  float temperature = voltage * 100;  // 0.01V = 1C

  Serial.print("Temp: ");
  Serial.print(temperature, 1);
  Serial.println(" °C");               // ALT-0176 => °
  delay(500);
}

to get a more stable result average 16 reads

void setup()
{
  Serial.begin(9600);
  Serial.println("lm35 test 0.1.00");
}
 
void loop()
{ 
  int val = 0;
  for (int i=0; i< 16; i++)  // add 
  {
    val += analogRead(0);
  }
  float voltage = val * (5.0 /(1023 * 16));  // factor 16 for averaging
  float temperature = voltage * 100; 

  Serial.print("Temp: ");
  Serial.print(temperature, 1);
  Serial.println(" °C");               // ALT-0176 => °
  delay(500);
}

Hey Thandanani- I have the same DFR sensor, although mine looks slightly different and doesn't say V2 on it, but essentially the same I'm sure.

As we speak, with your code I get 21C and my Fluke meter says 22, so it seems to be ok.

The conversion of 500/1024 is indeed correct: I usually just use /2, close enough for me.

Hi Rob,

I uploaded this code of yours on arduino, the readings are somewhat strange :~. Please see output from my code

void setup()
{
  Serial.begin(9600);
  Serial.println("lm35 test 0.1.00");
}
 
void loop()
{ 
  int val = 0;
  for (int i=0; i< 16; i++)  // add 
  {
    val += analogRead(0);
  }
  float voltage = val * (5.0 /(1023 * 16));  // factor 16 for averaging
  float temperature = voltage * 100; 

  Serial.print("Temp: ");
  Serial.print(temperature, 1);
  Serial.println(" °C");               // ALT-0176 => °
  delay(500);
}

temperatur_output.docx (104 KB)

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?

Hi Jimbo,

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);
}

temperatur_output.docx (95.4 KB)

I'd be inclined to try a different sensor: it's not your code, since I used your exact code with my sensor and got the right temperature.

Or try a different analog pin?

And as KenF says, you sure the wires are connected correctly? Maybe post a photo.....

thandana:
Hi Rob,

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);
}

Hi All

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?

What other sensor i could use?

When I said try another sensor, I meant one the same.

Your wiring's the same as I used on mine, with your code.

Check out this page

look about half way down where it shows Old version and New version You appear to have yours connected the same as the old version.

If you checkout the pinout of the actual chip perhaps you could buzz it through to be sure which version you have.

Hi KenF,

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

Hi All,

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?

I will test this later though.

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.

Hi All,

I have been playing around with the code again.In the below line of code, where do we get the value 5 that is being used in the formula?

float voltage = val * (5.0 /(1023 * 16));  // factor 16 for averaging

I changed this value to 4 and I seem to be getting accurate temperature readings now :slight_smile:

float voltage = val * (4.0 /(1023 * 16));  // factor 16 for averaging

The Analog to digital conversion on the arduino will give 0-1024 for a voltage range of 0 to 5v It's the reference to 5V.

If our reference voltage was 1 volt then a read value of 1024 would represent just 1 volt.

Hi KenF,

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
  }
}

delay(5000); // wait for a second

in case of a conflict between code and comment, code always wins ....

Hi,

The code is always right :slight_smile:

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

Your help will be highly appreciated.

May thandana, I read your code, but it there OK??

Let's discuss...

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...