Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 01: Button Module

In this lesson, you will learn how to use Raspberry Pi Pico W to interact with the onboard LED using a button. Pressing the button will light up the LED, and releasing the button will turn it off. This project is ideal for beginners as it offers hands-on experience with input and output operations on Raspberry Pi Pico W using MicroPython.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Raspberry Pi Pico W

BUY

Button Module

-

Breadboard

BUY

Wiring

../_images/Lesson_01_Button_Module_bb.png

Code

from machine import Pin
import time

# Set GPIO 19 as an input pin to read the button state
button = Pin(19, Pin.IN)

# Initialize the onboard LED of the Raspberry Pi Pico W
led = Pin('LED', Pin.OUT)

while True:
    if button.value() == 0:  # Check if the button is pressed
        led.value(1)  # Turn on the LED
    else:
        led.value(0)  # Turn off the LED

    time.sleep(0.1)  # Short delay to reduce CPU usage

Code Analysis

  1. Importing Modules

    The machine module is imported to interact with the GPIO pins, and the time module is for handling timing.

    from machine import Pin
    import time
    
  2. Setting up the Button

    GPIO 19 is configured as an input pin. This will read the state of the push button connected to it.

    button = Pin(19, Pin.IN)
    
  3. Setting up the LED

    The onboard LED is set up as an output pin, enabling us to turn it on or off programmatically.

    led = Pin('LED', Pin.OUT)
    
  4. Main Loop

    • An infinite loop is used to continuously check the state of the button.

    • If the button is pressed (button.value() == 0), the LED is turned on. Otherwise, it’s turned off.

    • A short delay of 0.1 seconds is added to reduce CPU usage.

    The button module used in this project has an internal pull-up resistor (see its schematic diagram), causing the button to be at a low level when pressed and remain at a high level when released.

    while True:
        if button.value() == 0:  # Check if the button is pressed
            led.value(1)  # Turn on the LED
        else:
            led.value(0)  # Turn off the LED
        time.sleep(0.1)  # Short delay to reduce CPU usage