Raspberry Pi LESSON 30: Control LEDs with Buttons on GPIO Pins

preview_player
Показать описание
This tutorial shows you how to control LED's using the GPIO pins on the Raspberry Pi using Python. This demonstrates use of the GPIO pins as both inputs and outputs.

See code and full lesson details at:
Рекомендации по теме
Комментарии
Автор

Thank you for taking the time to explain each step as you go through the process of both building the circuit and writing the code. I'm in the last semester of college and I have a capstone project to create for my final grade. I'm new to using the Raspberry pi and python programming, your lessons have been extremely helpful in helping me build my project. I'm building a traffic light with a crosswalk button that will stop the normal traffic light mode, turn on another LED to signal it;s safe to cross then start running the normal traffic light mode again. I hope you continue to your lessons on raspberry pi and python progrmming.

jasoncherry
Автор

Thanks Paul - watching the videos here continues to enlighten me with further insight on circuits and coding

rajcodes
Автор

SUDO = Substitute User Do commonly known as "Superuser" do !

Teacher_Ali
Автор

Hi Paul Just created this file on my RPi 2 can turn the LED on but cant turn it off!? Copied your python exactly except only using 1 LED and 1 Switch. Any help for this Python newbie appreciated Maurice

maurice
Автор

why dont you use just directly GPIO.output(LED1, not BS1) then BS1 = not BS1 then sleep(.5) so it will toggle, this 3 lines is equal to 8 line of yours

omer_usta
Автор

Great stuff...I would like a pi vibration sensor to trip a relay. The vibration sensor would send a signal to pi when it feels vibration and then would trip a relay for so man seconds.

davidmussaw
Автор

Hey Paul, I've been working on your tutorials since i got my sunfounder kit and it seems every time i do a course with more than one button the program either keeps a led idle as on or the program doesn't acknowledge the button. i did the dim-able led and this one with no avail.
yesterday i went through my program for hours trying to figure it out. loving the videos though, you are very knowledgeable and we are blessed to have you putting these out there for everyone.

CalenLAUR
Автор

May I know.. why you didn't cleanup.... in this program..

lightning_sree
Автор

How can you do this for one RGB LED with one button please?

ianscicluna
Автор

Paul, How would you use a limit switch in Tkinter module using the graphics interface which allows a user to push buttons to turn on LED's but also monitor the condition of the limit switch (on/off) so if the limit switch closes, the push buttons become operable and a graphic button would say "System Available" and when the limit switch opens, the virtual buttons become inoperable and graphic button roll over changes from "System Available" to "System Busy". I can't use "while True:" because when I do. the virtual buttons become inoperable. I would like a person to select from two buttons an option which activate a LED according to the push buttons.. Like select "Left Button" or "Right Button" and either LED will function as long as the limit Switch condition is true however if the limit switch is open, both buttons are inoperable until the limit switch is true again. Any ideas? Thanks for any response.. Just trying to solve a simple problem I could do in C or C# in 10 seconds that I can't do in Python due to my newbe experience in the development platform. I've been trying:
import Tkinter as tk
import tkinter.font
import RPi.GPIO as GPIO
from gpiozero inport LED, Button
from signal import pause
from time import sleep

win = tk.TK()
win.title("ARBOR")
myFont =tKinter.font.Font(Family = 'Helvectica', size = 24, weight = "bold")

led1 = LED(13) #These are actually three relays on pin 13, 6 and 26
led2 = LED(6)
led3 = LED(26)
button = Button(5) #sensing status of pin 5

GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN pull_up_down=GPIO.PUD_UP)


while True:
input_state = GPIO.imput(5)
if input_state == True:
button["text"] = "System in Use"
led3.on()
break
else:
button["text"] = "System Available"
led1.off()
led2.off()
led3.off()
break
#So the nested if statements below identify is another button is in use and
#renders the other buttons unavailable if conditions are not met

def selectOption1():
if led1.is_lit:
led1.off()
led3.off()
paperButton["text"] "Paper"
else:
if button["text"] != "System Available":
if plasticButton["text"] != "Please Wait!":
if glassButton["text"] != "Please Wait!":
if generalButton["text"] != "Please Wait!":
led1.on() #turn on relay to send 24V to main control
led3.on() # turn on relay to control local latch
paperButton["text"] = "Please Wait"
def selectOption2():
if led2.is_lit:
led2.off() #relay for plastic selection is off
led3.off() # relay for hopper latch is off
plasticButton["text"] "Plastic"
else:
if button["text"] != "System Available":
if paperButton["text"] != "Please Wait!":
if glassButton["text"] != "Please Wait!":
if generalButton["text"] != "Please Wait!":
led1.on() #relay for relay 1 is on
led3.on() #relay for hopper latch is activated
plasticButton["text"] = "Please Wait"
def selectOption3():
if led1.is_lit:
led1.off()
led3.off()
glassButton["text"] "Glass/Metal"
else:
if button["text"] != "System Available":
if paperButton["text"] != "Please Wait!":
if plasticButton["text"] != "Please Wait!":
if generalButton["text"] != "Please Wait!":
led1.on()
led3.on()
glassButton["text"] = "Please Wait"
def selectOption4():
if led2.is_lit:
led2.off()
led3.off()
generlButton["text"] "General Waste"
else:
if button["text"] != "System Available":
if paperButton["text"] != "Please Wait!":
if glassButton["text"] != "Please Wait!":
if plasticButton["text"] != "Please Wait!":
led13.on() #only the latch operates here.
generalButton["text"] = "Please Wait"
def exitProgram():
win.quit()
win.destroy()
#packing the GUI

