Edit: Breadboard, schematic, and code updated.

I accidentally fried my Pi by shorting it out through the GPIO pins, so I figured it would be a good idea to ask for some help before trying my current breadboard layout with my new Pi.

I have a Raspberry Pi 3 B+ and have connected it to these 2 breadboards: Schematic

This is what the breadboard layout looks like IRL:

Essentially, I want to have these 6 buttons cause the LEDs to light up and for additional code to run when they're pressed.

Using this resource, I wrote a script in Python 3 to handle inputs from the pins I wired the switches to:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

#Buttons 1-6
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Button #1
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Button 6

#LEDs 1-6
GPIO.setup(18, GPIO.OUT) #LED #1
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(16, GPIO.OUT) #LED #6


while True:
    input_state_1 = GPIO.input(4)
    input_state_2 = GPIO.input(17)
    input_state_3 = GPIO.input(27)
    input_state_4 = GPIO.input(22)
    input_state_5 = GPIO.input(5)
    input_state_6 = GPIO.input(6)

    if input_state_1 == False: #if Button #1 is pressed
        print('Button #1 Pressed')
        GPIO.output(18, GPIO.HIGH) #turn on LED #1
        time.sleep(0.2)
        GPIO.output(18, GPIO.LOW) #turn off LED #1
    if input_state_2 == False:
        print('Button #2 Pressed')
        GPIO.output(23, GPIO.HIGH) 
        time.sleep(0.2)
        GPIO.output(23, GPIO.LOW)
    if input_state_3 == False:
        print('Button #3 Pressed')
        GPIO.output(24, GPIO.HIGH)
        time.sleep(0.2)
        GPIO.output(24, GPIO.LOW)
    if input_state_4 == False:
        print('Button #4 Pressed')
        GPIO.output(25, GPIO.HIGH)
        time.sleep(0.2)
        GPIO.output(25, GPIO.LOW)
    if input_state_5 == False:
        print('Button #5 Pressed')
        GPIO.output(12, GPIO.HIGH)
        time.sleep(0.2)
        GPIO.output(12, GPIO.LOW)
    if input_state_6 == False:
        print('Button #6 Pressed')
        GPIO.output(16, GPIO.HIGH) #turn on LED #6
        time.sleep(0.2)
        GPIO.output(16, GPIO.LOW) #turn off LED #6

    time.sleep(0.1)

GPIO.cleanup()

I guess what I'm wondering currently is: Does the breadboard layout look correct for this application, and does the code look correct? Can multiple LEDs and switches share the same Ground pin? Do I need to set up Ground pins in Python?

  • Personally I don’t like this method of combining leds and buttons on inputs, Why not have 6 buttons connected to 6 GPIO inputs and 6 leds connected to 6 GPIO outputs then control with software. – CoderMike Sep 14 at 6:56
  • @CoderMike - I've updated the picture and code based on your suggestion -- I hope I understood what you were saying correctly – Rudy Sep 14 at 8:03
  • Difficult to tell from the photo - are each led and resistor connected to GND and a GPIO output? Are each button connected to GND and a GPIO input? I tend to add a resistor to the GND used for input - just incase. Add a time.sleep(0.1) to the end of your while true loop (avoids intensive loop). – CoderMike Sep 14 at 8:55
  • I believe yes and yes. I've updated the code as well to add a 0.1s sleep at the end of the loop. – Rudy Sep 14 at 9:08
  • All seems ok to me – CoderMike Sep 14 at 9:21

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.