Template: RPi Zero Relay Guide

From Waveshare Wiki
Jump to: navigation, search

Install Library

If you use the bookworm system, you can only use lgpio library, bcm2835 and wiringPi can't be installed and used.

BCM2835

#Open the Raspberry Pi terminal and run the following command
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.71.tar.gz
tar zxvf bcm2835-1.71.tar.gz 
cd bcm2835-1.71/
sudo ./configure && sudo make && sudo make check && sudo make install
# For more, you can refer to the official website at: http://www.airspayce.com/mikem/bcm2835/

WiringPi

#Open the Raspberry Pi terminal and run the following command
cd
sudo apt-get install wiringpi
#For Raspberry Pi systems after May 2019 (earlier than that can be executed without), an upgrade may be required:
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
gpio -v
# Run gpio -v and version 2.52 will appear, if it doesn't it means there was an installation error

# Bullseye branch system using the following command:
git clone https://github.com/WiringPi/WiringPi
cd WiringPi
. /build
gpio -v
# Run gpio -v and version 2.70 will appear, if it doesn't it means there was an installation error

lgpio

#Open the Raspberry Pi terminal and run the following command
wget https://github.com/joan2937/lg/archive/master.zip
unzip master.zip
cd lg-master
sudo make install

# You can refer to the official website for more: https://github.com/gpiozero/lg
  • Python
sudo apt-get update
sudo apt-get install python-serial
sudo pip install python-can

Download Examples

Open a terminal and run the following commands to download the demo codes:

sudo apt-get install p7zip-full
wget https://files.waveshare.com/upload/2/2f/RPi_Zero_Relay_Code.7z
7z x RPi_Zero_Relay_Code.7z  -r -o./RPi_Zero_Relay_Code
sudo chmod 777 -R RPi_Zero_Relay_Code/

CAN Bus

The sample demo uses two RPi-Zero-Relay modules. Python and C language demos are provided.

Configuration

  • Modify the config.txt file:
sudo nano /boot/config.txt
Add the following lines to the config.txt file and save:
dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25,spimaxfrequency=3000000
  • Reboot.
sudo reboot
  • Check if the driver is initialized normally.
dmesg | grep -i '\(can\|spi\)'
The result should be like the picture:
RS485 CAN HAT CAN1.png
If the MCP2515 driver is not initialized normally you need to reboot and check if the devices are connected properly (H to H and L to L).
RS485 CAN HAT CAN2.png

C Examples

  • Run the receive example:
cd ~/RPi_Zero_Relay_Code/CAN/wiringPi/receive/
make clean
make
sudo ./can_receive
After running the received code, the terminal is waiting for data.
RPi Zero Relay CAN 2.jpg
  • Run the Send examples:
cd RPi_Zero_Relay_Code/CAN/wiringPi/send/
make clean
make
sudo ./can_send
RPi Zero Relay CAN 1.jpg
Type data to send.
RPi Zero Relay CAN 3.jpg
In the receiving terminal, you will get the packet related to the id.
RPi Zero Relay CAN 4.jpg

Python examples

  • Open a terminal and run the commands:
cd RPi_Zero_Relay_Code/CAN/python/
#For receiving:
sudo python can_reveive.py
#For sending:
sudo python can_send.py

Connect to other CAN device

If you need to connect the RPi Zero Relay to other CAN devices, please note that:

  • Please check the connection, it should be H to H and L to L.
  • Make sure that the baud rate of both sides is the same, the default baud rate of examples is 100K.
  • Check if the CAN ID of both sides is the same.
  • If data is always lost while transmitting, please decrease the baud rate and test it again.
RPi Zero Relay CAN 9.jpgRPi Zero Relay CAN 10.jpg

RS485 Interface

The sample demo uses two RPi-Zero-Relay modules. Python and C language demos are provided.

Configuration

  • Open a terminal and run the command.
sudo raspi-config
Choose Interfacing Options -> Serial -> No -> Yes.
L76X GPS Module rpi serial.png
Disable the login shell function and enable the hardware serial port.
  • Reboot
sudo reboot
  • Connect the device, A to A and B to B.

C examples

  • Run the receive example:
cd RPi_Zero_Relay_Code/485/WiringPi/send
make clean
make
sudo ./485_receive
After running the received code, the terminal is waiting for data.
RPi Zero Relay 485 2.jpg
  • Run the Send example:
cd RPi_Zero_Relay_Code/485/WiringPi/send
make clean
make
sudo ./485_send
RPi Zero Relay 485 1.jpg
  • Input data and send:
RPi Zero Relay 485 3.jpg
  • The receiving terminal receives and prints:
RPi Zero Relay 485 4.jpg

Python examples

Open a terminal and run the commands:

cd RPi_Zero_Relay_Code/485/python/
#Receive example:
sudo python receive.py
#Send example:
sudo python send.py

Troubleshooting

If the RS485 cannot work normally, please check:

  • Please check if you have disabled the login shell function.
  • Please check the connection, it should be A to A and B to B.
  • Please test with USB to RS485, and make sure that all the settings are correct.