/RPI_Pico_micropython

RPI Pico micropython playground

Primary LanguagePythonMIT LicenseMIT

RPI Pico Playground

Messing around with the RPI pico.

asciicast

Installing Micropython on the Pi Pico

Drag and Drop Micropython

  1. Download the Micropython UF2 file for the board here
  2. Push and hold the BOOTSEL button and plug your Pico into the USB port of your Raspberry Pi or other computer. Release the BOOTSEL button after your Pico is connected.
  3. It will mount as a Mass Storage Device called RPI-RP2.
  4. Drag and drop the MicroPython UF2 file onto the RPI-RP2 volume. Your Pico will reboot. You are now running MicroPython
  5. You can access the REPL via USB Serial

Connecting via USB

The MicroPython firmware is equipped with a virtual USB serial port which is accessed through the micro USB connector on Raspberry Pi Pico. Your computer should notice this serial port and list it as a character device, most likely /dev/ttyACM0.

Install a serial terminal emulator

I use picocom:

$ sudo apt install picocom

Open the serial port as:

$ picocom /dev/ttyACM0

Quit the program with C-a-q(Ctrl+a+q) assuming that C-a is the escape character.

Ampy

Utility to interact with a CircuitPython or MicroPython board over a serial connection.

Installation

$ pip install adafruit-ampy

Usage

  1. List the files on the board
$ ampy --port /dev/ttyACM0 ls
  1. Put a file on the board
$ ampy --port /dev/ttyACM0 put main.py
  1. Remove a file from the board.
$ ampy --port /dev/ttyACM0 rm main.py
  1. Run a script on the board
$ ampy --port /dev/ttyACM0 run main.py

Configuration

For convenience you can set AMPY_PORT and AMPY_BAUD environment variables which will be used f the port parameter is not specified. To set these variables automatically each time you run ampy, copy them into a file named .ampy:

AMPY_PORT=/dev/ttyACM0
AMPY_BAUD=115200

Put this .ampy file to your project's directory and you can now run ampy within this directory without having to specify this parameters

$ ampy run main.py

Example usage

  1. Create the hello_world.py file and add whatever:
while 1:
    print("Hello world!")
  1. Put the file on the RPI Pico We put our hello_world.py file to the board as main.py so that it autoruns. Think of main.py as the entrypoint module run by the Pico on reset/reboot:
$ ampy put hello_world.py main.py
  1. List the files on the pico
$ ampy ls
/main.py
  1. Open up the boards serial port and do a soft reset/reboot with C-d(Ctrl+d) and the code will autorun!:
$ picocom /dev/ttyACM0
picocom v2.2

port is        : /dev/ttyACM0
flowcontrol    : none
baudrate is    : 9600
parity is      : none
databits are   : 8
stopbits are   : 1
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : no
send_cmd is    : sz -vv
receive_cmd is : rz -vv -E
imap is        : 
omap is        : 
emap is        : crcrlf,delbs,

Type [C-a] [C-h] to see available commands

Terminal ready

>>> 
Hello Micropython!
Hello Micropython!
Hello Micropython!
Hello Micropython!
Hello Micropython!
Hello Micropython!
Hello Micropython!
Hello Micropython!
Hello Micropython!
Traceback (most recent call last):
  File "main.py", line 2, in <module>
KeyboardInterrupt: 
MicroPython v1.19.1 on 2022-06-18; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> 

Quit picocom with C-a-q(Ctrl+a+q)

Resources

  1. Documentation
  2. Raspberry Pi Pico Python SDK
  3. Ampy

Credits

  1. Africa open hardware community
  2. Raspberry pi foundation for the free Pico :)

License

license

Copyright 2022 Daniel Chege Nduati