ADC using lm 35 sensor

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
/*
 * GccApplication3.c
 *
 * Created: 6/27/2017 1:51:57 PM
 * Author : Administrator
 */ 
#define F_CPU 12000000UL


#include <avr/io.h>
#include <avr/interrupt.h>

#include <util/delay.h>


//unsigned char numbers[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x67};
unsigned char numbers[10] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x18};
int buffer[3];
int count = 0;


int ReadADC(uint8_t ch)
{
    ADMUX=ch;
    //Start Single conversion
    ADCSRA |= (1<<ADSC);
    //Wait for conversion to complete
    while(!(ADCSRA&(1<<ADIF)));
    ADCSRA|=(1<<ADIF);
    return(ADC);
}
void initADC()
{
    ADMUX=(1<<REFS0);
    ADCSRA=(1<<ADEN)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1);
}

int main(void)
{
    
	DDRC=0b0000000;
    DDRD=0xFF; // PORT D as output port
    DDRB=0xFF; //PB0, PB1 and PB2 is segment select pins
    initADC();
    int analogVal;
	
    while(1)
    {
        analogVal=ReadADC(0);
        analogVal=analogVal/2;
        count=analogVal;
        
		  buffer[0] = (count/100);                
		  buffer[1] = (count/10)%10; 
		  buffer[2] = count%10;                 
		  		  
		  PORTD = 0b11111111;
		  PORTD = (numbers[buffer[0]] << 0);
		  PORTB = 0b111111;
		  PORTB = (numbers[buffer[0]] >> 7) + 0b111011;
		  _delay_ms(5);
		  PORTD = 0b11111111;
		  PORTD = (numbers[buffer[1]] << 0);
		  PORTB = 0b111111;
		  PORTB = (numbers[buffer[1]] >> 7) + 0b111101;
		  _delay_ms(5);
		  PORTD = 0b11111111;
		  PORTD = (numbers[buffer[2]] << 0); 
		  PORTB = 0b111111;
		  PORTB = (numbers[buffer[2]] >> 7) + 0b111110;
		  _delay_ms(5);
    }
    return 0;
}

I need to convert analog data to digital and display the digital value in 7 segmend leds. I need to add lm35 sensor to the analog input of the arduino. 
In this code I get a value of 511 even when the lm35 is not connected to the a0 of arduino.

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

The following code in the ReadADC changes your ADC reference from the value in the initialization...

    ADMUX=ch;

You enable ADC interrupts, where do you handle them?

David (aka frog_jr)

Last Edited: Sun. Jul 16, 2017 - 12:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sir what do you mean by where do you handle them?

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

hakugen wrote:
In this code I get a value of 511 even when the lm35 is not connected to the a0 of arduino.

What value do you expect to get from a floating, unconnected input?  A floating pin can have any level.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

When I connected it to a0 still same value.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
ADCSRA=(1<<ADEN)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1);

You enable the ADC interrupts above. You need to provide an interrupt handler (interrupt service routine, ISR).

You seem to be using Arduino routines, and I am not an Arduino user so perhaps since  you do not enable global interrupts, this will may not be a problem (although I thought the Arduino did enable  the I bit in SREG).

You are handling the ADC in your code, so you (probably) do not need to enable the ADC interrupts.

What device is this?

What is the voltage on AVcc?

 

Edit: I highly recommend that you add verbose comments to your code. A year and a multitude of projects later, having to remember what each of these are doing will (possibly) mean a revisit to the datasheet to jog your memory on what you were doing. For example:

    ADMUX=(1<<REFS0);     // Set reference to AVcc (with external cap on AREF pin)
    ADCSRA=(1<<ADEN)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1); // Enable ADC and ADC interrupt, set for divide-by-64 (12MHz/64= 187500)

 

David (aka frog_jr)

Last Edited: Sun. Jul 16, 2017 - 12:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using atmel studio 7 atmega328p.

The voltage i am using is 5V.

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

When you're struggling with the basics it good to take small baby steps:

 

Start by replacing the sensor with a simple potentiometer.

Any value between about 1k and 1M will do.

For safety add a current limiting resistor between the pot output and the ADC pin. (100 Ohm to 1Meg, value not critical).

This resistor wil prevent you damaging the potmeter if the ADC pin is configured as an output.

 

Then the first step is to ensure the ADC is configured as an input.

Use a DMM to verify you can vary the voltage on the ADC input between a low value and almost AVcc.

 

Then keep on tinkering with the ADC untill it gives some sensible output.

Then write some temperature conversion code and use the potmeter to simulate some temperatures.

Then connect the LM35 and verify your code still works.

 

 

 

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