Breaking

Pico Projects For Beginners - Raspberry Pi

Introduction

In this endeavor, you will connect a Raspberry Pi Pico to another computer and learn how to program it using MicroPython.

A Raspberry Pi Pico is a low-cost microcontroller device. Microcontrollers are miniature computers, but they have a tendency to lack large volume storage and peripheral devices that you can plug (for example, keyboards or monitors).

Important Update: 

Competition for Young Makers in China and the United States in 2021

An Raspberry Pi Pico includes GPIO pins, much like a Raspberry Pi computer, so it may be employed to control and get input from many different electronics.

Let See What You'll Make

You may connect a Raspberry Pi Pico for your computer, install the Thonny Python IDE, and compose a MicroPython program to blink the onboard LED. In case you've got additional components available, then you can also try out some more illustrations.

Raspberry pi Pico Project For Beginners

What you will need

Hardware

  • Soldered Raspberry Pi Pico Board
  • A computer that can run the Thonny IDE and application a Raspberry Pi Pico
  • A micro USB cable
  • A selection of electronics parts, such as a button, an LED with proper resistor, and a potentiometer (optional)
  • A breadboard and M-M jumper leads for linking additional elements (optional)
  • An outside 5V micro USB power supply (optional)

Software

The job can guide you through the installation of:

  • MicroPython firmware for Raspberry Pi Pico
  • The Thonny Python IDE

Things You'll Learn

  • The best way to load the MicroPython firmware onto a Raspberry Pi Pico
  • How to program a Raspberry Pi Pico with MicroPython
  • How to connect additional components to a Raspberry Pi Pico and compose MicroPython programs to socialize with them

Added Information for Teachers 

If you are completing this project in a school or other setting with a managed network, then you need to make sure you have the right permissions to mount a USB drive and then install applications.

Meet Tiny Pico Microcontroller

This really is a Raspberry Pi Pico. Hopefully your apparatus has had the header pins soldered on, but otherwise, you may love to take a look at our getting started using soldering resource.

Pico-Top-Headers
If you've got a breadboard, put your Raspberry Pi Pico on the board.

Place it so that the two headers are separated from the ravine from the middle.

Pico-Top-Breadboard
Plug your micro USB cable into the jack on the left side of the board.
Pico-Top-Plug-v2

Should you have to know the pin numbers to get a Raspberry Pi Pico, it is possible to refer to the next diagram.
Pico Board Pinout

In this step, you may install Thonny or be certain that you've got the latest version. Then you'll connect to a Raspberry Pi Pico and run some simple Python code using the Shell.

Thonny on Raspberry Pi

  • Thonny is currently installed Raspberry Pi OS, but may need to be upgraded to the latest version
  • Open a terminal window, either by clicking the icon at the upper left-hand corner of the screen or by pressing the Ctrl+Alt+T keys in the Exact Same time
  • From the window, type the following to update your OS and Thonny

sudo apt update && sudo apt upgrade -y

Install Thonny on other operating systems

  • On Windows, macOS, and Linux, you can install the Hottest Thonny IDE or upgrade an existing version
  • In a web browser, navigate to thonny.org
  • At the top right-hand corner of the browser window, then you may see download links for Windows along with macOS, and instructions for Linux
  • Download the applicable files and run them to set up Thonny

Open Thonny from the application launcher. It should look something like this:

thorny Editor

It's possible to utilize Thonny to write standard Python code. Type the following from the primary window, and then click on the Run button (you'll be asked to save the file).

print('Hello World!')

You're now ready to proceed to the next step and connect your Raspberry Pi Pico.

Add the MicroPython firmware

If you have never used MicroPython in your Raspberry Pi Pico, you will need to bring the MicroPython firmware.

Find the BOOTSEL button on your own Raspberry Pi Pico.

Pico-bootsel

Press the BOOTSEL button and hold it while you connect the other end of the micro USB cable into your PC. A Raspberry Pi is displayed in the picture below, but the exact same applies to any pc.
Pico-Raspberry-Pi-4-Plug
This sets your Raspberry Pi Pico to USB mass storage device manner.

At the bottom right-hand corner of the Thonny window, you will see the version of Python that you are currently using.

Click the Python version and select 'MicroPython (Raspberry Pi Pico)':

In case you don't see this choice, then check you've plugged into your Raspberry Pi Pico.

A dialogue box may pop up to install the most recent version of the MicroPython firmware on your Raspberry Pi Pico.

Click the Install button to copy the firmware to your own Raspberry Pi Pico.

Wait for the installation to complete and click Close.

Firmware Installation Menu

You don't have to update the firmware every time you use your Raspberry Pi Pico. Next moment, you can simply plug it in your pc without pressing on the BOOTSEL button.

Use the Shell

In this step, you may use the Thonny Shell to run some simple Python code onto your Raspberry Pi Pico.

Make sure your Raspberry Pi Pico is connected to your computer and you have chosen the MicroPython (Raspberry Pi Pico) interpreter.

Look in the Shell panel in the bottom of this Thonny editor.

You should see something similar to this:

Shell panel

Thonny is now able to speak with your Raspberry Pi Pico employing the REPL (read--eval--print loop), which allows you to type Python code to the Shell and watch the output signal. 

Now you can type commands directly into the Shell and they will run on your own Raspberry Pi Pico.

Type the following command.

print("Hello")

Hit Enter

print-hello-output

MicroPython adds hardware-specific modules, such as machine, which you may use to plan your Raspberry Pi Pico.

Let us make a machine.Pin object to coincide with the onboard LED, which is obtained using GPIO pin 25.

If you set the value of this LED to 1, it turns out on.

Enter the following code, make sure you tap Enter after each line.

from machine import Pin

led = Pin(25, Pin.OUT)

led.value(1)

You should see the onboard LED light up.

Sort the code to set the value to 0 to turn the LED off.

led.value(0)

Turn the LED on and off as many times as you'd like.

Tip: You can use the up arrow on the keyboard to quickly access preceding lines.

If you would like to write a longer program, then it is best to store it into a file. You'll do this in the next step.

Blink the onboard LED

The Shell is useful to make sure everything is functioning and test out quick commands. However, it's far better to place longer apps in a document.

Thonny can save and operate MicroPython apps directly in your Raspberry Pi Pico.

In this step, you will produce a MicroPython app to blink the onboard LED off and on at a loop.

Click from the main editor of Thonny.

Input the following code to toggle the LED.

from machine import Pin

led = Pin(25, Pin.OUT)


led.toggle()

Click the Run button to run your code.

Thonny will ask whether you want to save the file on This pc or the MicroPython device. Pick MicroPython device.

Enter blink.py as the document name.

Hint: You need to input the .Py file extension so that Thonny simplifies the document as a Python file.

Thonny will save your app to your own Raspberry Pi Pico and operate it.

You need to see the onboard LED switch between on and off every time you click on the Run button.

You can use the Timer module to specify a timer which runs a function in regular intervals.

Update your code so it looks like this:

from machine import Pin, Timer

led = Pin(25, Pin.OUT)

timer = Timer()


def blink(timer):

    led.toggle()


timer.init(freq=2.5, mode=Timer.PERIODIC, callback=blink)

Click Run along with your app will blink the LED on and off until you click the Stop button.

Save your project

Utilize digital inputs and outputs

Now you know the fundamentals, you can learn to control an external LED with your Raspberry Pi Pico, and get it to see input from a button.

Utilize a resistor between about 50 and 330 ohms, an LED, and a set of M-M jumper leads to join your Raspberry Pi Pico as shown in the image below.

Single LED

LED and resistor connected to the Pico

In this example, the LED is connected to pin 15. If you use another pin, don't forget to look up the number from the pinout diagram in the Meet Raspberry Pi Pico section.

Use the same code as you didn't blink the onboard LED, but alter the pin number to 15.

from machine import Pin, Timer

led Pin(25, Pin.OUT)

