The example below is only one way how to set up the Relay Module.
In this examples we have connected a PICO to the BEVRLink Manager PICO V2 with BEVRLink 4 Channel Relay and 4 manual test buttons 12V. With this relay module board you need the BEVRLink Power Supply 12V 2A.
Optional for a safe setup we recommend to use case for both the manager and relay boards: BEVRLink Cases
Set up CircuitPython
- Download latest CircuitPython Firmware
- Hold BOOTSEL while plugging the PICO in to your PC
- A USB Drive called RPI-RP2 should appear on your computer
- Drag and drop the downloaded UF2 to the Drive
- The board will automatically flash it self and reboot removing the drive
- You will get a new USB Drive attached called CIRCUITPY that will hold your code and files
Code
- Write the code in a code.py file
import time
import board
import busio
# Initialize the I2C bus:
i2cbus = busio.I2C(scl=board.GP1, sda=board.GP0)
i2caddress_Relay_1 = 0x38 # Address of relay card 1
i2caddress_Relay_2 = 0x39 # Address of relay expansion card
# Registers
INREG = 0x00 # Input read register
OUTREG = 0x01 # Output write register
IOCONF = 0x03 # Configure IO as Input or Output register
# Take over the i2cbus
while not i2cbus.try_lock():
pass
# Config 4 first pins as outputs and 4 last as inputs
i2cbus.writeto(i2caddress_Relay_1, bytes([IOCONF, 0xF0]))
i2cbus.writeto(i2caddress_Relay_2, bytes([IOCONF, 0xF0]))
# Define holder for buttons
input1 = bytearray(1)
input2 = bytearray(1)
while (True): # Loop
# Read the inputs and copy it to the output
i2cbus.writeto_then_readfrom(i2caddress_Relay_1, bytes([INREG]), input1)
i2cbus.writeto_then_readfrom(i2caddress_Relay_2, bytes([INREG]), input2)
value1 = ~(int.from_bytes(input1, 'big') >> 4) & 0x0F
i2cbus.writeto(i2caddress_Relay_1, bytes([OUTREG, value1]))
value2 = ~(int.from_bytes(input2, 'big') >> 4) & 0x0F
i2cbus.writeto(i2caddress_Relay_2, bytes([OUTREG, value2]))
Copy the code file code.py to the USB drive CIRCUITPY.
The code should autorun as soon as it saved on the drive, otherwise open a serial connection to the Pico shell and check for errors