Reading wrong LM35 value with Atmega8

Go To Last Post
27 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, I have temperature sensor lm35 and Atmega8A-PU.

I have connected

lm GND to avr GND

lm DQ to avr AC0

lm VDD to avr VCC via 4.6k Ohms resistor (if I do not use the resistor the lm35 gets insanely hot)

 

now i use this code to read the value and i display it on one number led display by showing the numbers gradually. E.g. if the temperature is 18.5 the display will show 1 then 8 then 5..

#ifndef F_CPU
#define F_CPU 1000000UL
#endif // F_CPU

#include <avr/io.h>
#include <util/delay.h>

#define DELAY_IN_MS 2000 /* 2 sec */

uint16_t ReadADC(uint8_t ch)
{
	//Select ADC Channel ch must be 0-7
	ch=ch&0b00000111;
	ADMUX|=ch;

	//Start Single conversion

	ADCSRA|=(1<<ADSC);

	//Wait for conversion to complete
	while(!(ADCSRA & (1<<ADIF)));

	//Clear ADIF by writing one to it
	ADCSRA|=(1<<ADIF);

	return(ADC);
}

int main()
{
	int numbers[] = { // LED display numbers
			0b01000000,
			0b01110011,
			0b00100100,
			0b00100001,
			0b00010011,
			0b00001001,
			0b00001000,
			0b01100011,
			0b00000000,
			0b00000001,
			0b11111111 // off
	};
    DDRD = 0b11111111;

	ADMUX=(1<<REFS1)|(1<<REFS0); // Internal 2.56 Voltage Reference
	ADCSRA=(1<<ADEN)|(1<<ADPS2);  // 16 division factor (62.5 kHz ADC frequency)

	int res;
    while(1)
    {
		uint16_t adc_value;
		adc_value=ReadADC(0)/4; // 2.5 mV resolution
		res = adc_value*10; // to simplify reading onto one digit led e.g 18.5
		PORTD = numbers[res / 10 / 10 % 10]; // tens e.g. 1
		_delay_ms(DELAY_IN_MS);
		PORTD = numbers[res / 10 % 10]; // units e.g. 8
		_delay_ms(DELAY_IN_MS);
		PORTD = numbers[res % 10]; // decimal e.g. 5
		_delay_ms(DELAY_IN_MS);
		PORTD = numbers[10]; // off
		_delay_ms(DELAY_IN_MS);
    }

    return 0;
}

 

The problem is, I am indeed getting about 18 degrees in a room that has 22.3 degrees, is there something wrong with my code? Or is the temperature sensor malfunctioning?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Increase wrote:

Hi, I have temperature sensor lm35 and Atmega8A-PU.

I have connected

lm GND to avr GND

lm DQ to avr AC0

lm VDD to avr VCC via 4.6k Ohms resistor

Please don't mess about with trying to describe circuit connections in text - draw a schematic!

 

if I do not use the resistor the lm35 gets insanely hot

Then there is something very fundamentally wrong!!

 

surprise

 

Probably, you have the connections wrong - VDD and GND swapped, perhaps?

 

is the temperature sensor malfunctioning?

 

Once it has got "insanely hot", it has probably been damaged.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
draw a schematic!

aka "circuit diagram".

 

Some tips here: https://electronicsclub.info/circuitdiagrams.htm

 

http://www.bbc.co.uk/bitesize/ks3/science/energy_electricity_forces/electric_current_voltage/revision/3/

 

EDIT

 

If you prefer video: https://www.youtube.com/watch?v=...

 

(but note that putting cells in parallel is not generally a good idea)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Dec 30, 2017 - 05:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Increase wrote:
lm VDD to avr VCC via 4.6k Ohms resistor (if I do not use the resistor the lm35 gets insanely hot)

awneil wrote:
Probably, you have the connections wrong - VDD and GND swapped, perhaps?

+1

 

I did that once...

 

