0

I have just started with the GPIO on my raspberry pi but can't get any of the code to work.

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
print "LED on"
GPIO.output(18,GPIO.HIGH)
time.sleep(1)
print "LED off"
GPIO.output(18,GPIO.LOW)

I have tried using both pin 12 and 18. I have the led connected to ground (pin 6) through a resistor and to pin 12. It looks like this

enter image description here

  • In that case you either have the LED back to front, or it is broken, or the resistor has much too large a value. Pin 12 is GPIO18. – joan Oct 28 '16 at 15:23
  • @joan it works when I just connect it to pin 1 – Rob M Oct 28 '16 at 15:31
  • The code is fine and toggles GPIO18. – joan Oct 28 '16 at 15:35
  • @joan then why doesn't the led light up – Rob M Oct 28 '16 at 15:37
  • The only other possibilities I can think of is your GPIO18 is broken or you are not connecting the LED to GPIO18. A photo will help with the latter. – joan Oct 28 '16 at 15:40
0

GPIO 18 is on pin 12.

GPIO 24 is on pin 18.

GPIO       pin  pin    GPIO
3V3         1    2      5V
0/2 (SDA)   3    4      5V
3 (SCL)     5    6      0V
4           7    8      14 (TXD)
0V          9   10      15 (RXD)
17 (ce1)   11   12      18 (ce0)
27         13   14      0V
22         15   16      23
3V3        17   18      24
10 (MOSI)  19   20      0V
9 (MISO)   21   22      25
11 (SCLK)  23   24      8 (CE0)
0V         25   26      7 (CE1)
           .......
0 (ID_SD)  27   28      1 (ID_SC)
5          29   30      0V
6          31   32      12
13         33   34      0V
19 (miso)  35   36      16 (ce2)
26         37   38      20 (mosi)
0V         39   40      21 (sclk)
| improve this answer | |
  • I meant that the led was connected to pin 12 then but I have also changed the code to pin 18. – Rob M Oct 28 '16 at 14:53
0

Maybe this adds another small bit of help: You can choose if you like to use the pin number or the gpio "name" like gpio 18. If you like to set the pin number, do

GPIO.setmode(GPIO.BOARD)

or use the gpio name with

GPIO.setmode(GPIO.BCM)

Also a good help for debugging with gpio's is the wiring pi gpio utility (from here) to access the gpio's from the command line. You can use

gpio write 12 1

to write a logic "high" to pin number 12. With your setup, this should light the led. But if you connect the LED from 5V or 3V3 to your GPIO pin, then the pin must be set "low" to let the LED glow.

| improve this answer | |
  • Just be aware the Wiring Pi library mentioned above has been deprecated - at some point it may well stop working. Please see wiringpi.com/wiringpi-deprecated for details. – Andyroo Apr 13 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.