Squeezing as much operation time out of an Arduino Uno powered by a battery can be a struggle, but it’s not as daunting as it might seem. You just need to know the right tricks.
I’ll dive into some power-saving techniques for your Arduino Uno in this article.
Understanding the Basics of Power Consumption
Arduino Uno boards, like many microcontrollers, use power even when they’re not performing any tasks. It’s similar to how a lightbulb still draws electricity when you dim it, just not as much.
The Arduino’s microcontroller has a clock that ticks billions of times a second. Each tick consumes a tiny bit of power. When doing complex calculations, the clock must tick more often, using more power.
But what if you could slow down that clock? Or even stop it when you’re not using it? That’s where power saving comes in.
Slowing Down the Clock
The Atmega328P on the Arduino Uno runs at 16 MHz. This is not as much as your computer’s several gigahertz of CPU clock, but it is respectable. I remember that the clock speed of my first computer, an Apple //e’s 6502, was just one megahertz.
Just like driving a race car in the city, you can save on fuel if you slow down.
By slowing down the clock, you can reduce power consumption. Arduino allows you to set the clock speed, which is a great way to save power. It’s called changing the clock “prescaler“.
Consider this: if your project involves monitoring a plant’s soil moisture, you don’t need a reading every millisecond. Slowing down the clock could be the perfect power-saving technique.
Here’s an example of how you’d do this using Arduino’s clock_prescale_set
function:
#include <avr/power.h>
void setup() {
clock_prescale_set(clock_div_8); // reducing the clock speed by factor of 8
}
void loop() {
// Your code here...
}
Utilizing Sleep Mode
If slowing down the clock is like a leisurely stroll, sleep mode is like napping. The Atmega328P on the Arduino Uno has a variety of sleep modes that can reduce power consumption even further.
For example, you can use the ‘Power Down’ mode to halt the microcontroller’s clock effectively. The only way to wake it up is with an external interrupt or a reset.
A common way to implement sleep mode involves a momentary push-button. When you press the button, an interrupt wakes the Arduino. Here’s a simple setup for this:
#include <avr/sleep.h>
const int buttonPin = 2; // button connected to pin 2
void wakeUp() {
// interrupt service routine
}
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonPin), wakeUp, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
}
void loop() {
sleep_enable();
attachInterrupt(digitalPinToInterrupt(buttonPin), wakeUp, LOW);
sleep_cpu(); // Sleep now. Continue from here after waking up
sleep_disable(); // Prevents sleep mode from reactivating
}
Reducing the On-Time of Components
You may think your Arduino is the power hog, but your peripherals can eat power too. LEDs, motors, and sensors all consume power. You can save quite a bit of juice by only powering them on when needed.
For instance, consider a project where an LED blinks for user input. You could set the LED to be on only when the button is pressed:
const int buttonPin = 2; // button connected to pin 2
const int ledPin = 13; // LED connected to pin 13
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // turn on LED only if button is pressed
} else {
digitalWrite(ledPin, LOW); // turn off LED otherwise
}
}
Using Interrupts Over Polling
Checking or ‘polling’ a sensor constantly uses more power than necessary. Instead, use interrupts. These tell your Arduino to wake up when a sensor needs attention, saving power.
As shown in the sleep mode example above, external interrupts are a great way to trigger a function only when necessary, rather than continuously checking a condition in the loop() function.
I have written other articles about interrupts, which you should check out if you are interested in more details.
Closing Thoughts
Power saving is all about efficiency, using resources only when necessary. You can optimize your Arduino Uno projects for longer battery life using these techniques.
For more in-depth information, here are some recommended resources:
- Nick Gammon’s guide on power saving
- Arduino Low Power library
- Arduino official documentation on interrupts
Also, be sure to explore other articles on TechExplorations for more Arduino tips and tricks. Don’t forget, the key is to experiment and have fun while doing it!