0

I was trying to light an LED using RPi.GPIO on my Raspberry Pi 2 and it's not working. I'm able to do the same using pi4j library in Java. Following is the Java code which I have used to light LED.

final GpioController gpio = GpioFactory.getInstance();
final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLED", PinState.LOW);
pin.setShutdownOptions(true, PinState.LOW);

pin.high();
Thread.sleep(1000);
pin.low();

gpio.shutdown();

And my python code using RPi.GPIO is,

import RPi.GPIO as GPIO
import time

pin = 12

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
time.sleep(5)
GPIO.output(pin, GPIO.LOW)
GPIO.cleanup(pin)

What might be the reason for this issue? Since pi4j is working I think no issues with my Raspberry Pi. I'm a newbie to Raspberry Pi, any help will be appreciated.

Circuit

  • Usually the problem is the numbering scheme in use. Pin 18 is GPIO 24. Is that the GPIO being used for your LED? – joan Dec 2 '15 at 20:00
  • @joan: I've used GPIO 24 too, no luck. I have connected my LED's +ve to GPIO_GEN1/GPIO 18/PIN 12. and -ve to PIN 6. – Nirmal Raghavan Dec 2 '15 at 20:03
  • Pin 12 is GPIO18. In your RPi.GPIO code you are using BOARD numbering. Which means by pin. So use 12 in that RPi.GPIO code. – joan Dec 2 '15 at 20:06
  • I've tried 12/BOARD and 18/BCM and different pins using both BOARD and BCM numbering. Nothing seems to be working. :( – Nirmal Raghavan Dec 2 '15 at 20:09
  • According to pi4j.com/pins/model-b-plus.html Pi4J number 1 is pin 12. Pin 12 is actually connected to GPIO 18. As far as RPi.GPIO is concerned that is 12 if BOARD numbering is being used and 18 if Broadcom numbering is used. Which model Pi are you using? – joan Dec 2 '15 at 20:14
1

Commenting device_tree=on fixed my issues with RPi.GPIOlibrary.Issue explained here.

  • This might just be the anser to my question as well. I added device_tree=on to get SPI working. – Chris H Dec 7 '15 at 11:50
0

Do not add "device_tree=on" to your /boot/config.txt file. It will prevent your pi to boot. Bad answer. This statement is WRT Raspberry Pi 2

  • Would you care to elaborate a bit on this answer? As it it now, it is a little bit hard to understand. – Bex Aug 18 '17 at 10:42

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.