HiggleBottom
Posts: 21
Joined: Sun Apr 22, 2012 7:09 pm

Re: Adding a light to GPIO pins

Mon Apr 23, 2012 7:34 am

Anyone know how to connect a light to GPIO pins on the Pi?

User avatar
croston
Posts: 703
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Adding a light to GPIO pins

Mon Apr 23, 2012 7:46 am

I used a 4.7k resistor then an LED to ground (0v). For a pinout, look here: http://elinux.org/RPi_Low-leve.....eripherals

Warning! Some of the pins go straight into the SOC with no buffer for safety. Get it wrong and you could brick your RPi! I recommend you invest in a lead with IDC connectors to prevent accidental shorting of the pins. I speak from experience!

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12238
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Adding a light to GPIO pins

Mon Apr 23, 2012 9:46 pm

Have a look at this:

http://www.designspark.com/con.....nds-review

Most GPIO pins of SoC's can drive much more current low than high, that is when the GPIO drives a pin low it does it with more "force", than when driving it high.

So its better to connect a resistor to the power supply, then drive it low with a LED in series, than to tie the resistor to GND, then try to drive it high with a LED in series.

If you drive the led in the first way, you can send several milliamperes through it, if you do it the second way, only a fraction of a milliampere.

In Crostons example there will only flow (when using a typical red LED, which has a forward voltage of about 2 Volt) about 0.3mA through the LED, only the most efficient LED's will work, and then still be quite faint. With the method shown in the above link (470E tied to 3V3) and also a red LED there will flow about ten times that, or 3mA, which is a safe value, and will make a LED of normal efficiency shine okay, and a high efficiency LED shine bright.

HiggleBottom
Posts: 21
Joined: Sun Apr 22, 2012 7:09 pm

Re: Adding a light to GPIO pins

Mon Apr 23, 2012 9:51 pm

I have no idea what any of those thing mean! Sry. I forgot to say I am kind of a beginner at GPIO and stuff like that.

User avatar
Gert van Loo
Posts: 2485
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Adding a light to GPIO pins

Tue Apr 24, 2012 4:58 am

croston said:


I used a 4.7k resistor then an LED to ground (0v). For a pinout, look here: http://elinux.org/RPi_Low-leve.....eripherals

Warning! Some of the pins go straight into the SOC with no buffer for safety. Get it wrong and you could brick your RPi! I recommend you invest in a lead with IDC connectors to prevent accidental shorting of the pins. I speak from experience!



By the way: The resistor in the picture is 470 Ohms. Not 4.7K. I would normally not be nit-picking but with 4.7K your LED might be too dim to see (especially if it is an old-fashion low intensity one) which would be big disappointment for whoever tries it and wonders why it does not work.

User avatar
rew
Posts: 423
Joined: Fri Aug 26, 2011 3:25 pm

Re: Adding a light to GPIO pins

Tue Apr 24, 2012 1:55 pm

However, for a modern high efficiency RED led, the 4k7 is about right.

The RED power led is IMHO too bright with a 1k current limiting resistor.

One thing to keep in mind is that the Broadcom chip probably does not have the most powerful gpio pin drivers. While it is easily possible to drive a 20mA led from an Atmel AVR, everybody else has output drivers that cannot handle such currents. I would recommend limiting the current to about 3mA max, preferably < 1.5mA.

With an 1k resistor you'd get about 1.5mA with a red led.
Check out our raspberry pi addons: https://www.bitwizard.nl/shop/

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12238
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Adding a light to GPIO pins

Tue Apr 24, 2012 2:22 pm

HiggleBottom said:


I have no idea what any of those thing mean! Sry. I forgot to say I am kind of a beginner at GPIO and stuff like that.



Hmm, it can indeed be a bit perplexing, how about this:

A GPIO (used as an output, it can also be programmed to act as in input but that is another story) has two output transistors. One output transistor is used to "lift" the voltage on the output to the level of the power supply (3,3 Volt), the other transistor is used to pull the voltage on the output pin down to ground. Obviously the two transistors are never turned on at the same time. Such a construction is common, and is referred to as a push-pull construction.

As it happens the transistor that can pull the voltage on the pin down is stronger (physically larger) than the "pullup" transistor, and thus is able to conduct more current.

This is true in many devices that have GPIO pins.

In case of the Raspberry PI, an electronic engineer called "Gert", has revealed that the large (pulldown) transistor can handle several mA, while the smaller (pullup) transistor only handles a fraction of a mA.

It just means that for a bright LED its better to use the larger (pulldown) transistor.

To do that you connect the LED, with a suitable series resistor, between the power supply and the GPIO pin.

User avatar
rew
Posts: 423
Joined: Fri Aug 26, 2011 3:25 pm

Re: Adding a light to GPIO pins

Wed Apr 25, 2012 8:09 am

mahjongg said:


