In this example, I am using three GPIO pins on an ESP32 to control six LEDs via Charlieplexing. The sketch demonstrates three different methods to control the GPIO pins, highlighting their performance differences. A breadboard layout with six LEDs connected to the three ESP32 pins is shown below:
The white wire connects to GPIO16, the green wire to GPIO17, and the yellow wire to GPIO18 of the ESP32.
Charlieplexing is a technique that allows controlling multiple LEDs with fewer GPIO pins than normally required. It works by utilizing the high-impedance state of microcontroller pins to control multiple LEDs with just a few GPIOs. Although only one LED can be on at a time, rapidly switching them creates the illusion that multiple LEDs are lit simultaneously.
For more details, see Charlieplexing on Wikipedia.
You can test the sketch without using the Arduino IDE or physical hardware by using the Wokwi simulator. Use the Project at this link.
The diagram.json defines the hardware, including the ESP32, a breadboard, six LEDs, and three resistors.
Then, press the play button, and the sketch will run in the simulator. Keep in mind that the simulator runs slower than real hardware, so time messurement is not posible.
- ESP (any version should work)
- Arduino IDE (tested with version 2.3.2)
- 6 LEDs and 3 resistors (I used 10Ω resistors, but zp to 330Ω should work)
- Breadboard (optional)
- Jumper wires (optional)
This sketch demonstrates Charlieplexing by controlling six LEDs using three GPIO pins. Since speed is important in Charlieplexing (to create the illusion of multiple LEDs being on), the sketch implements three methods to control the GPIO pins. These methods are designed to compare performance, focusing on execution speed.
-
Method 0: Uses the standard
pinModeanddigitalWritefunctions. This is the default, high-level Arduino approach, but it is relatively slow. -
Method 1: A more efficient method using the
gpio_configfunction from the ESP32 Hardware Abstraction Layer (HAL). This provides a significant speed improvement over Method 0. -
Method 2: The fastest method, utilizing low-level ESP32 GPIO functions such as
gpio_set_levelandgpio_set_direction. This method interacts directly with the hardware registers, minimizing overhead.
In the setup() function, each method is tested, and its performance is measured in microseconds using the ESP32's micros() function. The results for both the AllOff and LitLed operations are printed to the serial monitor for comparison.
- Connect the ESP32 to your circuit (see the Breadboard Design and the Schematic on Wikipedia). Use the GPIO pins defined in the sketch (
display_pins). - Upload the sketch to your ESP32 using the Arduino IDE.
- Open the serial monitor to view the LED sequence and speed test results.
The sketch first lights up each LED individually to verify the connections. Then, it measures and prints the time taken by each method to turn off all LEDs and light one up.
In the loop(), LEDs are lit up in alternating groups of three, with the method used to control the GPIOs switching after each cycle.
This code is open-source and available under the MIT License. Feel free to modify and use it for your own projects.
