Skip to main content

Light a pad when you press it

What this example shows​

Each pad switches its own LED. The first press lights it in the colour of its row (row 0 red, row 1 green, row 2 blue, row 3 orange), and the next press turns it off. Every press is printed in the shell.

The example code​

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

# FILE: ButtonLEDControl.py
# AUTHOR: Josip Šimun Kuči @ Soldered
# BRIEF: Example showing button-driven LED toggle on the Inputronic GRID.
# Each button toggles its corresponding LED on or off.
# First press turns the LED on in a row-specific color;
# second press turns it off.
# Demonstrates reading individual pads by row/col coordinate
# and reacting with per-LED color control using the same coordinates.
# 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()
grid.clearLEDs()
print("Press any button to toggle its LED.")

# Toggle state per button, indexed by [row][col]
ledOn = [[False] * 4 for _ in range(4)]

# Colors per row (R, G, B)
rowColor = [
(255, 0, 0), # row 0 — red
( 0, 255, 0), # row 1 — green
( 0, 0, 255), # row 2 — blue
(255, 128, 0), # row 3 — orange
]

# Previous pad state per [row][col] for edge detection
prevState = [[False] * 4 for _ in range(4)]

while True:
for row in range(4):
for col in range(4):
pressed = grid.readPad(row, col)

if pressed and not prevState[row][col]:
ledOn[row][col] = not ledOn[row][col]

if ledOn[row][col]:
r, g, b = rowColor[row]
grid.setLED(row, col, r, g, b, 255)
else:
grid.setLED(row, col, 0, 0, 0, 255)

print("Button (row {}, col {}) → LED {}".format(
row, col, "ON" if ledOn[row][col] else "OFF"))

prevState[row][col] = pressed

time.sleep_ms(20)

🔗Open the complete example

Examples/ButtonLEDControl.py at commit 36d3def

→

Expected result, after pressing the pad at row 0, column 0 twice:

ShellMicroPython
Press any button to toggle its LED.
Button (row 0, col 0) → LED ON
Button (row 0, col 0) → LED OFF

The pad's LED lights red on the first press and goes off on the second. When the board does not answer, InputronicGrid() raises an exception with the message Inputronic GRID not found. Check wiring.

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.

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.
grid.setLED(row, col, r, g, b, intensity=128)returns None

Sets the colour of the LED under one pad, by its place in the grid. Each colour value is scaled by intensity / 255 before it is sent. An I2C error is ignored.

ReturnsNothing.

Parameters

TypeNameDescription
introwRow, 0 to 3.
intcolColumn, 0 to 3.
intrRed, 0 to 255.
intgGreen, 0 to 255.
intbBlue, 0 to 255.
intintensityBrightness, 0 (off) to 255 (full). Optional, 128 by default.

Putting it together​

InputronicGrid() sets up I2C and checks the board, and clearLEDs() starts with every LED off. The loop reads all 16 pads with readPad() every 20 ms and compares each one with its state from the last pass, so a pad held down counts as one press. On a new press the example flips that pad's entry in ledOn and calls setLED() with the same row and column, at full intensity in the row's colour or with 0, 0, 0 to turn it off.