Context
I am building a small battery powered counting device. As with many other projects like this, saving power is critical. I am therefore trying to utilise the sleep mode wherever possible. This is using a 328P as a standalone MCU running on the 1MHz internal clock
The Hall Effect sensor I am using outputs Vcc when nothing is detected, and Ground when something is detected. It therefore seems well suited to being used as a trigger for an interrupt to wake up the 328P from sleep (so that I don't even need to use a watchdog timer.)
Below is an excerpt from my code. This code does put the processor into sleep as desired. However, from testing I have discovered that the device will not sleep when the magnet is left near the sensor, as its output stays LOW. As I only want to detect pulses of a moving piece of metal, this is wasting power. I only want to wake up when it is initially detected on each cycle.
void sleepLoop()
{
Serial.print("S");
delay(5); //scales to 40ms
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
noInterrupts();
attachInterrupt(digitalPinToInterrupt(countInterruptPin), sleepInterrupt, LOW);
interrupts();
sleep_cpu();
//The program will continue from here.
//First thing to do is disable sleep.
sleep_disable();
}
I therefore changed
attachInterrupt(digitalPinToInterrupt(countInterruptPin), sleepInterrupt, LOW);
to
attachInterrupt(digitalPinToInterrupt(countInterruptPin), sleepInterrupt, FALLING);
PROBLEM
Now my processor never goes to sleep. For debugging I have put in a serial print at the start of the loop so that I know it has entered this loop of code. Whether LOW
or FALLING
is used the 'S' is printed. I only know it is not sleeping because I am measuring the current flow.
I thought the sensor may be going LOW long enough to keep the device awake, but not long enough to register as a count (as I have a debounce in there) so I have checked the output from the sensor with an oscilloscope in case it was momentarily going LOW
, but I couldn't see any sign of this.
The findings from the oscilloscope make me feel like there is something about the interrupt function that I am overlooking. Any help would be greatly appreciated!
S
printed when signal is low (and sometimes when it goes back high). Are you sure your hall-effect sensor doesn't need a pull-up resistor? What kind of serial output do you get? What is the power-usage (normal, during sleep, during "apparently not sleep")?Serial.begin(38400); //Use Baud rate at 4800 on PC (due to 1MHz clock speed) // (38400/8)=4800