How to Use GPIO Pins as Outputs on Your Raspberry Pi - dummies

How to Use GPIO Pins as Outputs on Your Raspberry Pi

By Sean McManus, Mike Cook

GPIO lines can be made to act like a switch between the 3V3 power rail on the Raspberry Pi and the ground. Put that into action and use that switch to control something. The first thing you should control is a simple LED. These letters stand for lightemitting diode.

You probably understand the first two words, but the last one might need explaining. A diode is an electrical component that lets current pass in one direction but not in the other direction, a bit like a non-return valve. 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.


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.

Light is produced by exciting the atoms in it and then as these atoms decay to their normal state, they emit a small packet of light, a photon. In a fluorescent light, this happens in a gas, whereas in an LED, this happens in a solid crystal. The light’s color depends on the material the crystal is made from and several alloys can be used to make an LED.

You need a voltage to overcome the initial resistance of the LED, but after the voltage has been reached, you get a big change in current for a very small change in voltage. This is a device that does not obey Ohm’s law where the current is simply proportional to the voltage.

You can think of an LED as having a voltage-dependent resistance. For low voltages, the resistance is high, but for higher voltages, the resistance drops to a very low value. This is a non-linear device. This means you can’t just simply connect it up to a voltage and expect it to work. Whenever you use an LED, you always need some form of current-limiting device. A resistor is the simplest.

On the Internet, many people tell you that you do not need anything else when driving an LED. They connect an LED directly to a pin. However, an LED must always have something else attached in the circuit that limits the current through the LED. The simplest such thing is a resistor.

They might even have fancy-sounding technical descriptions justifying their assertions. They may say it is being rapidly turned on and off. This is known as multiplexing, which stops current from building up. However, they’re wrong, most through ignorance or misunderstanding, and a few are trying to be funny or malicious. Listening to such advice can end up damaging your computer.

A resistor is simple to add. You need one resistor per LED. The resistor value is not too critical. If you can’t be bothered calculating the exact value you need, take heart: You won’t go wrong with anything between 220 R and 470 R.

This method is sufficient for all low-powered LEDs; that is, LEDs that require a current of 100 mA (milliamperes) or less. The most popular form of LED has a maximum current rating of 20 mA.