Introduction
The LEO CR 8-Channel USB Relay Module allows you to control eight independent relay outputs and monitor eight digital inputs through a USB connection. Its simple command interface makes it easy to integrate the module into Python applications for automation, equipment control, testing, and monitoring.
Using Python and the PySerial library, you can communicate with the module, switch individual relays ON or OFF, read relay status, and incorporate relay control into your own scripts or applications.
The LEO CR8 also includes features such as relay timers, Smart Actions, and configurable failsafe behavior for applications that require automated operation.
Requirements
Before starting, ensure that you have the following:
- LEO CR 8-Channel USB Relay Module.
- USB-A to USB-C or USB-C to USB-C cable, as appropriate for your computer.
- Compatible 12V or 24V, 1A DC power supply, according to the module variant.
- Python 3 installed on your computer.
- PySerial library installed.
- The Numato Lab USB CDC driver.
Step 1: Connect the Module
- Connect the LEO CR8 to your computer using a USB cable.
- Connect the appropriate external DC power supply to the module.
- Confirm that the device is detected by your operating system.
- Identify the serial port assigned to the module.
On Windows, open Device Manager and look under Ports (COM & LPT). The module should appear as a serial port, such as COM3 or COM5.
On Linux, the device may appear as /dev/ttyACM0 or a similar name. On macOS, it may appear under /dev/tty.usbmodem....
Make a note of the serial port name, as it will be required in the Python program.
Step 2: Install PySerial
PySerial provides a convenient way to communicate with serial devices from Python.
Open a terminal or Command Prompt and run:
python -m pip install pyserial
To verify the installation, run:
python -m pip show pyserial
If the package is installed correctly, the command will display its version and installation details.
Step 3: Understand the Relay Commands
The LEO CR8 uses a human-readable command set over its USB serial interface. Each relay is identified using a three-digit, zero-based channel number.
The relay channels are numbered from 000 to 007.
| Operation | Command | Description |
|---|---|---|
| Turn ON Relay 0 | relay on 000 |
Activates Relay 0 |
| Turn OFF Relay 0 | relay off 000 |
Deactivates Relay 0 |
| Read Relay 0 status | relay status 000 |
Returns the current state of Relay 0 |
| Turn ON all relays | relay on all |
Activates all eight relays |
| Turn OFF all relays | relay off all |
Deactivates all eight relays |
| Read all relay states | relay status |
Returns the relay states as a hexadecimal bitmask |
| Read firmware version | ver |
Returns the firmware version |
For example, the command relay on 000 turns ON Relay 0, while relay off 000 turns it OFF.
For additional commands and configuration options, refer to the LEO CR8 documentation.
Step 4: Create a Python Program
Create a Python file named leo_cr8_control.py.
The following example demonstrates how to open the serial port, send commands to the module, and display the response.
import serial
import time
# Change this to the serial port assigned to your module.
PORT = "COM3"
# Open the serial connection.
module = serial.Serial(
port=PORT,
baudrate=19200,
timeout=1
)
def send_command(command):
"""Send a command and return the device response."""
module.reset_input_buffer()
module.write((command + "\r").encode("ascii"))
module.flush()
time.sleep(0.1)
response = module.read_all().decode(
"ascii", errors="replace"
).strip()
print(f"Command: {command}")
print(f"Response: {response}")
return response
try:
# Read the firmware version.
send_command("ver")
# Turn ON Relay 0.
send_command("relay on 000")
# Read the status of Relay 0.
send_command("relay status 000")
# Turn OFF Relay 0.
send_command("relay off 000")
# Read the status of all relays.
send_command("relay status")
finally:
module.close()
Important: The serial port name and communication settings must match your device and firmware configuration.
Step 5: Run the Python Program
Open a terminal in the directory containing leo_cr8_control.py and execute:
python leo_cr8_control.py
If the connection and command settings are correct, the program will communicate with the module and print the responses received.
You can verify the operation by observing the relay indicators, where available, and checking the connected load.
Step 6: Control Individual Relays
Once communication is working, you can create functions to control individual relay channels.
def relay_on(channel):
if not 0 <= channel <= 7:
raise ValueError("Channel must be between 0 and 7")
return send_command(f"relay on {channel:03d}")
def relay_off(channel):
if not 0 <= channel <= 7:
raise ValueError("Channel must be between 0 and 7")
return send_command(f"relay off {channel:03d}")
# Turn ON Relay 2.
relay_on(2)
# Turn OFF Relay 2.
relay_off(2)
These functions can be called from a larger Python application, such as a graphical user interface, automated test program, or equipment-control script.
For applications that need to determine the actual relay state, use the documented status command and interpret its response rather than assuming that every command succeeded.
Step 7: Read Digital Input States
In addition to relay control, the LEO CR8 provides eight digital inputs that can be monitored through Python.
To read an individual input, use:
send_command("gpi read 000")
To read all digital inputs in one operation, use:
send_command("gpi read")
Note: The digital inputs support signals within the module’s specified operating range, up to 24V. Do not apply voltages outside the documented limits.
Step 8: Extend the Integration
After basic communication is established, you can extend your Python application to support additional features:
- Automated sequencing: Switch multiple relays according to a defined sequence.
- Equipment control: Integrate relay operations into pump, valve, or other equipment-control applications.
- Input monitoring: Read digital inputs and respond to external signals.
- Relay timers: Configure supported delayed ON, delayed OFF, and toggle operations.
- Smart Actions: Configure input-triggered relay behavior that can operate without continuous host control.
- Status logging: Record command results and relay states for monitoring and troubleshooting.
- GUI integration: Add buttons, status indicators, and input displays using a Python GUI framework.
The module’s onboard automation features can also reduce dependence on a continuously running host application for supported operations.
Troubleshooting
The serial port cannot be opened
- Check that the module is connected through USB.
- Verify the correct COM port or device path.
- Close other applications that may already be using the port.
Commands do not produce a response
- Confirm that the command syntax matches the documented command set.
- Check the serial communication settings and command terminator.
- Test the command using a serial terminal before troubleshooting the Python code.
- Check whether authentication is enabled and whether the configured credentials are required.
The relay does not change state
- Verify the relay number; valid channel numbers are
000through007. - Read the relay status after sending the command.
- Check whether the relay is assigned to a Smart Action or relay timer, which may restrict manual control.
- Verify the external power supply and load wiring.
The program works once but not on subsequent commands
- Confirm that the serial port remains open while commands are being sent.
- Check that each command is transmitted using the correct framing.
- Ensure that the response is read and handled before sending the next command.
- Avoid opening the same serial port from multiple applications simultaneously.
- Add appropriate error handling and timeouts for robust operation.
What’s Next?
Integrating the LEO CR 8-Channel USB Relay Module into Python is straightforward using its USB serial interface and PySerial. With a few lines of code, you can control relay outputs, read relay status, monitor digital inputs, and build automation into your own applications.
For command details, hardware connections, electrical specifications, and advanced features, consult the LEO CR8 product page and user manual.