Warning
This page is located in archive.

Lab 01

Exercise Objectives

  • Installation of necessary SW
  • Introduce MicroPython Environment
  • First program

Micropython on Raspberry Pi Pico W

First code

In Thonny command line and write there

from machine import Pin

This will import functionality will import class Pin necessary to control GPIO pins. Intenal LED is connected to pin with symbolic name 'LED', so we can create objected to control this pin. Second parameter of the object constructor of direction of communication.

led = Pin('LED', Pin.OUT)

If the object exists, try following:

pin.on()

Other commands: led.off(), led.toggle()

Blinking using delay

This version of program uses sleep_ms(). Create script called main.py and save it to the RPi Pico. If the name of the script is as mentioned, code will be executed after restart.

from machine import Pin
import time
 
led = Pin('LED', Pin.OUT)
 
while True:
    time.sleep_ms(1000)
    led.toggle()

Blinking using timer

More advanced program uses Timer.

from machine import Pin, Timer
 
led = Pin('LED', Pin.OUT)
timer = Timer()
 
def blink(timer):
  led.toggle()
 
timer.init(freq=2.5, mode=Timer.PERIODIC, callback=blink)

courses/be2m37mam/labs/01.txt · Last modified: 2023/11/02 10:35 by viteks