Skip to main content

Read the buttons

What this example shows​

All 16 pads are read every 50 ms and printed as a 4x4 map in the shell: X for a pressed pad, . for a released one. The LEDs stay off.

The example code​

This is Examples/ReadButtons.py from the pinned module, unchanged. Save it to your board and run it.

# FILE: ReadButtons.py
# AUTHOR: Josip Šimun Kuči @ Soldered
# BRIEF: Example showing how to read all 16 buttons on the Inputronic GRID.
# Reads all 16 button states every 50 ms and prints a 4x4 grid map.
# Pressed buttons show 'X'; released buttons show '.'.
# WORKS WITH: Inputronic GRID: www.soldered.com
# LAST UPDATED: 2026-05-12

from machine import I2C, Pin
from InputronicGrid import InputronicGrid
import time

# If you aren't using the Qwiic connector, manually enter your I2C pins:
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# grid = InputronicGrid(i2c=i2c)

grid = InputronicGrid()

print("Inputronic GRID ready.\n")
grid.clearLEDs()

while True:
print("+---------+")
for row in range(4):
print("| ", end="")
for col in range(4):
print("X " if grid.readPad(row, col) else ". ", end="")
print("|")
print("+---------+\n")

time.sleep_ms(50)

🔗Open the complete example

Examples/ReadButtons.py at commit 36d3def

→

Expected result, with nothing pressed:

ShellMicroPython
Inputronic GRID ready.

+---------+
| . . . . |
| . . . . |
| . . . . |
| . . . . |
+---------+

A new map follows every 50 ms. While you hold a pad, an X takes the place of its .: the top line of the map is row 0 and the first character in a line is column 0.

Functions used​

InputronicGrid(i2c=None, address=INPUTRONIC_GRID_DEFAULT_ADDR)returns InputronicGrid

Creates the driver object and checks that the board answers. With i2c left as None it creates I2C(0, scl=Pin(22), sda=Pin(21)) on the ports the module recognises (ESP32, ESP8266 and one Soldered board), and raises an exception on a port it does not recognise.

ReturnsThe driver object. Raises an exception with 'Inputronic GRID not found. Check wiring.' when the board does not answer.

Parameters

TypeNameDescription
I2Ci2cA ready machine.I2C object. Optional.
intaddressI2C address of the board. Optional, 0x30 by default.
grid.clearLEDs()returns None

Turns all 16 LEDs off. It is the same as setAllLEDs(0, 0, 0).

ReturnsNothing.

grid.readPad(row, col)returns bool

Asks the board whether one pad is pressed right now, by its place in the grid. Each call sends the board a short I2C request and reads its one-byte reply.

ReturnsTrue while the pad is pressed. False when it is not, or when the I2C exchange fails.

Parameters

TypeNameDescription
introwRow, 0 to 3, top to bottom.
intcolColumn, 0 to 3, left to right.

Putting it together​

The example creates the object, prints its ready line and turns the LEDs off. Each pass of the loop calls readPad() once for every row and column and prints the result as one line per row.

To read all 16 pads with one request and reply instead of sixteen, the module also has readAllPads(), which returns a 16-bit number with bit row * 4 + col set for each pressed pad. No example uses it at this commit.