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:
- 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:
- 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.
- 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.
- Create the necessary directory: Create the required directory to store the pre-poweroff hooks:
- Download the service file and hook script: Fetch the required files for the pre-poweroff service and the check script:
- Grant execution permissions: Make the pre-poweroff script executable:
- Enable and start the pre-poweroff service: Reload the systemd daemon and enable the new service:
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:
- Update your system: Make sure your system has the latest updates by running:
- 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
- Enable the RTC overlay: Add the following line at the end of the file, after the [all] section:
- Restart the device: Save the changes and reboot the system to apply the configuration.
Uncomment the following line if it's commented:
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.
How to set up the UPS of a Raspberry based PLC