User Tools

Site Tools


charlieplexing_on_the_esp32

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
charlieplexing_on_the_esp32 [2020/08/31 10:30]
49.181.161.176 created
charlieplexing_on_the_esp32 [2021/02/02 01:24] (current)
Line 2: Line 2:
  
 The ESP32 has a stack of GPIO pins allowing you to attach a whole bunch of LEDs. But it's a neat practice to try using fewer pins with several LEDs using Charlieplexing. There'​s quite a few things you can learn along the way. The ESP32 has a stack of GPIO pins allowing you to attach a whole bunch of LEDs. But it's a neat practice to try using fewer pins with several LEDs using Charlieplexing. There'​s quite a few things you can learn along the way.
 +
 +For all experiments in this article, I'll be using pins 21, 22 and 23 on the ESP32.
  
 Going back to basics, a simple single LED on a GPIO pin would be like this: Going back to basics, a simple single LED on a GPIO pin would be like this:
Line 36: Line 38:
 {{ :​charlieplexing:​schematic-2-pin.png?​nolink |}} {{ :​charlieplexing:​schematic-2-pin.png?​nolink |}}
  
-Although not pictured, both pins reduce the voltage to the LEDs using a resistor each. If we set the first pin's mode to ''​OUTPUT''​ and set the pin itself to ''​HIGH'',​ voltage will be supplied from the pin. If the second ​pin's mode is also set to ''​OUTPUT'',​ but the pin is set to ''​LOW'',​ this is effectively making the pin become ''​GND'',​ providing a voltage potential drop, meaning that voltage will flow from pin 21, through the first LED and into pin 22.+Although not pictured, both pins reduce the voltage to the LEDs using a resistor each. If we set the pin 22's mode to ''​OUTPUT''​ and set the pin itself to ''​HIGH'',​ voltage will be supplied from the pin. If pin 23's mode is also set to ''​OUTPUT'',​ but the pin is set to ''​LOW'',​ this is effectively making the pin become ''​GND'',​ providing a voltage potential drop, meaning that voltage will flow from pin 22, through the first LED and into pin 23.
  
 This lights the first LED. LEDs being a diode means that the current cannot flow through the second LED. This lights the first LED. LEDs being a diode means that the current cannot flow through the second LED.
Line 42: Line 44:
 This concept of the pins being ''​OUTPUT''​ pins, one being ''​HIGH''​ and the other ''​LOW''​ is one of the important principles of Charlieplexing. This concept of the pins being ''​OUTPUT''​ pins, one being ''​HIGH''​ and the other ''​LOW''​ is one of the important principles of Charlieplexing.
  
-If pin voltages are swapped, pin 21 becoming ''​LOW''​ and pin 22 becoming ''​HIGH'',​ the reverse situation is created. Current will flow the other way around and light the second LED, but not able to pass through the first LED. +If pin voltages are swapped, pin 22 becoming ''​LOW''​ and pin 23 becoming ''​HIGH'',​ the reverse situation is created. Current will flow the other way around and light the second LED, but not able to pass through the first LED. 
  
 Let's build this onto the breadboard. The wiring is fairly simple so the breadboard layout is: Let's build this onto the breadboard. The wiring is fairly simple so the breadboard layout is:
  
-[]+{{ :​charlieplexing:​esp32-led-breadboard-charlie-2-pin.png |}}
  
 The following code would allow them to alternate: The following code would allow them to alternate:
Line 79: Line 81:
 </​code>​ </​code>​
  
-Don't worry about the efficiency of the listings ​in this article. They are verbose to make everything as clear as possible, not intended to be optimised. ​+Don't worry about the efficiency of the code examples ​in this article. They are verbose to make everything as clear as possible, not intended to be optimised. ​
  
 Send the code to the ESP32 and watch the LEDs swap back and forth. Very neat. One of the properties of Charlieplexing is that in truth, only one LED can be actually on at a time. But there is a trick to work around that. Using Pulse Width Modulation, in addition to switching pin configurations really quickly, you can make all LEDs appear to be on at the same time. Send the code to the ESP32 and watch the LEDs swap back and forth. Very neat. One of the properties of Charlieplexing is that in truth, only one LED can be actually on at a time. But there is a trick to work around that. Using Pulse Width Modulation, in addition to switching pin configurations really quickly, you can make all LEDs appear to be on at the same time.
  
-We can reproduce a crude form of PWM by simply changing the delay value in our listing ​to around ''​2'':​+We can reproduce a crude form of PWM by simply changing the delay value in our code example ​to around ''​2'':​
  
 <code c> <code c>
Line 104: Line 106:
 {{ :​charlieplexing:​schematic-3-pin.png?​nolink |}} {{ :​charlieplexing:​schematic-3-pin.png?​nolink |}}
  
-Notice there are now two groups of two LEDs, plus two more bridging across at the bottom. This arrangement lets us use lots of pin mode combinations to get all the LEDs to light up one at a time.+Notice there are now two groups of two LEDs, plus two more bridging across at the bottom. This arrangement lets us use lots of pin mode combinations to get each LED to light up on it's own.
  