Increase wrote:
The problem is, I am indeed getting about 18 degrees in a room that has 22.3 degrees, is there something wrong with my code? Or is the temperature sensor malfunctioning?

Well the sensor is probably damaged at this point so I would throw out the current one and replace it with a new one and DOUBLE CHECK your connections.

 

As far as the code is concerned, I believe the LM35 outputs 10mv/C IIRC, so how have you set your reference voltage for the ADC for example?  How do you have AVcc decoupled?

 

As far as using single conversions go, I would take multiple readings and average them together.  

 

JIm

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
I did that once...

Actually, I did it only the other day with a DS1820

 

blush

 

I believe the LM35 outputs 10mv/C IIRC

That is, indeed, what the datasheet says: http://www.ti.com/lit/ds/symlink/lm35.pdf

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

Increase wrote:
lm VDD to avr VCC via 4.6k Ohms resistor (if I do not use the resistor the lm35 gets insanely hot)

awneil wrote:
Probably, you have the connections wrong - VDD and GND swapped, perhaps?

+1

 

I did that once...

 

If I swap VDD with GND the value read is 0. When I was using the same sensor with raspberry pi, I also used the resistor, without which it was getting how. It was reading correct values back then (its already about 6 months since I was playing around with pi)

 

jgmdesign wrote:

 

Increase wrote:
The problem is, I am indeed getting about 18 degrees in a room that has 22.3 degrees, is there something wrong with my code? Or is the temperature sensor malfunctioning?

Well the sensor is probably damaged at this point so I would throw out the current one and replace it with a new one and DOUBLE CHECK your connections.

Ok I will throw it away and need to buy a new one first.

 

jgmdesign wrote:

As far as the code is concerned, I believe the LM35 outputs 10mv/C IIRC, so how have you set your reference voltage for the ADC for example?  How do you have AVcc decoupled?

 

As far as using single conversions go, I would take multiple readings and average them together.  

 

JIm

