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!

2.1 - Hello, LED!

Welcome to your first hardware project with the Raspberry Pi Pico 2 W! In this lesson, we’ll learn how to make an LED blink using MicroPython. This simple project is a great way to get started with physical computing and understand how to control hardware with code.

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

PURCHASE LINK

Pico 2 W Starter Kit

450+

Pico 2 W Kit

You can also buy them separately from the links below.

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Getting to Know Pico 2 W

1

2

Micro USB Cable

1

3

Breadboard

1

BUY

4

Jumper Wires

Several

BUY

5

Resistor

1(220Ω)

BUY

6

LED

1

BUY

Schematic

sch_led

By setting the GPIO pin high or low, you’re controlling the voltage output of that pin. When the pin is high, current flows through the LED (limited by the resistor), causing it to light up. When the pin is low, no current flows, and the LED turns off.

Wiring

wiring_led

Writing the Code

Note

  • You can open the file 2.1_hello_led.ino under the path of pico-2w-kit-main/arduino/2.1_hello_led.

  • Or copy this code into Arduino IDE.

  • Don’t forget to select the board(Raspberry Pi Pico) and the correct port before clicking the Upload button.

const int ledPin = 15;  // GPIO pin connected to the LED

void setup() {
  pinMode(ledPin, OUTPUT);  // Initialize the GPIO pin as an output
}

void loop() {
  digitalWrite(ledPin, HIGH);  // Turn the LED on
  delay(1000);                 // Wait for 1 second
  digitalWrite(ledPin, LOW);   // Turn the LED off
  delay(1000);                 // Wait for 1 second
}

After uploading the code, you should see the LED turn on for 1 second and turn off for 1 second.

Understanding the Code

  1. Variable Declaration:

    Declare a constant integer ledPin and assign it the value 15, which corresponds to GPIO pin 15 where the LED is connected.

    const int ledPin = 15;
    
  2. Setup Function:

    The setup() function runs once when the board is powered on or reset. Here, we initialize ledPin as an output pin using pinMode().

    void setup() {
      pinMode(ledPin, OUTPUT);
    }
    
  3. Loop Function:

    • The loop() function runs repeatedly after setup().

    • Use digitalWrite() to set the voltage of ledPin. Setting it to HIGH provides 3.3V, turning the LED on. Setting it to LOW drops the voltage to 0V, turning the LED off.

    • The delay(1000) function creates a 1-second pause between the on and off states.

    void loop() {
      digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    }
    

Additional Tips

  • Understanding the Resistor: The 220Ω resistor limits the current flowing through the LED, preventing it from burning out.

  • Polarity Matters: Ensure the LED is connected correctly. The longer leg is the positive anode and should be connected to the resistor leading to the GPIO pin.

  • Experiment: Try changing the delay(1000) values to make the LED blink faster or slower.

Conclusion

Congratulations! You’ve built your first hardware project with the Raspberry Pi Pico 2 W. This simple LED blinking project is a fundamental step into the world of physical computing. From here, you can explore more complex projects by adding buttons, sensors, and other components.