Hello LED

Figure 1 Breakout Board on Breadboard

 

INTRODUCTION

The Raspberry Pi (RPi) relies on a set of pins for the exchange of electrical signals with external components. A set of these pins serve as General Purpose Input/Output (GPIO) interfaces as shown in Figure 2 Raspberry Pi pin cross-reference below:

Figure 2 Raspberry Pi pin cross-reference

 

General Purpose Input/Output

A very common exercise is to use the GPIO pins to drive LED devices. This note illustrates the basic example and then extends it for multiple LED devices.

Caution

Since working with GPIO pins is experimental, some precautions should be taken to avoid exceeding the recommended electrical settings for all pins on the board. The RPi does not have any feature like the Arduino UNO board where pin 13 is connected to an on-board LED.

Hardware Attached on Top

There are several accessory boards (i.e. Hardware Attached on Top – HAT) that insulate the RPi from accidental exposure to electrical overload conditions. Some of these boards will be examined in a subsequent note. The LED tests with GPIO pins do not require a HAT.

SINGLE LED

The single LED example serves as the equivalent of the traditional Hello World exercise. A bare minimum number of lines of code serve as the introduction to using a GPIO pin.

Solution

The principle to light an LED device for this exercise, as shown in Figure 3 Basic Circuit, leverages a resistor to ensure that the electrical requirements of the LED (i.e. voltage and current) are met.

The value of the resistor, R1, depends on the characteristics of the LED device. For this exercise, using commonly available LED devices, the characteristics of the chosen LED are:

  • LED voltage drop: 1.2V
  • LED max current: 20mA
  • GPIO max voltage: 5V

The estimated value of the resistor to support the tests in this note is derived using the equation shown below:

Using conventional standards for commonly available resistors (for hobbyists), the preferred value for R1 may be 220 ohms. The nominal tolerance value of 5% is perfectly adequate for the exercise. Similarly, the watt rating for the resistor can be as low as practical for handling while assembling the circuit on the breadboard.

Bill of Materials

The table below lists the materials for the first exercise:

Part Qty Description Label
RPi 1 Raspberry Pi B rev 2 RPi1
LED, red 1 LED, color red, 633 nm LED1
Resistor 1 Resistor, 220 ohms ±5% tol R1
Wire 2 Dupont connecting wires
Breadboard 1 Breadboard, half-size

 

Figure 4 Single LED Test

Assembly

The layout for the first example, as shown below in Figure 5 Hello LED 1, uses the physical pin number 7 which also referenced as BCM4. The reference section has links to more detailed information on the correspondence between the physical pins and the Broadcom

(BCM) pin numbers.

Since the use of a breakout board (also known as cobbler board in some circles), limits the interaction with the physical pins of the RPi by transferring the equivalent connections directly to the breadboard. The practical exercises in this note were conducted using the Sunfounder RPi-GPIO Extensions Board v3.0

This particular board, illustrated in Figure 6 Sunfounder RPi-GPIO Extensions Board, has been superseded with improved pin connectivity for the newer models of Raspberry Pi.

Figure 7 Hello LED 1 schematic

Pin P7 of the extensions board has the following associations:

Physical pin number Broadcom (BCM) number Sunfounder Extensions board pin WiringPi pin number
7 4 P7 7

 

A key advantage of the Sunfounder extensions board is that the GND and VDC (5V and 3.3V) are applied to the buses on the edges of the breadboard. The 5V DC supply is on the right (or upper) side and the 3.3V DC is on the left (or lower) side. This option is no longer available in the newer 40-pin model but there are other manufacturers who support the earlier concept.

Schematic

The figure in the left column illustrates the schematic diagram of the assembly with only two discrete components (i.e. LED and resistor) and the RPi board. This note uses the B Rev 2 board but the instructions apply equally to current succeeding RPi models including the Raspberry Pi 3 and Raspberry Pi Zero models.

Test

The following lines of code are sufficient to test the initial exercise.

