The easiest way to turn on an LED using the Raspberry Pi is to wire it up to the +3.3v or +5v pins of the General Purpose Input Output (GPIO) header and ground (0v), making sure to place a resistor in series with the LED and ground. Bear in mind that It is important to always use a resistor with LEDs to limit the current. If too much current flows through the LED, it will burn very bright for a short period before burning out, so a resistor is used to limit current. Assuming a current of 20mA and a +3v3 supply it’s safe to use a resistor of 220Ω. Anything of higher resistance will make the LED dimmer. LEDs are diodes and as such only pass electricity one way. This means we need to make sure we put them in the right way round. They have a long leg and a slightly shorter leg. The long leg goes to the plus side and the shorter leg to the negative (or 0v) side.
This will light the LED, however, it will leave it permanently on. It would be nice if we could control the LED by turning it on or off. In order to do this we need to understand how the General Purpose IO header functions.
What is GPIO?
The GPIO pins on the Raspberry Pi can be found in on the opposite corner to the micro-usb power connector on the same side of the board. The GPIO connector is made up of a 26-pin 2.54 mm (100 mil) expansion header, marked as P1 on the board and arranged in a 2 x 13 strip. The 26 header pins can function as SPI, I2C, serial UART as well as providing +3v3, +5v power supply lines. The GPIO connector actually has a number of different types of connection on it. There are:
- 8 GPIO pins – these are the true General Purpose IO pins that do not have a dedicated purpose but can be used for a variety of things such as turning LEDs on and off etc. The GPIO pins which can be configured as Input/Output and turned on/off via software.
- The pins labelled SCL and SDA can be used for I2C. I2C interface pins allow you to connect and control external hardware modules with just two control pins. I2C or IIC stands for Inter-Integrated Circuit.
- The pins labelled MOSI, MISO and SCLK can be used to connect to high speed SPI devices. SPI is a similar idea to I2C but a different standard. SPI stands for Serial Peripheral Interface.
- Serial UART Rx and Tx pins for communication with serial peripherals.
The GPIO pins can be used as both digital outputs and digital inputs. As digital outputs, you can write programs that turn a particular pin HIGH or LOW. Setting GPIO pins HIGH sets them to +3v3; setting them LOW sets them to 0v. Diagram 2 illustrates the functions of the GPIO pins.
For more detail: How to Connect an LED to your Raspberry Pi