Delivery throughout Europe

Ordered before 16:00 = Shipped today

Fast delivery with DHL

XNUMX days return *


Country

Raspberry Pi Pico – Lesson 2: Raspberry Pi Pico LED flashing with push button

Discover a fascinating project with the Raspberry Pi Pico: Flash an LED at two speeds using MicroPython and a push button. This lesson is perfect for beginners who want to get acquainted with the Thonny programming tool and MicroPython. Learn step by step how to do the Raspberry Pi Pico connects to your computer, uploads programs and flashes the internal LED at variable speeds. The LED is conveniently located next to the USB connection on the Pico. Start your adventure in the world of Raspberry Pi with this practical and educational project!

This manual covers: 

  • Level - Beginner 25% 25%
  • Duration 20 min 25% 25%
  • Costs €15,00 20% 20%
Raspberry Pi Pico LED with push button

This is the second lesson from the introductory projects for the Raspberry Pi Pico. Before you start this lesson, we recommend that you first complete the previous lessons. You will find lesson 1 HERE.

Step 2 – Install software on the Raspberry Pi Pico

You can skip this step if you already did it in Lesson 1.
Around the Raspberry PiTo be able to program with MicroPython, we must first flash the firmware of the Pico.
This means that we provide the internal software that starts the pico with a special Python version.

Download firmware from the website below (uf2 file)
https://micropython.org/download/RPI_PICO/
https://micropython.org/download/RPI_PICO_W/
(Make sure you select the correct version of the firmware, the WiFi version and the non-WiFi version are different)
Press the white boot button on the pico and keep it pressed.
Plug the pico into a USB port on your computer. (Then you can release the button)
There will now be a drive letter, as accessible with a memory stick.
Copy the firmware file RPI_PICO_xxxxxx.uf2 to this drive
(pico will restart and the firmware will be installed)
Download and install Thonny on your PC.
(https://thonny.org/)

Select the MicroPython Interpreter below
Tools > Options > Interpreter > MicroPython (Raspberry Pi Pico)

 

Place the components as shown below on a breadboard.

Weerstand

The resistance is 220 ohms.

LED (Light Emitting Diode)

The LED works like a diode, which means that the current can only flow in one direction.
Please look at the overview below.

Push button (Tactile switch)

With the selected push button, legs 1 and 2 are connected to each other, as are 3 and 4.
There are also push buttons where this is different.

The diagram below can be quickly built to test the button and LED.
Here we have placed the button and the LED together with the resistor in a row, the button will directly control the LED.

Step 3 – Programming the Raspberry Pi Pico to make the LED flash with the push button

Now you can type in the program below in the Thonny interface.

from machine import Pin import time led = Pin(4, Pin.OUT) button = Pin(5, Pin.IN, Pin.PULL_DOWN) while True: if button.value(): led.toggle() time.sleep(0.1 ) else: led.toggle() time.sleep(1)

Explanation of the program:

  1. Import the pin class from the python machine module
  2. Import the time module
  3. .
  4. Define a variable ice with pin 4 as OUTPUT 
  5. Define a variable button with pin 5 as input, the internal pull-down resistor will make the input 0 if the button is not pressed. When pressing the button, this pin 5 is connected to 5V.
  6. .
  7. An infinite loop is started here. (Lines 8-13)
  8. If the value of button is true, lines 9 and 10 will be executed
  9. Toggle the LED (If it is off it will be turned on, and vice versa)
  10. Wait a tenth of a second
  11. If the value of button is NOT true (i.e. a 0), lines 12 and 13 will be executed.
  12. Toggle the LED (If it is off it will be turned on, and vice versa)
  13. Wait 1 second.

Then press save and save the program on your own computer.

Press the red stop sign button to restart the backend.
(Thonny will now reconnect to your Pico)

Then you could press the green button (Run current script).

The LED should flash slowly (1x per second)
If the button is pressed continuously, the LED will flash 10x as fast.

If the pico is now removed from the computer and reconnected, the program will not be started.
We only tested the programming code.

To enable automatic starting, we must save the program again, but then select the pico.

File > Save As > Raspberry Pi Pico

Save the program with its name main.py. The pico will automatically start the program with this name when connecting the Pico to a USB port or other power supply.

Step 4 – Result: Raspberry Pi Pico LED flashes with a push button

You now have Thonny installed and a Raspberry Pico connected to your PC. Electronics on it board connected and then wrote and executed the code.
Is the LED not flashing? Then take a look at the previous steps and try again!

Did you enjoy doing this project? Check out our other projects, or Les three from this Pico series!

The rating of www.elektronicavoorjou.nl at WebwinkelKeur Reviews is 9.3/10 based on 5005 reviews.