Requirements
Introduction
It is always important to save energy for environmentally-friendly reasons but, especially, in certain applications which consume cannot surpass specific thresholds. Because of these reasons, in this post we are going to see how to implement the Arduino Low-Power library with our industrial PLC's to save a small amount of energy that can be crucial on a lot of occasions. In the case of the M-Duino Family, the consume saved when the library function is applied is around 10mA respect the total PLC controller consumes, which is 70mAÂ approximately without using I/Os or special communications.Â
Application
We are going to see two example codes. Both of them use the Low.Power.idle() function which, depending on a time parameter, turn off for this defined period some peripherals of the PLC Arduino. These peripherals depend on the CPU of the board that the PLC has (see the table below) and they mainly feature like ADC, TIMER's, USART's, SPI and TWI (I2C), that can be configured in the function of your requirements, choosing to turn them ON, OFF or directly not acting on them. We must take into account that the examples that we are going to see are based on M-Duino, but can be adapted to Ardbox or 10 I/O's too (you will have to comment/uncomment the code blocks according to your industrial controller PLC).Â
PLC Family | Arduino Board | CPU |
M-Duino | Mega | ATmega2560 |
Ardbox | Leonardo | ATmega32U4 |
10 I/O's | Nano | ATmega328P |
Idle Wake Periodic
In this code, there is no need to initialize the LowPower library in the setup part, only the functions that you are going to require, like for example the Serial. In the loop part, it is going to be executed the commented LowPower.idle() function. The first parameter of this one is the period of actuation time, in this case, 8 seconds, and the other parameters are the peripherals that can be turned ON or OFF with this function, taking into account they are usually ON by default and that you are going to turn them OFF. All of these configuration parameters can be consulted in the list shown on the keywords.txt file, located inside the library. After this period of low-power for the defined peripherals, you can add your required functions such as reading sensor data. It is very important to consider all the parameters that are going to be disabled temporarily and if you require them or not because, for example, if you turn off the USART0 you will not be able to use the Serial0 for applications like printing messages for the defined period.Â
#include "LowPower.h" void setup() { Serial.begin(9600); } void loop() { Serial.println("Starting Low-Power Mode in 3 seconds"); delay(3000); // Enter idle state for 8 s with the rest of peripherals turned off // Each microcontroller comes with different number of peripherals // Comment off line of code where necessary // ATmega2560 (Arduino Mega: M-Duino) LowPower.idle(SLEEP_8S, ADC_OFF, TIMER5_OFF, TIMER4_OFF, TIMER3_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART3_OFF, USART2_OFF, USART1_OFF, USART0_OFF, TWI_OFF); // ATmega328P, ATmega168 (Arduino Nano: 10I/Os Nano ) //LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, // SPI_OFF, USART0_OFF, TWI_OFF); // ATmega32U4 (Arduino Leonardo: Ardbox) //LowPower.idle(SLEEP_16S, ADC_OFF, TIMER4_OFF, TIMER3_OFF, TIMER1_OFF, // TIMER0_OFF, SPI_OFF, USART1_OFF, TWI_OFF, USB_OFF); // Do something here // Example: Read sensor, data logging, data transmission. Serial.println("Do something here"); }
Idle Wake External Interrupt
#include "LowPower.h" void setup() { Serial.begin(9600); } void loop() { Serial.println("Starting Low-Power Mode in 3 seconds"); delay(3000); attachInterrupt(digitalPinToInterrupt(I0_5), wakeUp, RISING); // ATmega2560 (Arduino Mega: M-Duino) LowPower.idle(SLEEP_FOREVER, ADC_OFF, TIMER5_OFF, TIMER4_OFF, TIMER3_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART3_OFF, USART2_OFF, USART1_OFF, USART0_OFF, TWI_OFF); // ATmega328P, ATmega168 (Arduino Nano: 10I/Os Nano ) //LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, // SPI_OFF, USART0_OFF, TWI_OFF); // ATmega32U4 (Arduino Leonardo: Ardbox) //LowPower.idle(SLEEP_16S, ADC_OFF, TIMER4_OFF, TIMER3_OFF, TIMER1_OFF, // TIMER0_OFF, SPI_OFF, USART1_OFF, TWI_OFF, USB_OFF); detachInterrupt(digitalPinToInterrupt(I0_5)); Serial.println("After interruption!"); } void wakeUp() { //Put here the code that you want to execute when the interrpution is executed Serial.println("INTERRUPT I0_5"); }
Low-Power Arduino Library PLC Application