Built-in project · Raspberry Pi Pico W

Pico W passenger counter: PIR and four digits

MicroPython on a Pico W: each time a PIR sensor on GP16 sees someone, a pin interrupt adds one to the count on a four-digit display. The whole build, a Raspberry Pi Pico W and 7 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 30 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $9.55. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Counting events with a pin interrupt
  • Multiplexing a four-digit display
  • Shifting bits out to a 74HC595 in MicroPython
  • Persistence of vision
  • Using a motion sensor's digital output
  • Difficulty: Intermediate
  • Time: About 30 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 11

How it works

A motion sensor on GP16 goes high when it sees someone. The Pico counts each rising edge with an interrupt, so every pass adds one to the total.

The total is shown on a four-digit display. The segments come from a 74HC595 shift register on GP18, GP19 and GP20. The four digit select pins on GP10 to GP13 turn one digit on at a time, so fast that all four look lit.

The program prints the count to the console whenever it changes.

Parts list

11 parts, plus the jumper wires. They retail for about $9.55; on Mokxi they're free.

Kits that can build it

These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω (1) pin 2Raspberry Pi Pico W pin GP10
Resistor, 220 Ω (2) pin 2Raspberry Pi Pico W pin GP11
Resistor, 220 Ω (3) pin 2Raspberry Pi Pico W pin GP12
Resistor, 220 Ω (4) pin 2Raspberry Pi Pico W pin GP13
74HC595 shift register pin SHCPRaspberry Pi Pico W pin GP20
74HC595 shift register pin STCPRaspberry Pi Pico W pin GP19
74HC595 shift register pin DSRaspberry Pi Pico W pin GP18
Motion sensor pin OUTRaspberry Pi Pico W pin GP16
Four-digit seven segment, multiplexed pin B74HC595 shift register pin Q1
Four-digit seven segment, multiplexed pin C74HC595 shift register pin Q2
Four-digit seven segment, multiplexed pin D74HC595 shift register pin Q3
Four-digit seven segment, multiplexed pin E74HC595 shift register pin Q4
Four-digit seven segment, multiplexed pin F74HC595 shift register pin Q5
Four-digit seven segment, multiplexed pin G74HC595 shift register pin Q6
Four-digit seven segment, multiplexed pin DP74HC595 shift register pin Q7
74HC595 shift register pin GNDGround
74HC595 shift register pin MR3.3 V
74HC595 shift register pin OEGround
Four-digit seven segment, multiplexed pin A74HC595 shift register pin Q0
74HC595 shift register pin VCC3.3 V
Resistor, 220 Ω (4) pin 1Four-digit seven segment, multiplexed pin DIG4
Resistor, 220 Ω (3) pin 1Four-digit seven segment, multiplexed pin DIG3
Resistor, 220 Ω (2) pin 1Four-digit seven segment, multiplexed pin DIG2
Resistor, 220 Ω (1) pin 1Four-digit seven segment, multiplexed pin DIG1
Motion sensor pin VCC3.3 V
Motion sensor pin GNDGround

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico W
# Passenger counter: every time the PIR on GP16 sees someone, its rising
# edge adds one, and the four-digit display shows the count.
from machine import Pin
import time

DIGITS = [0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F]
ds = Pin(18, Pin.OUT)
latch = Pin(19, Pin.OUT)
clock = Pin(20, Pin.OUT)
selects = [Pin(n, Pin.OUT, value=1) for n in (10, 11, 12, 13)]   # digit 1 to 4

def segments(byte):
    for i in range(7, -1, -1):
        ds.value((byte >> i) & 1)
        clock.value(1)
        clock.value(0)
    latch.value(1)
    latch.value(0)

def show(text):
    # text is four characters, digits or spaces, with an optional "." after
    # one of them. Each digit is lit for a moment in turn, fast enough
    # that the eye sees all four.
    cells = []
    for ch in text:
        if ch == "." and cells:
            cells[-1] |= 0x80
        else:
            cells.append(DIGITS[int(ch)] if ch.isdigit() else 0)
    for n, byte in enumerate(cells[-4:]):
        segments(byte)
        selects[n].value(0)
        time.sleep_ms(2)
        selects[n].value(1)

count = 0

def passed(pin):
    global count
    count += 1

Pin(16, Pin.IN).irq(trigger=Pin.IRQ_RISING, handler=passed)
shown = -1
while True:
    if count != shown:
        shown = count
        print("passengers", count)
    show("%4d" % count)

Code walkthrough

The key functions of the sketch, and what each one does.

segments main.py

Sends one byte to the shift register. For each bit, from the top down, it sets the data pin, then pulses the clock. A pulse on the latch pin then copies the byte to the outputs, which lights the segments.

def segments(byte):
    for i in range(7, -1, -1):
        ds.value((byte >> i) & 1)
        clock.value(1)
        clock.value(0)
    latch.value(1)
    latch.value(0)

show main.py

Takes four characters and turns them into segment bytes, with a decimal point added when a "." follows a digit. Then for each digit in turn, it sends its segments, pulls that digit's select pin low for 2 ms and releases it. Repeating this fast makes all four digits look steady.

def show(text):
    # text is four characters, digits or spaces, with an optional "." after
    # one of them. Each digit is lit for a moment in turn, fast enough
    # that the eye sees all four.
    cells = []
    for ch in text:
        if ch == "." and cells:
            cells[-1] |= 0x80
        else:
            cells.append(DIGITS[int(ch)] if ch.isdigit() else 0)
    for n, byte in enumerate(cells[-4:]):
        segments(byte)
        selects[n].value(0)
        time.sleep_ms(2)
        selects[n].value(1)

passed main.py

The interrupt handler for the motion sensor. It adds one to the global count. The main loop picks up the new value and updates the display.

def passed(pin):
    global count
    count += 1

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The display flickers or only one digit shows This project
    Each digit needs its own select pin: GP10, GP11, GP12 and GP13 in order, each through its 220 Ω resistor.
  • One pass counts more than once This project
    The sensor output can stay high or pulse while it sees movement. Each new rising edge adds one.
  • Outputs show garbage, or the register never clears. 74HC595 shift register
    Tie OE low and SRCLR (pin 10) high. Left floating, these pins make the chip behave randomly. Then pulse the latch pin after shifting the byte.
  • The bits come out in the wrong order, or LEDs are dim. 74HC595 shift register
    Choose MSBFIRST or LSBFIRST in shiftOut to flip the order. Give each LED its own resistor, since each output can source only about 6 mA safely.
  • Digits look dim, flicker, or show ghosts of other digits. Four-digit seven segment
    Refresh each digit quickly in a loop, at least 100 Hz for the whole set. Turn all segments off before switching digits. Do not use delay() for long pauses while displaying.
  • Segments burn out or the pins are overloaded. Four-digit seven segment
    Use a resistor of about 220 ohms on each segment line. Check whether the display is common cathode or common anode, since that sets which level turns a segment on.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.