In this blog we would like to show some of the features of our product IA004 -Rpi UPS Board using Raspberry Pi and Node RED.
Figure 1: Block Diagram
- Raspberry pi.
- ARMtronix board (IA004).
- 3.7V Li-Ion/Li-Po Battery.
- RTC Battery.
- Sensors with 4-20mA current output.
Figure 2 LED Representation
Table 1: LED STATE
|LED No||ON State||OFF State|
|LED1(D2)||When the board is running on Battery||When the mains supply is present.|
|LED2(D6)||When the battery is charging.||When the battery is full charged.|
|LED3(D4)||When the Raspberry Pi is ON.||When Raspberry Pi is OFF.|
|LED4(D3)||When the mains supply is present.||When the mains supply is absent|
Steps to setup the Raspberry Pi:
- Insert the microSD card into the card slot on the underside of the raspberry pi.
- Plug the USB keyboard and mouse into the USB ports of the raspberry pi.
- Plug the HDMI cable into the monitor and connect the other end of the cable into the raspberry pi.
- Connect the ARMtronix IA004 – Rpi UPS Board to the raspberry pi header using 40Pin FRC or stack up using onboard 40 pin female berg header.
- Use the external 230V AC to 5V DC power adapter and connect to USB ports of IA004 – Rpi UPS Board.
- Switch on to power adapter to boot up the raspberry pi. A power indicator light will begin to glow, which means that the Pi is connected properly.
NOTE: Care must be taken while connecting the ARMtronix board to the raspberry Pi, i.e. both must be connected in such a manner that the 1st pin of IA004’s 40-Pin header (J2) is connected to the 1st pin in the header of the raspberry pi as shown in the below diagram.
Figure 3: Armtronix Board and Raspberry Pi 1st Pin Indication
Setting up the RTC:
- To setup an RTC, you need to setup/enable the i2c bus on the raspberry pi. You can do this by uncommenting a line in config.txt. “sudo nano /boot/config.txt”
- Scroll down until you find the line “#dtparam=i2c_arm=on”
- And uncomment it by removing the # then exit and save (ctrl-X then y). This will enable the I2C bus (well it will enable on your next reboot).
- Next, install the I2C tools by typing: “sudo apt-get install i2c-tools”. This will install some handy utilities for the I2C and then to reboot “sudo reboot”
- Now verify that the board has RTC by running “sudo i2cdetect -y 1” at the command line. You will get ID #68 in the terminal (the I2C address of the DS1307 Real Time Clock chip).
Figure 4: I2C address of DS1307
- We need to load the RTC module by running: “sudo modprobe rtc-ds1307”
- Now we need to be running as the super user; type in: “sudo bash”
- and then, “echo ds1307 0x68> /sys/class/i2c-adapter/i2c-1/new_device”
- Type exit to stop being the ‘root’ user.
- Check the time on the RTC device using “sudo hwclock -r”
Figure 5: Date and Time
NOTE: If this is the first time the module has being used, it will report back Jan 1 2000, so you need to set the time to the current time. Run sudo hwclock -w to write the system time to the RTC. We can then verify it with sudo hwclock -r.
- So, now the RTC is set, but the next time we boot the pi, it will not know that it needs to read the time from the RTC. If, raspberry is not connected to the internet it will again be showing the wrong time.
- To make raspberry pi to use the real time clock, you need to add the RTC kernel module to the file /etc/modules so it will be loaded when the raspberry pi boots. In terminal window run sudo nano /etc/modules and add rtc-ds1307 at the end of the file.
Figure 6: /etc/modules
- Next, you need to add DS1307 device creation at boot by editing the /etc/rc.local file by running the command “sudo nano /etc/rc.local” in the terminal window.
- Now add the following lines to the file.
“echo ds1307 0x68>/sys/class/i2c-adapter/i2c1/new_device ” “sudo hwclock -s” “date”
Figure 7: /etc/rc.local File
- Then to save the file, press Ctrl+X, Y then return.
- To check this, you need to shut down the raspberry pi, unplug any ethernet cable and/or wifi dongle and then turn the raspberry pi back ON. Now, use the following command in a terminal window to check the date is set, “date”
- Next time when you reboot the raspberry pi, it will read the time from the Real Time Clock (RTC).
Figure 8: Updated Time and Date
Battery monitoring and reading the ADC values:
The ADS1115 Analog to Digital Converter (ADC) from Texas instruments is used on-board. There are two channels accessible for user to read external analog input voltage from 0 V – 3.3 V DC from sensor like devices on the board to read the ADC Values and one channel that is channel-0 is being used to monitor the backup-battery level by reading its voltage.
Table 2: ADC Channels
|ADC Channel No||Purpose|
|ADC Channel 0||To read Battery voltage|
|ADC Channel 1||Not Used|
|ADC Channel 2||To read the ADC value|
|ADC Channel 3||To read the ADC value|
For the battery monitor and controlling feature here we have made simple example flow in the Node-RED. In this example, the node ‘test’ will inject the input for every 30 sec. to the node ‘adc’, the node ‘adc’ will read the ADC value of the channel-0 and it will display the adc value through the ‘ADC Value’ debug node. After that we are using the function node (‘Battery Value’), where we wrote small code to convert that adc value to the battery voltage and display the value through the ‘battery converted value’ debug node. Then we have one more function node (‘Battery Monitor’), where we are reading the battery converted value and when read value goes below predefined cut-off value, node red will automatically encounter the shutdown command and the Raspberry pi will shut down. The following figure is the flow for the battery monitoring.
Figure 9: Node Red Flow
The below figure shows the code of the function nodes Battery Value and Battery Monitor.
Figure 10: Battery Value
Figure 11: Battery Monitor
Consider you are using Raspberry Pi for your industrial automation application and powered it using external power source via 24V DC to 5V DC pi-hat.
Problem-1: If the power fails randomly or abrupt disconnection of power by user, then the OS or other files of Pi may be corrupted due to this, your stored data may be lost and system will call for maintenance. To bring back the system to normal state, a new memory card has to be bought, new image has to be loaded, which may consume sufficient amount of your precious time as well as money on redoing this again and again.
Problem-2: If pi is in turned off condition and power supply is present due to any reason, the power source has to be plugged out and connected back to boot/reboot.
Solution: To solve the above stated problems, we have come up with this hardware which acts as UPS to the pi, so that you can save the data and softly auto shutdown the pi, in case of mains power failure. Even though the power via battery is present due to UPS feature, it will reboot the pi, if power returns and it will continue its task and user need not to worry of manually power booting.
IA004 has a circuit, where we are using GPIO-09 and it will be always high when the board is ON. Here we are using AND logic between the gpio-9 and mains input, when the power goes off, the Raspberry pi will still powered ON using the battery voltage and when the battery voltage goes below the mentioned value (must be greater than 3.2V), it will automatically shut down the Raspberry Pi which is the safe way to turn off the Pi. By this we can avoid the memory corruption. Next on the presence of the mains power, the board will reboot itself automatically and battery will start to charge.
NOTE: For this application, by default we must make the GPIO9 high. It will become low only when the whole system is off. Here we are making it high by using the Node-RED.
Figure 12: GPIO9 Status