News    The Project    Technology    RoboSpatium    Contribute    Subject index    Download    Responses    Games    Gadgets    Contact   




<<< Microcontroller-Set         LED Matrix >>>

Switching LEDs

Video about switching LEDs


GPIO

GPIO of a Raspberry Pi
Figure 1:
GPIO means General Purpose Input/Output, thus you can use those pins in either input or output mode by changing nothing but a few lines of software code. In order to switch a load as demonstrated in this chapter, the output mode is needed.
In output mode you can read either 0V or 3.3V at the GPIO of the Raspberry Pi and 5V at the pin of the Arduino. That voltage is recorded between ground and the GPIO pin. If a load is connected betwen ground and GPIO, that pin is in fact a voltage source, causing a conventional current flowing out of the GPIO through the load into the ground pin. Same as with all real voltage sources, the current that pin can supply is limited. Your computing device will get destroyed when exceeding that limit!

LEDs

LED in with round housing
Figure 2:
In this chapter I am talking about switching LEDs. Note the correct polarity of a light emitting diode when connecting that device to a voltage source; the cathode must point to ground of your circuit. The housing of this round LED is flattened on the side of the cathode pin. Furthermore, the cathode pin is shorter than that of the anode. The anode must point to the positive terminal of your circuit.
GPIO and LED
Figure 3:
Never connect an LED directly between ground and GPIO...
GPIO with LED and series resistor
Figure 4:
...you must insert a series resistor.
That resistor limits the current flowing through the circuit. The value of resistance must meet to requirements:
The maximum current of the LED that is listed in the datasheet as 20mA for this type as well as the maximum current the GPIO can supply.

Source and sink current

GPIO in source current mode
Figure 5:
Whenever a load is connected between GPIO and ground a conventional current, that is a flow of positive charges, exits the GPIO and flows through the loads to the ground pin if the pin is on HIGH level. The GPIO suplies a so called source current.
GPIO in sink current mode
Figure 6:
However, you can also switch the load between GPIO and the +5V pin at the Arduino...
GPIO in sink current mode Raspberry Pi
Figure 7:
...or the +3.3V pin at the Raspberry Pi respectively.
At the Raspberry the load has to be connected between GPIO and the +3.3V pin, because +5V on a GPIO will destroy your Pi!
With the load connected between GPIO and +3.3V, there is no difference in potential whenever the GPIO is on HIGH level while a conventional current flows from the +3.3V pin through the load into the GPIO whenever that pin is on LOW level.
A so called sink current enters the GPIO.

Current limit

GPIOs with load
Figure 8:
Same as with all real world voltage sources, the current a GPIO can supply is limited. Your computing device will get destroyed when exceeding that limit.
Raspberry Pi and Arduino with LED
Figure 9:
The current is limited by a series resistor whose resistance value must meet two conditions:
The maximum current of the LED that is listed in the datasheet as 20mA for the type in this picture as well as the maximum current the GPIO can supply.The recommended current limit per pin for the Arduino is 20mA while that value should not exceed 16mA at the Raspberry Pi. With Ohm's law we can calculate the lowest resistance value of a load connected to a GPIO considering the output voltage of that pin. We get 250 Ohms for the Arduino and 206.25 Ohms for the Raspberry Pi. To avoid going to the limit, I am using 270 Ohms for the Arduino and 220 Ohms for the Raspberry Pi.


The LED can also set the current limit! When using "Low Current LEDs" the maximum current can be as low as 3mA. With that we get a resistance value for the Arduino:

R = 5V / 0.003A = 1666Ω

and the Raspberry Pi:

R = 3.3V / 0.003A = 1100Ω

Total current

Multiple GPIOs with LEDs
Figure 10:
When switching multiple LEDs you must consider the total current flowing!
At the Raspberry Pi, the total current should not exceed 100mA. The Pi 3 has 28 GPIOs by what we get approximately 3mA per GPIO if all outputs are connected to a load:
I = 100mA / 28 = 3.57mA
That results in a 1 kiloohms series resistor when neglecting the voltage drop across the LED:
R = 3.3V / 3.57mA = 924Ω
Multiple GPIOs with LEDs and various resistors
Figure 11:
With a 1kΩ series resistor, the middle LED isn't that bright anymore. Instead of reducing the value of the series resistor to 68Ω as demonstrated at the LED at the bottom, you should use LEDs with a higher efficiency. Even with the 1 kiloohms series resistor, resulting in a current of less than 1mA, the white LED at the top is clearly brighter than the green one.
Multiple GPIOs with LEDs in source current mode
Figure 12:
The total current an Arduino can handle is 300mA, however there is a difference if the GPIOs are operated as current source or current sink. In sorce current mode the total current is split into two groups of pins:
Pin 0 to 4 and pin 14 (A0) to 19 (A5) = 150mA
Pin 5 to 13 = 150mA
You are on the safe side when limiting the current to 10mA per pin. Without considering the current drop across the LED we get:
R = 5V / 0.01A = 500Ω
As you can see, even with a 560 Ohms series resistor, the LEDs are bright enough to see whether a GPIO is turned on or off.
Multiple GPIOs with LEDs in sink current mode
Figure 13:
In sink current mode the total current is split into 3 groups of pins:
Pin 0 to 4 = 100mA
Pin 5 to 13 = 100mA
Pin 14 to 19 = 100mA
Same as in source current mode you are on the safe side by using 560Ω resistors.

Limit the current per GPIO to 10mA at the Arduino and 3mA at the Raspberry Pi to stay on the safe side! The resulting series resistor for the Arduino is 560Ω and 1kΩ for the Raspberry Pi.

Output impedance

Voltage drop GPIO with load
Figure 14:
Whenever a load gets connected to a GPIO, the output voltage drops.
With the 270 Ohms series resistor we get a voltage drop of 0.23V and 0.33V with the 180 Ohms series resistor at the Arduino.
At the Raspberry Pi we get a reading of 0.17V with the 220 Ohms series resistor and 0.38V with 68 Ohms.
Same as with all real-world voltage sources, the output voltage is lower with a load connected to a GPIO.

Current from one GPIO to another

Current between GPIOs
Figure 15:
Here, two LEDs, each having a series resistor are switched between two GPIOs. The cathode of the greed LED points to the left GPIO, that of the red LED to the right GPIO. Whenever both pins are on LOW level, there is no difference in potential between the GPIOs and so both LEDs are turned off. With the right GPIO switched to HIGH level, a current exits that pin, flowing through the branch with the green LED into the left GPIO. The right GPIO serves at current source, the left one as current sink. The red LED is in reverse mode, by what the current running through that branch is negligible and the red light is turned off.
Current between GPIOs
Figure 16:
If the right GPIO is on LOW level and the left GPIO on HIGH, a conventional current exits the left pin, flowing through the branch with the red LED into the right GPIO. Now, the green LED is in reverse mode and so turned off while the red LED is connected with forward polarity and so lighted up.
Current between GPIOs Raspberry Pi
Figure 17:
One series resistor for both LEDs works fine, because only one LED is connected in forward direction at a time - that's what I am demonstrating with the Raspberry Pi.

A current also flows from one GPIOs to another whenever there is a difference in potential and you must keep the magnitude of that current below the maximum rating.
Shorting GPIOs will also cause damage of your computing device, thus you should use insulated plugs for wiring up your peripherals!

Software

The software examples of the video are available as download.


<<< Microcontroller-Set         LED Matrix >>>


News    The Project    Technology    RoboSpatium    Contribute    Subject index    Archives    Download    Responses    Games    Links    Gadgets    Contact    Imprint   





Twitter YouTube Hackaday Patreon TPO