The sample code above uses GPIO.BOARD conventions to reference the pin used to drive the LED. The alternate version of the code using the

GPIO.BCM convention is shown below:

 

Figure 10 Hello Five LEDs

MORE LIGHTS

The RPi boards have many GPIO pins, as shown in Figure 2 Raspberry Pi pin cross-reference, that are good candidates to extend the exercise to more than one LED device. The next test toggles the state of five LED devices in sequence. Each LED device blinks three times. The sequence is repeated five times.

Sunfounder RPi-GPIO Extension Board

Since the exercises on the remainder of this note rely on the Sunfounder RPI-GPIO Extension Board v3, the following table can serve as short but quick reference to the GPIO pins in the different access methods:

Sunfounder RPI-GPIO Extension Board v3
Left Right
Breakout Physical WiPi BCM Breakout Physical WiringPi BCM
CE1 26 7 P0 11 0 17
CE0 24 8 P1 12 1 18
SCLK 23 11 P2 13 2 27
MISO 21 9 P3 15 3 22
MOSI 19 10 12 P4 16 4 23
RXD 10 16 15 P5 18 5 24
TXD 8 15 14 P6 22 6 25
SCL 5 9 3 P7 7 7 4
SDA 3 8 2 GND

 

Assembly

The breadboard layout to blink five LED devices is shown below:

 

In order to keep the arrangement simple, the specifications of the LED devices and resistors are constant for all instances.

Part

Qty

LED, assorted colors

5

220Ω resistors

5

RPi

1

Wires

6

Breadboard

1

Figure 12 Shopping List for Five LED Test

Schematic

The schematic corresponding to the assembly in Figure 11 Hello LED with five devices is shown below:

Test

This test uses the BCM pin numbers instead of the BOARD pin numbers. The Appendix section in this note has the basic code for the test. A video of a test run is available at YouTube.

WiringPi

Gordon Henderson made available the WiringPi package that is currently installed with the standard Raspbian image. The original library was intended for C/C++ programming. Subsequently, the library was ported for use with Java, Perl, Python, Ruby and Tcl

Pin Table

The table headers are:

  • Board – physical pin on the Raspberry Pi
  • WiPi – WiringPi pin reference number
  • BCM – Broadcom (BCM chip) pin number
  • Name – mnemonic for reference purpose
Board WiPi BCM Name Board WiPi BCM Name
1 3.3V 2 5V
3 4 5V
5 6
7 7 4 8
9 GND 10
11 0 12
13 14 GND
15 16
17 18
19 20 GND
21 22
23 24
25 GND 26

Command Line Utility

The utility gpio is run without super user privileges from the command line. Detailed usage information on the utility is available from the corresponding man page by typing man gpio at the command line.

Blink one LED

The C++ source code to blink one LED using WiringPi pin number 7 (see table above for corresponding reference to other nomenclature) is shown below:

#include #define pinLED 7 int main(int argc, char **argv) { wiringPiSetup(); pinMode(pinLED, OUTPUT); for (;;) { digitalWrite(pinLED, HIGH); delay(1000); digitalWrite(pinLED, LOW); delay(1000); } return 0; } ” src=”file:///C:/Users/baqwa/AppData/Local/Temp/msohtmlclip1/01/clip_image035.png” v:shapes=”_x0000_s2052″>

 

CONCLUSION

This note examined the basic technique to access the GPIO pins on Raspberry Pi boards. The success of the test is manifested by the blinking of the LED device assigned to the pins under tests. The references in the succeeding section provide further insight to the use of GPIO pins on Raspberry Pi boards.

REFERENCES

GPIO: Raspberry Pi Models A and B

Introducing Raspberry Pi HATs

Simple Guide to the RPI GPIO Header and Pins

Sunfounder RPi-GPIO Extension Board

WiringPi, GPIO Interface library for the Raspberry Pi

 


APPENDIX

 

 

 

 

 

 

 

 

Advertisements

About charnumber

Still learning...
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s