How to set up the UPS of a Raspberry based PLC

October 8, 2024 by
How to set up the UPS of a Raspberry based PLC
Boot & Work Corp. S.L., Martí Schnabel

Enchancing Raspberry Pi PLC with UPS and RTC capabilities

Discover how Raspberry Pi-based PLCs integrate crucial functionalities like Uninterruptible Power Supply (UPS) and Real-Time Clock (RTC), boosting both reliability and accuracy for your automation projects.These functionalities are configured automatically via the provided installation scripts—one for the RPI PLC V4 and another for the RPI PLC V6. Each script (install.sh) sets up these features along with other essential components. However, when using UPSafePI, these scripts are not required. This post will explain how to set up the UPS and RTC features and how to effectively utilize them. For more information and technical specifications about UPS Shield check out this post.

UPS Shield: Maximizing Raspberry Pi PLC uptime

All the Raspberry Pi- based PLC include the UPS Shield. It is an UPS Smart Shield that provides 5 seconds of electricity in front of a power supply cut off. This extra time of electricity provides that the Raspberry follow a correct Shut Down process, avoiding any SD corruption issue. The recharge time of the UPS is less than 1 minute, and the reconnecting time is less that 20 seconds.

How to enable controlled shutdown for Raspberry Pi PLC?

The Raspberry based PLCs can perform a controlled shutdown once the UPS detects that the main power supply has been cut. This ensures all active processes are ended and the SD card image and files are protected.

To enable the controlled shutdown on the Raspberry Pi, follow these steps:

  1. Modify the configuration file: The process is now managed using device tree overlays. Add the following two lines to the /boot/firmware/config.txt file:
  2. dtoverlay=gpio-poweroff,gpiopin=23,active_low
    dtoverlay=gpio-shutdown,gpio_pin=24,gpio_pull=up
    
    • The gpio-poweroff overlay ensures the system powers off gracefully using GPIO pin 23.
    • The gpio-shutdown overlay detects when the system should shut down using GPIO pin 24.
  3. Save and reboot: After editing and saving the file, reboot the Raspberry Pi to apply the changes.

For all Raspberry PLC devices except UPSafePI, the set up of the UPS can be done by executing the installation script for V4 or for V6. Follow this tutorial for more information about how to install an operative system to the Raspberry and about execution of the installation script.

The system will now perform a controlled shutdown when the main power is lost, ensuring the protection of data and processes.

Pre-power off commands: safeguarding your data

Before your system powers down, ensure all critical operations are completed using pre-poweroff hooks. This guide provides detailed steps on setting up directories, downloading necessary service files, and scripts to automate crucial tasks before shutdown.

  1. Create the necessary directory: Create the required directory to store the pre-poweroff hooks:
  2. sudo mkdir -p /etc/rpishutdown/hooks
    
  3. Download the service file and hook script: Fetch the required files for the pre-poweroff service and the check script:
  4. sudo curl -L https://apps.industrialshields.com/main/rpi_experimental/rpiplc/rpishutdown-pre-poweroff.service \
         -o /lib/systemd/system/rpishutdown-pre-poweroff.service
    sudo curl -L https://apps.industrialshields.com/main/rpi_experimental/rpiplc/check-pre-poweroff-hook.sh \
         -o /etc/rpishutdown/hooks/check-pre-poweroff
    
  5. Grant execution permissions: Make the pre-poweroff script executable:
  6. sudo chmod ugo+x /etc/rpishutdown/hooks/check-pre-poweroff
    
  7. Enable and start the pre-poweroff service: Reload the systemd daemon and enable the new service:
  8. sudo systemctl daemon-reload
    sudo systemctl enable rpishutdown-pre-poweroff
    sudo systemctl start rpishutdown-pre-poweroff
    

Any type of file (Bash Scripts, Python, C++, etc.) can be executed, but the most important thing is that the file must be called pre-poweroff without any extension (and you must indicate the file type with the corresponding Shebang).  The script execution time cannot surpass ~6 seconds, as it is the Raspberry’s Power Off period.

This setup ensures that any custom commands in the check-pre-poweroff script will be executed before the system powers off.

Real-Time Clock (RTC) setup: ensuring time precision

Integrating the DS3231 RTC with your Raspberry Pi PLC can be a game-changer, especially for low-power applications where time accuracy is paramount. Follow these detailed steps to enable and configure the RTC module, ensuring your system keeps accurate time, even when powered down.

Benefits of using an RTC:

  • Low power consumption, ideal for systems running on alternate power sources.
  • Frees the main system from handling time-keeping tasks.
  • Can be more accurate than other time-keeping methods.

Follow these steps to enable RTC functionality on your Raspberry based PLC:

  1. Update your system: Make sure your system has the latest updates by running:
  2. sudo apt-get update
    sudo apt-get -y upgrade
    
  3. Enable I2C on the Raspberry Pi: Open the configuration file based on your system version:
    • For Bullseye: /boot/config.txt
    • For Bookworm or later: /boot/firmware/config.txt
  4. Uncomment the following line if it's commented:

    dtparam=i2c_arm=on
    
  5. Enable the RTC overlay: Add the following line at the end of the file, after the [all] section:
  6. dtoverlay=i2c-rtc,ds3231
    
  7. Restart the device: Save the changes and reboot the system to apply the configuration.

As for the UPS service, for all Raspberry PLC devices except UPSafePI, the set up of the UPS can be done by executing the installation script for V4 or for V6. Follow this tutorial for more information about how to install an operative system to the Raspberry and about execution of the installation script.

Leveraging UPS and RTC for advanced Raspberry Pi projects

Capitalize on the full potential of your Raspberry Pi PLC by implementing UPS and RTC functionalities. This setup not only enhances reliability but also expands the capabilities of your projects. Dive deeper into technical specifications and additional resources to maximize your PLC's performance.

References and further reading

For those looking to expand their knowledge or troubleshoot potential issues, this section provides links to additional resources and detailed documentation, ensuring you have access to all the information you need to succeed with your Raspberry Pi PLC projects.


​Search in our Blog

How to set up the UPS of a Raspberry based PLC
Boot & Work Corp. S.L., Martí Schnabel October 8, 2024
Share this post
Tags

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.

PLC Comparison