Getting Started with MicroPython
Currently, the MicroPython downloads page shows 186 boards currently supported with a binary download available.
This means it shouldn't be hard to find a compatible board to work with.
For this "Getting Started" class, I will focus on the Raspberry Pi Pico board.
Just like many of the libraries available to download for peripheral support,
some "tweeking" may be required to get things working for you.
Raspberry Pi Pico
RP2040 processor, 133MHz, 264KB RAM, 2Mbyte FLASH
Raspberry Pi Pico2
RP2350 processor, 150MHz, 520KB RAM, 4Mbyte FLASH
Each of the above boards is available with WiFi and/or pre-installed headers
Class Prerequisites
• Windows / Linux / Mac PC (Laptop) • Thonny IDE, version 4.1.7 (or later) https://thonny.org/ • MicroPython Capable Development Board Any of the of the boards identified on this page: https://micropython.org/download/ • USB Cable for Development Board
Terminology
REPL
A Python REPL (Read-Eval-Print Loop) is an interactive way to execute Python code. It reads user input, evaluates it, prints the result, and loops back to read more input.
Outline
- General Python – Comments
- General Python - Looping – Using “for”, “range()”, and “print()”
- General Python – Looping – Using “continue” and “break”
- General Python – Variables, “global” keyword
- General Python - Lists
- General Python – Functions
- MicroPython – Defining Pins, Pin functions
- MicroPython – LEDs, Buttons
General Python – Comments
Comments begin with: # Comment block – Begin and End with three quotes, single or double: ’’’ or ””” See comments.py
#comments.py # Comments begin with a '#' character. Either on a blank line x = 3 # or following a line of code # If you have a large multi-line comment or wish to comment out some code # you can use three quotes to begin and end the comment block ' ' ' # Using single quotes for this comment block i = 7 fruit = 'apple' brain = 'dead' salary_adjustment = -0.07 # This year we have a negative adjustment ' ' ' actual_code = "here" """ # Using double quotes for this comment block i35 = 'stopped' i635 = "parking lot" i75 = 'nail biter' """ more_code = "more more more" print(x, actual_code, more_code) # using a comma between items adds a space
General Python - Looping – Using “for”, “range()”, and “print()”
See range.py
#range.py # Show some examples using the range() function # 0-9 for i in range(10): print(i) print() # print emtpy line # 5-9 for i in range(5,10): print(i) print() # 10-1 (count down using a negative step value) for i in range(10, 0, -1): print(i) print() # 0-9 all on one line (we defined our own line ending of none) for i in range(10): print(i,end=) print('\n') # display value as hex for i in range(16): print(hex(i)) print() # display value as uppercase hex for i in range(16): print(hex(i).upper())
General Python – Looping – Using “continue” and “break”
See continue.py
#continue.py # Show examples using "continue" and "break" keywords # continue example for i in range(100): if(i % 3): # 1, 2, 4, 5, .... continue # go back to top of loop print(i) # break example if i > 50: break # exit loop
General Python – Lists, Tuples, Set, Dictionaries
Initially, each of these groups appears similar (although they are each different) creating possible confusion. The selection for each is based on the context.
List
• Lists are used to store multiple items in a single variable. • List items are ordered, changeable, and allow duplicate values. • List items are indexed, the first item has index [0], the second item has index [1] etc • This is similar to an array, but may contain different data types. • Written with [square brackets] https://www.w3schools.com/python/python_lists.asp Example: fruit_list = ["apple", "banana", "cherry", "pumpkin"]
Tuple
• Tuples are used to store multiple items in a single variable. • Tuple is a collection which is ordered, unchangeable, allow duplicate values. • Tuple items are indexed, the first item has index [0], the second item has index [1] etc • Written with (round brackets) https://www.w3schools.com/python/python_tuples.asp Example: file_tuple = ('readme.md', 0x8000, 0, 462) # name, type, inode, size
Set
• Sets are used to store multiple items in a single variable. • Set items are unordered, unchangeable, and do not allow duplicate values. • Written with {curly brackets} https://www.w3schools.com/python/python_sets.asp Example: software_set = {'Jim', 'Doug', 'Sameer'}
Dictionary
• Dictionaries are used to store data values in key:value pairs. • A dictionary is a collection which is ordered, changeable and do not allow duplicates. • Written with {curly brackets} https://www.w3schools.com/python/python_dictionaries.asp Example: Wifi_access = { "ap": "Q7TG33", "pw": "mysecretpw", "pref_chan": 11 }
General Python – Functions
def hurrah(count): for _ in range(count): print('Hurrah') hurrah(7)
General Python – Variables, “global” keyword
Inside a function, all variables are considered part of the function. A variable, “count”, inside a function, won’t affect a global “count”, unless the keyword, “global” is used. Use the “global” keyword if you want to change a global variable inside a function. See: functions.py
MicroPython – Defining Pins, Pin functions
The Pin class from the machine module is used to define and configure processor pin functionality. The pin’s id may be a number, string, or tuple depending on the processor family. Pin mode can be: Pin.IN, Pin.OUT, Pin.OPEN_DRAIN, Pin.ALT, Pin.ALT_OPEN_DRAIN, Pin.ANALOG Pin pull can be: None, Pin.PULL_UP, Pin.PULL_DOWN Pin value, only valid for Pin.OUT and Pin.OPEN_DRAIN modes and specified initial output Pin drive specifies the output power of the pin and can be Pin.DRIVE_0, Pin.DRIVE_1, … Pin alt specified the alternate function for the pin Pin.init(), Pin.value(), Pin.__call__(), Pin.on(), Pin.off(), Pin.irq(), Pin.low(), Pin.high(), Pin,mode(), Pin.pull(), Pin.drive(), and Pin.toggle() are used to further define the pin or get pin information. See: https://docs.micropython.org/en/latest/library/machine.Pin.html#machine-pin https://docs.micropython.org/en/latest/library/machine.html
MicroPython – LEDs, Buttons
Blink the LED
#led_blink.py import utime from machine import Pin led=Pin(25,mode=Pin.OUT) while True: led.on() utime.sleep(.5) led.off() utime.sleep(.5)
When button is pressed, turn on LED
#button.py from machine import Pin # machine is an internal class # See: https://docs.micropython.org/en/latest/library/machine.html led=Pin('LED',Pin.OUT) # For Pico, GPIO25 == 'LED' button=Pin(15,Pin.IN, Pin.PULL_UP) # Input with pull-up while True: if button.value() == 0: # Button is grounding the signal led.value(1) else: # Pull-up raising the signal led.value(0)
See: led_blink.py & button.py
References:
https://www.w3schools.com/python/python_syllabus.asp