KevsRobots Learning Platform
By Kevin McAleer, 3 Minutes
In this lesson we’ll look at the different jobs a Pico GPIO pin can do. The clever part is that most pins can do several jobs: you choose which one in your code.
The Raspberry Pi Pico has 26 usable GPIO pins (GP0 to GP22 and GP26 to GP28). Each one can be set up as:
Digital I/O - send or receive simple on/off (1 or 0) signals: LEDs, buttons, switchesPWM (Pulse Width Modulation) - switch on and off very fast to dim LEDs, set motor speed or position servos. Every GPIO pin can do PWMUART - simple two-wire serial, used by GPS modules and other microcontrollersI2C (Inter-Integrated Circuit) - a two-wire bus that can talk to many sensors and displays at onceSPI (Serial Peripheral Interface) - a faster four-wire bus, used by displays and SD cardsThree pins have an extra trick:
ADC (Analog-to-Digital Converter) - GP26, GP27 and GP28 can measure a voltage between 0V and 3.3V, so you can read potentiometers and analog sensorsThe rest of the 40 pins around the edge are power (3V3, VBUS, VSYS) and ground (GND) pins, plus a couple of control pins (RUN, 3V3_EN).
Digital I/O pins send or receive digital signals, which can control LEDs, read button presses, or signal other digital devices.
PWM lets you control the brightness of LEDs, the speed of motors, or the position of servos by adjusting the duty cycle of the PWM signal.
ADC pins read analog signals from sensors, such as light sensors or potentiometers, and convert them into numbers your code can use.
UART, I2C and SPI let the Pico communicate with other devices, such as sensors, displays, or other microcontrollers. See the glossary for I2C and SPI.
The code below lights the Pico’s on-board LED:
import machine
led = machine.Pin("LED", machine.Pin.OUT)
led.value(1)
import machine gives us access to the Pico’s hardwaremachine.Pin("LED", machine.Pin.OUT) sets up the on-board LED pin as an outputled.value(1) sets the pin high (3.3V), so the LED turns onPico or Pico W?
On the original Pico the on-board LED is wired to
GP25, so you’ll often seePin(25, Pin.OUT)in older tutorials. On the Pico W the LED is connected through the wireless chip, soPin(25)won’t work. Using the name"LED"works on both boards with current MicroPython firmware.
Change led.value(1) to led.value(0) and run the code again. Then try led.toggle() a few times from the Thonny shell.
Quick quiz 1 of 2
Which Pico pins can be used for PWM?
Quick quiz 2 of 2
Which pins can read an analog voltage with the ADC?
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments