Physical Computing: Switching LEDs with an Arduino or Raspberry Pi
Articles,  Blog

Physical Computing: Switching LEDs with an Arduino or Raspberry Pi

In this video about “Physical Computing” I will show you, how to switch LEDs using GPIOs I am using a Raspberry Pi 3 and an Arduino Uno for the demonstrations. 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 video, the output mode is needed. In output mode we 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 between 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. In this video I am using LEDs as load. 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. Never connect an LED directly between ground and GPIO; 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. The recommended current limit per pin for the Arduino is 20mA… …while that value should not exceed 16mA at the Raspberry Pi. With that, the Arduino can drive the LED with maximum current while the Raspberry Pi sets a lower limit. 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 series resistor is either connected between cathode of the LED and ground… …or between anode of the LED and GPIO – the circuit works properly with both versions, as long as you consider the correct polarity of the LED. With wrong polarity, the LED wont get lighted up, even if the GPIO is turned on, however there is no danger of causing damage to your board with the LED being inserted with reverse polarity. If the LED doesn’t light up even if the polarity is correct, check the voltage on the GPIO: Even with the LED being turned on, we can read more than 4.5V at the Arduino… …and more than 3V at the Raspberry Pi. The voltage across the series resistor at the Arduino is 2.61V, by what we get in turn a current of 9.7mA flowing though that device as well as through the LED. The reading is 1.12V at the resistor connected to the Raspberry Pi by what the resulting current is just 5.1mA. One source of error for the discrepancy between reading and expected current value is the voltage drop across the LED that has to be considered in our calculation. In the datasheet, the voltage drop in forward configuration of this LED is listed as 2V typically. When subtracting those 2V from the GPIO voltage, we get a minimal resistance of 150 Ohms for the Arduino and 81 Ohms for the Raspberry Pi. I am using 180 Ohms for the Arduino and 100 Ohms for the Raspberry Pi. At the Arduino the voltage drop is now 2.44V, according to a current of 13.6mA… …and 0.91V according to 9.1mA at the Raspberry Pi, which is closer to the expected reading. With a 68 Ohms series resistor we get a reading of 0.78V according to 11.5mA which is still far from the 16mA we wanted to get. The issue is: whenever a load is connected to the 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. With the higher resistance values you are on the safe side, thus to avoid causing damage of your Raspberry Pi or Arduino, don’t go to the limits if there is no need to. As you can see, the LED with the 68 Ohms resistor is slightly brighter, however in practice that difference in luminosity is usually negligible. Now you can turn the LEDs on or off by software – you can get sample codes on the project page. If you understood how to switch one LED, it’s an easy job to switch multiple LEDs. When doing so, you must note another limitation! The GPIOs of the Arduino are switched in groups and each of that group has a current limit of 150mA in total. Ideally, no more than 14 LEDs can draw a current of 20mA simultaneously. If all 20 GPIOs are connected to a load, you are on the safe side if you limit the current to 10mA per pin. We get a resulting resistance value of 500 Ohms while not considering the voltage drop accross the LED. 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. The total current limit of the Raspberry Pi is 100mA. With that, no more than 6 LEDs can draw the maximum current of 16mA. The Raspberry Pi shown here, has 28 GPIOs, resulting in a maximum current of approximately 3mA per pin if all of the outputs get connected to the highest load possible. That results in a 1 kiloohms series resistor when neglecting the voltage drop across the LED. As you can see, the LED isn’t that bright anymore. Instead of reducing the value of the series resistor, 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 shown here is clearly brighter than the green one. Same as before, in this circuit the LED and the series resistor are connected between GPIO and ground. In that configuration the conventional current, that is a flow of positive charges, exits the GPIO and flows through the loads to the ground pin. The GPIO supplies a so called “source current”. However, you can also switch the load between GPIO and the +5V pin at the Arduino… …or the +3.3V pin at the Raspberry Pi respectively. In that configuration the cathode of the LED must be joined with the GPIO either directly… …or through the series resistor. Now, if the pin is on HIGH level, there are +5V between ground and GPIO as well as between ground and the +5V pin of the Arduino. There is no difference in potential between both ends of the load, consequently the LED it not lighted up. Not until the GPIO is switched to LOW level, a conventional current exits the +5V pin, flowing through the load to the GPIO that is now on ground level. A so called “sink current” enters the GPIO. Same as in source current mode, the output voltage is lowered whenever a load is connected to the GPIO – we get a reading of 0.22V with the 270 Ohms series resistor. When serving as current sink, the maximum current entering the Arduino should not exceed 300mA with three groups of pins having a maximum of 100mA each. Same as before, limiting the sink current to 10mA per pin keeps you on the save side. That type of circuit also works with a Raspberry Pi, however 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 GPOI 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. We get a voltage shift of 0.37V when connecting a 68 Ohms series resistor and an LED to the GPIO. Same as before, the total sink current should not exceed 100mA by what we get approximately 3mA per pin to stay on the safe side. 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. As soon as the left GPIO is also switched to HIGH level, once more the difference in potential between both pins is zero, none of the LEDs is lighted up. Not until the right GPIO falls back to LOW level, 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. 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. The bare wires seen in this video are only for demonstration purposes – in practice you must insulate your wires to avoid damage – remember that what can go wrong, will go wrong! Now you should know about the base principles of connecting peripherals to a GPIO and you can start your own experiments by doing some coding. You can get sample codes with deeper explanation on my project pages. If you’d like to support me in creating more videos about “Physical Computing” you can press the donate button on my pages – many thanks to all existing backers! Thanks for watching and: “I’ll be back!”


Leave a Reply

Your email address will not be published. Required fields are marked *