-We'​ll ​start by looking at what is needed to light LED1. If we set pin 21 to be ''​OUTPUT''​ and ''​HIGH''​ to supply voltage, and on pin 22, we set to ''​OUTPUT''​ and ''​LOW''​ to act as ''​GND'',​ then we can be pretty sure that current will happy flow on that path and LED1 will light up.+We'​ll ​trace the schematic to see what is needed to light LED1. If we set pin 21 to be ''​OUTPUT''​ and ''​HIGH''​ to supply voltage, and on pin 22, we set to ''​OUTPUT''​ and ''​LOW''​ to act as ''​GND'',​ then we can be pretty sure that current will happily ​flow on that path and LED1 will light up.
  
 But what about pin 23? What's happening with this one? What do we set this to? We don't really want this pin to supply any voltage. Do we want it to be ''​GND''​ too? But what about pin 23? What's happening with this one? What do we set this to? We don't really want this pin to supply any voltage. Do we want it to be ''​GND''​ too?
Line 114: Line 116:
 So the basic idea is this: one pin will be ''​OUTPUT''​ and ''​HIGH''​ to supply voltage, one will be ''​OUTPUT''​ and ''​LOW''​ to act as a ''​GND'',​ and a third pin will be disconnected. That's clever, right? So the basic idea is this: one pin will be ''​OUTPUT''​ and ''​HIGH''​ to supply voltage, one will be ''​OUTPUT''​ and ''​LOW''​ to act as a ''​GND'',​ and a third pin will be disconnected. That's clever, right?
  
-That's the second principle of Charlieplexing:​ you can turn pins off by setting ​then as ''​INPUT''​.+That's the second principle of Charlieplexing:​ you can turn pins off by setting ​them as ''​INPUT''​.
  
 We would configure lighting LED1 like this: We would configure lighting LED1 like this:
Line 126: Line 128:
 {{ :​charlieplexing:​3-pin-led1-other-paths.png?​nolink |}} {{ :​charlieplexing:​3-pin-led1-other-paths.png?​nolink |}}
  
-Won't all three LEDs be lit? Well, it's true that LED1 and LED5 are in parallel, so more current could be drawn to power both. But LED5 and LED4 are in series. Therefore the forward voltage is too great for our supply. For two 1.8V LEDs, there is no enough voltage (plus resistors) to power both LEDs if only 3.3V is being output. 3.3V < 3.6V.+Won't all three LEDs be lit? Well, it's true that LED1 and LED5 are in parallel, so more current could be drawn to power both. But LED5 and LED4 are in series. Therefore the forward voltage is too great for our supply. For two 1.8V LEDs, there is not enough voltage (plus resistors) to power both LEDs if only 3.3V is being output. 3.3V < 3.6V.
  
 Therefore, there is only enough voltage to power LED1. Therefore, there is only enough voltage to power LED1.
  
-What about powering LED4? How do we determine the INPUTs and OUTPUTs here? I find it helpful to start tracing ​the schematic:+What about powering LED4? How do we determine the INPUTs and OUTPUTs here? We'll trace the schematic ​again:
  
 Voltage should be supplied on pin 23 as ''​OUTPUT''​ and ''​HIGH'',​ and pin 22 should be ''​OUTPUT''​ and ''​LOW''​ to provide ground. That will give the most obvious current path. Voltage should be supplied on pin 23 as ''​OUTPUT''​ and ''​HIGH'',​ and pin 22 should be ''​OUTPUT''​ and ''​LOW''​ to provide ground. That will give the most obvious current path.
Line 166: Line 168:
 | LED6 | OUTPUT LOW  | INPUT       | OUTPUT HIGH | | LED6 | OUTPUT LOW  | INPUT       | OUTPUT HIGH |
  
-Now it is time to breadboard the schematic. This can be tricky. I like to take the existing schematic and stretch it out to be as similar as possible ​as how it will be on a breadboard. I used KiCad to move the existing schematic to be laid out like this:+Now it is time to breadboard the schematic. This can be tricky. I like to take the existing schematic and stretch it out to be as similar as possible ​to how it will be on a breadboard. I used KiCad to move the existing schematic to be laid out like this:
  
 {{ :​charlieplexing:​3-pin-schem-stretched.png?​nolink |}} {{ :​charlieplexing:​3-pin-schem-stretched.png?​nolink |}}
Line 290: Line 292:
  
 Hope that helps you work through Charlieplexing on ESP32. Let me know if it helped and send me a pic of your breadboard. I'll post it on this page. Hope that helps you work through Charlieplexing on ESP32. Let me know if it helped and send me a pic of your breadboard. I'll post it on this page.
 +
 +Here's my lovely effort cramming everything on a small breadboard and using a 10-LED block:
 +
 +{{ :​charlieplexing:​charlieplexed-led-bar.jpg?​nolink |}}
charlieplexing_on_the_esp32.1598869803.txt.gz ยท Last modified: 2021/02/02 01:24 (external edit)