Skip to content

GateBerry - Features and Communications

RS485

RS485 is a serial communication standard that allows data transmission over long distances and between multiple devices using a single pair of wires. It is resistant to electrical noise and commonly used in industrial environments.

Important features and values ​​to consider

Interface typeRS-485 half-duplex
Communication speed (baud rate)Configurable
(maximum baud rate of 3 Mbps)
Connectors and wiringPush-in connectors (A+ and B-)
Termination resistor120 ohms (software configurable)

Recommendation

It's recommended to use twisted pair to minimize electromagnetic interference (EMI).
If you're in a noisy environment, use shielded twisted pair (STP).

Note

The PLC is able to enable and disable the built-in 120 ohm termination resistor by software, to activate it if you are at one end of the bus and avoid the reflection of the signals.

Warning

Communication status management is not automatic. The sending/receiving status must be managed manually, controlling the DE/RE pin connected to GPIO 27

Connections

Configurations

The GateBerry PLC provides an RS485 communication interface connected to the serial port /dev/rs485. The following instructions describe how to configure and use this interface for both data transmission and reception.

1. Serial Port Configuration

Before starting communication, the serial port baud rate must be configured. Ensure that this baud rate matches the one used on the RS485 network.

For example: To set the baud rate to 38400 bps, run:

stty -F /dev/rs485 38400 raw -echo

2. Receiving Messages

To enable data reception on the RS485 bus, the DE/RE (Driver Enable / Receiver Enable) control pin must be set to a low level (0). In this setup, the control pin corresponds to GPIO27.

Enable receive mode with the following command:

./test/GATEBERRY_V9/GATEBERRY/set-digital-output DE_RE 0

Once configured, incoming data can be viewed using:

cat /dev/rs485

3. Sending Messages

To transmit data over RS485, the DE/RE control pin must be set to a high level (1). Use the following command to enable transmit mode:

./test/GATEBERRY_V9/GATEBERRY/set-digital-output DE_RE 1

You can then send data through the serial port as follows:

echo "MSG" > /dev/rs485

4. Termination Resistor Configuration

The RS485 bus termination resistor can be controlled via GPIO17 on the Raspberry Pi:

  • LOW = termination resistor disabled (default state)
  • HIGH = termination resistor enabled (only activate if the PLC is at the end of the line)

Check the current state of the termination resistor:

pinctrl get 17
Set the termination resistor LOW (disabled):
pinctrl set 17 op dl
Set the termination resistor HIGH (enabled):
pinctrl set 17 op dh

5. Additional Notes

  • Ensure that all devices connected to the RS485 bus share the same baud rate and serial communication parameters (data bits, parity, and stop bits).
  • Proper control of the DE/RE line is essential to avoid bus collisions.
  • Only enable the termination resistor on the device located at the end of the RS485 line.
  • Correct termination and biasing resistors improve signal integrity on longer RS485 networks.

I²C

I²C (Inter-Integrated Circuit) is a serial communication protocol that allows multiple devices to communicate over just two wires—one for data and one for the clock. It’s commonly used for short-distance communication between microcontrollers and peripherals.

Important features and values ​​to consider

Interface typeI²C
Connectors and wiringPush-in connectors (SDA (data) and SCL (clock))
Signal voltage5V (Pull-up resistors)

Danger

Remember that GateBerry's I²C bus has a 5V pull-up, so do not connect devices that only support I²C at 3.3V.

Connections


Configurations

Note

By default, it comes enabled with our image and includes the necessary tools for its use. Nevertheless, this is how it would be enabled and used.

Enable I²C

  1. Run the following command to open the GateBerry configuration tool:
    sudo raspi-config
    
  2. Navigate to Interfacing Options > I2C and select Yes to enable I²C.
  3. Reboot the Raspberry Pi to apply the changes:
    sudo reboot
    

I²C Tools

To interact with I²C devices, it is also highly recommended to install the i2c-tools package (installed by default):

