rocketscream / Low-Power

Low Power Library for Arduino
www.rocketscream.com
1.26k stars 345 forks source link

sir, i am running standalone atmega328P 5v 8MHz ( with internal oscillator) and RCWL-0516 Microwave Radar Sensor with low power library #125

Closed myhobby-projects closed 1 month ago

myhobby-projects commented 1 month ago

sir thanks for sharing low power library on github....

sir, i am running standalone atmega328P 5v 8MHz (with internal oscillator) and i am using RCWL-0516 Microwave Radar Sensor for sending interrupts

the code works perfectly for atmega328P 5v 8MHz(with internal oscillator) without low power library..

but when low power library is used. RCWL-0516 Microwave Radar Sensor responds some times with 10 seconds delay and some times it will not work at all and some times it works very quickly... sir what i meant to say is,the entire behavior is erratic when low power library is used at 8MHz( internal oscillator) with external interrupt

sir below sketch is very simple code that lights up led when the RCWL-0516 detects the movement..for the below code i used 5v supply for atmega328p and also for the RCWL-0516

// ** INCLUDES ***

include "LowPower.h"

volatile byte state = LOW; const byte ledPin = 6;

// Use pin 2 as wake up pin const int wakeUpPin = 2;

void wakeUp() { //Serial.println("state changed"); state = !state; // Just a handler for the pin interrupt. }

void setup() { // Configure wake up pin as input. // This will consumes few uA of current. pinMode(wakeUpPin, INPUT);
pinMode(ledPin, OUTPUT); }

void loop() { // Allow wake up pin to trigger interrupt on low. attachInterrupt(0, wakeUp, CHANGE);

// Enter power down state with ADC and BOD module disabled.
// Wake up when wake up pin is low.
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 

// Disable external pin interrupt on wake up pin.
detachInterrupt(0);
digitalWrite(ledPin, state);

// Do something here
// Example: Read sensor, data logging, data transmission.

}

sir kindly help me, as the google search didnt provide any solution to this behavior.. i am trying to use 8MHz (with internal oscillator).so that i can power the atmega328p directly with battery(3.7v)

myhobby-projects commented 1 month ago

sir,i found the solution to my problem...i understood that since internal 8MHz oscillator of atmega3298p is slow in waking the micro-controller after sleep.so the atmega328p couldnt detect the 2seconds pulse generated at the interrupt pin by RCWL-0516 microwave sensor..

so i have changed the code in below line attachInterrupt(0, wakeUp, CHANGE);

instead of detecting the fast transition of output from low to high..i changed the code to detect the "HIGH" signal present at the RCWL-0516 output pin.

attachInterrupt(0, wakeUp, HIGH);

now my code works fine without any erratic behavior

working code

// ** INCLUDES ***

include "LowPower.h"

volatile byte state = LOW; const byte ledPin = 13;

// Use pin 2 as wake up pin const int wakeUpPin = 2;

void wakeUp() { //Serial.println("state changed"); state = !state; // Just a handler for the pin interrupt. }

void setup() { // Configure wake up pin as input. // This will consumes few uA of current. pinMode(wakeUpPin, INPUT);
pinMode(ledPin, OUTPUT); }

void loop() { // Allow wake up pin to trigger interrupt on low. attachInterrupt(0, wakeUp, HIGH);

// Enter power down state with ADC and BOD module disabled.
// Wake up when wake up pin is low.
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 

// Disable external pin interrupt on wake up pin.
detachInterrupt(0);
digitalWrite(ledPin, state);

// Do something here
// Example: Read sensor, data logging, data transmission.

}