CASP supports accessing micro-controller pins from a standard Python script on host computer through low latency serial or WiFi communication. This feature allows the micro-controller pins to be visualized as if they were part of the host computer. This example shows how to easily access Arduino Uno pins from a Python script on host computer through serial communication. It also shows how to plot and control the data from a GUI based control panel invoked from a Python script.
The Python code in this example implements following logic
- Display ADC (pin-A0) and digital input pin (pin-2) values
- Blink Arduino Uno on-board LED (pin-13)
- Toggle digital output pin-12 value
- Vary PWM on pin-3
- Vary Servo angle on pin-9
Pre-Requisites
- Arduino Uno R3 board.
- Compatible USB cable.
- CASP software along with Arduino BSP is installed. From CASP GUI install CASP Python libraries. Please refer this page for details.
- Download the project zip file from this link and extract the downloaded file. The Python files are located in the ‘python’ folder of the extracted directory.
Procedure
- Connect Arduino Uno board to the host computer through a serial cable.

Flashing Arduino Uno with the binary file: In the extracted project directory, open terminal or command prompt in the ‘python’ folder and type the following command twice (without and with serial port as argument).
#first type the below command to display available serial ports in which Arduino Uno serial port is one of them
python(or python3) rgpio_flasher.py
#now repeat the above command with serial port (to which Arduino board is connected) as an argument
python(or python3) rgpio_flasher.py "COM3"
#the above command should program Arduino Uno board with the hex file supporting python communication
Once the board is programmed, disconnect the USB cable. Connect suitable peripherals such as LDR or IR distance sensor to pin-A0, limit switch to pin-2, external LED or relay module to pin-12, DC motor driver module to pin-3, servo to pin-9 etc. of Arduino Uno and re-connect the USB cable to the board.
Run the basic Python script file: Open ‘rgpio_from_pc.py’ in a text editor. Locate text “COM62” and replace it with the actual serial port to which Arduino Uno board is connected. Save and close the file. Run terminal or command prompt in ‘python’ folder. Type python or python3 with ‘rgpio_from_pc.py’ as argument and press Enter key. The Python code should start communicating with the board and execute the implemented logic. Python script from ‘rgpio_from_pc.py’ file is shown below.
# This example demonstrates how to use CASP Remote GPIO from Python script that runs on host computer. Ensure matching target's casp_pins.py file in present in the script folder before running.
import sys
import time
#modify this path to point to 'casp_user_data/pycasp' directory in your system
#sys.path.append("C:/Users/AL/Documents/casp_user_data/pycasp/src")
import casp_rgpio as rgpio
import casp_pins as pins
#initialize GPIO instance
rgpio.Init()
#displays available serial ports
#rgpio.SerialPorts()
#establish communication with the board. loop till the connection is established
#modify serial port ("COM62") to which the target is connected on your system.
#to know about available serial ports, use above commented command.
connected: bool = False
while connected == False:
connected = rgpio.ConnectSerial("COM62", 115200, True, 1, 16, 16) #port, baud, enable_dtr, delay_msec, read_bytes, write_bytes
time.sleep(5)
#run a for loop to blink on-board LED, read ADC value and control pwm and servo every one second
pwm_val = 0
servo_val = -90
for i in range(1000):
di = rgpio.ReadDigital(pins._2_DI)
adc = rgpio.ReadAnalog(pins.A0)
print(f"di={di}, adc={adc}")
#
rgpio.WriteDigital(pins._13_LED, (i % 2)) #blink logic
rgpio.WriteDigital(pins._12_DO, (i % 2))
#
pwm_val += 50
if pwm_val > 4095:
pwm_val = 0
rgpio.WriteAnalog(pins._3, pwm_val) #pwm control
#
servo_val += 10
if servo_val > 90:
servo_val = -90
rgpio.WriteAnalog(pins._9_SERVO0, servo_val) #servo control
#
time.sleep(1)
#close CASP GPIO instance
rgpio.Close()
The contents of ‘casp_pins.py’ file referred in above code is shown below
"""! Python module for GPIO pins from pin source: rgpio_arduino_uno_nano_r3_serial.pins
Target details:
Name: ArduinoUnoR3
Read size (bytes): 16
Write size (bytes): 16
Communication type: serial
Serial port: set during runtime
Serial baud: 115200
Serial flow control (0-none, 1-dtr, 2-hardware, 3-software): 0
IP address: set during runtime
IP port: 0
"""
#Pin details:
_2_DI = 0 #DI0
_4_DI = 1 #DI0
_7_DI = 2 #DI0
_8_DI = 3 #DI0
A4_DO = 0 #DO0
A5_DO = 1 #DO0
_12_DO = 2 #DO0
_13_LED = 3 #DO0
A0 = 1 #ADC
A1 = 3 #ADC
A2 = 5 #ADC
A3 = 7 #ADC
_3 = 1 #PWM
_5 = 3 #PWM
_6 = 5 #PWM
_11 = 7 #PWM
_9_SERVO0 = 9 #PWM
_10_SERVO1 = 11 #PWM
Run advanced Python script file that shows a GUI panel for plotting and control: Open ‘rgpio_from_pc_plot.py’ in a text editor. Locate text “COM62” and replace it with the actual serial port to which Arduino Uno board is connected. Save and close the file. Run terminal or command prompt in ‘python’ folder.
- Run GUI Panel Server: Type python or python3 with ‘rgpio_plot_n_control_server.py’ as argument and press Enter key. This will start GUI panel server.
- Run the Python script: Type python or python3 with ‘rgpio_from_pc_plot.py’ as argument and press Enter key. The Python code should start communicating with the board and execute the implemented logic. Output control panel displaying pin values from Arduino Uno board is shown below. You can control LED, DO, PWM and servo from the panel UI.

