Using GPIO Pins as Outputs
We showed in Chapter 14 how the GPIO lines could be made to act like a switch between the 3V3 power rail and the ground. Now it’s time to put that into action and use that switch to control something. The first thing you should control is a simple LED. We say they’re simple because they are ubiquitous nowadays, but they still require a little explanation. The letters stand for light-emitting diode.
You probably understand the first two words, but the last one might need explaining. A diode is an elecPIO.setwarnings(False) # to remove spurious warnings on re runtrical component that lets current pass in one direction but not in the other direction, a bit like a non-return valve. The symbol tells a little story about this because the current arrives on the long side of the triangle, called an anode, and gets squashed down to a point and out through the barrier at the other end, the cathode. Current flowing in the other direction towards the cathode hits the barrier and can’t flow through the device. In a diode, the current flows from the anode to the cathode, as shown in the following image.
In an LED, the current does exactly the same thing, but as it flows, it also generates light. In a normal incandescent bulb, light is generated because the filament gets hot and glows white-hot. In an LED and fluorescent light tube, the light creation mechanism is different. ...