Working with two analog sensors, ATmega16

Status
Not open for further replies.

SMarc

Newbie
Joined
Apr 18, 2022
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
34
I want to work with two analog sensors connected to ATmega16. The first one is the temperature sensor (LM35), connected to PA0. The LED should be ON when the temperature goes high. The second one is the photoresistor and it's connected to PA1. I want to turn the LED ON when the lighting is low.
I was looking for a solution all over the internet, but couldn't find something similar to my situation. They all just say one thing- you can't read two analog sensors at the same time.
I have this code that works just for one sensor.

C:
#define F_CPU 1600000UL
#include <avr/io.h>

void ADC_Init()
{  DDRA=0x0;           
   ADCSRA = 0x87;           
   ADMUX = 0x40;}

uint16_t ADC_Read(uint8_t channel) {
    ADMUX= (ADMUX & 0xF0) | (channel & 0x0F);
    ADCSRA |= (1 << ADSC);
    while (ADCSRA & (1 << ADSC));
    return ADC;  }
int main(void)
{
    ADC_Init();
    DDRB=0xFF;
    uint8_t value;
    
    while (1) {
        value=ADC_Read(0);
        
        if (value < 70)
        {PORTB|=(1<<0);}
        else
        {  PORTB &=~(1<<0); }
        }
}


Can you somehow help me to modify this code, give me advice on how can I do it, or some example?
 

Hi,

why "at the same time" ? Jus do one after the other.

It´s just the usual way for software to process one thing after the other.

***
Indeed the "real" problem is not clear to me.
--> What do you expect? and what exactly is not as expected?

Klaus
 

So, you want the LED to turn on if either the light is low or the temperature is high? That's an OR function.

And, to add to what Klaus, and, apparently, the rest of the universe is telling you, the ATMega can only read one AD channel at a time. But I don't see why this is a problem.
 

Yeah, I want to switch between those two sensors constantly.
This code works just for one senor, when I try to adjust it the program gives weird results or nothing at all.
I tried something like this:
Code:
    while (1) {
        switch (ADMUX)
        {
            case 0x40:
            value=ADC_Read(0);
            if (value > 90)
            {PORTB|=(1<<0);}
            else
            {  PORTB &=~(1<<0);}
            break;
            case 0x41:
            value=ADC_Read(1);
            if (value < 70)
            {PORTB|=(1<<4);}
            else
            {  PORTB &=~(1<<4);}
            break;
        }
}

Can you help me figure out what I'm doing wrong?
 
Last edited by a moderator:

Hi,

Yeah, I want to switch between those two sensors constantly.
please give a complete description.

I try to adjust it the program gives weird results or nothing at all.
This gives us no information.
Please tell us exactly what you expect to see and what you see instead.... And your test setup.

Code: It´s a snippet (incomplete). Nobody of us can know what ADMUX is, and who or what controls ADMUX.
Nobody knows your schematic.

And please mind: You don´t spend time to add comments to your code, so why should anybody spend time to solve this riddle about the function of your code.
And comments are not only for us, if you later want to add some code or change something in your code .. you will urgently need some comments.

Klaus
 

Take one thing at a time. Integrate it to the end then move to the next one. For example, if you start with a temperature sensor, you will find so many solutions for it on the internet. Focus on it and ensure that it works well, then move to the next thing. Thanks.
 

I do not see in your while loop with CASE statement where you change
ADMUX to the other channel ?

Your LEDs connected to bit 0 and bit 4 of PORTB ? Connected GND >> LED >>
resistor >> PORTB pin ?

Where do you initialize PORTB on startup ? Just rely on ATMEGA startup set to 0 ?


Regards, Dana,
 

I tried to switch from 0x40 to 0x41 in the case loop.
Let me upload the schematic, it may be helpful.
I forgot to write the main loop code, where I intitialized PORTA and PORTB:
DDRA=0x0;
ADCSRA = 0x87;
DDRB=0xFF;


 

You have handled the fact there are alternate pin functions on some GPIO pins ?




Regards, Dana.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…