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 Raspberry Pi Pico2 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-GP26) and digital input pin (pin-GP2) values
  • Blink on-board LED
  • Toggle digital output pin-GP6 value
  • Vary PWM on pin-GP16
  • Vary Servo angle on pin-GP10

Pre-Requisites

  • Raspberry Pi Pico2 board.
  • Compatible USB cable.
  • CASP software along with Raspberry Pi Pico 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 Raspberry Pi Pico2 board to the host computer through a serial cable.

Flashing Raspberry Pi Pico2 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 Raspberry Pi Pico2 serial port is one of them
python(or python3) rgpio_flasher.py
#now repeat the above command with serial port (to which the board is connected) as an argument
python(or python3) rgpio_flasher.py "COM3"
#the above command should program Raspberry Pi Pico2 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-GP26, limit switch to pin-GP2, external LED or relay module to pin-GP6, DC motor driver module to pin-GP16, servo to pin-GP10 etc. of Raspberry Pi Pico2 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 Raspberry Pi Pico2 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("COM37", 115200, True, 1, 24, 24) #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.GP2_DI)
    adc = rgpio.ReadAnalog(pins.GP26)
    print(f"di={di}, adc={adc}")
    #
    rgpio.WriteDigital(pins.GP_LED_DO, (i % 2)) #blink logic
    rgpio.WriteDigital(pins.GP6_DO, (i % 2))
    #
    pwm_val += 50
    if pwm_val > 4095:
        pwm_val = 0
    rgpio.WriteAnalog(pins.GP16, pwm_val) #pwm control
    #
    servo_val += 10
    if servo_val > 90:
        servo_val = -90
    rgpio.WriteAnalog(pins.GP10_SERVO, 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_rpi_pico_pico2_serial.pins
Target details:
 Name: RaspberryPiPico
 Read size (bytes): 24
 Write size (bytes): 24
 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:
GP0_DI = 0 #DI0
GP1_DI = 1 #DI0
GP2_DI = 2 #DI0
GP3_DI = 3 #DI0
GP4_DI = 4 #DI0
GP5_DI = 5 #DI0
GP6_DO = 0 #DO0
GP7_DO = 1 #DO0
GP8_DO = 2 #DO0
GP9_DO = 3 #DO0
GP_LED_DO = 4 #DO0
GP26 = 1 #ADC
GP27 = 3 #ADC
GP28 = 5 #ADC
TEMP = 7 #ADC
GP16 = 1 #PWM
GP17 = 3 #PWM
GP18 = 5 #PWM
GP19 = 7 #PWM
GP20 = 9 #PWM
GP21 = 11 #PWM
GP22 = 13 #PWM
GP10_SERVO = 15 #PWM
GP11_SERVO = 17 #PWM
GP12_SERVO = 19 #PWM
GP13_SERVO = 21 #PWM
GP14_SERVO = 23 #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 Raspberry Pi Pico2 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 Raspberry Pi Pico2 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("COM37", 115200, True, 1, 24, 24) #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.GP2_DI) #digital input
    adc = rgpio.ReadAnalog(pins.GP26) #analog input
    server.WriteI(0, di)
    server.WriteI(1, adc)
    print(f"di={di}, adc={adc}")
    #
    led_on = server.ReadI(0)
    rgpio.WriteDigital(pins.GP_LED_DO, led_on) #led control
    #
    do_on = server.ReadI(1)
    rgpio.WriteDigital(pins.GP6_DO, do_on) #digital output
    #
    pwm_val = server.ReadI(2)
    rgpio.WriteAnalog(pins.GP16, pwm_val) #pwm control
    #
    servo_val = server.ReadI(3)
    rgpio.WriteAnalog(pins.GP10_SERVO, servo_val) #servo control
    #
    time.sleep(0.05)

#close CASP GPIO instance
rgpio.Close()

Related Links