Atduino: go to sleep tiny ATtiny


ATtiny testing sleep and brown-out

And that involves setting pins to ‘interrupt-on-change’ and getting the chip to ‘wake-on-interrupt’ – all stuff I could have done in assembler, but how to do in Arduino-speak.

Big Dan rides to the rescue

Luckily for me, someone called Big Dan has picked the bones out of the problem and produced this excellent clear blog.

Take a look at the code to see how neat it is (this is simplified, see Big Dan’s page for full version):

#include <avr/sleep.h>
#include <avr/interrupt.h>
const int switchPin                     = 3;
const int statusLED                     = 2;

void setup() {
    pinMode(switchPin, INPUT); 
    digitalWrite(switchPin, HIGH);
    pinMode(statusLED, OUTPUT);
    
void sleep() = _BV(PCIE);                     // Enable Pin Change Interrupts
    PCMSK  // sleep
ISR(PCINT0_vect) 
    // This is called when the interrupt occurs, but I don't need to do anything in it
    

void loop() 
    sleep();
    digitalWrite(statusLED, HIGH);
    delay(1000);
    digitalWrite(statusLED, LOW);
    

The sleep routine is a separate function, called from the main loop.

As usual with Arduino, the ‘pin numbers’ aren’t the actual chip pin numbers, unless by coincidence. The led pin was correct for me, only the push button pin needed reassigning (I added a push button header to the little programmer made before).

It worked immediately – thanks Big Dan – waking and flashing the led on both push-button edges- so both press and release.

Consumption in sleep was, as I remember, 69μA – which includes 20μA from having the brown-out detector enabled, but not the 150μA ADC, which is turned off by the code.
This should be more like 1μA + brown-out, so a little bit more research is required – for example, the analogue comparator takes 35μA and I am not sure if this is on or off by default.

 



Source link

Leave a Reply