2

I have tried connecting a LED with a button like this (from "Adventures in RPi"), but with the far end of the button plugged at G (instead of F). breadboard

And I ran it with the following Python program (by following instructions from the said book):

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.IN)
while True:
    if GPIO.input(24):
        GPIO.output(23, True)
    else:
        GPIO.output(23, False)
        time.sleep(0.1)

But when I tried running the program it didn't work. To debugg it I changed the if code block to this:

    print("button")
    ##GPIO.output(23, True)

And it resulted in many prints of "button"...

  • How can I fix the program (or wiring?) so that the button will turn the LED on?

Thx in advance! :)

EDIT: Here are photos of the actual wiring: photo of wiring 1

7
  • Yes if you tell Python to print something, it will do so indefinitely unless you've written code to tell it otherwise.
    – Darth Vader
    Apr 25, 2016 at 19:02
  • 2
    There only a few things to go wrong. 1) the LED is the wrong way around, 2) the LED is not connected to GPIO 23. Can you post a photo of your set-up? Diagrams may show your intent - they do not show what you have done.
    – joan
    Apr 25, 2016 at 19:18
  • @Joan do you mind if I steal your phrasing above re: intent vs done? I like your wording better than mine. Apr 25, 2016 at 19:56
  • @SteveRobillard Not at all, it'a a bit of a cliche. :-)
    – joan
    Apr 25, 2016 at 20:07
  • @SteveRobillard If you could steal Joan's sentence then she wouldn't be able to remember it anymore, right? Splitting hairs, I know...
    – user29510
    Apr 25, 2016 at 20:41

1 Answer 1

1

Try using

from gpiozero import LED, Button
from signal import sleep

Then you can do

led = LED(24)
button = Button(23)

And do

button.when_pressed = led.on
button.when_released = led.off
pause()

This makes things simpler, and it might just work.

And are you sure your wires are lined up with your button? In the real photo it's hard to tell. Maybe you can try making the resistor under the LED? I don't know if your raspberry pi breadboard only goes one way or not. Plus, you do realize that 3 wires are going into the button in your wiring right? The yellow one, the black one and the blue one. I'm talking about the wires that are connected straight-up to the raspberry pi.

1
  • do you also want to use gpio.board Oct 29, 2019 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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