Energy/ressource efficient way to wait for an event to happen

When I want the Arduino to wait for a button to be pressed or any other sort of event, I would usually do something like

void loop {
  if(digitalRead(5)){
    CODE TO BE EXECUTED
  }
}

Now what I guess the Arduino does is running the loop function at maximum speed even when the button connected to PIN 5 is not pressed for days.

I imagine that this will cost a lot of energy and possibly also wear off the processor very quickly. Is there any more efficient way to do this kind of thing or is this the standard way to do it?

One way is to put the processor to sleep. I use ESp32's so that will be the example I use.

The ESP32 can be put into a deep sleep mode where the current drawn by the main CPU gets down to 25uA. By using the ULP, that the 2nd processor of the ESP32, I can set up a program to 'watch' for a button press, record the button press, wake the main dual core CPU and pass the button press to the CPU.

1 Like

It won't wear out the processor, but you are correct that it consumes energy. You can put the processor into a deep sleep low power state where an input signal will wake it up.

Here's a useful tutorial: Tutorial:A guide to putting your Arduino to sleep — The Arduino Maker Man

1 Like

Thanks a lot guys, this was exactly what I was looking for. I'll mark the 2nd one as solution, because it's targeting the Arduino and provides a tutorial. But your answer was also helpful, @Idahowalker

The processor on an UNO draws about 45 milliamps (0.045 amps). Multiply by Volts (5) to get Watts: 0.225. That's about two kilowatt-hours a year. Unless you are running off a battery, the power use is negligible.