8
Contents 1 Warnings 2 Aims 3 The Hardware 3.1 Theory 3.2 Circuit 1 - Basic LED Driving Circuit 3.2.1 Basic LEDx8 Test Module 3.3 Circuit 2 - LED Driving Circuit (using Transistor Switching Circuit) 3.3.1 Transistor Selection 3.3.2 Calculating R1 - LED Current Limiting Resistor 3.3.3 Calculating R2 - Transistor Base Resistor 3.3.3.1 R2max - Based on driving current requirement (Ic) 3.3.3.2 R2min - Based on the GPIO pin source current 3.3.4 Testing 4 The Software 4.1 TI LaunchPad 4.2 RPi 5 References RPi Tutor ial EGHS:LED output From eLinux.org Back to the Hub, or the Tutorials page. GPIO Hardware & Software Tutorials: GPIO Protection Circuits Basic Circuits: LED output - DC motor - Switch Input - Analogue Sensor Intermediate Circuits: Shift Registers - Analogue to Digital - Driving Circuit - Alpha-Numeric Display Advanced Circuits: Control Hardware Over The Internet Warnings While most of these circuits may interface directly to the RPi, the use of a buffered interface (such as the one supplied by the Gertboard) is recommended which will help protect against damage. Alternatively, RPi Tutori al EGHS: LED output - eLinux.org http://elinux.org/RPi Tutorial EGHS: LED output 1 sur 8 11/8/2013 12:19

TutorialEGHS_LEDpdf.pdf

Embed Size (px)

Citation preview

7/27/2019 TutorialEGHS_LEDpdf.pdf

http://slidepdf.com/reader/full/tutorialeghsledpdfpdf 1/8

Contents

1 Warnings

2 Aims

3 The Hardware

3.1 Theory

3.2 Circuit 1 - Basic LED Driving Circuit

3.2.1 Basic LEDx8 Test Module

3.3 Circuit 2 - LED Driving Circuit (using Transistor Switching

Circuit)

3.3.1 Transistor Selection

3.3.2 Calculating R1 - LED Current Limiting Resistor 

3.3.3 Calculating R2 - Transistor Base Resistor 

3.3.3.1 R2max - Based on driving current requirement

(Ic)

3.3.3.2 R2min - Based on the GPIO pin source current

3.3.4 Testing

4 The Software

4.1 TI LaunchPad

4.2 RPi

5 References

RPi Tutorial EGHS:LED output

From eLinux.org

Back to the Hub, or the Tutorials page.

GPIO Hardware & Software Tutorials:

GPIO Protection Circuits

Basic Circuits: LED output - DC motor - Switch Input - Analogue Sensor 

Intermediate Circuits: Shift Registers - Analogue to Digital - Driving

Circuit - Alpha-Numeric Display

Advanced Circuits: Control Hardware Over The Internet

Warnings

While most of these circuits may interface directly to the RPi, the use of a

buffered interface (such as the one supplied by the Gertboard) is

recommended which will help protect against damage. Alternatively,

i Tutorial EGHS:LED output - eLinux.org http://elinux.org/RPi_Tutorial_EGHS:LED_output

ur 8 11/8/2013 12:19

7/27/2019 TutorialEGHS_LEDpdf.pdf

http://slidepdf.com/reader/full/tutorialeghsledpdfpdf 2/8

experiment with one of the Alternative Test Platforms.

Extreme caution should be exercised when interfacing hardware at a low level,

you may damage your RPi, your equipment and potentially yourself and others.

Doing so is at your own risk!

Aims

The purpose of this guide is to enable control of an LED via the GPIO pins of 

the RPi.

This is the embedded version of writing a program to display "Hello World" and

is the first step in getting started.

The first stage will be to build the hardware we are going to use, and then we

shall look at the software which will drive it.

Note:

Until RPi devices are available, I can not confirm this will work on a real RPi.

For now, I shall be using the TI LaunchPad (see Alternative Test Platforms

for details) to test the hardware on (as it is cheap and the logic levels similar).

The Hardware

