Hi all,
For my project I'm facing an issue. The atMega328P reboots itself continuously(so it can't run it's code).
I have
- NRF24L01 set up as transmitter (with 10uF capacitor on Vcc / ground)
- 2x AA 2500mAh rechargeable batteries (Panasonic Eneloop) (2,4-2,5V in total)
- 1x atMega328p (8MHz
- 1x LED (which is used as power indicator, if you turn on the device it will be ON for 2 seconds and then turn OFF)
Technical details
Power consumption in standby: 0.02mA
Power consumption on transmitting: 7mA
How much used: daily base around 20 times for a couple of seconds.
With this details, I did assume my setup could run for at least a year
So currently I'm testing this project, it did run for around 2 weeks fine. After that the power indicator started blinking like it was continuously booting..Resetting gave the same results. When measuring the voltage, it was still 2.4V.
However, giving the batteries a bit of charge and putting them back in, seemed to fix it. But how can this happen?
Currently I'm performing the same test, but using 2x alkaline (1,5V). Not sure if it will fix the problem, since it runs for around a week now.
Is there something wrong in my code?
The receiver is DC powered, so I think only the transmitter code will do;
/*
=== file: THE BULB SoLooTion SYSTEM
=== version: V7.0
=== last update: 22-07-20
=== notes: transmitterCode
=== www.thebulb.nl
*/
#include <LowPower.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24Network.h>
RF24 radio(9, 10); // CE, CSN
//enable this package if it's the ladies version
struct package
{
int id=1; //id for ladies (1,2,3,4,5) id for gents (6,7,8,9,10) ; choose a desired ID
byte toiletStatus = 2; //2 = occupied ladies, 5 = occupied gents
char dataToSend ='1'; //auto-increment txNum
};
//enable this package if it's the gents version
/*
struct package
{
int id=6; //id for ladies (1,2,3,4,5) id for gents (6,7,8,9,10); choose a desired ID
byte toiletStatus = 5; //2 = occupied ladies, 5 = occupied gents
char dataToSend ='1'; //auto-increment txNum
};
*/
char dataToSend = '1';
typedef struct package Package;
Package data;
byte addresses[][6] = {"0"};
const byte ledPin = 8; //debug only
const byte interrupt_pin = 2;
unsigned long interval = 1000;
volatile byte state = LOW;
void wake(){
// cancel sleep as a precaution
sleep_disable();
// precautionary while we do other stuff
detachInterrupt (0); // gents
detachInterrupt (1); // ladies
} // end of wake
void setup() {
Serial.begin(9600); //temp
pinMode(ledPin, OUTPUT);
pinMode(interrupt_pin, INPUT);
for (int i = 0; i < 20; i++) {
if(i != 2)//just because the button is hooked up to digital pin 2
pinMode(i, OUTPUT);
}
clock_prescale_set(clock_div_256);
//Disable ADC - don't forget to flip back after waking up if using ADC in your application ADCSRA |= (1 << 7);
ADCSRA &= ~(1 << 7);
//ENABLE SLEEP - this enables the sleep mode
SMCR |= (1 << 2); //power down mode
SMCR |= 1;//enable sleep
//DISABLE BOD
MCUCR |= (3 << 5); //set both BODS and BODSE at the same time
MCUCR = (MCUCR & ~(1 << 5)) | (1 << 6); //then set the BODS bit and clear the BODSE bit at the same time
//SET CLOCK PROCESSOR
CLKPR = 0x80;
CLKPR = 0x01;
radio.begin();
radio.openWritingPipe( addresses[0]);
radio.setRetries(3, 5);
radio.setChannel(115);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate( RF24_250KBPS ) ;
radio.stopListening();
//boot light indicator
digitalWrite(ledPin, HIGH);
delay (2000);
digitalWrite(ledPin, LOW);
}
void loop() {
ADCSRA = 0;
// the interrupt must be attached each loop
attachInterrupt(digitalPinToInterrupt(interrupt_pin),interrupt_routine,RISING);
LowPower.powerDown(SLEEP_FOREVER,ADC_OFF,BOD_OFF); // sleep until interrupt
detachInterrupt(digitalPinToInterrupt(interrupt_pin)); // remove interrupt
// the usual wake routine that turns on the LED
if (state==HIGH){
send();
delay(250);
}
if (state==HIGH){
state = LOW;
digitalWrite(ledPin,LOW);
attachInterrupt(digitalPinToInterrupt(interrupt_pin),interrupt_routine,RISING);
delay(interval);
}
}
void interrupt_routine(){
state = HIGH;
}
void send(){
radio.write(&data, sizeof(data));
Serial.print("\n");
Serial.print("\nID:");
Serial.print(data.id);
Serial.print("\nToilet status:");
Serial.print(data.toiletStatus);
Serial.print("\nUnique number:");
Serial.print(data.dataToSend);
data.id = data.id;
data.toiletStatus = data.toiletStatus;
data.dataToSend = dataToSend +=1;
if (dataToSend >= '6'){
dataToSend = '0';
}
delay(100);
}
Here is the schematic, be aware!! I'M NOT USING AN ADAFRUIT PIR OR SIMILAR.
Instead I use the best PIR that exists in my opinion; the Panasonic EKMB series.
In my setup I use the EKMB12... range which consumes ultra-low power.
Oh and sorry, I forgot to draw one component. I use a 10uF capacitor on Vin and GND of the NRF24L01