button=tk.Button(win, text='System Available', font=myFont, bg='blue', height=2, width=32) #button to change according to limit switch condition
button.grid(row=0, sticky=tk.NSEW)

paperButton=tk.Button(win, text='Paper', font=myFont, command=selectOption1, bg='blue', height=2, width=38)
paperButton.grid(row=1, sticky=tk.NSEW)

plasticButton=tk.Button(win, text='Plastic', font=myFont, command=selectOption2, bg='blue', height=2, width=38)
plasticButton.grid(row=2, sticky=tk.NSEW)

glassButton=tk.Button(win, text='Glass/Metal', font=myFont, command=selectOption3, bg='blue', height=2, width=38)
glassButton.grid(row=3, sticky=tk.NSEW)

generalButton=tk.Button(win, text='General Waste', font=myFont, command=selectOption4, bg='blue', height=2, width=38)
generalButton.grid(row=4, sticky=tk.NSEW)

exitButton=tk.Button(win, text='Exit', font=myFont, command=exitProgram, bg='red', height=2, width=38)
exitButton.grid(row=5, sticky=tk.E)

tk.mainloop()

but it's hanging in the while True: arg.

yapandasoftware
Автор

Paul McWhorter
This is really for anyone who can help me....I have a project I am making that uses two buttons (player 1 button and player 2 button) to try and stop a blinking LED when it's lit up. But I can't get my buttons to work. So, I used your video to make a test program to see if the buttons would work just by your code. It still doesn't. What it is doing on my project, and in this program, is spamming my shell with the print statement "button1 was pressed" and "button2 was pressed". So it's essentially reading that both buttons were pressed over and over again, but they are not. I don't know how to fix this, as the code for this code is exactly like yours, but it's still doing it. Can anyone help me with this? Google isn't really helpful, as I can't find any reference on why it's doing

erikahalpin
Автор

Please bear with my limited understanding of electricals here.
In my code, I have my input pin pulled up to V+, which goes to one end of my switch. The other end of my switch, rather than going straight to ground (like shown in the video), I have it go through an LED first, then finally to GND. I thought doing this would save me some pin real estate by not having to use an extra output pin to control the LED.

When I test my code to see button press, if the button goes straight to GND like in the video, input reads 0, which is expected, just like in the video.

When that LED is there, it shines very dimly when the button is pressed, which I think makes sense, because the pull up resistor has some massive impedance like 10k ohm or something in the pi, so there's not much current going through the LED. What's strange to me is that instead of reading 0, when the button is pressed, it still reads 1, as if the circuit is open. What is it about the LED being between the switch and ground that causes this?

jn
Автор

I understand the purpose of the BS1 and BS2 variables, but I don't understand how the program is supposed to understand them. Like the pins and the LEDs are linked to certain physical pins on the Pi, but suddenly there's this variable called "BS1, " and all the program sees is that it is "False." Can anyone explain this?

jakeangelastro
Автор

Thank you Mr. Mcwhorter. This is the biggest python sketch I've ever wrote. 😅 I like python, I don't care what they say.(I'm all c/c++) I've learned so much from your videos, thank you sir. You truly are a legend, best teacher I've ever had. (I'm 31)

erygion
Автор

Hi, Paul. Your Raspberry Linux courses very very good to me, watched 30 courses so far, very professional. Thanks.

williamwu
Автор

I'm going to assume, if you pressed and held either button, due to the sleep interval the respective light would just blink? Correct?

CatGuyTX
Автор

Excellent, thanks very much, this is the information I need to automate a repetitive manufacturing press.

harleycruiser
Автор

Love your videos. Quick question. So after you run buttonLED.py, how can you work in a stop command and GPIO cleanup at the end? So that you can stop the program at any time and it will clean up the pins when you enter the stop command.

jasonhill
Автор

Thank you so much Mr.McWhorter, although when I was doing this along with you I made so many mistakes which made me pull my hair out, lesson learnt, never try to run ahead of the teacher hahaha,

aynursunagatullin
Автор

Hi, what should I do to read values different like between 0-1000?

calpardo
welcome to shbcf.ru