Python script from ‘rgpio_from_pc_plot.py’ file is shown below.
# This example demonstrates how to use CASP Remote GPIO from Python script that runs on host computer. Ensure matching target's casp_pins.py file in present in the script folder before running.
import sys
import time
#modify this path to point to 'casp_user_data/pycasp' directory in your system
#sys.path.append("C:/Users/AL/Documents/casp_user_data/pycasp/src")
import casp_rgpio as rgpio
import casp_pins as pins
import casp_udio
server = casp_udio.UdioClass()
server.ConnectEth("127.0.0.1", 42001, False, 1, 8, 8) #ip, port, tcp, delay, udi_size, udo_size
#initialize RGPIO instance
rgpio.Init()
#displays available serial ports
#rgpio.SerialPorts()
#establish communication with the board. loop till the connection is established
#modify serial port ("COM62") to which the target is connected on your system.
#to know about available serial ports, use above commented command.
connected: bool = False
while connected == False:
connected = rgpio.ConnectSerial("COM62", 115200, True, 1, 16, 16) #port, baud, enable_dtr, delay_msec, read_bytes, write_bytes
time.sleep(5)
#run a for loop to blink on-board LED, read ADC value and control pwm and servo every one second
for i in range(1000):
di = rgpio.ReadDigital(pins._2_DI) #digital input
adc = rgpio.ReadAnalog(pins.A0) #analog input
server.WriteI(0, di)
server.WriteI(1, adc)
print(f"di={di}, adc={adc}")
#
led_on = server.ReadI(0)
rgpio.WriteDigital(pins._13_LED, led_on) #led control
#
do_on = server.ReadI(1)
rgpio.WriteDigital(pins._12_DO, do_on) #digital output
#
pwm_val = server.ReadI(2)
rgpio.WriteAnalog(pins._3, pwm_val) #pwm control
#
servo_val = server.ReadI(3)
rgpio.WriteAnalog(pins._9_SERVO0, servo_val) #servo control
#
time.sleep(0.05)
#close CASP GPIO instance
rgpio.Close()