3-Bit Binary Counter Using Raspberry Pi Pico W on Wokwi Simulator – Step-by-Step Guide


Step 1 – Create a New Project on Wokwi

Go to wokwi.com, sign in, and create a new project. Select Raspberry Pi Pico W as your board. Wokwi will automatically place the Pico W on a breadboard for you.

Step 2 – Add Components

Search and drag the following components onto the breadboard: 3 LEDs (red), 3 resistors (330 Ω), and 3 push buttons (green, blue, and yellow as shown in the screenshot). Place the LEDs and resistors on the right side and the buttons below them.

Step 3 – Wire the Components

Diagram.json:

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-breadboard",
      "id": "bb1",
      "top": 53,
      "left": -322.4,
      "rotate": 90,
      "attrs": {}
    },
    {
      "type": "board-pi-pico-w",
      "id": "pico",
      "top": -156.85,
      "left": -34.85,
      "attrs": { "env": "micropython-20241129-v1.24.1" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 28,
      "left": 1.4,
      "rotate": 270,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": 66.4,
      "left": 1.4,
      "rotate": 270,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn1",
      "top": 151.4,
      "left": -9.6,
      "attrs": { "color": "green", "xray": "1", "bounce": "0", "key": "1" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn2",
      "top": 189.8,
      "left": -9.6,
      "attrs": { "color": "blue", "xray": "1", "bounce": "0" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": 32.75,
      "left": 124.8,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": 71.15,
      "left": 124.8,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn3",
      "top": 228.2,
      "left": -9.6,
      "attrs": { "color": "yellow", "xray": "1", "bounce": "0" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": 104.8,
      "left": 1.4,
      "rotate": 270,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r3",
      "top": 119.15,
      "left": 134.4,
      "attrs": { "value": "220" }
    }
  ],
  "connections": [
    [ "bb1:34t.a", "bb1:tn.27", "green", [ "h0" ] ],
    [ "bb1:38t.a", "bb1:tn.31", "green", [ "h0" ] ],
    [ "bb1:20b.i", "bb1:32b.i", "green", [ "h0" ] ],
    [ "bb1:19b.j", "bb1:36b.j", "green", [ "h0" ] ],
    [ "r1:1", "bb1:22t.a", "green", [ "v0" ] ],
    [ "r1:2", "bb1:tp.17", "green", [ "v0" ] ],
    [ "r2:2", "bb1:tp.21", "green", [ "v0" ] ],
    [ "bb1:19t.a", "bb1:25t.a", "green", [ "h0" ] ],
    [ "r2:1", "bb1:26t.a", "green", [ "v0" ] ],
    [ "bb1:3t.a", "bb1:tp.3", "green", [ "h0" ] ],
    [ "bb1:tn.4", "bb1:5t.a", "green", [ "h0" ] ],
    [ "bb1:20t.b", "bb1:21t.b", "green", [ "h0" ] ],
    [ "bb1:17t.b", "bb1:29t.b", "green", [ "h0" ] ],
    [ "bb1:17b.i", "bb1:40b.h", "green", [ "h0" ] ],
    [ "bb1:42t.a", "bb1:tn.35", "green", [ "h0" ] ],
    [ "r3:1", "bb1:30t.a", "green", [ "v0" ] ],
    [ "r3:2", "bb1:tp.24", "green", [ "v0" ] ],
    [ "led1:A", "bb1:21t.c", "", [ "$bb" ] ],
    [ "led1:C", "bb1:22t.c", "", [ "$bb" ] ],
    [ "led2:A", "bb1:25t.c", "", [ "$bb" ] ],
    [ "led2:C", "bb1:26t.c", "", [ "$bb" ] ],
    [ "btn2:1.l", "bb1:36b.f", "", [ "$bb" ] ],
    [ "btn2:2.l", "bb1:38b.f", "", [ "$bb" ] ],
    [ "btn2:1.r", "bb1:36t.e", "", [ "$bb" ] ],
    [ "btn2:2.r", "bb1:38t.e", "", [ "$bb" ] ],
    [ "btn3:1.l", "bb1:40b.f", "", [ "$bb" ] ],
    [ "btn3:2.l", "bb1:42b.f", "", [ "$bb" ] ],
    [ "btn3:1.r", "bb1:40t.e", "", [ "$bb" ] ],
    [ "btn3:2.r", "bb1:42t.e", "", [ "$bb" ] ],
    [ "led3:A", "bb1:29t.c", "", [ "$bb" ] ],
    [ "led3:C", "bb1:30t.c", "", [ "$bb" ] ],
    [ "pico:GP0", "bb1:1b.h", "", [ "$bb" ] ],
    [ "pico:GP1", "bb1:2b.h", "", [ "$bb" ] ],
    [ "pico:GND.1", "bb1:3b.h", "", [ "$bb" ] ],
    [ "pico:GP2", "bb1:4b.h", "", [ "$bb" ] ],
    [ "pico:GP3", "bb1:5b.h", "", [ "$bb" ] ],
    [ "pico:GP4", "bb1:6b.h", "", [ "$bb" ] ],
    [ "pico:GP5", "bb1:7b.h", "", [ "$bb" ] ],
    [ "pico:GND.2", "bb1:8b.h", "", [ "$bb" ] ],
    [ "pico:GP6", "bb1:9b.h", "", [ "$bb" ] ],
    [ "pico:GP7", "bb1:10b.h", "", [ "$bb" ] ],
    [ "pico:GP8", "bb1:11b.h", "", [ "$bb" ] ],
    [ "pico:GP9", "bb1:12b.h", "", [ "$bb" ] ],
    [ "pico:GND.3", "bb1:13b.h", "", [ "$bb" ] ],
    [ "pico:GP10", "bb1:14b.h", "", [ "$bb" ] ],
    [ "pico:GP11", "bb1:15b.h", "", [ "$bb" ] ],
    [ "pico:GP12", "bb1:16b.h", "", [ "$bb" ] ],
    [ "pico:GP13", "bb1:17b.h", "", [ "$bb" ] ],
    [ "pico:GND.4", "bb1:18b.h", "", [ "$bb" ] ],
    [ "pico:GP14", "bb1:19b.h", "", [ "$bb" ] ],
    [ "pico:GP15", "bb1:20b.h", "", [ "$bb" ] ],
    [ "pico:GP16", "bb1:20t.c", "", [ "$bb" ] ],
    [ "pico:GP17", "bb1:19t.c", "", [ "$bb" ] ],
    [ "pico:GND.5", "bb1:18t.c", "", [ "$bb" ] ],
    [ "pico:GP18", "bb1:17t.c", "", [ "$bb" ] ],
    [ "pico:GP19", "bb1:16t.c", "", [ "$bb" ] ],
    [ "pico:GP20", "bb1:15t.c", "", [ "$bb" ] ],
    [ "pico:GP21", "bb1:14t.c", "", [ "$bb" ] ],
    [ "pico:GND.6", "bb1:13t.c", "", [ "$bb" ] ],
    [ "pico:GP22", "bb1:12t.c", "", [ "$bb" ] ],
    [ "pico:RUN", "bb1:11t.c", "", [ "$bb" ] ],
    [ "pico:GP26", "bb1:10t.c", "", [ "$bb" ] ],
    [ "pico:GP27", "bb1:9t.c", "", [ "$bb" ] ],
    [ "pico:GND.7", "bb1:8t.c", "", [ "$bb" ] ],
    [ "pico:GP28", "bb1:7t.c", "", [ "$bb" ] ],
    [ "pico:ADC_VREF", "bb1:6t.c", "", [ "$bb" ] ],
    [ "pico:3V3", "bb1:5t.c", "", [ "$bb" ] ],
    [ "pico:3V3_EN", "bb1:4t.c", "", [ "$bb" ] ],
    [ "pico:GND.8", "bb1:3t.c", "", [ "$bb" ] ],
    [ "pico:VSYS", "bb1:2t.c", "", [ "$bb" ] ],
    [ "pico:VBUS", "bb1:1t.c", "", [ "$bb" ] ],
    [ "btn1:1.l", "bb1:32b.f", "", [ "$bb" ] ],
    [ "btn1:2.l", "bb1:34b.f", "", [ "$bb" ] ],
    [ "btn1:1.r", "bb1:32t.e", "", [ "$bb" ] ],
    [ "btn1:2.r", "bb1:34t.e", "", [ "$bb" ] ]
  ],
  "dependencies": {}
}

Connect each LED through a 330 Ω resistor to these GPIO pins: LED 1 to GP20, LED 2 to GP21, LED 3 to GP22. Connect each button to: Button 1 to GP15, Button 2 to GP16, Button 3 to GP17. The other leg of each button goes to GND. All LED cathodes also connect to the shared GND rail. Link that GND rail to any GND pin on the Pico W.

Step 4 – Write the Code

Open the code editor and write a MicroPython script that reads each button using PULL_UP (so pressing a button gives a 0, which you invert to 1). Each button state is then written to its matching LED. The decimal value is calculated as (button1 × 4) + (button2 × 2) + (button3 × 1). All states are printed to the serial monitor every 500 ms.

Step 5 – Run the Simulation

Click the green Play button. The serial monitor will start printing the state of all three buttons, their binary values, and the decimal equivalent. Initially everything reads 0. Press any button on screen and the matching LED lights up, and the serial output updates instantly.

Step 6 – Read the Serial Output

The serial monitor shows the state of each button (0 or 1), the 3-bit binary number, and the integer equivalent. For example, if only Button 1 is pressed, the output shows binary 100 and Integer Equivalent: 4. Since there are 3 bits, the counter can represent any value from 0 to 7.

CODE:

from machine import Pin
from utime import sleep

button_1 = Pin('GP14', Pin.IN, Pin.PULL_DOWN)
button_2 = Pin('GP15', Pin.IN, Pin.PULL_DOWN)
button_3 = Pin('GP13', Pin.IN, Pin.PULL_DOWN)
led_1 = Pin('GP17', Pin.OUT)
led_2 = Pin('GP16', Pin.OUT)
led_3 = Pin('GP18', Pin.OUT)

def handler_1(x):
    led_1.high()
    return

def handler_2(x):
    led_2.high()
    return

def handler_3(x):
    led_3.high()
    return

button_1.irq(handler_1,Pin.IRQ_RISING)
button_2.irq(handler_2,Pin.IRQ_RISING)
button_3.irq(handler_3,Pin.IRQ_RISING)

led_1.low()
led_2.low()
led_3.low()

try:
    play_again = True
    while play_again==True:
        sleep(0.5)
        button_1_state = button_1.value()
        button_2_state = button_2.value()
        button_3_state = button_3.value()
        d = 4*button_3_state + 2*button_1_state + 1*button_2_state

        print('The state of button 1 is: ', button_1_state)
        print('The state of button 2 is: ', button_2_state)
        print('The state of button 3 is: ', button_3_state)
        print('3-bit binary number: ')
        print('button 1: ', bin(button_1_state))
        print('button 2: ', bin(button_2_state))
        print('button 3: ', bin(button_3_state))
        print('Integer Equivalent: ', d)
        print('\n')
        # play_again_str = input('Play again? Y or N: ')
        # if play_again_str == 'N'or play_again_str == 'n':
        #     play_again = False
        #     print('Thanks for playing! Goodbye!')
        led_1.low()
        led_2.low()
        led_3.low()

except KeyboardInterrupt:
    print('\n')
    print('You sunk my battle ship!')

finally:
    led_1.low()
    led_2.low()
    led_3.low()


Comments