I am commenting this in the code above, reference voltage is 2.56V which gives me 2.5 mV resolution. The LM35 itself has 10 mV so it is 4 times less, thats where I get the / 4.. (more details on that here http://extremeelectronics.co.in/avr-tutorials/interfacing-temperature-sensor-lm35/)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is best I can do (I do not include LED connections as they would make things messy):

 

 

edit:forgot the resistor: updated

Last Edited: Sat. Dec 30, 2017 - 06:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Increase wrote:
This is best I can do

That's perfectly fine.

 

Note that it is a convention (not entirely rigid) that schematics should generally read left-to-right - so inputs are on the left, and signals "flow" to the right.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The resistor remains a puzzle - you certainly should not need it, and your LM35 certainly should not get hot.

 

Something is still fundamentally wrong.

 

Are you sure it's an LM35?

 

You've labelled its output as "DQ" - but that's not what the linked tutorial calls it, nor what the datasheet calls it.

 

You haven't actually got a DS1820 or suchlike instead - have you ... ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

omg, you're right. I am still a beginner and when I asked for LM35 in a shop I actually got DS18B20 (https://www.gme.cz/data/attachments/dsh.530-067.1.pdf) without even realizing. I am sorry, this is stupid.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
You haven't actually got a DS1820 or suchlike instead - have you ... ?

And whose Vcc and GND are reverse of the LM35

 

LM35:

 

DS1820:

I see a certain resistor mentioned earlier that is used differently here as well.....

 

Jim

 

EDIT: OP beat me to the post button

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Increase wrote:
omg, you're right.

It does happen occasionally!

 

laugh

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The thing is I was looking into the correct spec, so I connected the wires correctly. If I put the resistor as shown on jgmdesigns schema, the sensor gets hot, only if the resistor is put as drawn on my schema, the sensor remains cool. I don't know why.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have the power pins reversed.  LOOK at both pinouts of the LM35 and the DS1820...they are reversed.  Also what voltage are youpowering the circuit with?  TheLM35 can tolerate high voltages while the DS1820 not so much

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

nope, trust me, VDD is connected to 5V, GND to ground and DQ to the MCU as shown on my schematics. I know they are reversed from LM35, but I was using datasheet of DS1820, just not realizing its not LM35...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Increase wrote:
nope, trust me,

Like you knew you were using an LM35? wink

 

Now which one are you planning on using as both devices have VERY different outputs.  The LM35 has an Analog output, the DS1820 has a digital output.

 

JIm

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

right now, I'd like to try more with current DS1820, even though I don't know if its still okay, but I can't get my hand on anything else until monday. Then I would consider LM35 as it seems easier to handle.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Increase wrote:

The thing is I was looking into the correct spec, so I connected the wires correctly. If I put the resistor as shown on jgmdesigns schema, the sensor gets hot, only if the resistor is put as drawn on my schema, the sensor remains cool. I don't know why.

 

There is something very odd happening here.

 

The active current for a DS1820 is 1mA. With that resistor in series with the power rail a 4.6V drop will happen across the resistor with means that on a 5V power supply the DS1820 will only see 0.4V so it can never work. And as it can never work it won't get hot.

 

(It's actually a bit more complicated as it will never draw 1mA with a 0.4V rail, but it certainly won't work.)

"This forum helps those that help themselves."

"How have you proved that your chip is running at xxMHz?" - Me

"If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

Increase wrote:
nope, trust me,

Like you knew you were using an LM35? wink

 

Now which one are you planning on using as both devices have VERY different outputs.  The LM35 has an Analog output, the DS1820 has a digital output.

 

JIm

I am gonna kill myself. :D

Ok, I had it in reverse, you know why? Because I didn't realize the schema in the doc is BOTTOM VIEW. There is so many new things for me and then such small details just evade my brain..grr..

 

So when I connect the wires correctly I get value of 0. I guess the sensor is already screwed. I think that is all for now, until I buy a new one.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 I get value of 0.

The LM35 puts out an analog voltage, and one reads it with an ADC, does some math conversion, and gets the temperature.

 

The DS18B20 is a DIGITAL temperature sensor.

You have to read the data sheet.

You have to send it digital commands, and read back digital data.

All doable, but rather complex for a beginner if you are not using a canned library for the interface.

 

JC 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
With that resistor in series with the power rail ... it can never work. And as it can never work it won't get hot.

But OP said it got hot without the resistor!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Increase wrote:
I guess the sensor is already screwed

Yes, I think so.

 

Quote:
I think that is all for now, until I buy a new one.

Indeed.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Datasheet page 1: "Current drain < 60uA"

60uA * 4k6 = 280mV

5V-280mV = 4V72 and LM35 should work down to 4V.

Best guess: Sensor is fried, possibly a short somewhere.

 

I don't really understand why anyone would order a single 50ct sensor while experimenting.

I usually buy batches of 10 or so.

Swapping out a sensor (uC) on a breadboard is a very fast and easy way to diagnose circuit problems / mysteries.

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
I don't really understand why anyone would order a single 50ct sensor while experimenting.

Well, "retail" they're more like $2 (online, Digikey) - possibly even more, as it sounds like the OP actually got it from a local, physical store.

 

But even so it is, as you say, good to have spares ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The more accurate grades are even more expensive. Digikey wants $7.47 (quantity 1) to $3.187 (quantity 1000) for an LM35CAZ.

- John

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

exactly i got it from local store, and back when I was buying it I didn't realize I can destroy the sensor in an instant. good i didn't buy more anyway because LM35 seems better now than the DS... :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Increase wrote:
I didn't realize I can destroy the sensor in an instant

(almost?) any electronic component will be destroyed "in an instant" by gross abuse like connecting its power supply backwards!

 

 

EDIT

 

For discussion on this point, go to: https://www.avrfreaks.net/forum/s...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Jan 2, 2018 - 04:41 PM