AVR Solar Tracker

Discussion in 'Embedded Systems and Microcontrollers' started by domino89, Sep 24, 2013.

  1. domino89

    Thread Starter New Member

    Sep 24, 2013
    22
    0
    Hello iam trying to make a solar tracker based on avr microcontroller (Atmega8).
    It will be using 2 LDR's to compare light intensity and then send decision to motor driver L293d. I am kinda new to avr's... I know how to run one ADC from for example Potentiometer. I know i cant make two adc reads at one time. How should i read two ADC inputs, "save" them and then compare those two values.
    I would be grateful for any advices.
     
  2. Shagas

    Active Member

    May 13, 2013
    802
    74
    Actually you can read two adc reads at one time , not really at one time but good enough for your purposes.
    Atmega 8 has eight adc channels . What happens is your read one channel , then you immediately after read the second channel . There is a slight delay in the order of microseconds between the readings (according to the speed that you set your adc up) but for your purposes you can consider it as "instant"

    Read the datasheet on the ADC section .
    The ADMUX register has the 'MUX' bits which basically operate a multiplexer which selects input 0-7 of the adc . You need to set the ADMUX before you take a reading for the right channel to be enabled .
    As default the MUX bits are set to zero which configures your ADC input as channel 0 (pin 40) , if you want to use the other channels (pins 33-39) then you will have to configure the MUX bits


    This guy has a tutorial on the avr ADC:

    http://maxembedded.com/2011/06/20/the-adc-of-the-avr/


    When you set the adc to read a value , the 10 bit value comes out in the ADC register so you can put :

    Code ( (Unknown Language)):
    1.  
    2. int LDR1 = ADC;
    3.  
    then take another reading and make

    Code ( (Unknown Language)):
    1.  
    2. int LDR2 = ADC;  // (the ADC register will now have the value of the latest reading)
    3.  
    and then compare them
     
  3. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Why not put the two LDR's in series, between +5v and ground?

    The middle point between the two LDRs can go to a single ADC input, which will be 2.5v when both LDRs match, or higher/lower etc if one LDR is illuminated more than the other.

    Then you have a single ADC reading that tells you if the sun is ahead or behind your panel tracking.
     
    absf likes this.
  4. Art

    Distinguished Member

    Sep 10, 2007
    785
    61
    Long time no see :)

    This is why you are Sir RB!


     
  5. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Nah, my knighthood hasn't come through yet. The Queen is too busy giving them to comedians and TV personalities... ;)
     
  6. Art

    Distinguished Member

    Sep 10, 2007
    785
    61
    It's exactly what I needed, I have two cells in a box where the top comes off
    like an iPad box, I want to check if the lid has been opened properly by sensing light at both ends.
     
  7. ftsolutions

    Active Member

    Nov 21, 2009
    48
    2
    Personally, I think that I'd still go with the (2) separate LDR inputs/readings - that way, one can also diagnose issues, such as if 1 LDR sensor is damaged, or if you need to calibrate for nonlinearities or unbalanced offset between the two. But either way can work.
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    It looks like the Atmega8 (I've never worked with one) is a 32 pin device with 6 or 8 analog inputs, an ample set of pins for this task.

    Thus it is senseless and probably counterproductive to go cheap and combine pins. You may still get left/right info for the sun but you loose overall intensity, so you need other ways to detect night (it's dark!) to keep from chasing fireflies all night long.
     
Loading...