Welcome to Raspberry Pi Pico 2 repository
3. Button control LED light
3. Button control LED light
1. Learning objectives
- Learn the basic use of the pins of the Raspberry Pi Pico 2 .
- Understand how to control the LED light with buttons.
2. Hardware construction
This course.
LED light*1
Button*1
220Ω resistor*1
The circuit wiring diagram is as follows:
3. Program analysis
Thonny programming
For the use of ThonnyIDE, please refer to the previous environment construction related courses.
ximport machineimport utimeled_external = machine.Pin(15, machine.Pin.OUT)button = machine.Pin(14, machine.Pin.IN)while True:if button.value() == 1:led_external.value(1)utime.sleep(2)led_external.value(0)
import machine
The machine library contains all the instructions MicroPython needs to communicate with Pico and other MicroPython-compatible devices, extending the language of physical computing.
import utime
The "utime" library. This library handles everything to do with time, from measuring it to inserting delays into your program. The units are in seconds.
led_external = machine.Pin(15, machine.Pin.OUT)
Configure IO15 as an output pin.
button = machine.Pin(14, machine.Pin.IN)
Configure IO14 as an input pin.
button.value()
Get the value of the button pin.
led_external.value(1)
Set the LED light pin to a high level.
utime.sleep(2)
This calls the sleep function from the utime library, which will pause the program for any number of seconds you type - in this case, 2 seconds.
4. Experimental phenomenon
After the program is downloaded, press the button and the LED light will turn on for 2 seconds.