//ESP32 charlieplexing demo
/*
//diagram.json is generated with this js, you can run in the console and copy/paste the output
//learn about charlieplex wiring here [not affiliated] https://stkwans.blogspot.com/2012/05/designing-large-charlieplex.html
function generateDiagram() {
const PINS = ['D18', 'D19', 'D21', 'D22', 'D23', 'D25', 'D26', 'D27', 'D12', 'D4'];
const NUM_PINS = PINS.length;
//more options maybe for larger matrices https://lastminuteengineers.com/esp32-pinout-reference/
//const PINS = ['D18', 'D19', 'D21', 'D22', 'D23', 'D25', 'D26', 'D27', 'D12', 'D4', 'D5', 'D13', 'D14', 'D15', 'D32', 'D33', 'D2'];
//const PINS = ['D2', 'D4', 'D5', 'D12', 'D13', 'D14', 'D15', 'D16', 'D17', 'D18', 'D19', 'D21', 'D22', 'D23', 'D25', 'D26', 'D27', 'D32', 'D33'];
let project = {
version: 1,
author: "ESP32 Charlieplex Demo",
editor: "wokwi",
parts: [
{ type: "wokwi-esp32-devkit-v1", id: "esp", top: 0, left: 0, attrs: {} }
],
connections: [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ]
]
};
// Generate LED parts
let ledIndex = 1;
for (let i = 0; i < NUM_PINS; i++) {
for (let j = 0; j < NUM_PINS; j++) {
if (i !== j) {
let top = 50 + i * 40;
let left;
if (j > i) {
// Upper right triangle, shifted left
left = 100 + (j - 1) * 40;
} else {
// Lower left triangle
left = 100 + j * 40;
}
project.parts.push({
type: "wokwi-led",
id: `led${ledIndex}`,
top: top,
left: left,
attrs: { color: "red" }
});
project.connections.push([`led${ledIndex}:A`, `esp:${PINS[i]}`, "red", ["hidden"]]);
project.connections.push([`led${ledIndex}:C`, `esp:${PINS[j]}`, "black", ["hidden"]]);
ledIndex++;
}
}
}
return JSON.stringify(project, null, 2);
}
console.log(generateDiagram());
*/
#define DELAY 100 // milliseconds between LED changes
// ESP32 GPIO pins
const uint8_t LEDS[] = { 18, 19, 21, 22, 23, 25, 26, 27, 12, 4 };
const uint8_t LED_COUNT = sizeof(LEDS);
const uint8_t TOTAL_LEDS = LED_COUNT * (LED_COUNT - 1);
void setup() {
// Initialize all pins as INPUT (high impedance state)
for (int i = 0; i < LED_COUNT; i++) {
pinMode(LEDS[i], INPUT);
}
}
void lightLED(int row, int col) {
// Set all pins to input (high impedance)
for (int i = 0; i < LED_COUNT; i++) {
pinMode(LEDS[i], INPUT);
}
// Set the anode pin to HIGH (OUTPUT mode)
pinMode(LEDS[row], OUTPUT);
digitalWrite(LEDS[row], HIGH);
// Set the cathode pin to LOW (OUTPUT mode)
pinMode(LEDS[col], OUTPUT);
digitalWrite(LEDS[col], LOW);
}
void loop() {
for (int row = 0; row < LED_COUNT; row++) {
for (int col = 0; col < LED_COUNT; col++) {
if (row != col) { // Skip when row and column are the same
lightLED(row, col);
delay(DELAY);
}
}
}
}