How to use GPIO of Raspberry Pi Pico as Output Pins

Posted

in

by

One of the most important features of the Raspberry Pi Pico is its General-Purpose Input/Output (GPIO) pins. These pins can be used to control external devices, such as LEDs, motors, and relays.

The Raspberry Pi Pico has 26 GPIO pins, numbered from GP0 to GP25.
Each pin can be individually programmed to perform a specific task.

Steps to Use GPIO as Output Pins

To use the GPIO pins of Raspberry Pi Pico as output pins, follow these steps:

Step 1: Set up your Raspberry Pi Pico board and connect it to your computer via USB.

You can make the KiCad project like following.
Connect a LED via a 220 ohm resistor to the GP4 pins.

You can download and use the symbol and footprint from link below

Raspberry Pi Pico Symbol in KiCad 7 Project Library

To calculate the value of resistor you can use the Ohm’s Law Calculator
Use the following values
V (voltage): 3.3
A (Current) : 0.015
when you press calculate it will give you a resistance value of 220 ohm.

Remember never exceed the current value of 0.015A or 15mA for small leds as higher current may damage the component.

Schematic Diagram Showing LED connected to GP4 Pin via a 220 ohm resistor.

Step 2: Open your preferred code editor and create a new Python script.

Step 3: Import the required libraries by adding the following lines of code at the beginning of your script:

import machine
import utime

The “machine” library provides access to the GPIO pins of Raspberry Pi Pico, and the “utime” library allows us to add delays in our code.

Step 4: Define the pin number you want to use as an output pin. For example, if you want to use GP5 as an output pin, add the following line of code:

led_pin = machine.Pin(4, machine.Pin.OUT)

Here, we are using the “machine.Pin” class to define the pin number (4) as an output pin.

Step 5: Turn on the LED by setting the output value of the pin to 1. Add the following line of code:

led_pin.value(1)

This will set the output value of the GP4 pin to 1, which will turn on the LED connected to it.

Step 6: Add a delay in your code to keep the LED on for a specific time. For example, if you want to keep the LED on for 2 seconds, add the following line of code:

utime.sleep(2)

This will add a 2-second delay in your code.

Step 7: Turn off the LED by setting the output value of the pin to 0. Add the following line of code:

led_pin.value(0)

This will set the output value of the GP4 pin to 0, which will turn off the LED connected to it.

Step 8: Save your code and run it on your Raspberry Pi Pico board. You should see the LED turn on for 2 seconds and then turn off.

Complete Code

import machine
import utime

led_pin = machine.Pin(4, machine.Pin.OUT)

led_pin.value(1)

utime.sleep(2)

led_pin.value(0)

Example Code to try

import machine
import time

led_pin = machine.Pin(4, machine.Pin.OUT)

led_pin.value(1)  # turn on the LED
time.sleep(1)     # wait for a second
led_pin.value(0)  # turn off the LED
time.sleep(1)     # wait for a second

for i in range(10):
    led_pin.value(1)  # turn on the LED
    time.sleep(0.5)   # wait for half a second
    led_pin.value(0)  # turn off the LED
    time.sleep(0.5)   # wait for half a second

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *