Authorized Online Retailers:

Buy from USA Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES ここでご購入を!

In this lesson, you will learn how to read digital data (1/0 which represents high/low voltage) from a Pico digital pin and output 0/1 data (low voltage/high voltage signals) to Pico digital pins. This type of programming is also called digital I/O (input/output) programming.

In order to program digital I/O, firstly we need to know the Pico pin layout.

From above picture, you can see most of the Pico pins have a light green label which is the GPIO   number.  i.e. Physical pin number 20, green label is GP15. This means Physical Pin 20’s GPIO number is 15. This is very important in python programming. We will use this GPIO number to write/read data from that pin.

  1. Raspberry Pi Pico board and microUSB cable
  2. A computer to run Thonny Python IDE
  3. A breadboard
  4. LED x 1 pc
  5. 220 ohm resistor
  6. Push button x 1 pc
  7. Some jumper wires

In above circuit graph, you can see that:
LED negative pin connected to GND through 220 ohm resistor
LED Positive pin connected to GP16
Push button pin 1 connected to 3.3V
Push button pin 2 connected to GP14

You can download Lesson 2 Python code from http://osoyoo.com/driver/pico/lesson2/pico-lesson2.py.

You can use Thonny to open pico-lesson2.py and load it to Pico later.

Here is the full code with comments:

from machine import Pin
import time

led = Pin(16, Pin.OUT) #set GP16 as OUTPUT pin
button = Pin(14, Pin.IN,Pin.PULL_DOWN) #set GP14 as OUTPUT pin

while True:
    if button.value():
        print("Button is pressed!")
        led.toggle()
        time.sleep(0.5)

Step 1: Connect the Pico board to one of the USB ports on your PC.

Step 2: If you haven’t installed Thonny software or don’t know how to use the Thonny IDE, please refer to Lesson 1.

Step 3: Open the Thonny Python IDE and click ‘Run’ to select ‘MicroPython for Raspberry Pi Pico’ as the interpreter.:

Also, please select the COM port to which your Pico board is connected.

After that, click ‘OK’ to save the settings.

Step 4: Copy the Python code to the Thonny window as follows:

Step 5: Click the little ‘play’ button (►) and the save page will pop up as shown in the following photo.

Select ‘Raspberry Pi Pico’ as the destination.

Step 6: Name the file as ‘pico-lesson2.py’ and then click ‘OK’

Step 7: Click the little ‘play’ button (►) again to run the Python code.


Now you can try to press the Push Button in the breadboard. You will see the LED status will toggle whenever you press the button.


Also, in the Thonny Shell window, you will see ‘Button is pressed!’ when you press the button.

After the operation is complete, press Ctrl+C to end the command

Learn More about parts SKU:2021005900

No. Picture Product Name Link
1 Raspberry Pi Pico Board https://osoyoo.store/products/raspberry-pi-pico-flexible-microcontroller-board-based-on-the-raspberry-pi-rp2040-dual-core-arm-cortex-m0-processor-1-pc?variant=39875307864175
2 Servo motor https://osoyoo.store/products/micro-servo-sg90-blue-for-arduino-v2-0-robot-carmodel-lacc200610?variant=31648847560815
3 Infrared Sensor Module https://osoyoo.store/products/hc-sr501-pir-motion-sensor-for-arduino?variant=31957812838511
4 RFID Module and card https://osoyoo.store/products/osoyoo-rfid-module?variant=31970646130799
5 Push Buttons and Hats https://osoyoo.store/products/push-button-and-hats-5pcs?variant=39879253131375
6 LED(6 x White, 6 x Red, 6 x Yellow, 6x Green) https://osoyoo.store/products/led-packs-for-arduino?variant=31957816442991
7 Servo motor https://osoyoo.store/products/sg90-micro-servo-for-arduino-raspberry-pi-robot?variant=31957821096047
8 Piezo Buzzer Module https://osoyoo.store/products/piezo-buzzer-module-1pcs?variant=39879251656815
9 I2C LCD Display(16×2) https://osoyoo.store/products/i2c-lcd1602-display?variant=39875437101167
10 Philips Screwdriver https://osoyoo.store/products/phillips-screwdriver?variant=31930635780207https://osoyoo.store/products/single-channel-relay-module-for-arduino-raspberry-pi?variant=31955954401391
11 Solderless Prototype Breadboard https://osoyoo.store/products/solderless-prototype-breadboard-1pcs?variant=39879255457903
12 Potentionmeter (10K adjustable resistor) https://osoyoo.store/products/potentionmeter-10k-adjustable-resistor-3pcs?variant=39879281016943
13 40Pin M to M Jumper Wires https://osoyoo.store/products/40pin-m-to-m-jumper-wires?variant=39879256539247
14 20Pin M to F Jumper wires 15cm https://osoyoo.store/products/20pin-15cm-female-to-female-cable?variant=31930663305327