Firmware for an automatic card shuffler (Ali, Oz, Oz, Oz) powered by RP2040-Zero with TFT display (ST7735), rotary encoder (KY-040), and TB6612FNG motor driver.
-
6 Shuffling Strategies
- Quick — Both motors at full speed
- Riffle — Fast alternating pulses between motors
- Strip — Short bursts from one motor while other runs slow
- Wash — Random chaotic mixing pattern
- Box — Systematic alternating speed phases
- Custom — User-configurable parameters
-
Visual Menu with card suit icons (♠♥♦♣)
-
Rotary Encoder navigation with button to start/stop
-
PWM Motor Control with soft start/stop ramping
-
Progress Bar during shuffling
| Component | Model | Connection |
|---|---|---|
| Microcontroller | RP2040-Zero | — |
| Display | ST7735 128x160 | SPI0 (GPIO 5-10) |
| Encoder | KY-040 | GPIO 14-16 |
| Motor Driver | TB6612FNG | GPIO 2-3 (PWM) |
USB-C
┌─────┐
5V ──┤● ●├── GND
GND ──┤● ●├── 3V3 ← VCC (display, encoder, TB6612)
GP29 ──┤● ●├── GP0
GP28 ──┤● ●├── GP1
GP27 ──┤● ●├── GP2 → PWMA (Motor 1)
GP26 ──┤● ●├── GP3 → PWMB (Motor 2)
Encoder A GP15 ──┤● ●├── GP4
Encoder B GP14 ──┤● ●├── GP5 → CS
└──┬──┘
┌───────┴───────┐
GP11 GP10 GP9 GP8 GP16
│ │ │ │ └─→ Encoder BTN
│ │ │ └──────→ DC
│ │ └───────────→ RST
│ └────────────────→ BL
│
GP6 GP7 GP12 GP13
│ └────────────────→ MOSI
└─────────────────────→ SCK
| Function | GPIO | Connect to |
|---|---|---|
| Display SCK | GP6 | SCL |
| Display MOSI | GP7 | SDA |
| Display CS | GP5 | CS |
| Display DC | GP8 | DC |
| Display RST | GP9 | RES |
| Display BL | GP10 | BLK |
| Encoder A | GP14 | CLK |
| Encoder B | GP15 | DT |
| Encoder BTN | GP16 | SW |
| Extra Button | GP4 | Start/Stop |
| Motor 1 PWM | GP2 | TB6612 PWMA |
| Motor 2 PWM | GP3 | TB6612 PWMB |
RP2040-Zero TB6612FNG Motors
─────────── ───────── ──────
3V3 ───────────────→ VCC
GND ───────────────→ GND ←───────────────── GND (power supply)
VM ←────────────────── +5-12V (power supply)
GP2 ───────────────→ PWMA
3V3 ───────────────→ AIN1 AOUT1 ──→ Motor 1
GND ───────────────→ AIN2 AOUT2 ──→ Motor 1
GP3 ───────────────→ PWMB
3V3 ───────────────→ BIN1 BOUT1 ──→ Motor 2
GND ───────────────→ BIN2 BOUT2 ──→ Motor 2
3V3 ───────────────→ STBY
Requires Pico SDK and cmake. The SDK will be fetched automatically during build.
brew install cmake # macOS| Command | Description |
|---|---|
make |
Build firmware (.uf2) |
make clean |
Clean firmware build |
make flash |
Build and flash to device |
make sim |
Build SDL simulator |
make sim-run |
Run simulator |
make sim-clean |
Clean simulator build |
make help |
Show all commands |
SDL-based simulator for fast UI iteration without flashing hardware.
brew install sdl2 # install SDL2
make sim # build simulator
make sim-run # run simulator
make sim-clean # clean simulator build| Key | Action |
|---|---|
| ←/↑ | Previous item (CCW) |
| →/↓ | Next item (CW) |
| Enter/Space | Select / Start |
| ESC | Quit |
sim/
├── sim_main.c # Main loop with SDL events
├── sim_display.c # ST7735 emulation via SDL (3x scale)
├── sim_encoder.c # Keyboard → encoder events
├── sim_motor.c # Motor stubs (printf output)
├── sim_pico.h # Pico SDK stubs
└── pico/stdlib.h # Fake header for compatibility
RP2040-Zero:
- Hold BOOT button
- Connect USB-C cable (or tap RESET while holding BOOT)
- Run
make flashor copybuild/card_shuffler.uf2toRPI-RP2drive - Device will reboot and run the firmware
- Rotate encoder to select a shuffling strategy
- Press encoder to start shuffling
- Press again to stop early (motors will ramp down smoothly)
Edit src/config.h to change:
- Pin assignments
- Display dimensions
- Motor PWM frequency
- Timing constants
- UI colors (RGB565 format)
Edit src/motor/strategies.c to customize:
- Strategy durations
- Motor speeds
- Pulse timings
src/
├── main.c # Application entry point and state machine
├── config.h # Hardware configuration
├── display/
│ ├── st7735.c/h # Display driver
│ ├── graphics.c/h # Drawing primitives and font
│ └── icons.c/h # 24x24 card suit bitmaps
├── ui/
│ └── menu.c/h # Menu system with icon grid
├── input/
│ └── encoder.c/h # Rotary encoder with debouncing
└── motor/
├── motor.c/h # PWM motor control
└── strategies.c/h # Shuffling algorithms
MIT
.------..------..------..------..------..------.
|A.--. ||D.--. ||W.--. ||0.--. ||R.--. ||D.--. |
| (\/) || :/\: || :/\: || :/\: || :(): || :/\: |
| :\/: || (__) || :\/: || :\/: || ()() || (__) |
| '--'A|| '--'D|| '--'W|| '--'0|| '--'R|| '--'D|
`------'`------'`------'`------'`------'`------'