This circuit will be placed on a door with a magnetic reed switch to detect door change states.
Meaning, that if the door either opens or closes, the LED will blink and the circuit will go to deep sleep.
Note: The circuit design nor the code logic does not check whether the door is open or closed.
My goal is to run this circuit with a single Li battery.
So far this design with the code, is working to my expectations.
#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);
// Flash quick sequence so we know setup has started
for (int k = 0; k < 10; k = k + 1) {
if (k % 2 == 0) {
digitalWrite(statusLED, HIGH);
}
else {
digitalWrite(statusLED, LOW);
}
delay(250);
}
}
void sleep() {
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
PCMSK |= _BV(PCINT3); // Use PB3 as interrupt pin
ADCSRA &= ~_BV(ADEN); // ADC off
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // replaces above statement
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sei(); // Enable interrupts
sleep_cpu(); // sleep
cli(); // Disable interrupts
PCMSK &= ~_BV(PCINT3); // Turn off PB3 as interrupt pin
sleep_disable(); // Clear SE bit
ADCSRA |= _BV(ADEN); // ADC on
sei(); // Enable interrupts
}
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);
}
However, I'm seeing excessive power consumption during deep sleep.
When the reed switch is closed (connected to the ground potential), the chip wakes up blinks the LED and goes back to deep sleep mode. Power consumption shows now as 15mA.
Next, when the reed switch is open (floating), the chip wakes up blinks the LED and goes back to deep sleep mode. Now power consumption is in sub-microamps. which great.
How come there are differences in sleep mode power consumption depending on the reed switch state, mainly when it is closed?
You are right. When I wrote that, I didn't consider that calling the two instructions would have the same effect as if I wrote registers directly. I only paid attention to the INPUT parameter.
With the switch closed it will draw more than if it is open but should only be at most 165uA.
Can't explain why it's 15mA unless your meter is wrong or the 85 is damaged.
Sorry for been silent such along time as been not well.
The meter was faulty.
When the switch is closed, as you've suggested it draws about 0.131mA.
And when open Zero.
May I ask, is there a way that I could have this working at 2 or 3 uA level?
Maybe using some other technique?
Anyone have an idea?
Because this project i've been following for sometime now, has been able to achive that.
Since you now appear to have a working meter you can experiment with using a weak external pullup resistor instead of the internal pullup resistor.
If you don't have to register the change in state of the gate immediately, you could wake the microcontroller every say 8 seconds, switch on the pullup resistor and test the state of the switch, act as necessary, then switch off the pullup and sleep again.
I stopped that video after a few seconds. Can you find a schematic and code for the ESP solution which does not involve sitting through a special effect loaded video ?
sorry, there is none for the latest version.
Apparently he is selling this product in Tindy.
However, there is a schemetic for his previouse version of the board, but it only detects a single state change (either open or close).
That project has, in the mean time, been migrated to an ESP32.
Anyway, he appears to achieve very low power not by an inbuilt sleep mode of the ESP but by externally detecting an event using low power discrete components, then powering up the ESP for the duration of the event processing, data transmission etc. then shutting down the ESP.
The (old) circuit looks very elaborate for that task but I may have missed some subtleties by a quick scan.