Turning ADC on and off.

Hello,
In Nick Gammon’s post about turning off the ADC, he does it in the setup() routine. I don’t know what ADCSRA=0 does, but it seems to save a lot of power. Can you use this command to control ADC on/off in the loop part of the program instead of the setup? Can I turn ADC on and off as needed? What kind of implications does that have? I read that the analog input pins use capacitors to read voltage - do I need to do a ADCSRA=1, delay for 2 seconds, then perform the analog read in order to get accurate readings? Then I want to turn off ADCSRA and sleep the controller.

http://www.gammon.com.au/forum/?id=11497

#include <avr/sleep.h>

void setup () 
{

  for (byte i = 0; i <= A5; i++)
    {
    pinMode (i, OUTPUT);    // changed as per below
    digitalWrite (i, LOW);  //     ditto
    }
    
  // disable ADC
  ADCSRA = 0;  
  
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
  sleep_enable();
 
  // turn off brown-out enable in software
  MCUCR = bit (BODS) | bit (BODSE);
  MCUCR = bit (BODS); 
  sleep_cpu ();  

}  // end of setup

void loop () { }

Can I turn ADC on and off as needed?

What happens when you try?

I'm using a "voltage divider" - and changing the bottom resistor values, but I always get 0 from the analogRead() command.

I've also tried different delays, 2 second, 5 seconds, but the reading is always 0.

Your code stops at the loop() so … is it the code?

No wiring diagram etc … how do you know that 0 is the wrong value? (what is the refernec voltage and the setup thereto?) Try using the Example sketch AnalogInSerialOut to verify your circuit.

arusr:
Hello,
I don’t know what ADCSRA=0 does, but it seems to save a lot of power.

It is explain in the datasheet of your micro-controler.
Plus many other very valuable information.

Less than 20 pages to read, it’s worth making an effort.

arusr:
do I need to do a ADCSRA=1

No, you need to write the correct value to ADCSRA to enable the ADC and configure it correctly for the mode the Arduino uses. It would be possible to work out what the correct value is either by reviewing the Arduino code which initialises the ADC, or by reading the microprocessor’s data sheet.

If you’re only doing this to save power, perhaps one of the power management libraries already implements this option; using an existing working solution would be much easier than implementing your own.