Raspberry Pi Pico Tutorial

by James Crowfoot in Circuits > Microcontrollers

190 Views, 3 Favorites, 0 Comments

Raspberry Pi Pico Tutorial

20240821_101353.jpg

Introduction

In this tutorial, I will guide you on how to start coding with the raspberry pi pico with micropython. I will walkthrough the code and explain what each section does and provide the full code at the end of each section.

Supplies

Supplies


Hardware

  1. Raspberry Pi Pico / Pico W or Raspberry Pi Pico 2 with headers.
  2. Computer that runs linux,mac os, or windows
  3. micro usb cable

Electronic components

  1. Breadboard
  2. Male to male jumper wires
  3. Male to female jumper wires
  4. LEDs
  5. Pushbuttons
  6. potentiometer
  7. Photoresistor
  8. PIR sensor

Software

  1. A compatible IDE, I will use Thonny.
  2. UF2 file for your Pico

Setup

Downloading the software


If you have not already got a compatible IDE (Integrated Development Environment) on your device, download one.

I will be using Thonny, It is a good python IDE for beginners and is FREE! You can download it here: Thonny, Python IDE for beginners


Setup of Thonny


Once you have downloaded Thonny, open it up and click "Run" on the menu bar at the top.

Next, click "select interpreter" and change it from "The same interpreter that runs Thonny (default)", to Micropython (Raspberry Pi Pico).


Setup the Raspberry Pi Pico


Download the UF2 file for your Pico here.

To program your device, follow these steps:

  1. Push and hold the BOOTSEL button while connecting your Pico with a USB cable to a computer. Release the BOOTSEL button once your Pico appears as a Mass Storage Device called RPI-RP2.
  2. Drag and drop the MicroPython UF2 file onto the RPI-RP2 volume. Your Pico will reboot. You are now running MicroPython.


Connecting to Thonny


While your Pico is plugged into your device, click the stop button in Thonny or Ctrl+F2.

If this does not work try unplugging it an plugging it back in.


We are ready to code! :D

Blinking an LED

Basic Blink


In this step we will learn to make an LED blink.

In Thonny, open a new file and save it as main.py

.py is the file extension for python files and main is just the name we have given it. main.py is a special file as it will always run as soon as the Pico gets power, but you don't need to worry about that for now.

The first line of code we will type is:

from machine import Pin

This imports the function Pin from the machine MicroPython library. The machine library is a fundamental library when coding with the raspberry pi pico. You will use to control the GPIO pins (General Purpose Input Output pins).

the next line is:

from time import sleep

This imports the function sleep from the time python library, this will allow us to wait a certain about of time in our code.


Now we will define our LED, we will be using the inbuilt LED, which is located at Pin 25, we will do this by using the following code:

led = Pin(25,Pin.OUT)

What this code is doing is it is defining the variable led as the pin 25 which we are also defining as an output pin.


now to make the led blink we will use the following code:

led.value(1) #turns led on
sleep(1) #waits 1 second
led.value(0) #turns led off

led.value(1 or 0) sets the state of the led to on (1) or off (0), sleep(number) makes the code wait that many seconds


your code should look like this:

from machine import Pin
from time import sleep

led.value(1)
sleep(1)
led.value(0)

now you can press the run code button (green circle with triangle in it) or press F5 to run the code.

your LED should blink once.


Loops


We will now learn a little bit about loops. There are two ways in which you can do a loop in python, a for loop or a while loop.

A for loop would look like this:

for i in range(0,10):
print(i)

This code will loop 10 times and will list the numbers 0 to 10.


A while loop would look like this:

while True:
print("loop forever")

This loop will loop forever as it will loop while the statement is True. you can escape a while loop with break.


Blinking an LED in a loop


here are two ways to loop an led blinking:

For Loop ()

from machine import Pin

from time import sleep
led = Pin(25,Pin.OUT)

for i in range(0,10): #loops 10 times
led.value(1) #turns LED on
sleep(0.5) #waits for 0.5 seconds
led.value(0) #turns LED off
sleep(0.5) #waits 0.5 seconds


While loop (forever)

from machine import Pin
from time import sleep
led = Pin(25,Pin.OUT)

while True:
led.value(1) #turns LED on
sleep(0.5) #waits for 0.5 seconds
led.value(0) #turns LED off
sleep(0.5) #waits 0.5 seconds

Button Control LED

0.jpg

In this step you will also need a breadboard and pushbutton

Pinout Diagram


Button Read


Wire up your Raspberry Pi Pico to a breadboard with a button, have one button pin going to ground and the other going to Pin 16.


First, we will import our libraries

from machine import pin
from time import sleep

Next, we will define our pushbutton

button = Pin(16,Pin.IN,Pin.PULL_UP)

we have defined it as Pin.IN as we are reading its value, and we also add a pull up resistor by adding Pin.PULL_UP

we can now read its state by using

button.value()

if it is equal to 1 then the button is not pressed and if it is 0 then it is pressed.

we can put this into a loop like so:

from machine import Pin
import time

button = Pin(16, Pin.IN, Pin.PULL_UP)

while True:
if button.value() == 0:
print("THe button is pressed")
else:
print("The button is released")
time.sleep(0.1)


Button control LED


We will modify our existing code and circuit and add a LED controlled by the button.

I am going to use an external LED for this project, which I will plug into Pin 15, using a 220 ohm resistor. Pinout Diagram

we will add some lies of code to control the LED and this should be the result

from machine import Pin
import time

button = Pin(16, Pin.IN, Pin.PULL_UP)
led = Pin(15,Pin.OUT)

while True:
if button.value() == 0:
print("THe button is pressed")
led.value(1)
else:
print("The button is released")
led.value(0)
time.sleep(0.1)

Reading a Potentiometer

To read resistance, we need to use a special type of pin called an Analogue to Digital Converter, or labelled as ADC on the pinout diagram. These pins are 26,27 and 28. we will use Pin 26.

use the code provided below

import machine
import time

potentiometer = machine.ADC(26) #sets pin to read using ADC

while True:
pot_value = potentiometer.read_u16() #sets the variable pot_value to the value it reads
print(pot_value) #prints out this value
time.sleep(0.1)

Photoresistor

We will use the same code we used for the potentiometer with the photoresistor, which changes resistance dependent on light.

Connect the photoresistor pins between Pin 26 and Ground

use the code below

import machine
import time

photoresistor = machine.ADC(26) #sets pin to read using ADC

while True:
photo_value = photoresistor.read_u16() #sets the variable photo_value to the value it reads
print(photo_value) #prints out this value
time.sleep(0.1)

Try covering the photoresistor with your hand or shining a light on it and watch the value change.

PIR Sensor

A PIR sensor stands for Passive Infrared Sensor. It can detect movement. It is commonly used in burglar alarms in buildings, commonly situated in the corner of a ceiling.

To wire this up we will connect VCC to 3v3 pin, GND to ground and OUT to pin 16

The code we will be using to utilise this component is:

from machine import Pin
from time import sleep

pir_sensor = Pin(16, Pin.IN, machine.Pin.PULL_DOWN)
led = Pin(25, Pin.OUT)


def pir_handler(pin):
print("Motion detected!")
for i in range(0,20):
led.toggle()
sleep(0.1)

pir_sensor.irq(trigger = machine.Pin.IRQ_RISING, handler = pir_handler)

while True:
led.value(0)
sleep(5) #delay for 5 seconds.
led.value(1)
sleep(0.05)
led.value(0)
sleep(0.05)

Further Reading

If you want to learn more I recommend looking at the raspberry pi projects page to look at fun projects.

Hope this helped.

Thanks for reading!