sudo apt update
sudo apt install i2c-tools
These tools allow us to scan devices connected to the I²C bus and verify their operation.

For example, before communicating with a device, it is useful to scan the I²C bus to detect the addresses of connected devices. Use the following command in the terminal:

i2cdetect -y 1
There are several other useful tools as well; here is a summary:

Command Description Basic Example
i2cdetect Detects devices on the I²C bus i2cdetect -y 1
i2cdump Displays the contents of registers i2cdump -y 1 0x23
i2cget Reads a value from a register i2cget -y 1 0x23 0x00
i2cset Writes a value to a register i2cset -y 1 0x23 0x01 0x80
i2ctransfer Advanced read/write operations i2ctransfer -y 1 w1@0x23 0x00 r2

RTC

A Real-Time Clock (RTC) is an electronic device or integrated circuit that continuously keeps track of the current time and date, even when the main system is powered off, typically using a backup battery. It is widely used in computers, microcontrollers, and embedded systems to maintain accurate timekeeping.

The configurations for using our integrated RTC are located in the file: /boot/firmware/config.txt and are as follows:

dtparam=i2c_arm=on
dtoverlay=i2c-rtc,pcf85363

Once the overlay is configured and the module is detected using the command sudo i2cdetect -y 1, it should appear at address 0x51. The operating system (OS) should recognize it as a hardware clock.

Use the hwclock command with the hardware option (-r or --show):

sudo hwclock -r
This will display the current time stored in the RTC module's memory.

If the system time (the one normally displayed, set by the network/NTP) is correct and you want to save it to the RTC:

sudo hwclock -w
This overwrites the RTC time with the current Raspberry Pi system time. It is essential to perform this at least once to set the RTC’s initial time.

If the system has booted without network access and the time is incorrect, you can use the time stored in the RTC to set the system time:

sudo hwclock -s
Typically, this step is performed automatically at Raspberry Pi startup if the overlay is configured correctly and the service that attempts to "save" unsynchronized time is disabled.

UPS

Industrial Shields UPS is a device that, thanks to its supercapacitors, can provide temporary electrical power to the GateBerry during a power outage or supply interruption. This ensures that ongoing processes can be safely completed and prevents memory corruption.

The unit comes preconfigured from the factory, with all necessary settings and services already set up.

The hardware configuration is done in: /boot/firmware/config.txt

dtoverlay=gpio-poweroff,gpiopin=23,active_low
dtoverlay=gpio-shutdown,gpio_pin=24,gpio_pull=up

Pre-power off commands: safeguarding your data

Before shutting down the system, ensure that all critical operations are completed using the pre-shutdown hooks.

The configurations you will see below are already set by default. If you want to learn how to use them, proceed to the following step: Usability test.

This service comes pre-installed by default on the OS, it is installed with the command:

sudo apt install rpishutdown-pre-poweroff=1.0.0

This guide provides detailed steps to configure directories, download the necessary service files, and set up scripts to automate crucial tasks before shutdown.

  1. Create the necessary directory (if no exists): Create the required directory to store the pre-poweroff hooks:

    sudo mkdir -p /etc/rpishutdown/hooks
    

  2. Download the service file and hook script: Fetch the required files for the pre-poweroff service and the check script:

    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
    

  3. Grant execution permissions: Make the pre-poweroff script executable:

    sudo chmod ugo+x /etc/rpishutdown/hooks/check-pre-poweroff
    

  4. Enable and start the pre-poweroff service: Reload the systemd daemon and enable the new service:

    sudo systemctl daemon-reload
    sudo systemctl enable rpishutdown-pre-poweroff
    sudo systemctl start rpishutdown-pre-poweroff
    

Usability test

As you may have seen earlier, there is a dedicated service that monitors for power outages. In such cases, it executes the file located at: /etc/rpishutdown/hooks/pre-poweroff.

Info

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).

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

Expansion Boards

The GateBerry includes one slot for additional communication. Currently, it supports the following connection options: