Skip to content

uCompute — User Buttons Particularities

JPMichon edited this page Oct 31, 2026 · 4 revisions

📐 Buttons Interface

To reduce physical component sizes and save highly valuable digital GPIO pins on the Raspberry Pi Pico microcontroller, the uCompute Junior implements a classic embedded hardware optimization trick: a multi-button analog resistor ladder scale.

Instead of routing three separate push-buttons (BTN1, BTN2, BTN3) to three independent digital pins, all buttons are arranged along a sequential voltage divider chain and sampled simultaneously through a single Analog-to-Digital Converter (ADC0 / GPIO 26).


🔬 Hardware Circuit Analysis

The circuit operates via a precise configuration of four resistors and a hardware smoothing filter capacitor:

  • R10 (10kΩ): Serves as the primary system pull-up resistor tied directly to the steady 3.3V analog power rail.
  • R9, R8, R7 (1kΩ each): Arranged sequentially in series down the line toward the button mechanical pads.
  • C1 (10nF): Wired in parallel with the Analog input line (GPIO26) to serve as a low-pass hardware filter. This capacitor suppresses mechanical switch contact bounce (debouncing) and layout thermal noise spikes, ensuring clean analog sampling readouts.
image

🧮 Mathematical Voltage Dividers & ADC Windows

When any push-button is closed, it anchors its respective node along the resistor chain directly to the system ground (GND). This forms a custom voltage divider equation with the 10kΩ pull-up resistor ((R_{\text{high}})).

MicroPython translates this analog voltage into a standard 16-bit raw integer ranging from 0 to 65535 via the read_u16() method.

Bouton pressé Formule théorique Tension théorique Valeur ADC 16-bit attendue Fenêtre de détection suggérée
Aucun (Idle) $V_{out} = 3.3\text{V}$ $3.30\text{V}$ $65535$ $> 60000$
BTN3 $V_{out} = 3.3\text{V} \times \frac{0\text{k}}{10\text{k} + 0\text{k}}$ $0.00\text{V}$ $0$ $0 - 2000$
BTN2 $V_{out} = 3.3\text{V} \times \frac{1\text{k}}{10\text{k} + 1\text{k}}$ $0.30\text{V}$ $\approx 5957$ $4000 - 8000$
BTN1 $V_{out} = 3.3\text{V} \times \frac{2\text{k}}{10\text{k} + 2\text{k}}$ $0.55\text{V}$ $\approx 10922$ $9000 - 13000$

💻 Optimized MicroPython Processing Implementation

The processing method Read_Keys() handles button prioritization naturally. Because it scans via ascending cascading constraints (< 7000 up to < 16000), the logic evaluates cleanly even if resistor tolerances shift slightly due to temperature or variations during student hand-soldering.

import machine
import utime

# Initialize the analog button interface array on ADC Channel 0
BTN_Analogue = machine.ADC(26)

def WaitInputKey():
    """
    Blocks loop execution and spins until a user key event is captured.
    Safe-bounded above 64000 to filter out transient line noise.
    """
    while BTN_Analogue.read_u16() > 64000:
        utime.sleep(0.05)

def Read_Keys():
    """
    Samples the analog voltage value and returns the mapped key label.
    Windows are configured with wide tolerance cushions to prevent key drift.
    """
    _read = BTN_Analogue.read_u16()
    _KeyPress = 'null'
    
    if _read < 7000:
        _KeyPress = 'up'       # BTN3 Map Node
    elif _read < 12000:
        _KeyPress = 'select'   # BTN2 Map Node
    elif _read < 16000:
        _KeyPress = 'down'     # BTN1 Map Node
        
    return _KeyPress

Clone this wiki locally