[last updated: 2024-09-20]
RP2040-Zero home page
Thonny with RP2040
Terminal with RP2040
blink LED program on RP2040
OLED program on RP2040
Python home page
-----------------------------
--------------------------------------------------------------------------------------------------------------
- Doing the procedures here assumes you've already loaded MicroPython onto your RP2040 (See: RP2040-Zero home page)
- The RP2040 loaded with MicroPython runs python programs and single commands.
- When you apply power to the RP2040 (without pressing the boot PB),
Your device will execute main.py
(not sure... it may also execute boot.py or prog.py or something similar}
- The Bottom Line:
- While we say that the RP2040 with MicroPython
"runs python programs" ...
- It's more accurate to say that it:
"runs one program",
that being: main.py
So if you have python code you want to run,
and you want to save it to your RP2040
and have it run on powerup with or without being connected to a host PC,
You must put it into the main.py program file
Easiest way to do that is with Thonny (link above).
- If you just want to run a single command, for testing/development, use a terminal (link above).
--------------------------------------------------------------
- Installing "contributed" libraries:
- These are libraries that you find somewhere on the web, ie. Not in the usual default Thonny repository on PiPi.
- The usual disclaimers apply: I don't really know what I'm doing, these are just notes that worked for me...
- Find a library you want to use, often on github. Download a zip file.
Extract the zip file and put it into its own folder, somewhere that makes sense.
I put mine in ...Tech/rPi/RP2040/MicroPython/libraries.
AFAIK you don't really need to save these at all after you've uploaded them...
- In Thonny, View (top menu bar) --> Files
This will open the sidebar showing some folder on your PC, and below that, the files on the RP2040
- Click in the top section (blue text on my install) to go to some folder.
Navigate to where you saved your new library files.
Click to select the new library folder (NOT the zip file, but the folder with all the extracted files)
- Right-click, select Upload to/
- The new library should be installed into MicroPython and can be loaded into your programs with the usual import command.
--------------------------------------------------------------
- MicroPython Commands:
- GPIO:
- from machine import Pin
- p0 = Pin(pinGPIOnum, Pin.OUT)
p0.value(1) # turn on, 0 to turn off
- p0 = Pin(pinGPIOnum, Pin.IN)
p2 = Pin(2, Pin.IN, Pin.PULL_UP)
p2State = p2.value() # reads the pin
-
-
- onboard LED:
-----------------------
- timing functions:
- time.sleep()
- time.sleep_ms()
-----------------------
- serial:
- ... best guess so far, with caveat this is what worked on my RP2040-Zero clone ...
3 ways to get serial on the RP2040: hardware, software, and software-PIO
- Hardware:
- The RP2040 provides two hardware-based UARTS with configurable pin selection.
Serial1 is UART0, and Serial2 is UART1.
eg. UART.init(baudrate=9600, bits=8, parity=None, stop=1, tx=..., rx=..., *, ...)
from machine import UART
uart = UART(1, 9600) # for UART1 (Serial2)
uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
- for UART0 (Serial1):
tx=0 and rx=1 were the only ones that worked (though I did not try them all...)
- for UART1 (Serial2):
tx/rx = 4/5 or 8/9 worked (though I did not try them all...)
- Write data to serial port with:
Note the .write command requires a string, or byte array, or bytes object as argument, not a number.
- Read data from serial port with:
uart.read(10) # read 10 characters, returns a bytes object
uart.read() # read all available characters
uart.readline() # read a line, ie. a string terminated with CR or LF
uart.readinto(buf) # read and store into the given buffer
- Software:
- FWIW: default settings for softwareSerial that I used on the nano RS485 programs is: 8 bits, NO parity, 1 stop bit
- i2c:
- The pinout for the zero shows about 11 pairs of pins that can be used for sda/scl for i2c
However in trying to get an OLED working, I could not get all of them to work, though didn't test exhaustively.
- sda/scl pairs that worked: 2/3, 26/27
- PIO:
Instantiate a SerialPIO(txpin, rxpin, fifosize) object in your sketch and then use it the same as any other serial port. Even, odd, and no parity modes are supported, as well as data sizes from 5- to 8-bits. Fifosize, if not specified, defaults to 32 bytes.
- from some forum post...In pin order, the sketch uses Serial1 (0,1) SerialPIO (2,3 4,5 6,7) Serial2 (8,9) SerialPIO (10,11) to give a total of 6 serial ports without interfering with the USB port.
-----------------------
- initialize OLED:
-----------------------
- write text to OLED:
-----------------------
- draw graphics to OLED:
- oled.vline(0, 0, 32, 1) # left border # draw vertical line x=0, y=0, height=32, colour=1=white
- oled.hline(...
- oled.fill(0) ... 0 fills with black, 1 with white
-----------------------
- rotate screen:
-----------------------
--------------------------------------------------------------
- Links: