Linux Format

Master the Pi Pico GPIO

Get to grips with the Raspberry Pi Pico’s GPIO to open the gateway to a range of simple projects.

-

The Pi Pico comes pre-installed with MicroPytho­n, a version of Python 3 for microcontr­ollers. It is regularly updated and new versions can be found at https://bit.ly/lxf298 upython. It is prudent to update MicroPytho­n before trying this tutorial.

Our circuit for this project is a simple LED toggle switch – a detailed circuit diagram is included in the download link. A push button is used to turn the LED on and off. The Pico reads the GPIO pins and reacts to the change. Download and install Thonny for your OS and connect your Pico to the computer. Open Thonny, go to Tools > Options and click on the Interprete­r tab. Set the interprete­r to be MicroPytho­n (Raspberry Pi Pico), then set the serial/

COM port to auto-detect the Pico. Thonny should now detect the Pico and the Python shell confirms this.

We start the project code in a blank document. The first step is to import two modules of prewritten code that will enable us to interact with the GPIO pins, and control pauses or delays in the code: from machine import Pin import time

Next we create two objects. The first, led, sets up our LED on GPIO15 as an output – we want current to flow to it in order for it to illuminate. The second object, button, is a push button on GPIO16. This is an input and we pull the pin high so it has current flowing. When the button is pressed, it connects GPIO16 to GND, causing the pin to pull low and toggle the LED on/off: led = Pin(15, Pin.OUT) button = Pin(16, Pin.IN, Pin.PULL_UP)

Turn the LED pin off. This is basic housekeepi­ng – the pin should already be off.

led.low()

Create a while True loop that will indefinite­ly run the project code:

while True:

The code is now indented inside the loop; this means that it works within the loop. The first task is to create a conditiona­l test that checks whether the button has been pressed. If it has been pressed, the GPIO pin changes state from high to low (0).

if button.value() == 0:

When the button is pressed, the LED is toggled on or off, depending on its current state. A short message is printed to the Python shell for debug purposes. A delay of 0.2 seconds is added in order to reduce accidental double pushes, called debounce:

led.toggle() print(“Toggle”) time.sleep(0.2)

The final two lines are part of the conditiona­l test and relate to when the button is not pressed. In this circumstan­ce, pass is used to allow the loop to repeat:

else:

pass

Click on File > Save and save the code as button_ blink.py to the Raspberry Pi Pico. Click on the run button (green play button) to start the code. Now press the button to toggle the LED on and off.

This introducti­on is just a fraction of what the Raspberry Pi Pico can do. For more projects, head to Tom’s Hardware, where there is a full section of projects to try out: https://bit.ly/lxf298toms.

 ?? ?? Your first electronic­s project on the Raspberry Pi Pico is intentiona­lly easy. It builds our knowledge and confidence to tackle the next project.
Your first electronic­s project on the Raspberry Pi Pico is intentiona­lly easy. It builds our knowledge and confidence to tackle the next project.

Newspapers in English

Newspapers from Australia