It's easy to use the RS-232 Full Pinout Breakout with Python or CircuitPython, and the built-in UART module. This module allows you to easily write Python code to send and receive UART messages.
You can use this driver with any CircuitPython microcontroller board or with a computer that has GPIO, UART and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library. For Blinka, this example was tested with a Raspberry Pi. To use the example as-is with built-in UART, you'll need to follow these configuration steps outlined in the Blinka Learn Guide.
You'll need an external RS-232 device to use this example with the breakout:
CircuitPython Microcontroller Wiring
First, plug in an RS-232 device to the DE-9 connector on the RS-232 breakout. Then, wire up the RS-232 breakout to your board. The following is the breakout wired to a Feather RP2040:
- Feather 3.3V to breakout Vin (red wire)
- Feather GND to breakout GND (black wire)
- Feather RX to breakout RX (green wire)
- Feather TX to breakout TX (blue wire)
The following is the breakout wired to a Feather RP2040 using a solderless breadboard:
- Feather 3.3V to breakout Vin (red wire)
- Feather GND to breakout GND (black wire)
- Feather RX to breakout RX (green wire)
- Feather TX to breakout TX (blue wire)
Python Computer Wiring
Since there are dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.
Here's the Raspberry Pi wired to the RS-232 breakout. You'll plug in an RS-232 device to the DE-9 port on the breakout:
- Pi 3.3V to breakout Vin (red wire)
- Pi GND to breakout GND (black wire)
- Pi RX to breakout RX (green wire)
- Pi TX to breakout TX (blue wire)
Here's the Raspberry Pi wired up using a solderless breadboard:
- Pi 3.3V to breakout Vin (red wire)
- Pi GND to breakout GND (black wire)
- Pi RX to breakout RX (green wire)
- Pi TX to breakout TX (blue wire)
CircuitPython Usage
To use with CircuitPython, you'll need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the code.py file to your CIRCUITPY drive.
There are no additional libraries needed for this example. All of the modules used are built-in.
No additional libraries need to be added to the /lib folder on your CIRCUITPY drive.
Python Usage
Once you have the library pip3
installed on your computer, copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:
python3 code.py
Make sure to follow the built-in UART setup steps if you are using the UART on the Raspberry Pi.
Example Code
If running CircuitPython: Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
If running Python: The console output will appear wherever you are running Python.
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries # SPDX-License-Identifier: MIT import time import board # baud rate for your device baud = 38400 # Initialize UART for the CH9328 # check for Raspberry Pi # pylint: disable=simplifiable-condition if "CE0" and "CE1" in dir(board): import serial uart = serial.Serial("/dev/ttyS0", baudrate=baud, timeout=3000) # otherwise use busio else: import busio uart = busio.UART(board.TX, board.RX, baudrate=baud) print("Enter commands to send to the RS-232 device. Press Ctrl+C to exit.") while True: user_input = input("Please enter your command: ").strip() if user_input: # send the command with a telnet newline (CR + LF) uart.write((user_input + "\r\n").encode('ascii')) # empty buffer to collect the incoming data response_buffer = bytearray() # check for data time.sleep(1) while uart.in_waiting: data = uart.read(uart.in_waiting) if data: response_buffer.extend(data) # decode and print if response_buffer: print(response_buffer.decode('ascii'), end='') print()
The code begins by initializing the UART port. There is a check for a Raspberry Pi single board computer. If a Raspberry Pi is detected, then serial
is used instead of busio
for UART over GPIO. The baud rate for your RS-232 device is defined with baud
. Edit this value to match your RS-232 device.
# baud rate for your device baud = 38400
In the loop, the code waits for user input in the serial console. If any incoming data is received from the RS-232 device, it is printed to the serial console.
The commands that you send will vary by the RS-232 device that you connect to the RS-232 breakout. The output below was sent to a StarTech HDMI switcher. The AVI=n
commands switch the selected HDMI port and the VS
command gives a current status of the device.
Text editor powered by tinymce.