Before we even get started with the GPIO, lets make an LED light up by simply wiring it to the +3.3v supply and 0v.
So… We have a yellow wire from the Pi’s +3.3v supply to the breadboard and this connects to an LED then via a 270Ω (ohm) resistor to 0v. The green wire connects back to the Pi.
(Note that here and in the following pages, the Fritzing breadboard layout is slightly different from the photos – it’s the same circuit, just laid out in a way that makes it easy to see in the images)
Refer to the diagram here to work out the pins we’re using. From this view the 3.3v pin on the GPIO connector is to the top-left, or pin number 1 on the connector.
In electronics terms, our circuit diagram looks like this:
A quick word about the electronics involved. LEDs are Light Emitting Diodes and the diode part is important for us – they only pass electricity one way, so 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. If we’re cut the legs short (as I have done here), then another way is to look at the side of the LED – there will be a flat section. Think of the flat as a minus sign and connect that to the 0v side of the circuit.
If we allow too much current through the LED, it will burn very bright for a very short period of time before it burns out, so we need a resistor to limit the current. Calculating the resistor value is not difficult but for now, just use anything from 270Ω to 330Ω. Anything higher will make the LED dimmer.
So now we have a lit LED. What we really need to do is make it easily turn on and offable – preferably by a command or program running on the Raspberry Pi.
We need to move the yellow wire to one of the programmable GPIO pins. We’ll move it to wiringPi pin 0 (GPIO-17) which is notionally the first user GPIO pin. (It’s physical location is pin 11 on the GPIO connector)
Do check against the wiring diagram to work out which pin on the connector to use. The LED will initially be off because normally the GPIO pins are initialised as inputs at power-on time.
If you haven’t already done so, download and install wiringPi. This will give you some libraries to use in C programs, but also a command-line program which we can use to test the GPIO with.
Type the commands:
gpio mode 0 out gpio write 0 1 gpio write 0 0
If all has gone well, the LED should come on, then go off again. The first gpio command above sets pin 0 to output mode, the 2nd sets pin 0 to the value “1” – logic 1 which puts a 3.3v signal on the pin, and turns the LED on, and the last one turns it off again.
A word about GPIO pin numberings…
t’s often customary to refer to the GPIO pins on a microcontroller by the pin number on the chip originating the signal (or by the internal register name and bit-number). The Arduino system decided that that was complex for newcomers and used a system called wiring which started the pin numbers at zero and worked upwards. This had the advantage of making sure that when they moved to new chips which possibly had different internal configurations, etc. the pin numbers would remain the same. I have adopted this scheme in my wiringPi library for the Raspberry Pi, but I also allow for the native GPIO numbering scheme too. You will probably see the GPIO numbering scheme elsewhere though, so I’ll give examples using both schemes.
If you look at the pins chart, then it gives both values. You can see that wiringPi pin 0 is GPIO-17. wiringPi pin 1 is GPIO-18, wiringPi pin 2 is GPIO-21 an so on. To use the GPIO pin numbering then you need to pass the -g flag to the gpio program:
gpio -g write 17 1 gpio -g write 17 0
This should turn the LED on then off again.
And on to part 2 where we’ll add in 2 more LEDs.
Hi Mr.
firstly thank you so much for those great lectures about RPi, I just loaded wiringPi, manage to get blink.c work.
But when I am attempting to type gpio command from command line (it`s LXTerminal?) it says : bash : gpio : command not found error
I just tried to change dierctories and try again but failed, I am asking for if you have any idea to fix it,
thanks in advance
Did you remember to do the
sudo make install
in the gpio directory?
That should put the executable file in /usr/local/bin. See if it’s there with
ls -l /usr/local/bin/gpio
If that’s fine, then you might want to inspect your PATH variable to see if it includes /usr/local/bin in it.
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
$ which gpio
/usr/local/bin/gpio
$ ls -l /usr/local/bin/gpio
-rwsr-xr-x 1 root root 15866 Jul 6 10:47 /usr/local/bin/gpio
-Gordon
Actually I follow the notes written on (http://log.liminastudio.com/writing/tutorials/tutorial-how-to-use-your-raspberry-pi-like-an-arduino) to download and install , then this one does not include the installation part after first sudo make install (now I recognized)
So, what should we do after this first `sudo make install` ?
Then I type cd /sys/class/gpio where gpio lies in, and type make but it gives an error saying `make. *** no targets specified and no makefile found. Stop`
Is there any other gpio dir available?
If youy start here:
https://projects.drogon.net/raspberry-pi/wiringpi/
Then it ought to have all the info you need, or fast forward to:
https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/
it has all the download and install information…
I’ll drop a note to the author of that site to make sure it’s up to date!
however, after doing the sudo make install in the wiringPi direcytory, then the next thing to do is compile your own program – wiringPi by default doesn’t use any of the /sys/class/gpio stuff, so looking there won’t really help you.
-Gordon
Basically, the point I can not get is ;
I don’t know where gpio directory is, I can not understand what this … means. The same is available for …example directory.
After doing first sudo make install , what things we should put :
cd …/gpio to compile your own program, I feel it`s a weird question but I just get stuck on that. I refer to sys/class/gpio because it`s the only gpio directory that I know available.
thank you so much for your kind and patient answers Mr Gordon
Have sent you an email..
-Gordon
Any chance I could have a copy of that e-mail you sent to Mert. I have the exact same issue.
Thanks.
Hard to work out what you’re replying to (maybe I need to start a forum!)
Just detail the issue here, or email me and I’ll see if I can get you sorted out,
-Gordon
Hi,
thank you very much for your nice tutorial! 🙂
But i stuck a little bit at my first LED…i installed all your libs and can turn on and off the LED with the “gpio” command. Thats great, but the LED lights up just a bit and not with 100% power…whats wrong? 🙁
If the LED is lighting up and you can turn it on and off, then I’d look at the resistor your using. What value is it? Ideally it should be somewhere between 220 and 330 ohms – a higher value will also work, but the higher the value the dimmer the LED will be.
If you’re not sure about the colour codes, then wikipedia has a good article on it: http://en.wikipedia.org/wiki/Resistor_colour_codes
-Gordon
Oh yes, you are right! 🙂
I have 2 reistors with higher values…now i tried the lower one and it works fine. Thank you very much.
Thanks for the post, it’s been really helpful. Just one query,
“We need to move the yellow wire to one of the programmable GPIO pins.”
Do I need to turn off my RPi to do this?
If I was saying anything in any sort of official capacity, then I’d say “Yes, turn the Pi Off”, but since I’m not saying anything in an official capacity, then sure, just move it…
Just take care. I rarely turn my Pi’s off when re-wiring them, but I have managed to reboot them – presumably by shorting something out and so-far I see to have “gotten away” with it… Connecting that wire going to the LED to any pin is highly unlikely to have any issues though. Connect it to the +5V and the LED will be a little brighter, connect it to a pin configured as an input, or 0V and nothing will happen.
-Gordon
Can you tell me please, what’s the technical name of the brown single pin connector you have used to connect to the pi please? I’ve been trying to find where to buy some but not been successful. I’m probably just not googling the right things.
How much current can you draw off the GPIO pin? I’ve seen another design where they used a transistor to drive the LED rather than driving it straight off the pin, I assume because they’re concerned about the current draw. I guess you are drawing about 10mA in this case if I have done my sums right. Is that the maximum limit?
Thanks
Charles
Trying to work out what’s brown there – green maybe? I bought a load of male to female jumper leads off here a while back, but they seem out of stock: http://www.skpang.co.uk/catalog/jumper-wires-premium-150mm-mf-pack-of-10-p-909.html
although they’re still selling the stater kits which contain a bunch of these leads. (as well as the usual male to male jumper leads for use with a normal breadboard)
As for current – don’t exceede 16mA on a single pin – I have driven 17LEDs off the GPIO before – see http://unicorn.drogon.net/pi17leds.jpg
Each of those is taking about 10mA, so 170mA in total – but I’m told that slighty exceeds the 3.3v supply, so probably not a good thing to do!
-Gordon
Thanks for the reply, that’s exactly what I was looking for. “Jumper wire” is the magic search term I needed to find them for sale. Found some F/M ones available on Amazon.
Thanks also for the advice on current draw.
I’ve set up the above test circuit. This works to a degree, but with an unexpected problem.
gpio -g write 17 0 causes the led to light.
gpio -g write 17 1 causes the led to go off.
Also, if I try:
echo “17” > /sys/class/gpio/export
echo “out” > /sys/class/gpio/gpio17/direction
Then:
echo “0” > /sys/class/gpio/gpio17/value lights the led.
echo “1” > /sys/class/gpio/gpio17/value makes the led go off.
I get the same reverse effect with Raspbian “wheezy” and Arch Linux ARM.
Any ideas please, as I’m really stuck at the moment.
Thanks
Dave
My suspicion is that you’ve wired it up the other way round, so I think you’re going:
GPIO -> LED -> Resistor -> +3.3v
rather than
GPIO -> LED -> Resistor -> 0v
Check your connections and try again!
It’s not wrong to wire it up that way, but then everything works the other way round..
-Gordon
Thanks Gordon, that was exactly what I had wrong.
I’ve wired GPIO -> LED -> Resistor -> 0v and it works as expected now.
Regards
Dave
Great! always the simple ones that you trip over 🙂
-Gordon
Hi Gordon,
When i add the resistor to the circuit the LED will not light. Im using an LED from a computer case (the power on one!) and its apparently rated at 3.3v. Does this mean i do not need a resistor?
It’s possible. I know some LEDs do have built-in resistors – especially if they were made to replace an older incandescant bulb … Do you have a multimeter? If so, connect it in series with the LED to the Pi’s +3.3v pin and ground and check the current – if it’s under 15mA then you’ll be fine.
-Gordon
Gordon,
Thanks for the speedy reply. I have double checked the LED and it is actually lit, just very dim. Is it possible the resistor is too … resistant?! I have used a 270 as you said above.
Easiest way to check is with an ammeter if you have one. Probably no harm dropping the value of the resistor though. Try with the LED connected directly to the 3.3v pin and 0v before connecting to a GPIO pin, but really a multimeter will tell you what current it’s drawing.
Do you have any other LEDs – anything in the scrap pile you can salvage?
But it does sound like the LED has an internal resistor to me though.
-Gordon
I don’t fully understand the resistor.
Is it a pull up/down or neither? I am guessing neither as all components are always connected (no switch/transistor?)
The bit that is confusing me.
“If we allow too much current through the LED, it will burn very bright for a very short period of time before it burns out, so we need a resistor to limit the current.”
yet the resistor is on 0v out of the LED.
Does this lower the current draw on the entire circuit?
Yes, the resistor lowers the current.
Think of it as a tap on a water pipe – it lowers the flow rate – imagine filling small bottles from your main tap – turn it on too much and you’ll overfill the bottle and waste water – or potentially break the bottle if it comes out really fast. The resistor is doing a similar thing here. It’s reducing the current through the circuit, keeping it at a safe level for the LED.
-Gordon
So if the resistor is on the other side of the led (3.3v -> res -> led+ -> 0v) why does this reverse everything?
Does the resisitor pull current from the gpio pin (pull it high) and if so does this mean the default led state is on?
It doesn’t actually matter which side of the LED the resistor is on. The only think that’s important here is the way round the LED is. (And if you get it wrong it simply won’t work – you won’t break it)
It’s easier to think of current being pushed – by the supply – in this case the 3.3v pin on the Pi – it will push the current from the 3.3v pin to the 0v or ground pin. In our circuit it will be psuhed via the LED (which will light up) and pushed via the resistor (which will reduce the flow – or slow it down in our water terms)
You really need to go and study some books on elementary electronics …
-Gordon
Yes. Annoyingly i know the above but cant grasp why the resistor placement reverses the logic in code as per your conversation with dave.
Pull up/down reading confused me some what.
I am actually a software dev and also wire up basic stuff (relays/push to make triggers etc), professionally.
I wonder if we’re thinking of different things – as you mentioned pull-up/down… That’s normally used for inputs not outputs. For an output, the position of the resistor doen’t matter, but for an input it might well matter.
An input will connect the input pin to either the +3.3 side or to the 0v side. To stop random noise appearing on the pin, you would normally use a resistor to either +3.3 or 0v. So if you connect the resistor to +3.3 then to the pin, then the pin to a switch going to 0v, then the pin will read high normally, and read low when you push the button.
I’m suspecting you’re reading a few pages ahead (More LEDs and a Button page?) – I’m going by the page that you’re sending the feedback on which is the first page in the 3 pages for the project – thinking you’re going on about the single LED…
-Gordon
My stupidity boils down to me interrupting a previous post.
GPIO -> LED -> Resistor -> +3.3v
rather than
GPIO -> LED -> Resistor -> 0v
I interpreted it as the resistor had changed position but it hasn’t, Coupled with my reading on pull ups/downs, i lost it.
Hope it’s sorted now though!
You can connect gpio – LED/Resistor – 3.3v that works ok, but its not my prefered way to do it.
-Gordon
Thanks for your time and help.
Hi Gordon,
Great post. Quick question – if my circuit board had the same set-up except it had its own power (2 x 1.5v batteries). Could the pi act as just a switch by completing the circuit (switch LED on/off) between 2 pins?
It could do – the Pi’s GPIO pins can sink up to 16mA, so by connecting the 0v lines together then you can use the Pi to control the LEDs, however it might be prudent to use something like an open-connector output type of arrangement to buffer the 2 supplies – e.g. uln2803 or simlar…
-Gordon
Hello,
Everything seems to work fine. But the LED is always on. Even if I put GPIO 17 on low. Which I can check with the command “gpio readall”. What can be the problem? I did set the GPIO 17 as an output.
Thanks
Make sure you use the -g command if you’re using the BCM_GPIO numbering scheme. From my diagram on that page you can use
either
gpio -g mode 17 out
gpio -g write 17 1
or
gpio mode 0 out
gpio write 0 1
-Gordon
Thank you for such a well laid out tutorial. Once again thank you!
Hi Gordon
First of all thank for your well explained and detailed tutorials.
I’d like to learn more about circuit making so I was wondering if you could point me to some site/book/tutorial about the theory and basics behind electronic and circuits.
Tanks
Alessandro
Difficult for me to make a suggestion here – my biggest issue is that I started doing this some 35+ years ago, so have just built it up along the way (along with spending 8 years in a university too!)
What there is a lot of though is introductions to electronics based on the Arduino platform now – that’s quite a well established platform and the electronics principles are the same (although the Pi is 3.3v and the Arduino is typically 5v) and just keep on googling!
-Gordon
Hi Gordon, i m a newbie on Linux and RAspberry Pi, and m starting from scratch, i installed WiringPi and dont know where to write these commands ?
gpio mode 0 out
gpio write 0 1
gpio write 0 0
Kindly help me.. Thanks..
You type then into a terminal, so if you have started X, then you need to open an “lxterm” (I think it’s called) and type the commands there. The same place you typed the commands to install wiringPi.
-Gordon
“LXTerminal” thank you very much 🙂
Thank you very much for posting this easy to follow tutorial! Helps those of us getting started find our bearings. Looking forward to more.
Hi Gordon, I would like to ask one more thing, i have got 16 LEDs on my breadboard, i want to blink them in a specific pattern though the GPIO pins line by line and dont want to use the Wiring pi but use the ‘gpio -g write n 1’ and stuff. which file would i need to include instead of #include and how would i use them like in wiring pi we used to do
pinMode(n, OUTPUT); and digitalWrite(n, 1); Thanks..
If you’re using the gpio command, then the easiest way to do it is in a bash script, but if you want to do it in a C program (or other language), then you can use the system() function – which executes a command from inside your program.
So:
#include <stdlib.h>
…
int i ;
char cmd [100] ;
…
for (i = 0 ; i < 16 ; ++i) { sprintf (cmd, "/usr/local/bin/gpio write %d 1", i) ; system (cmd) ; } will work in C. Most other languages have something similar to system() too. However it's terribly innefficient - it will be slow! -Gordon
Your have amazing patience with all us “idiots”.
Having major problems reading colours on small resisters from Skpang starter kit A. I thought they were wrongly supplied but then realized I was reading orange as red and now they make sense
Any practical advice how to identify colours on such small components.
A good ‘daylight’ desk lamp and a magnifier. (Which is what I use)
And a multimeter when all else fails…
-Gordon
It is very helpful procedure. Just keep posting an article like this so that it is easy to us people who have no idea about Raspberry Pi on how to get information like this.Keep up the good work.
hy sir could you help me how to stream video using raspberry pi using wifi dongle?? please help meeee 🙁
Hi,
It’s not my area of expertiese I’m afraid. I’ve really no idea where to start, other than google…
-Gordon
Thank you Mr Gordon for your excellent tutorial. I love the way you have written and illustrated it.
Just to say thanks for the circuit details, photo’s and diagrams. I’ve just successfully got my LED flashing a treat. Using RISCos BTW
Muchas gracias. Ha funcionado perfectamente.
MAnuel Santos, MADRID
Weird problem here…
When i try the commands without anything connected to the GPIO it seems to work if i check with the “gpio readall” command. “gpio -g write 17 1” will set pin 17 to high etc.
However, when i connect my LED to ground and pin 17 and then write “gpio -g write 17 1” the pin will still be set to low according to the “gpio readall” command. The mode is set to out.
Wish I knew what the answer was… I’ve just tried it on a Pi here (with a LED + resistor connected) and it seems to work OK:
write 0 – LED off:
+----------+------+--------+------+-------+
| wiringPi | GPIO | Name | Mode | Value |
+----------+------+--------+------+-------+
| 0 | 17 | GPIO 0 | OUT | Low |
write 1 – LED on:
+----------+------+--------+------+-------+
| wiringPi | GPIO | Name | Mode | Value |
+----------+------+--------+------+-------+
| 0 | 17 | GPIO 0 | OUT | High |
-Gordon
Hi Gordon,
I want to get a LED blink on SD card activity. Can you help me on that?
The on-board LED already blinks on SD card activity. It’s the green one next to the Red power LED.
-Gordon
Yes that fine. But I want a LED from GPIO not the on board ones.
Thanks
Hm. Not sure really – it might be possible to change the trigger LED for the mmc action, but I really don’t know how.
-Gordon
I’m after the exact same thing. A power LED is easy (GPIO 2/6) but I’d really like to get an activity light wired to the exterior of the case I’m building.
This might be a stupid question, but I am building a similar circuit as an example for my kids who wanted to know how traffic lights worked. After selecting the LEDs, I wanted to calculate the resistor value for myself (mostly for my own learning purposes but also to double check), and given a red LED @ 2V 30mA which seems common on all the sites I looked at I got a resistor value of 47ohm, based on “R = (Vs – Vl) / I” -> “43.33 = (3.3 – 2.0) / 0.03” and rounding up to the nearest standard resistor value.
How is that so different from the 270ohm you are using? Significantly different LED characteristics or just going for a much safer value to increase the life of the LED? If the latter is there any specific rule or guide you followed to get to that value, or just trial and error to see what’s bright enough without having to get near the theoretical limit?
Thanks!
I made it up as I went along…
So once we have that out of the way, we can do it properly…
If your LED drops 2v and needs 30mA then you do indeed need a resistor of 47Ω
However the Pi has a limit of 15mA per GPIO pin, so you’re exceeding the limits… Best pick an LED that needs less current or increase the resistor and accept a dimmer LED.
In general, for the common Red, Green and most Yellow LEDs you can “get away” with a resistor that’s 10x the source voltage – so for 3.3v then you need 330Ω I chose 270 here just to give them a bit more intensity. What you find with the common LEDs (and I don’t mean the fancy blue white, pink or the high intensity ones here) is that that works relatively well and the difference in brightness from it being done properly, to using a bigger resistor is barely noticable to the human eye.
I picked that value based on the LEDs I had to hand when I designed the board – however what I didn’t have at the time was the specification of the blue LEDs – note that they’re much brighter then the other colours – the ones I originally used were far dimmer, so I thought sticking to 270Ω was going to be OK. Ah well.
So although I “made it up”, I knew from experience that it was a relatively safe value, if not the best value. Stick to your calculations, but keep an eye on the Pi’s GPIO current limits.
-Gordon
Hi,
Just a question. Even considering the 15 mA limit, shouldn’t the resistor (for a 3.3V pin) have to be somewhere between 73 Ω and 87 Ω ?
It all depends on the voltage drop the LED has – and what current you actually want to flow through it. Most common red/green/yellow LEDs work well using a resistor that’s 100 times the voltage – so 330 works ok with 3.3v, and so on.
Doing the sums properly, then you can work out the exact resistor needed. So a common red LED probably has a vF of 2.0 volts and wants 20mA for full brightness – but the max we can safely pull is 15mA, so using Ohms law, R = V/I = (3.3 – 2.0) / 0.015 = 86, or 100 ohms which is the nearest standard value.
I used 270 ohms because it was handy and safe should the LED have short circuited. (max. current through 270 ohms at 3.3v is I = V/R = 3.3/270 = 12mA) With a 100 ohm resistor the max. current should the LED short circuit would be 33mA – more than double the recommended limit.
If I was doing this on a commercial project, making 1000’s of devices where I wanted the maximum brightness, then I would do the sums and make sure the drivers are doing their thing correctly, but for hobbyist use here, 270 or 330 is more than OK and errs on the side of caution, also the brightness of an LED driven at full current and half current is really not that noticable. Try an experiment yourself and see!
-Gordon
I followed your LED example but connected a single SPDT relay module instead. The Pi is just driving the input of an optoisolator so no current issues. As a bonus the relay module has an LED so I can determine when it is energized. I don’t have a specific application yet, but this opens up a whole world of possibilities. Thanks for your work on this project.
Hi Gordon,
I’m working on a little project using wiringPi and am having an issue with one pin in particular. I’m using a rev 1 RPi, and am trying to toggle pin 0 (By the GPIO numbering), so the SDA pin. I’m having no problem with any other pin, but this one in particular is throwing a curve at me.
I’m exporting and initializing the pin with the following bash script (Which is the same for all the other pins I’m using):
gpio export 0 out
gpio -g mode 0 out
gpio -g write 0 0
And after running this, gpio readall still shows that pin as being HIGH. If I run the write command again, it stays high. Like I said, every other pin that I’m trying to use is working as expected, but this one is being a jerk.
Any hints or tips?
You don’t need the ‘export’ command unless you are using wiringPiSetupSys() in your program.
Reading a pin in output mode is supposed to tell you the state of the pin, /not/ what you wrote to it, so if something is actually pulling the pin high enough for it to trigger a reading then you’ll read that. Pins 0 & 1 on a Rev 1 have on-board 1.8KΩ pull-ups, so that might be what you’re seeing, but it does seem unlikely.
One solution is to simply not read the pins, but to keep track of what you write.
-Gordon
I am using wiringPiSetupSys, in my program, so that’s why I’ve got the exports in there.
As a bit of context, I have pins 0 and 1 going to pins S0 and S1 of a TI ADC10064 (They are the input selects for the ADC). There isn’t any voltage coming back on those pins, and even when I use a multimeter to check the voltage on the traces, I always see pin 0 as being at 3.3V, while I can toggle pin 1 up and down no problem and see the appropriate change from 0 to 3.3, and back. When I disconnect this pin from the ADC entirely, I still get the same result, with pin 0 always outputting 3.3V, no matter whether I tell it to output high or low.
Essentially I was trying to verify that my multimeter readings were correct. I’ll see if fiddling with the pull-ups does anything useful.
Thanks!
BCM_GPIO pins 0 and 1 (on a Rev 1 board, wiringPi pins 8 and 9) are the I2C Pins on the Pi and these have on-board 1.8KΩ pull-up resistors. Maybe that’s what you’re seeing?
-Gordon
It’s possible, although I would have expected pin 1 to cause just as many problems, but it was working just fine. I ended up switching that connection to pin 21 though, and now everything is working as expected.
Thanks for writing a great library!
Hi Gordon,
Thanks for your awesome tutorial and all your replies. Hopefuly I’m not doubling up here. I’m starting with the static led and resistor across the 3.3v. I could only find a 20kohm ish resistor to start with and when all connected up I got nothing. Then managed to find a 1kohm resistor and got a little flicker as the pi starts up but nothing else.
When I checked with another power supply I get light so I know the circuits all good. As another test I hooked up my multimeter to check the voltage across the 3.3v and ground but I get nothing.
Do you think my problem is too much resistance? Even though it runs from an alternate power supply ok?
All a bit of a mystery to me? Any ideas?
Thanks
Iain
a 1K resistor ought to make a typical small LED light up – but it will be dim.
So make sure you’re connecting it to the right pins. Pin 1 on the P1 connector is the +3.3v supply. If you connect that to the resistor then to the LED then to the 0v line then the LED should light up – as per this page
https://projects.drogon.net/raspberry-pi/gpio-examples/tux-crossing/gpio-examples-1-a-single-led/
(it doesn’t matter if the LED is first or the resistor is first – but it does matter which way round the LED goes – the long leg to the +3.3v side)
-Gordon
Hi Gordon
I tried to read a voltage but it only worked for a fraction of a second
then my Pi made a loud noise and now it smells funny and will not boot.
I’m wondering what I have done wrong.
Perhaps you could comment on my hookup which I have drawn below.
Many thanks
Live 0———–[10 ohms]———— To GPIO16
Ntrl 0——————————– To gnd
Riiight… Bit late for an april fools…
-Gordon
Sorry about the little prank but was reading your comments and I can’t believe the level of patience you have.
Nice articles by the way.
Cheers!
-Gordon
Hi Gordon, Thanks for your great work on WiringPi.
I’ve built a small bash script which uses gpio mode 0 out, gpio write 0 1, gpio write 0 0 etc. and it works fine when I run it as sudo. When I run it as a boot script in /etc/init.d, the script runs, but the gpio commands don’t seem to take effect. I thought that perhaps it was to do with the order my script is loaded, but when I restart my script after boot its just the same.
Am I missing something? Should the gpio command work in a script loaded from init.d?
Thanks for your help.
Rob
Firstly – you do not (and really shouldn’t) use sudo with the gpio command – it’s a set-uid program so doesn’t need it.
Startup files don’t have any path set, so to run them you either need to set the PATH variable to include /usr/local/bin, or explicitly use gpio like /usr/local/bin/gpio
-Gordon
i am just 11 so you might have to be a bit patiant with me so here is my question: i have just started using python for making leds blink on and off . so i can do all the basics like make the led blink sos and put on a little led light show. but now i am working with buttons, i can wire up the button so i can switch the leds on and off manually using the switch but now what i want to do is write a python script so that when i press a button the led will start a pattern or something but i am mainly concertraiting on when i push/pull the button it will change the currunt led on to the one next to it, or doing that with maybe 4 or 5 leds so i can switch back and forth between leds
many thanks
Jake
(p.s please try and be very clear because i am only 11)
: }
Hi Jake,
Unfortunately I don’t program in Python, so you’ll need to get someone else to help you there. I only write code in C and BASIC. (And shell scripts)
However, try to think in a higher level – you’ll need some sort of array or list to represent each LED, and a variable to hold the current position. So you light the LED at the first position, then you might do something like:
repeat
wait for button push
turn off LED at the current position
wait for button to be released
add 1 to position
if position > last LED then position = 0
light LED at position
cycle
So the code between the repeat…cycle lines would be run for ever.
But how to translate that into Python – you’ll need to find someone who can write Python to help you there.
If you want to look at some examples in another language – then look at the tuxx.sh example – that’s bash script, but it does something similar – it waits on the button being pushed then goes through a sequence of LEDs before waiting on the button again.
-Gordon
Hi
Just bought a pi and stumbled onto this page. I cannot believe the amount of effort you put in, replies etc.
If only all teachers had your ability and patience.
Keep up the good work.
Cheers
Eugene de Weerd
South Africa
Thanks,
-Gordon
Hey! I tried this, and it all installed correctly, but when I run the commands, they execute fine, but nothing happens when I connect the leads…
Make sure the LEDs are the right wy round
-Gordon
I have RaspberryPI Revision 0003 (Model: B (ECN0001); PCB Revision: 1.0; Memory: 256MB; Notes: Fuses mod and D14 removed). I have created your circuit ant tested it with arduino and it is ok. Then, i used last raspbian and installed wiring pi and i have:
sudo ./gpio readall
+———-+——+——–+——+——-+
| wiringPi | GPIO | Name | Mode | Value |
+———-+——+——–+——+——-+
| 0 | 17 | GPIO 0 | IN | Low |
| 1 | 18 | GPIO 1 | IN | Low |
| 2 | 21 | GPIO 2 | IN | Low |
| 3 | 22 | GPIO 3 | IN | Low |
| 4 | 23 | GPIO 4 | IN | Low |
| 5 | 24 | GPIO 5 | IN | Low |
| 6 | 25 | GPIO 6 | IN | Low |
| 7 | 4 | GPIO 7 | IN | Low |
| 8 | 0 | SDA | IN | High |
| 9 | 1 | SCL | IN | High |
| 10 | 8 | CE0 | IN | Low |
| 11 | 7 | CE1 | IN | Low |
| 12 | 10 | MOSI | IN | Low |
| 13 | 9 | MISO | IN | Low |
| 14 | 11 | SCLK | IN | Low |
| 15 | 14 | TxD | ALT0 | High |
| 16 | 15 | RxD | ALT0 | High |
+———-+——+——–+——+——-+
Then, i execute the example code, with gpio readall i see the changes, but the led never turn on. Also connected led to resitor and 3V and ground the led is always turn off (with arduino it turn on). The raspberry gpio is broken? But the leds on-board are ok.
What’s happen? I’m desperate 🙁
Connect it like: GPIO -> Resistor -> LED -> 0v
or try turning the LED round. The flat does to the 0v side and the long-leg goes to the positive side.
-Gordon
The connections are ok but doesn’t
work.
Solved. The plug on gpio was broken
please can anyone help me with c program for blinking LED using GPIO pin
Go here: http://wiringpi.com/examples/blink/
Enjoy!
-Gordon
Thanx a lot for your instructions.
After installing WiringPi the easy way i have started my first projects with the raspberry. It’s a little bit more tricky as working with my arduino.
There i use fritzing for design and the arduino software to write the programs.
So i will read more and more to get closer with my pi.
greetings Achim from germany
You can still use Fritzing – e.g. see here:
Blink example
-Gordon
Hi Gordon
Thank you for your excellent tutorial, great stuff. I have a simple question. The LED I am trying to switch on is 5m away. Do you think it will still be OK to make up a 5m cable (eg using Cat5 perhaps to shielding) and drive it directly from the GPIO? Ideally, I wouldn’t want to add more components, eg relays, just wanted your thoughts on driving the LED that is 5m away from the PI.
You’ll be OK with using e.g. cat 5 cable for an LED. No need for shielding.
-Gordon
Gordon,
Many thanks buddy, all of your hard work and instruction have been crucial for my current project. I seemed to have somehow hard set on of the pins on my board, and can’t get it to reset. This also leads to failures on many python examples that check for status before execution.
I ran your pintest program to validate this, and it also appeared there as well. Here is the output of the pintest:
Please make sure everything is removed and press the ENTER key to continue,
or Control-C to abort…
The main 8 GPIO pins 0: 7: Pin 6 failure – expected 0, got 1
One fault detected.
The 4 pins on the P5 connector 17:20: OK
The 5 SPI pins 10:14: OK
The serial pins 15:16: OK
The I2C pins 8: 9: OK
Faults detected! Output of ‘readall’:
+———-+-Rev2-+——+——–+——+——-+
| wiringPi | GPIO | Phys | Name | Mode | Value |
+———-+——+——+——–+——+——-+
| 0 | 17 | 11 | GPIO 0 | IN | Low |
| 1 | 18 | 12 | GPIO 1 | IN | Low |
| 2 | 27 | 13 | GPIO 2 | IN | Low |
| 3 | 22 | 15 | GPIO 3 | IN | Low |
| 4 | 23 | 16 | GPIO 4 | IN | Low |
| 5 | 24 | 18 | GPIO 5 | IN | Low |
| 6 | 25 | 22 | GPIO 6 | IN | High |
| 7 | 4 | 7 | GPIO 7 | IN | Low |
| 8 | 2 | 3 | SDA | IN | High |
| 9 | 3 | 5 | SCL | IN | High |
| 10 | 8 | 24 | CE0 | IN | Low |
| 11 | 7 | 26 | CE1 | IN | Low |
| 12 | 10 | 19 | MOSI | IN | Low |
| 13 | 9 | 21 | MISO | IN | Low |
| 14 | 11 | 23 | SCLK | IN | Low |
| 15 | 14 | 8 | TxD | IN | Low |
| 16 | 15 | 10 | RxD | IN | Low |
| 17 | 28 | 3 | GPIO 8 | IN | Low |
| 18 | 29 | 4 | GPIO 9 | IN | Low |
| 19 | 30 | 5 | GPIO10 | IN | Low |
| 20 | 31 | 6 | GPIO11 | IN | Low |
+———-+——+——+——–+——+——-+
Thoughts?
It does look like some pins are stuck high in the input mode. The only way to test outputs might be to hook up some LEDs. Do make sure nothing is plugged into the Pi though when you run this test, and that it’s not sitting on aything condictive. It’s also worthwhile inspecting the underside with a magnifier to see if there is any fine wires, or solder splashes, etc. between the pins. Note that the I2C pins (SDA, SCL) will always read high due to the on-board 1.8K pull-up resistors..
-Gordon
Gordon,
Anyway that you know of I could have set the status of this pin, that would not allow a GPIO command to successfully set it back?
Not sure I understand what you’re after here – if you read a pin set to output mode then you should read its output value. Is that what you’re after?
-Gordon
More along the lines of how the 6th Pin got set to High, and why I can not reset, or set it back to Low.
I figure I either managed to do so during testing between Java, C, Python, or any of the other scripts I was running, or the physical pin has a hardware issue.
Jon, did u find an answer about pin 6 ?
Hi Gordon,
Thanks for your easy to follow tutorial. I bought the following starter kit,
http://www.ebay.co.uk/itm/130944832051?ssPageName=STRK:MESELX:IT&_trksid=p3984.m1555.l2649
It contained all the components required to get started.
This is the first time that I got a PC to control something in the real world. I got the LED working.
Thanks.
Looks like a cheap little kit with enough fun bits to get you going. Glad it’s working for you!
-Gordon
Can I plug different 12 LED lights into Raspberry pi to indicate traffic light system. Please give me a response. Thank you…
Yes.
http://unicorn.drogon.net/pi17leds.jpg
https://projects.drogon.net/the-raspberry-ladder-board/
-Gordon
Hi Gordon,
I’m attempting to turn on and off some fairy lights. Pretty basic stuff.
I’ve got my PiFace connected to the RPi and I’m interfacing the hardware using a web user interface.
When I call the following command without the fairy lights connected the onboard output LED0 lights up.
When I connect the wires from the fairy lights to Output 0 and the +5V connector and hit the “ON” button it calls the same script that worked before. However, every time I do this the system hangs (interestingly all of fairy lights do light up). I can’t get any access via the web or ssh. A physical power cycle is required.
Any ideas?
Thanks in advance.
Sorry Gordon. I forgot the command.
It is as follows :
How are you powering the fairy lights? If you’re powering them all off the Pi, then I suspect you’re overloading the 4v supply into the Pi. You only have about 200mA spare if the Pi is being powered via the micro USB connector – are you sure your lights take under 200mA?
-Gordon
Hi sir. I just want an idea how can i connect a gsm modem to my pi. Will all the gpio pins will be used? If so, how can i interface to leds, motors etc if all of them were used? Or is there a way to connect the gsm modem to a usb port so i can use the other pins for interfacing. I didnt really need right now to know how exactly to do it. but i really need an idea asap. Thanks in advance!
Use the USB port. then you can use standard software like wvdial, etc. to talk to it.
-Gordon
For those looking for a really easy language to use with the PI can I suggest YOUSRC. Used with loads of school pupils and a great success.
http://www.yousrc.com/functions5.asp?AREA=PI
Hello Gordan Sir..!
Can you please tell me how to use internet on R-pi using usb teathering?
Also tell me the settings required to turn on internet using ethernet, having a dynamic ip.
Thanks.
Hi Gordon, I wonder if you could help me introduce a powerup/pwerdown delay to my circuit. Essentially allowing the LED to fade in/out instead of instant on/off.
I’ve powered my LED’s directly from each GPIO pin as such…
GPIO -> Resistor -> LED -> 0v
All seems fine and I can turn LED’s on/off no problem.
I tried introducing a capacitor (470uF@16V) as such…
GPIO -> Capacitor(to gnd) – > Resistor -> LED – >0v
If I keep the GPIO pin high and manually insert the connector into the breadboard circuit where it should be I get the desired effect, but doing it programmatically it resorts to instant off/fade on!
I suspect it’s because when the GPIO goes to 0v it’s negating the capacitor discharge effect and the only way I can see this working is using a transistor as a switch. Any other thoughts on doing this without transistors?
You run a real risk of damaging the GPIO pin with that capacitor. When you set the pin to low, it’s not just dis-connected, but connected to the 0v line via a driver – it’s driven low, so the capacitor will discharge immediately into the GPIO pin with the potential for high current damage.
It’s also an effective short-circuit to 0v when you set the pin high – until it’s charged up.
If you just want to fade LEDs in and out, use the softwarePWM module. See the softPwm example program in the examples directory.
-Gordon
Thanks for the important advice! I’ve removed them imediately upon reading your response! I’m an old C pogrammer so it’s nice to see the library you refer to is in C :-). I’ve been picking up Python in the last couple of weeks and wlthough it’s fairly easy from what I’ve doen so far, I’d much prefer to fall back on an old language I used to use like C.
Just a couple of follow up questions..
I’m driving 15 LED’s from the GPIO. Will the soft PWM lib, or more to the point, the Pi itself, be able to handle this from a CPU usage perspective. To put into context, the lights will not be in constant use, but triggered by an external event on one of the GPIO input pins. On trigger they will perform a pattern and then off until the next triggered event.
Do you have any suggetions as to an editor/IDE for C on the Pi? I’m using nano with Python but wondered if there was a good C IDE?
Please feel free to remove this from the thread and email instead if we’re going off topic.
Steve
Have replied in email, but will just this here anyway – 15 LEDs on the softPwm will be fine – it’s about 0.5% cpu usage per LED. Just set the pwm value to 0 to turn them off.
I use vim and Makefiles for all my code – some people are having good results with Geany though…
-Gordon
Gordon,
You are a wizard! Please keep making more awesome tutorials. I have a problem and I can not figure out what I am doing wrong.
“gpio mode 0 out
gpio write 0 1
gpio write 0 0” does not work for me but “gpio -g write 17 1
gpio -g write 17 0” does. I checked my connections a few times and I feel like I am connected alright. Any ideas? Thanks so much!
Jesse
gpio write 0 1
and
gpio -g write 17 1
are functionally identical. wiringPi pin 0 is bcm_gpio pin 17. There’s no reason the first version shouldn’t work.
-Gordon
I feel so dumb right now. I can light up the raspi like you did in the first step. I followed your steps to get wired in and it seems to have worked. But, for some reason when I enter the command, nothing happens. The red is plugged into pin 11 and then into the bread board at f1. I have it set up exactly like yours, but when I type the commands, nothing happens. What could cause this?
Aha!! nvm it turned out to be a simple breadboard error! Thanks for the great tutorial!!
Can I use a 470 ohm resistor with this or does it have to be the one you use
470 will be fine – the LED will just be a bit dimmer.
-Gordon
Hi there.
In order to slower the current flowing into the led you need to put the resistor before and not after. In your example you slowing current flowing from resistor back to the ground.
You got this wrong.
I think you’ll find that your thoughts are flawed. It really does not matter which side of the LED the resistor is on.
-Gordon