▲ The circuit to be analyzed in this article
This is how it started.
I bought an electric toothbrush, which cost a few dozen yuan:
The actual item received looks like this:
There is 1 button and 6 LED lights on the toothbrush handle:
Take a look at the circuit board:
It can be seen that the microcontroller used on the circuit board only has 8 pins, but it has to control 1 button, 6 LED lights, and 1 vibrating motor:
At first glance, the GPIO should not be enough.
The microcontroller removes the power pin (positive pole) and GND pin (negative pole), leaving only up to 6 GPIO pins.
So how do you make it enough?
It turns out that through clever circuit design, this electric toothbrush uses 3 GPIOs to control 6 LED lights, saving GPIO.
This clever circuit is as follows:
The 6 LED lights are of the same model. For easy viewing, red and green colors are used to distinguish two different directions.
1. Principle analysis
This circuit uses three states of the microcontroller GPIO:
high level
low level
High resistance state
The so-called "high resistance state" means that the GPIO shows great resistance to the external circuit. Because the impedance is very large, it hardly draws in current and does not output current to the outside world.
Each LED light lights up individually and is divided into six situations.
1. When only LED1 lights up, the status of each GPIO of the microcontroller is as follows: (The red line with an arrow is the current loop)
2. When only LED2 lights up, the status of each GPIO of the microcontroller is as follows:
3. When only LED3 lights up, the status of each GPIO of the microcontroller is as follows:
4. When only LED4 is on, the status of each GPIO of the microcontroller is as follows:
5. When only LED5 is on, the status of each GPIO of the microcontroller is as follows:
6. When only LED6 lights up, the status of each GPIO of the microcontroller is as follows:
It is organized as follows:
It's that simple!
2. Summary and improvement
The above actually uses a method called "Charlieplex".
Why is it called Charlie multiplexing?
It's very simple, because this method comes from Charlie Allen, an engineer at Maxim Semiconductor Company.
Charlie multiplexing is a method that can effectively save GPIO when driving LEDs, especially when driving a large number of LEDs.
Using this method, n GPIOs can drive n*(n-1) LEDs, so:
You can drive 2 LEDs using 2 GPIOs.
6 LEDs can be driven using 3 GPIOs.
12 LEDs can be driven using 4 GPIOs.
And so on.
The basis for this method to be realized is:
The three states of the microcontroller GPIO: high level, low level, and high resistance state.
LED has unidirectional conductivity.
Charlie’s approach to reusing designs:
Two LEDs are connected in series between any two GPIO pins. The two LEDs are connected in parallel and the LED directions are opposite.
When you want to light up a specific LED, set the GPIO pins connected to both ends to high level and low level respectively, and set the remaining GPIO pins to high impedance.
The circuit of the 6 LED lights in the electric toothbrush above is designed in this way.
Let's start with the simplest one and experience the circuit design of Charlie multiplexing step by step.
1. It is easiest to use 2 GPIOs:
When LED1 is on:
When LED2 is on:
Only the high-level and low-level states are used here, and there is no need to use the high-impedance state.
2. When using 3 GPIOs, it has been analyzed before:
It can be equivalent to the following figure:
It can be seen that there are indeed two parallel LEDs connected in series between any two GPIOs, and the LED directions are opposite.
3. The same principle, when using 4 GPIOs:
When LED1 is lit:
The situations in which other LEDs light up are no longer listed.
Charlie's reuse of such a circuit connection will also cause some problems.
First of all, when the LED lights up, the current is completely output by the GPIO of the microcontroller, so there are certain requirements for the current driving capability of the GPIO. When designing a circuit, you should pay attention to check the current driving capability of the MCU you are using. The following figure is an explanation of the GPIO current driving capability in the STM32 microcontroller:
Secondly, if an LED is open-circuited or short-circuited, the flow of current will be disrupted, and the logic of the LED lighting up will become confusing. In the worst case, the circuit will demand a large current from the GPIO, causing damage to the microcontroller. The figure below assumes that LED1 is short-circuited. When LED5 is lit, LED3 will also light up:
3. Continue to advance
What should I do if I want to light up more than two LEDs at the same time?
Just light them up alternately. As long as the switching speed is fast enough, due to the persistence of vision effect of the human eye, they will appear to light up at the same time.
It is worth mentioning that if there are many LEDs that are to be lit at the same time, such as a large-scale LED dot matrix, then there are some new issues that need to be paid attention to, and there are quite a few tricks.
This article will not be expanded upon. Interested readers can inquire and practice by themselves and continue to advance.
4. Finally
I want to thank this electric toothbrush for giving me the opportunity to write "Charlie Reuse".
It’s just that updates have slowed down again recently. After all, I don’t make money writing public accounts, so I still work hard and try to buy an expensive electric toothbrush!
Recommended in the past
Disassemble the old rice cooker temperature limiter and verify its principle
[ Teardown ] A platform’s 19 yuan children’s phone watch. How does it work?
[ Teardown ] Alipay box, Alipay received 1 million!