As it happens the transistor that can pull the voltage on the pin down is stronger (physically larger) than the "pullup" transistor, and thus is able to conduct more current.


No. that's not true. In fact it's even likely to be physically smaller than the other one.  The thing is: it is an N-MOSFET. The other one is a P-MOSFET. The N-MOSFET works on electrons moving. The electrons are more "mobile" in the semiconductor than P-charges, so N-mosfets and NPN transistors can be made more easily and efficient than their P-counterparts.

Manufacturers would have to go pretty asymmetric (physically) to get both "drive strengths" the same. Atmel is the only one that I know of that does this.
Check out our raspberry pi addons: https://www.bitwizard.nl/shop/

User avatar
Gert van Loo
Posts: 2485
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Adding a light to GPIO pins

Wed Apr 25, 2012 10:16 am


In case of the Raspberry PI, an electronic engineer called "Gert", has revealed that the large (pulldown) transistor can handle several mA, while the smaller (pullup) transistor only handles a fraction of a mA.


Sorry but that is not true. I never said that but that is not the important part. The asymmetrical drive strength (pull down is more powerfull then pull up) is no longer true. The CMOS transistors in the pads are almost symmetrical these days.

There are still reasons why sometimes you might specify a higher sink current then a source current but that has more to do with power nets, power droop and ground bounce. We are now getting waaaaay beyond the amateur status.

And nobody has told our dear novice yet that LED 's work only in one direction. The long pin has to be connected to the higher voltage. So you do

GPIO----[resistor]---long pin [LED] short pin ---- ground

Or

3V3 ---[resistor]---long pin [LED] short pin ---- GPIO

To complete our lecture: you can swap the LED and resistor if you like.

User avatar
meltwater
Posts: 1014
Joined: Tue Oct 18, 2011 11:38 am

Re: Adding a light to GPIO pins

Wed Apr 25, 2012 7:31 pm

