1
Advanced Arduino Coding Power Saving
BY ENG. HOSSAM ARAFA
Outline •
Power Management and Sleep Modes
•
Various Power Saving Options
•
Components may consume high Energy rates
•
Different Sleep Modes
•
How to put Arduino into Sleep
•
The difference between sleep_cpu() and sleep_mode()
•
Turning Turni ng off brown-out detection & ADC
•
Turning on or off selected modules
•
Uploading to Atmega328 Using an Arduino Board
•
Examples
•
References
2
3
Power Management and Sleep Modes •
•
•
•
Sleep modes enable the application to shut down unused modules in the MCU, thereby saving power. The AVR provides various sleep modes allowing the user to tailor the power consumption to the application’s requirements. To get the most out of the battery time it is necessary for the processor to sleep whenever it can. But the avr-sleep (or any other sleep-mode) wont be much of a point if you have an ordinary Arduino-, Nano, Mega, UNO or any other that has a voltage-regulator on it, and other components that draw lot of power. The voltage regulator consumes 7mA continuously … When enabled, the Brown-out Detector (BOD) actively monitors the power supply voltage during the sleep periods. To further save power, it is possible to disable the BOD in some sleep modes.
Various Power Saving Options Use as many of the techniques listed here as are practical in your application •
Run the processor at a lower frequency
•
Run the processor at a lower voltage
•
Turn off unneeded internal modules in software (e.g. SPI, I2C, Serial)
•
Turn off brownout detection (BOD)
•
Turn off the Analog-to-Digital converter (ADC)
•
Turn off the watchdog timer
•
Put the processor to sleep
•
Don't use inefficient voltage regulators - if possible run directly from batteries
•
Don't use power-hungry displays (e.g. indicator LEDs, backlit LCDs)
•
Arrange to wake the processor from sleep only when needed
•
Turn off (with a MOSFET) external devices (e.g. SD cards, temperature sensors) until needed
4
Components may consume high Energy rates by using the development board •
Voltage regulators (for +5V and +3.3V)
•
A USB interface chip (for the USB port)
•
A "power" LED
Atmega328p processor on its own Using a "bare bones" board saves quite a bit of power.
Figure 1 – Sleep Modes
5
6
Different Sleep Modes After trying a simple program that is toggling LED Without sleep 11.74 mA When LED_OFF Period (LED_ON ~ 17.5 mA) •
SLEEP_MODE_IDLE: 5.5 mA
•
SLEEP_MODE_ADC: 3.4 mA
•
SLEEP_MODE_PWR_SAVE: 1.6 mA
•
SLEEP_MODE_EXT_STANDBY: 1.6 mA
•
SLEEP_MODE_STANDBY : 0.86 mA
•
SLEEP_MODE_PWR_DOWN : 0.29 mA
•
SLEEP_MODE_PWR_DOWN + disable ADC : 0.02 mA ~ 22 uA
Figure 2 – Sleep Modes
7
Cont. Different Sleep Modes After trying a simple program that is toggling LED
Video 1 – The Application Without sleep (Arduino dev. Board)
8
Cont. Different Sleep Modes After trying a simple program that is toggling LED
Video 2
The Application Without sleep
Cont. Different Sleep Modes After trying a simple program that is toggling LED with ADC Disable and Power Down Sleep Mode
Video 3
The Application With sleep + ADC Disable (Arduino dev. Board)
9
10
Cont. Different Sleep Modes After trying a simple program that is toggling LED
SLEEP_MODE_IDLE
SLEEP_MODE_ADC
SLEEP_MODE_STANDBY
SLEEP_MODE_PWR_SAVE
SLEEP_MODE_PWR_DOWN
Figure 3
SLEEP_MODE_EXT_STANDBY
SLEEP_MODE_PWR_DOWN + disable ADC
Different Sleep Modes
11
Cont. Different Sleep Modes After trying a simple program that is toggling LED Without Sleep Assume Battery : 5V - 800 mAh Time span = 800 mAh / 11mA = 72h ---------------------------------With Power Down + disable ADC Time span = 800 mAh / 0.02 mA = 40000h = 40000/24 = 1667 day = 4.6 Years
Figure 4 – PWR_DOWN + disable ADC
12
How to put Arduino into Sleep #include
void Init_Sleep() { set_sleep_mode (SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_cpu (); sleep_disable(); } // end of Function void setup () {} void loop ( ) { }
The difference between sleep_cpu() and sleep_mode() Quoting from Power Management and Sleep Modes Library Atmel AVR Lib Reference
Figure 5 – sleep_cpu
Figure 6 – sleep_mode
13
14
Turning off brown-out detection & ADC #include void Init_sleep () { // With that there the power consumption drops a large amount ADCSRA = 0; // disable ADC set_sleep_mode (SLEEP_MODE_PWR_DOWN); sleep_enable(); // turn off brown-out enable in software // MCUCR |= (1<
Turning on or off selected modules Depending on #include Various macros (they vary by processor) let you power on or off individual modules: Enabling: power_adc_enable(); // ADC converter power_spi_enable(); // SPI
#include
power_usart0_enable(); // Serial (USART)
void setup ()
power_timer0_enable(); // Timer 0
{
power_timer1_enable(); // Timer 1 power_timer2_enable(); // Timer 2 power_twi_enable(); // TWI (I2C) -------------------------------------------------------Disabling: power_adc_disable(); // ADC converter power_spi_disable(); // SPI power_usart0_disable();// Serial (USART) power_timer0_disable();// Timer 0 power_timer1_disable();// Timer 1 power_timer2_disable();// Timer 2 power_twi_disable(); // TWI (I2C)
ADCSRA = 0; // disable ADC power_all_disable (); // turn off all modules } // end of setup void loop ( ) { }
15
16
Uploading to Atmega328 Using an Arduino Board If you have a new ATmega328 (or ATmega168), you'll need to burn the Bootloader onto it. Click Here to shows How to burn Bootloader onto Atmega328p Once your ATmega328p has the Arduino bootloader on it, you can upload programs to it using the USB-to-serial convertor (FTDI chip) on an Arduino board. To do, you remove the microcontroller from the Arduino board so the FTDI chip can talk to the microcontroller on the breadboard instead. The diagram at right shows how to connect the RX and TX lines from the Arduino board to the ATmega on the breadboard. To program the microcontroller, select Tools > Programmer > Arduino as ISP
Figure 7
Circuit Schematic
Examples Initially: How to Use a Multimeter to measuring Current?
17
18
Cont. Examples This Application sending a sensor reading every half second and implementation of sleep mode and wake-up by WDT(0.5s)
Figure 9 – Application 1
Cont. Examples
19
20
References I would like to thank all the sources which this information is collected from them, and I wish them every success. •
arduino.cc
•
Atmega328p Datasheet
•
http://playground.arduino.cc/Learning/ArduinoSleepCode
•
http://www.atmel.com/webdoc/AVRLibcReferenceManual/group__avr__slee p.html
•
http://www.gammon.com.au/forum/?id=11497
•
http://www.nongnu.org/avr-libc/user-manual/group__avr__sleep.html
•
http://donalmorrissey.blogspot.com.eg/2010/04/putting-arduino-diecimila-to-sleep-part.html
Contact Information I hope if you use the parts of the slides, mentioned the source in order not waste this effort.
[email protected]