[Solved] ADCSRA usage to turn ADC on/off

Hello,
I've been looking at Nick Gammon's tutorial of an "Example of watchdog timer interrupt" here:

Quarter way down the page is the "watchdog timer interrupt" example. I'd like to be able to turn the ADC on/off like he does, but I don't understand how he does it.

He first saves the value of ADCSRA. Then sets ADCSRA=0 (turns off ADC) before telling the controller to sleep. So all it takes to turn off ADC is to do "ADCSRA = 0"?

When he wakes up from sleep, he restores the ADCSRA to the saved value, and that turns ADC back on again?

Here is what I think are the relevant lines:

  byte adcsra_save = ADCSRA;
  byte prr_save = PRR;

  ADCSRA = 0;  // disable ADC
  PRR = 0xFF; // turn off various modules
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);   // sleep mode is set here
  attachInterrupt (0, wake, LOW);   // allow grounding pin 2 to wake us
  sleep_mode ();            // now goes to Sleep and waits for the interrupt
  detachInterrupt (0);      // stop LOW interrupt
  
  ADCSRA = adcsra_save; // stop power reduction
  PRR = prr_save;

When I try this on a simple routine, turning ADCSRA = 0 doesn't seem to have any effect on analogRead() results. I first run the program with the line commented out. Then I uncomment it and reload the sketch. The analog reading is still correct. I'm using a voltage divider, with the pin between the two resistors going to A0 pin. The value makes sense; it's 511, or about half the voltage of the rail.

int analogvalue;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  Serial.begin(9600);

}

// the loop routine runs over and over again forever:
void loop() {
  
  //if I comment this line out, ADC reading still works.  Why?
  ADCSRA = 0;  //Disable ADC
  
  delay(2000);               // wait for a second
  analogvalue = analogRead(A0);
  Serial.println(analogvalue);
  delay(1000);               // wait for a second
}

When I try this on a simple routine, turning ADCSRA = 0 doesn't seem to have any effect on analogRead() results.

Look at the source code for analogRead(). See if it turns the ADC (back) on.

//if I comment this line out, ADC reading still works. Why?
ADCSRA = 0; //Disable ADC

You are not turning off the adc if you comment out that line?

void setup()
{
  
  Serial.begin(9600);
  Serial.println(__FILE__);
  
  Serial.print("ADCSRA = ");
  Serial.println(ADCSRA);
  Serial.println("Save ADCSRA ");
  byte adcsra_save = ADCSRA;
  Serial.println("Set ADCSRA = 0");
  ADCSRA = 0;
  Serial.print("ADCSRA = ");
  Serial.println(ADCSRA);
  Serial.print("Analog read port A0 = ");
  Serial.println(analogRead(A0));
  Serial.print("ADCSRA = ");
  Serial.println(ADCSRA);
  ADCSRA = adcsra_save;
  Serial.println("Restore ADCSRA");
  Serial.print("ADCSRA = ");
  Serial.println(ADCSRA);
  Serial.print("Analog read port A0 = ");
  Serial.println(analogRead(A0));  
}

void loop()
{
  
}

adcOnOffOn.ino
ADCSRA = 135
Save ADCSRA
Set ADCSRA = 0
ADCSRA = 0
Analog read port A0 = 0
ADCSRA = 0
Restore ADCSRA
ADCSRA = 135
Analog read port A0 = 453

It seems to work just fine.

Thanks. I see now. I wasn't saving ADSCR.

Hi. I just tried doing that. But I wish I could use it in the loop. when I tried it didn't work.