Test Example-7b: Accessing Raspberry Pi Pico GPIO Pins from Python

Test Example-7b: Accessing Raspberry Pi Pico GPIO Pins from Python

This example demonstrates how to read/write GPIO pin values of Raspberry Pi Pico board (connected to the host computer through serial port) using a Python script on the host computer. Even through this test example uses Raspberry Pi Pico board, same procedure can be adopted for other Pico series micro-controller boards supported by CASP.

Pre-Requisites

  • Raspberry Pi Pico board with a USB cable.
  • CASP software is installed along with CASP Python Libraries.
  • Raspberry Pi Pico BSP is installed.
  • The project files are located at CASP installed directory ‘CASP/support/examples/casp_gpio_examples/rpi_pico_series_boards/casp_gpio_pico_serial’. These are also available for download at this link.

Programming the Board

Before starting this example, the target board should be programmed with binary file that enables CASP GPIO communication protocol. Please follow below steps to program the target board with the binary file that enables CASP GPIO communication protocol.

  • Connect Raspberry Pi Pico board to the USB of the computer
  • Run CASP and click on Tools > Programmer
  • It opens a programmer dialog box. Set board as RaspberryPi_Pico, programmer as Programme0, select serial port to which the board is connected and select the binary file from CASP installed directory ‘CASP/support/examples/casp_gpio_examples/rpi_pico_series_boards/casp_gpio_pico_serial/bin/casp_gpio_pico.uf2’
  • Click ’Program’ button to program the board with the above selected binary file. Screen shot is shown below.

Accessing GPIO from Python

  • Close existing CASP program.
  • Open terminal in directory ‘CASP/support/examples/casp_gpio_examples/rpi_pico_series_boards/casp_gpio_pico_serial/python’.
  • Start Python by typing ‘Python’ or ‘Python3’ in the terminal. Type following commands in Python to access GPIO values from the target board
> import casp_gpio as casp #import CASP GPIO module

> import casp_gpio_pins as pins #import CASP GPIO Raspberry Pi Pico pin description module ‘casp_gpio_pins.py’ located in the same directory where the terminal is running

> casp.Init() #initialize CASP GPIO client process on host computer

> casp_gpio.SerialPorts() #display available serial ports (used to identify the serial port to which the board is connected)

> casp.ConnectSerial("COM34") #starts communicating with the target board using GPIO protocol on COM35. Change the serial port as per your setup.

> casp.Write(pins.DO_GP_LED, 1) #LED ON

> casp.Write(pins.DO_GP_LED, 0) #LED OFF

> casp.Read(pins.ADC_GP26) #Read ADC value from pin-GP26

> casp.Read(pins.DI_GP0) #Read digital input pin GP0 value

> casp.Write(pins.PWM_GP16, 500) #Writes PWM value 500 (range 0 to 1000) to pin-GP16

> casp.Write(pins.SERVO_GP10, 45) #Writes servo angle 45 (range -90 to 90) to pin-GP10
  •  Below is the screen shot of above commands. You may try other pins from ‘casp_gpio_pins.py’

Related Links