Theory 

This is only a brief and rough overview, since the basics are covered in a lotmore detail in many other places (see below).

The GPIO pins on the RPi when defined as an Output is able to cause the

voltage on the pin to go HIGH (source) or LOW (sink). This allows signals to be

sent to other processors and devices like LEDs. However it is important to

remember that the pin will only be able to Source or Sink very small currents,

so higher powered devices (such as motors) can not be driven directly from a

GPIO pin.

NOTE:Depending on the specification of the RPi GPIO pins, the current SOURCE ability may be better,

than the SINK (or vice-a-versa).

i.e. If the RPi is able to SINK more current than it can SOURCE, then any driving circuit should

be between the RPi 3.3V pin and the GPIO pin (rather than GPIO pin and GND).

For additional detail theory see  [1]

Circuit 1 - Basic LED Driving Circuit

The resistor R1 is used to limit the current going through the LED (which has

hardly any resistance), without the resistor, the LED will draw as muchcurrent as it can until it burns out (or burns out your GPIO pin).

The value you select for R1 will depend on the current required by the LED

(upto 20mA depending on the LED used - check the datasheet) and the source

i Tutorial EGHS:LED output - eLinux.org http://elinux.org/RPi_Tutorial_EGHS:LED_output

ur 8 11/8/2013 12:19

7/27/2019 TutorialEGHS_LEDpdf.pdf

http://slidepdf.com/reader/full/tutorialeghsledpdfpdf 3/8

Basic LED

Output

Circuit 1

Basic LEDx8 Test Module

current limit of the GPIO (launchpad is ~20mA), the RPi has a

50mA limit for the 3.3V supply line.

We also need to know the forward voltage required by the LED

to light, typically around 2V-3.5V depending on colour [2].

Finally, the output voltage of the RPi (and LaunchPad) GPIO is

3.3V output level.

Vout = 3.3V

