0
0
Arduinoprogramming~7 mins

Reducing power consumption tips in Arduino

Choose your learning style9 modes available
Introduction

Saving power helps your Arduino run longer on batteries. It also keeps your project cool and efficient.

When building battery-powered devices like remote sensors.
In projects where you want to extend battery life as much as possible.
For wearable gadgets that need to run all day without charging.
When making solar-powered Arduino projects.
To reduce heat and energy waste in any Arduino setup.
Syntax
Arduino
// Example: Using sleep mode to save power
#include <avr/sleep.h>

void setup() {
  // Prepare to sleep
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
}

void loop() {
  // Go to sleep
  sleep_cpu();
  // Arduino sleeps here until an interrupt wakes it
}

Sleep modes help Arduino use less power by turning off parts it doesn't need.

You can choose different sleep modes depending on how much power you want to save.

Examples
Disables parts like ADC, SPI, and timers to save power when they are not needed.
Arduino
// Turn off unused modules
power_adc_disable();
power_spi_disable();
power_timer0_disable();
Slowing down the clock reduces power use but also slows the program.
Arduino
// Reduce clock speed to save power
// Not all boards support this
// Example for AVR boards
CLKPR = (1 << CLKPCE);
CLKPR = (1 << CLKPS1) | (1 << CLKPS0); // Divide clock by 8
Arduino sleeps until a button press or signal on pin 2 wakes it up.
Arduino
// Use sleep mode with interrupt to wake up
attachInterrupt(digitalPinToInterrupt(2), wakeUp, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
Sample Program

This program puts the Arduino to sleep to save power. It wakes up when you press a button connected to pin 2. It also turns off some parts to save more power.

Arduino
#include <avr/sleep.h>
#include <avr/power.h>

void wakeUp() {
  // This function is called when Arduino wakes up
}

void setup() {
  pinMode(2, INPUT_PULLUP); // Button on pin 2
  attachInterrupt(digitalPinToInterrupt(2), wakeUp, LOW);

  // Turn off ADC and SPI to save power
  power_adc_disable();
  power_spi_disable();

  // Set sleep mode to power down
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();

  Serial.begin(9600);
  Serial.println("Going to sleep...");
}

void loop() {
  sleep_cpu(); // Arduino sleeps here
  sleep_disable();
  Serial.println("Woke up!");
  delay(1000); // Wait a bit before sleeping again
  sleep_enable();
}
OutputSuccess
Important Notes

Always test your sleep and wake-up code carefully to avoid your Arduino getting stuck asleep.

Turning off unused modules can save a lot of power but make sure you turn them back on if needed.

Using interrupts to wake up is better than constantly checking inputs in a loop.

Summary

Use sleep modes to reduce power when Arduino is idle.

Turn off unused hardware modules to save energy.

Use interrupts to wake Arduino from sleep efficiently.