Some info for you (the whole reason I've been writing it, gpio and hardware gets complicated very fast)!

http://elinux.org/RPi_Tutorial.....6_Software

It is in the development stages at the moment, so treat with caution and care (just like you should your RPi).  Feedback/corrections are welcome!
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12238
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Adding a light to GPIO pins

Wed Apr 25, 2012 9:17 pm

Rew & Gert,

Yes, I was maybe oversimplifying things with my "larger and smaller" transistors, but discussing electron and hole mobility is a bit too advanced for a person asking how to connect a LED to a GPIO port, so I "simplified" the mental model a bit.

However, I was unaware that asymmetrical drive strength is no longer true (for modern CMOS devices).

A lot of people are wondering what the safe pulldown and pullup current limits are for the GPIO pins of this Broadcom SoC. Perhaps Gert can make some competent declaration about this subject.

User avatar
Gert van Loo
Posts: 2485
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Adding a light to GPIO pins

Thu Apr 26, 2012 7:37 am

There is not really a safe pulldown / pullup current. The drive strength of the pads is programmable between 2 and 16 mA. (That is not in the current data sheet but as so many people want to know about the GPIOs I will produce an addendum).

The real issue is with SSO which stands for Simultaneous Switching Outputs. I can write a long lecture but I assume there is enough information about that on the WWW so I suggest you look it up there. Basically you can draw 16mA from one GPIO but you can't from all GPIO's and even less if you switch multiple at the same time. (Just as an example: To drive all 53 GPIOs with 16mA would require 864mA. More then the whole board uses at the moment.)

User avatar
meltwater
Posts: 1014
Joined: Tue Oct 18, 2011 11:38 am

Re: Adding a light to GPIO pins

Thu Apr 26, 2012 7:56 am

Gert said:


There is not really a safe pulldown / pullup current. The drive strength of the pads is programmable between 2 and 16 mA. (That is not in the current data sheet but as so many people want to know about the GPIOs I will produce an addendum).

The real issue is with SSO which stands for Simultaneous Switching Outputs. I can write a long lecture but I assume there is enough information about that on the WWW so I suggest you look it up there. Basically you can draw 16mA from one GPIO but you can't from all GPIO's and even less if you switch multiple at the same time. (Just as an example: To drive all 53 GPIOs with 16mA would require 864mA. More then the whole board uses at the moment.)


SSO…Imagine you have kids bouncing on your bed while you are trying to read a book.  The higher they bounce (larger the current) and the more often they do it (switching speed) the less chance you'll be able to read in peace (due to bounce and noise).  Add a few more kids (extra channels) and it gets harder still.

- that is my current understanding on it anyway…

As Gert has mentioned previously, this is just down to having everything so small and tiny packed within such a small package.  If you design to use small currents (by using additional driving components), then this shouldn't become an issue (they can bounce on the bed next door instead!).

Thanks Gert for the info…I know this is something a lot of people keep asking, and it gets lost in the forum posts.  I'll try to add some thing to the wiki pages when I get some time.

Is the info for setting these driving levels in the datasheet, do you have any code snippets which show it?  Thanks.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam

User avatar
Gert van Loo
Posts: 2485
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Adding a light to GPIO pins

Thu Apr 26, 2012 10:13 am

Ok here is the document about how to control the GPIO pads:



As always beware of what your are doing. e.g. playing with the GPIO bank which connects to the SD-card can well render your PI dead  and require a reset.

User avatar
meltwater
Posts: 1014
Joined: Tue Oct 18, 2011 11:38 am

Re: Adding a light to GPIO pins

Thu Apr 26, 2012 10:35 am

Thank you very much!

Added to the wiki document section and the GPIO section, fingers crossed people will read it.  I've been working on aiming for about 6mA in most of my test circuits on the TI Launchpad.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam

GizmoB73
Posts: 80
Joined: Fri Jan 27, 2012 12:51 pm

Re: Adding a light to GPIO pins

Thu Apr 26, 2012 11:05 am

Here's some code for it...

Using the simple

GPIO—-[resistor]—long pin [LED] short pin —- ground

circuit from Gerts post a few days backs, I have hacked together some very simple demo code, based heavily on the stuff from Dom and Gert.

This code is focussed on setting one of the GPIO's on the R-PI (0 to 7) to high and low so that it could actually switch an LED on and off as per the original post.

I have tested and run this myself on my Pi and it works fine.

I hope that if this does actually post up (without being butchered by the BB), that someone will find it useful.

//
//  How to write an output to GPIO pins 0-7 from C-code on the Raspberry-Pi
//  Example program
//  26-April-2012
//  Gary Bierton
//  Based very heavily on code from Dom and Gert
//

// Access from ARM Running Linux

#define BCM2708_PERI_BASE        0x20000000
#define GPIO_BASE                (BCM2708_PERI_BASE + 0x200000) /* GPIO controller  */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <unistd.h>

#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)

int  mem_fd;
char *gpio_mem;
unsigned char *gpio_map;
char *spi0_mem, *spi0_map;

// I/O access
volatile unsigned *gpio;

// Translate GPIO Pins on the R-PI board to those on the BCM2835 SoC
#define GPIO_PIN_0 0x11        // BCM2835 GPIO 17
#define GPIO_PIN_1 0x12        // BCM2835 GPIO 18
#define GPIO_PIN_2 0x15        // BCM2835 GPIO 21
#define GPIO_PIN_3 0x16        // BCM2835 GPIO 22
#define GPIO_PIN_4 0x17        // BCM2835 GPIO 23
#define GPIO_PIN_5 0x18        // BCM2835 GPIO 24
#define GPIO_PIN_6 0x19        // BCM2835 GPIO 25
#define GPIO_PIN_7 0x04        // BCM2835 GPIO 04

// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |=  (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))

#define GPIO_SET *(gpio+7)  // sets   bits which are 1 ignores bits which are 0
#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0

void setup_io();

int main(int argc, char **argv)
{ int rep;

// Set up gpi pointer for direct register access
setup_io();

/************************************************************************\
* You are about to change the GPIO settings of your computer.          *
* Mess this up and it will stop working!                               *
* It might be a good idea to 'sync' before running this program        *
* so at least you still have your code changes written to the SD-card! *
\************************************************************************/

// Set GPIO Pin n to output.
int intGpioPin = GPIO_PIN_7;    // Substitue GPIO_PIN_7 for any of the GPIO pins (0-7) in the definitions above.
INP_GPIO(intGpioPin);            // We have to set to INP before we can set OUT.
OUT_GPIO(intGpioPin);            // Set the GPIO pin as an output so that we can write to it.

// Toggle the selected GPIO Pin 5 times.
for (rep=0; rep<5; rep++)
{
GPIO_SET = 1<<intGpioPin;  // Set the GPIO pin high.
sleep(1);                    // Sleep 1 second.
GPIO_CLR = 1<<intGpioPin;  // Set the GPIO pin low.
sleep(1);                    // Sleep 1 second.
}

return 0;
} // main

//
// Set up a memory regions to access GPIO
//
void setup_io()
{
/* open /dev/mem */
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem \n");
exit (-1);
}

/* mmap GPIO */

// Allocate MAP block
if ((gpio_mem = (char *) malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) {
printf("allocation error \n");
exit (-1);
}

// Make sure pointer is on 4K boundary
if ((unsigned long)gpio_mem % PAGE_SIZE)
gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE);

// Now map it
gpio_map = (unsigned char *)mmap(
(caddr_t)gpio_mem,
BLOCK_SIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_FIXED,
mem_fd,
GPIO_BASE
);

if ((long)gpio_map < 0) {
printf("mmap error %d\n", (int)gpio_map);
exit (-1);
}

// Always use volatile pointer!
gpio = (volatile unsigned *)gpio_map;

} // setup_io

Return to “General discussion”