timer Timer()


def blink(timer):

    led.toggle()


timer.init(freq=2.5, mode=Timer.PERIODIC, callback=blink)

Run your app and your LED should start to blink. If it's not working, check your wiring to be sure the LED is linked.

Next, let us try and control the LED using a button.

Add a button to your own circuit as shown in the diagram below.

Pico Button ANd LED

LED and button onto a breadboard

The button is on pin 14, and can be connected to the 3.3V pin in your Raspberry Pi Pico. This means when you set up the pin, you have to inform MicroPython which it's an input pin and has to be hauled down.

Create a new file and include this code.

from machine import Pin

import time


led = Pin(15, Pin.OUT)

button = Pin(14, Pin.IN, Pin.PULL_DOWN)


while True:

    if button.value():

    led.toggle()

        time.sleep(0.5)

Run your code and then once you press the button, the LED should click or off. If you hold the button down, it is going to flash.

Save your job 

Control LED brightness with PWM

Pulse width modulation, allows you to give analogue behaviours to electronic devices, such as LEDs. This means that instead of an LED being simply off or on, you can control its brightness.

For this action, you may use the circuit from the previous step.

Open a new file in Thonny and add the following code.

from machine import Pin, PWM

from time import sleep


pwm = PWM(Pin(15))


pwm.freq(1000)


while True:

    for duty in range(65025):

pwm.duty_u16(duty)

sleep(0.0001)

for duty in range(65025, 0, -1):

pwm.duty_u16(duty)

sleep(0.0001)

 Save and run the document. You should see the LED pulse bright and dim, in a constant cycle.

The frequency (pwm.freq) informs Raspberry Pi Pico how often to change the electricity between off and on for the LED.

The duty cycle informs the LED for how much time it ought to be on every time. For Raspberry Pi Pico at MicroPython, this can range from 0 to 65025. 65025 would be 100% of the time, so the LED would remain bright. A value of around 32512 would indicate that it should be on for half of the time.

Have a play with the pwm.freq() worth and the pwm.duty_u16 values, as well as the length of time for the sleep, to get a sense of how you can correct the brightness and pace of this pulsing LED.

Save your job 

Control an LED having an analogue input

Your Raspberry Pi Pico has input hooks which can receive analogue signals. This means that instead of just reading the values of 1 and 0 (on and off), it could read values between.

A potentiometer is the best analogue device for this activity.

Replace the button in your circuit using a potentiometer. Follow the wiring diagram below to link it to an analogue pin.

pot_and_LED

Potentiometer connected using an LED into the Pico

In a brand new file in Thonny, you may first read the resistance of the potentiometer.

Add this code to a different file, then run it.

from machine import ADC, Pin

import time


adc = ADC(Pin(26))


while True:

    print(adc.read_u16())

    time.sleep(1)

Turn the potentiometer to understand your maximum and minimum values.

They need to be about between 0 and 65025.

Now you can use this value to control the duty cycle for PWM on the LED.

Change the code to the following. Once you have run it, then tune the dial on the potentiometer to control the LED's brightness.

from machine import Pin, PWM, ADC


pwm = PWM(Pin(15))

adc = ADC(Pin(26))


pwm.freq(1000)


while True:

duty = adc.read_u16()

pwm.duty_u16(duty)

Save your job 

Power your Raspberry Pi Pico

If you would like to run your Raspberry Pi Pico without it being connected to a computer, you need to use a USB power supply.

Safe working voltages are between 1.8V and 5.5V.

To automatically run a MicroPython program, simply save it to the apparatus with the name main.py

In Thonny, click on the File menu and then Save for the last program you wrote.

When prompted, choose 'MicroPython apparatus' in the pop up menu.

Name your file main.py

You can now disconnect your Raspberry Pi Pico from your computer and utilize a micro USB cable to connect it to a cell energy source, like a battery pack.

Once joined, the main.py file should operate automatically so you are able to interact with the components attached to your Raspberry Pi Pico.

Save your job 

Popular Posts