Using the Raspberry Pi Pico

This discussion has a more recent version.
MicroCenterOfficial
edited April 2022 in Maker

The freshly-released Pico is Raspberry Pi's first venture into Arduino boards. But without an on-board OS like Raspberry Pi's other boards, the Pico's utility is a bit more niche and might require a bit of an explanation for newcomers and veterans alike. That's where we come in.

Getting Started with MicroPython

The Raspberry Pi Pico is very straightforward. You'll need a Linux, Windows, Mac PC, or a Raspberry Pi set up as a desktop. You'll need to install MicroPython on your Pico. Visit https://pico.raspberrypi.org/getting-started/ and click on "Getting started with MicroPython." Download the UF2 file and follow the instructions under "Drag and drop MicroPython."


You'll also need to download and install Thonny. Thonny is a Python Integrated Development Environment, or IDE and console in one, allowing you to write code and interact with the Pico in the same program. Thonny is available on all platforms and is preinstalled on the Raspberry Pi OS.

Thonny Setup

After installing and launching Thonny, click "Select Interpreter" under "Run."


Next, set the interpreter to "Micro Python (Generic)" and choose the proper port. I've had good luck with the automatic port selection option, but you may need to select a port manually.

 

Hello World Script

Our first script will be the classic "Hello World" script. Enter the following text in the text editor:

print("Hello World")

Then click the "Run" icon under "View" and "Run" in the menu bar. It's a green circle with a white "play" icon in it. Thonny will ask where you would like to save the file. Select MicroPython device and give the file a descriptive name, like "hello.py" – make sure to include the .py.


You should see the text "Hello World" in the terminal at the bottom of the window.


Setting up LED Blink

Our next script will be a single blinking LED. Using a breadboard, an LED, and a 1k resistor, wire up the following circuit:


In this circuit, we connected the positive side of the LED to GPIO 1 and the negative side ground with a 1k resistor. This resistor is called a current limiting resistor, and it reduces the amount of current flowing through the circuit by increasing the total resistance of the circuit. The value of this resistor isn't significant. A few hundred ohms is adequate. The higher the value of the resistor, the dimmer the LED will be. Current limiting resistors serve to protect the LED and keep it from burning out. Since the Pico uses 3.3v logic, you can usually get away without a current limiting resistor, but it's a good idea to include one to be safe.

Replace the hello world code with the following code and run it as described above:

import machine #library for hardware; allows us to access pins

import time #for delays

redLed = machine.Pin(1, machine.Pin.OUT) #initialize GPIO 1 as output for red LED

while (1): #infinite loop; runs until manually stopped

    redLed.toggle() #if LED is on, turn off. Else, turn on.

    time.sleep(.5) #pause for 1/2 second
<br>

This will turn the LED on for half a second then off for half a second until the code is interrupted. You may have noticed some words change colors when you paste or type this into Thonny. Thonny highlights specific keywords to make them easier to identify. For example, control keywords (like "while" and "import") are bold magenta, and strings (anything surrounded by quotation marks) are green. Comments, which are anything preceded by a #, show up in light gray. These aren't part of the code but are used to explain what the code is doing.

We can make things a little more interesting by wiring up this circuit:


This is essentially the same circuit as the last example, but it has been duplicated two times.

After wiring up this circuit, run the following code:

import machine #library for hardware; allows us to access pins

import time #for delays


redLed = machine.Pin(1, machine.Pin.OUT) #initialize GPIO 1 as output for red LED

yellowLed = machine.Pin(2, machine.Pin.OUT) #initialize GPIO 2 as output for yellow LED

greenLed = machine.Pin(3, machine.Pin.OUT) #initialize GPIO 3 as output for green LED


redLed.value(0) #Turn off red LED

yellowLed.value(0) #turn off yellow LED

greenLed.value(0) #turn off green LED


while (1): #infinite loop; runs until manually stopped

    redLed.toggle()

    time.sleep(.5)

    redLed.toggle()

    yellowLed.toggle()

    time.sleep(.5)


    yellowLed.toggle()


    greenLed.toggle()

    time.sleep(.5)

    greenLed.toggle()


    yellowLed.toggle()

    time.sleep(.5)

    yellowLed.toggle()

 

The LEDs will flash in red, yellow, green, and yellow until the script is interrupted.

 

Pushbutton Input

Next, we'll look at some simple inputs with a pushbutton. Wire up this circuit using the same components we used before, plus a pushbutton:


Here we have removed the yellow LED and installed a pushbutton. One side of the button is wired to GPIO 4, and the other is wired to 3.3v. Notice we're using two legs on opposite corners of the pushbutton rather than the same side. It is the easiest way to ensure the contacts you use aren't connected inside the switch.

Next, load and run this code:

<p>import machine #library for hardware; allows us to access pins

import time #for delays


redLed = machine.Pin(1, machine.Pin.OUT) #initialize GPIO 1 as output for red LED

greenLed = machine.Pin(3, machine.Pin.OUT) #initialize GPIO 3 as output for green LED

button = machine.Pin(4,machine.Pin.IN) #initialize GPIO 4 as input for button


redLed.value(0)  #set LED initial values

greenLed.value(1)


while(1):

    if button.value():  #if button is pressed, button.value == 1

        redLed.toggle()  #Toggle the LEDs

        greenLed.toggle()

        time.sleep(.5)  #Pause for half a second

    #If the button isn't pressed nothing happens and the while loop executes again
</p>

 

When you push the button, both lights will toggle. Either the red or green LED will always be on, but never both at the same time. If you hold the button, the lights will toggle every half second until you let go. The delay in the while loop causes this. This delay also slows down the code's execution. If you remove it, the lights will flash faster than you can press the button, causing it to flash multiple times each time you press the button.

 

While these examples are straightforward, they form the building blocks of many projects. The LEDs could be replaced with buzzers, motors via an H bridge or other motor controller, or brighter 12v LEDs via a MOSFET or relay. The pushbutton could be replaced with a bump switch, PIR motion sensor, or photoresistor.

Beyond the basic digital I/O we covered in these examples, the Pico also supports several serial protocols, including IIC and SPI, allowing you to connect a vast range of sensors, displays, and other I/O devices. The Pico also supports pulse width modulation (or PWM) and analog inputs. PWM can be used to vary the intensity of LEDs or drive servos, and analog inputs can be used to take readings from potentiometers, thermistors, or photoresistors. Since it's all programmed in MicroPython, any libraries or tutorials written for other MicroPython boards will port to the Pico without much trouble.

If you're looking for a starting point for your project, we offer many components and component sets in store:

 

Inland Pi Kit Deluxe Parts Pack

Inland PIR Motion Sensor Module

37 in 1 Sensor Kit

Elenco Electronics LEDK-80 80 Piece LED Component Kit

NTE Electronics LED 3MM Blue Water Clear Lens 1500MCD & 1/8W 220 OHM Resistor – 25 Pack

Inland 400 Tie-Points Breadboard – 3 Pack

Inland Dupont Jumper Wire 20cm – 3 Pack

NTE Electronics 5MM Photoresistor (LDR) – 10 Pack

Inland Blue 9G Servo – 3 Pack

We love seeing what our customers build

Submit photos and a description of your PC to our build showcase

Submit Now
Looking for a little inspiration?

See other custom PC builds and get some ideas for what can be done

View Build Showcase

SAME DAY CUSTOM BUILD SERVICE

If You Can Dream it, We Can Build it.

Services starting at $149.99