Vled = 2V (I'm using RED)

Iled = 5mA = 0.005A

R1 = (Vout – Vled)/Iled

= (3.3 - 2)/0.005

= 260ohms (so 270ohms is closest preferred value)

If in doubt, use a bigger resistor (=less current & less brightness) and test if 

good enough by connecting across the 3.3V and ground pins (if you are just

experimenting you are unlikely to need LEDs shining at their full brightness

anyway).

For instance, one of my test circuits uses 470ohms (which only gives 2.7mA on 3.3V, but the samecircuit can be connected to a 12V supply without blowing the LED - rated @20mA).

Basic LEDx8 Test Module

Basic LEDx8 Test Module (Control pins at top, GND connection at bottom-

right).

Circuit with track breaks

marked in red

The above test module has been built to allow easy testing of GPIO outputs by

driving up to 8 LEDs. The resistor value 330 ohms is used (keeps the current

draw fairly low).

i Tutorial EGHS:LED output - eLinux.org http://elinux.org/RPi_Tutorial_EGHS:LED_output

ur 8 11/8/2013 12:19

7/27/2019 TutorialEGHS_LEDpdf.pdf

http://slidepdf.com/reader/full/tutorialeghsledpdfpdf 4/8

Higher Power LED Driving

Circuit

Circuit 2 - LED Driving Circuit (using Transistor Switching Circuit)

For more detailed information about basic 

transistor circuits, some useful information is 

here  [3]  

In order to drive a slightly higher current, the

use of a transistor circuit will be required.Since all the driving current will be drawn

through Vcc and through the transistor, the

RPi 5volt line can be used for Vcc (this will

limit the available current to 1Amp total draw 

from the USB supply itself - including the RPi

draw). The current limit will be the nominal

current the transistor can handle.

Note: Vcc = 5V in this example, but the 3V3 supply can be used too,

although remember that the 3V3 should be not used to supply over

120mA in total.

Transistor Selection

This circuit requires an NPN transistor. This circuit is not suitable for other 

types of transistor (example PNP, FET).

There is a huge range of transistors available, so I will pick a common & cheap

one (BC548 (http://uk.farnell.com/fairchild-semiconductor/bc548/transistor-

npn-to-92/dp/1467872) or BC108 (http://uk.farnell.com/multicomp/bc108

/transistor-npn-to-18/dp/9206736)) and see how well it suits.

More details on specific transistors see  [4]  

The key characteristics of interest are:

maximum collector current Ic(max) : 100mA [BC548], 200mA [BC108]

minimum current gain hFE(min) : 110

Suggested hFE:

hFE(min) > 5 x (Iload/Iinput)

We assume we want to draw a very low current from the RPi GPIO,

so even with an hFE=110 and drawing only 5mA we can drive 110mA

(over BC548's Ic(max) limit anyway).

The current transistor I have available at the moment is (ZTX 653

(http://uk.farnell.com/diodes-inc/ztx653/transistor-npn-e-line/dp/9525580)):

maximum collector current Ic(max) : 2A

minimum current gain hFE(min) : 100

The use of a transistor allows the bulk of the driving current to pass through

i Tutorial EGHS:LED output - eLinux.org http://elinux.org/RPi_Tutorial_EGHS:LED_output

ur 8 11/8/2013 12:19

7/27/2019 TutorialEGHS_LEDpdf.pdf

http://slidepdf.com/reader/full/tutorialeghsledpdfpdf 5/8

the transistor to ground, with only a small switching current required to be

driven from the GPIO pin. For low powered driving circuits, most transistors

will be suitable.

Transistors do have a limited amount of current handling ability, which can be

improved by coupling together as a Darlington pair 

(http://www.kpsec.freeuk.com/components/tran.htm#darlington) (often

available in a single package). Also higher powered switches such as mosfets,and even relays can be driven for higher power requirements.

Calculating R1 - LED Current Limiting Resistor 

The value of R1 is similar to before, but since the driving voltage is higher, the

same resister will allow more current, thus the LED will be brighter (unless we

use a larger resistor).

When the transistor is on the voltage drop is minimal VCE(sat)(90-200mV), sowe will just consider the LED voltage drop.

Vcc = 5V

Vled = 2V (I'm using RED)

Iled = 5mA = 0.005A

R1 = (Vcc – Vled)/Iled

= (5 - 2)/0.005

= 600ohms (so 560ohm or 680ohm will probably be fine)

Calculating R2 - Transistor Base Resistor 

The value of R2 can be determined, as follows:

There seems to be two possible ways to approach this, one is to work out your 

required driving current through the collector (Ic) i.e. the driving current of the

LED, and the other is to determine it's value from the recommended source

current of the GPIO pin (for the RPi, 5mA or less is recommended).

The latter makes most sense to me, but will try both and see how they

compare.

R2max - Based on driving current requirement (Ic)

Vc = 3.3V

hFE = 110

Ic = 100mA = 0.1A (may as well aim for full load)

R2 = (Vc x hFE) / (5 x Ic)

= (3.3 x 110) / (5 x 0.1)

= 726ohms

Although, since we don't need 100mA this resistor can probably be far larger.

i Tutorial EGHS:LED output - eLinux.org http://elinux.org/RPi_Tutorial_EGHS:LED_output

ur 8 11/8/2013 12:19

7/27/2019 TutorialEGHS_LEDpdf.pdf

http://slidepdf.com/reader/full/tutorialeghsledpdfpdf 6/8

LED

Transistor 

Circuit Test

R2min - Based on the GPIO pin source current

To fully switch on the transistor, most transistors require (Vbe) Base Emitter 

Turn-On Voltage to be around 700mV on the base (it depends slightly on the

type, see it's data sheet).

Vgpio = 3.3V

Iout = 5mA = 0.005

Vbe Base Emitter Turn-On Voltage = 0.7V

R2 = (Vgpio - Vbe) / Iout

= (3.3 - 0.7) / (0.005)

= 520ohms (so 560ohm nearest value)

(For reference, if a Darlington pair was used,

Vbe would be 1.4V (effectively driving two

transistors, so R2 would be 380ohms)).

Testing

We can test our calculated values by using a simple prototype circuit, and

compare an LED driven directly and through the (ZTX 653 (http://uk.farnell.com/diodes-inc/ztx653/transistor-npn-e-line/dp/9525580)) transistor.

In the following circuit, Vcc = 5V (main supply voltage - red wires) and Vgpio =

3.3V (representing the GPIO output - bottom blue wire) is connected to the

transistor base through R2 (560ohm) resistor.

There is very little difference between the LED brightness*, and

even when the transistor base is connected directly to 3.3V

(Vgpio) there is no change(indicating that the transistor is

saturated i.e. fully turned on).

*Note, there is slight difference due to the current drawn by the transistor itself.

Also, the LED test circuit is used from before, rather than the calculated R1.

I suggest by using a combination of these two calculations you

will obtain, a max value (R2max - Based on driving current

requirement (Ic)) and a min value (R2min - Based on the GPIO

pin source current) for R2.

Generally the larger the R2 is the less current will be drawn from the GPIO pin,however less current will be available through the transistor for the load (Ic) if 

R2 is too large.

The Software

While the RPi is not available, I can only confirm the TI LaunchPad code works

for me.

TI LaunchPad

Sample test code for Basic LEDx8 Test Module (tested on TI MSP430G2553

device).

i Tutorial EGHS:LED output - eLinux.org http://elinux.org/RPi_Tutorial_EGHS:LED_output

ur 8 11/8/2013 12:19

7/27/2019 TutorialEGHS_LEDpdf.pdf

http://slidepdf.com/reader/full/tutorialeghsledpdfpdf 7/8

 

Basic LEDx8 Test Module input pins 0-7 wired to device Port1:0 to Port1:7, plus

GND connection.

Code:

hwmap_ledx8module.h (http://www.pastebucket.com/1455) - Hardware

Mapping File (LED0 to LED7 mapped to GPIO Port1:0 to Port1:7)

main.c (http://www.pastebucket.com/1452) - Main calling functions

ledx8module.c (http://www.pastebucket.com/1453) - Sample routines

ledx8module.h (http://www.pastebucket.com/1454) - Mapping LED types

etc

RPi

The above circuits should work with code similar to that given in (RPi 

Low-level peripherals#Code examples) section.

References

↑ Introduction To Embedded Programming - GPIO Output

(http://www.scriptoriumdesigns.com/embedded/gpio_out.php)

1.

↑ Wikipedia:LED Forward Voltages (http://en.wikipedia.org

/wiki/LED#Colors_and_materials)

2.

↑ The Electronics Club:Transistor Circuits (http://www.kpsec.freeuk.com

/trancirc.htm).

3.

↑ The Electronics Club:Transistor (http://www.kpsec.freeuk.com

/components/tran.htm)

4.

Raspberry Pi

Startup

Buying Guide - SD Card Setup - Basic Setup -

Advanced Setup - Beginners Guide -

Troubleshooting

HardwareHardware - Hardware History - Low-level

peripherals - Expansion Boards

Peripherals Screens - Cases - Other Peripherals

SoftwareSoftware - Distributions - Kernel - Performance

- Programming - VideoCore APIs - Utilities

ProjectsTutorials - Guides - Projects - Tasks -

DataSheets - Education - Communities

Retrieved from "http://elinux.org

/index.php?title=RPi_Tutorial_EGHS:LED_output&oldid=198878"

Category: RaspberryPi

i Tutorial EGHS:LED output - eLinux.org http://elinux.org/RPi_Tutorial_EGHS:LED_output

ur 8 11/8/2013 12:19

7/27/2019 TutorialEGHS_LEDpdf.pdf

http://slidepdf.com/reader/full/tutorialeghsledpdfpdf 8/8

This page was last modified on 8 December 2012, at 09:06.

This page has been accessed 48,798 times.

Content is available under a Creative Commons Attribution-ShareAlike 3.0

Unported License.

i Tutorial EGHS:LED output - eLinux.org http://elinux.org/RPi_Tutorial_EGHS:LED_output