Low-Power Arduino Library PLC Application

Controller Arduino PLC
September 30, 2021 by
Low-Power Arduino Library PLC Application
Boot & Work Corp. S.L., Quesada Dani Salvans

Requirements

Arduino based

PLC

 

How to use Interrupt Inputs with industrial Arduino boards

 

Arduino Low-Power Library


Low-Power GitHub Repository

 

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 FamilyArduino BoardCPU
M-Duino MegaATmega2560
ArdboxLeonardoATmega32U4
10 I/O'sNanoATmega328P


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

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, the interrupt is initialized, here you must take into account the attributes of the function; the pin used as interruption with the corresponding function digitalPinToInterrupt() in this case digitalPinToInterrupt(I0_5), the handler function where you must put everything that you want to be executed when the interruption is activated, in this case, wakeUp, and the mode of interruption activation, in this case RISING edge. After that, the LowPower.idle() function is activated considering the same previously commented points but, in this case, with a different period: SLEEP_FOREVER which will turn the referred peripherals off until the interruption is activated. Afterward, there is the detachInterrupt() function, and you can add code in the loop part after that, but it is only going to be executed just after the interruption handler code. 

#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");
}
Your Dynamic Snippet will be displayed here... This message is displayed because you did not provided both a filter and a template to use.

​Search in our Blog

Low-Power Arduino Library PLC Application
Boot & Work Corp. S.L., Quesada Dani Salvans September 30, 2021

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>