Code alterations to perhaps save power

Hi all,

I’ve got a simple motion-sensing nightlight for our bathroom, and I’m looking for ways to make it use less battery power. I find that the two 2000 mA*H AAs last about 14 days before I need to recharge them. It’s a PIR sensor that, when triggered, makes a single standard 5mm LED light up for 20 seconds – but only if the light sensor registers that the light level is below a given threshold. The MCU is an ATtiny85 bootloaded to 1 MHz (internal oscillator, BOD disabled).

I wouldn’t mind incorporating some sleep functionality into the ATtiny85 – ideally, I’d LOVE to have the MCU be asleep until motion is detected, then do a quick check of the light levels and light the LED if needed. Any ideas? I’ve used the watchdog timer for this chip in other applications; just unsure how I’d use it for this one where everything gets triggered off of the PIR sensor signal.

Here’s the code I’m currently using:

// ATMEL ATTINY85 / ARDUINO
//
//                  +-\/-+
// Ain0 (D 5) PB5  1|    |8  Vcc
// Ain3 (D 3) PB3  2|    |7  PB2 (D 2) Ain1
// Ain2 (D 4) PB4  3|    |6  PB1 (D 1) pwm1
//            GND  4|    |5  PB0 (D 0) pwm0
//                  +----+


const int LightSensorPin = 3;    // analog in, to ADC3 for light sensor, pin 2 on ATtiny85
const int ledPin = 0;
const int LightLevelThreshold = 200;

const int PIRsensorPin = 4;  // digital in, to ADC1 for light sensor, pin 7 on ATtiny85

void setup()
{
  pinMode(ledPin, OUTPUT);   // declare LED as output
  pinMode(LightSensorPin, INPUT); // declare photoresistor as input
  pinMode(PIRsensorPin, INPUT);  // declare PIR sensor as input
}

void loop()
{
  int rate = analogRead(LightSensorPin);
  if (rate < LightLevelThreshold) // then too dark to see bowl
  {
    int val = digitalRead(PIRsensorPin);  // read input value
    if (val == HIGH)                  // check if the input is HIGH
    {
      analogWrite(ledPin, 255);     // turn LED on if motion detected
      delay(20000);
      analogWrite(ledPin, 0);      // turn LED off
    }
  }
  else
 {
   digitalWrite(ledPin, LOW);
  }
}

Thanks for the help!
Tom

tlharv:
I wouldn’t mind incorporating some sleep functionality into the ATtiny85 – ideally, I’d LOVE to have the MCU be asleep until motion is detected, then do a quick check of the light levels and light the LED if needed. Any ideas?

From your code, it appears that the input from the PIR goes high when motion is detected. So you can’t use INT0 because only a LOW on that pin will wake the chip up from sleep mode. However, you can use a pin change interrupt to wake up the mcu from sleep mode when the state of that input changes.