The CH9328 can allow you to do 'funky' things. In this example, you can have one desktop or single-board computer 'type' into a device such as a computer or mobile device by running our Python script and having it send UART data via a USB-to-UART converter.

Wiring
You'll plug the USB to TTL cable into the computer running the CPython script. The cable will connect to the CH9328 breakout RX and GND connections. The CH9328 breakout will plug into the computer that you want to send the HID commands to.
The USB to TTL cable has four wires: red power, black ground, white RX into USB port, and green TX out of the USB port. You'll be using the black ground wire and the green TX wire.
- USB to TTL cable GND to breakout JST SH GND (black wire)
- USB to TTL cable TX to breakout JST SH RX (white wire)
The CH9328 should be in Mode 0. Switches 2, 3 and 4 should all be switched on. The mode needs to be selected before the breakout is powered on.
Make sure that the CH9328 onboard switches are set to Mode 0 (switch 2, 3 and 4 on) before powering it up.
CPython CH9328 Code
To run the script you will need a desktop or single board computer with Python 3 installed.
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT import sys import serial import keyboard port = '/dev/ttyUSB0' # Replace with your actual serial port # Define a mapping for special characters when shift is pressed SHIFTED_KEYS = { '1': '!', '2': '@', '3': '#', '4': '$', '5': '%', '6': '^', '7': '&', '8': '*', '9': '(', '0': ')', '`': '~', '-': '_', '=': '+', '[': '{', ']': '}', '\\': '|', ';': ':', "'": '"', ',': '<', '.': '>', '/': '?' } def send_key(serial_port, key): """ Send a key press to the CH9328 via UART. Parameters: serial_port (serial.Serial): The serial port connection. key (str): The key to send. """ serial_port.write(key.encode('ascii')) serial_port.flush() def send_empty_report(serial_port): """ Send an empty HID report to reset the state of the device. Parameters: serial_port (serial.Serial): The serial port connection. """ try: empty_report = bytearray([0] * 8) serial_port.write(empty_report) serial_port.flush() except serial.SerialException as e: print(f"Failed to send empty report: {e}") def main(): # Configure the serial connection baudrate = 9600 # Default baud rate for CH9328 in Mode 1 timeout = 1 with serial.Serial(port, baudrate, timeout=timeout) as ser: print("Listening for keyboard inputs. Press 'ESC' to exit.") def on_key_event(event): if event.event_type == 'down': key = event.name if len(key) == 1: # Only process single character keys if keyboard.is_pressed('shift'): # Check if shift is pressed key = SHIFTED_KEYS.get(key, key.upper()) send_key(ser, key) elif key == 'space': send_key(ser, ' ') elif key == 'enter': send_key(ser, '\n') send_empty_report(ser) # Hook the keyboard event keyboard.hook(on_key_event) # Wait for ESC to exit keyboard.wait('esc') if __name__ == "__main__": main() sys.exit()
CPython Dependencies
First, you'll use pip
to install the Python libraries required to run the script:
pip install pyserial pip install keyboard
Some versions of RapsberryPi OS use the 'venv' virtual environment and may balk at installing packages at the root level. 'keyboard' needs to be run at root level (which is scary and dangerous, beware) If you run into the 'error externally-managed-environment` use this instead:
sudo pip install pyserial --break-system-packages sudo pip install keyboard --break-system-packages
Customize the Script
Open the script in your preferred text editor or IDE. At the top of the code, you'll need to update port
to match the COM port that your USB to TTL cable is plugged into. Otherwise, the script will not work.
port = '/dev/ttyUSB0' # Replace with your actual serial port
Update the COM port before running the script!
Run the Script
After adding your COM port, you can run the code.py script inside a terminal window on your computer with:
sudo python code.py
The keyboard
library requires that the script be run as an administrator or root
, depending on your operating system.
After the script starts to run, any keyboard typing that takes place on your computer will be mirrored via the USB TTL cable and CH9328 breakout. In the .GIF below, the script is running on a Raspberry Pi and the CH9328 is connected to a Windows PC.
Page last edited January 22, 2025
Text editor powered by tinymce.