Raspberry Pi - Getting Inputs from Buttons...
ฝัง
- เผยแพร่เมื่อ 2 ก.พ. 2025
- Here is another Raspberry Pi GPIO tutorial... This tutorial builds on my previous 2 tutorials available to view via my Channel. In this tutorial you will learn how to get an input from a simple button.
www.youtube.com...
Commands used:
To change directory - cd /****/*****/*****/
To enable your python program - sudo python yourprogram.py
Code write up in this forum post: www.raspberrypi...
Please subscribe and tell your friends of my videos! Feel free to comment on what you would like me to do next or if you have any problems!
The Raspberry Pi Guy
theraspberrypiguy@gmail.com
www.amazon.co.u...
11 years and still Useful, thank you!
Great tutorial thanks. One question, 'protection' to be safe should you have not added another resistors or a Zener diode? I'm interest in knowing if this is needed for the 3.3V connection.
"Python is the best beginner's language"
Not only is it not the best language, it's NOT the best for beginners. It is however, my favorite language :) I have my RPi running a few devices atm. I control them all over TCP connections... very fun and pretty easy!
Hey, great video!
Do you really need that 2nd while loop? after your if clause wouldn't the thread go back to the start of the 1st while loop and re-get the value of gpio.input(17), which if the button is not held down would be true?
Great video. I'm going to hook mine to a magnetic door sensor, so it will close the circuit when the door is opened and open it again when it's shut. How would the code change to execute the print on initial circuit close? I want to send an email when the door opens initially, not wait for it to close.
Thanks for any and all help
Your tutorials are excellent! Keep it up man!
I couldnt use sound on this video for some reason could i use this for creating a controller mounted onto the case of a portable pi im making
Python is great for learning. What do you suggest Haskell?
nice! can you write a wiring diagram?
How many ohm resistor?
Isn't the internal loop to prevent the from redisplaying multiple time? Seems like the main loop would update your variable.
Great video m8! Choosing a red wire for the ground was a bit confusing though.
Thanks.
I use the 26 pin connector from the pi to the experimenter's bread board pretty much at all times.
using a button like that to click intending on making something a bit portable I have two momentary switches was just wondering for a left and right click how i would go about it i get that i could run at boot the script once I have it but i dunno where to begin on the script python to me is like french i can jumble through and read it but inderpendant writing is borderline impossible help please
great work have you any code on a stepper motor that works
as I'm new to coding and 65yrs old I do find it quit fun. kevin
Excellent work, man. Thanks for doing this.
How would you set up this button to be a power button for the raspberry pi? hit the button and it shuts it down, hit it again and it turns it on.
Nice vid, thanks! Im twice your age and you're teaching me stuff! Going to have a go at this over the weekend.
Out of interest where did you get your parts from? Over the counter electronics store or online? Links would help a noob like me ;)
Hello. i am trying to wire 6 buttons to the rasberry pi b+ model. Each will have its own functions. any suggestions on how the wiring should be made and what code will work in python. Very new to rasberry pi but loving it thus far. Thanks in advance
me too
I hooked up a momentary shut down button on my raspberry pi so when I press it the pi shuts down. I got the idea from instructable. Now I would like to monitor the 10 flashes using a GPIO pin. Can that be done and how? I really need to insert the python code that will copy the flashes on Pi and output it via an external led
I still got the question isn't it possible to connect the button directly by 3.3v and on the other side to the imput GPIO? When button not pressed no voltage on input GPIO pin when pushed 3.3v on the input GPIO pin? Why do we need two circuits? May the 3.3V damage the raspberry?
Is it possible to make it wireless using rf433 receiver and transmitter? For example the button is connected to a transmitter and the rpi is connected to a transmitter. And when i press the button, it will show "button has been pressed" on the rpi
@10:54 The word ("embedded") and it's meaning certainly being used in** a grammatically correct way; however, it is not the proper terminology. In programming, loops which can be described in this was are known as "nested" loops. Any loop, test, or other function, wherein another independent non-procedural series of statements occur is given the quality of being "nested". This covers nested if-statements, nested loops, and the concepts of applied incursion (v. procedural) functions. I'm not familiar with python enough to say "use a do-while loop," - but it looks like that would have made a lot more sense here as well.. Maybe a boolean flag, and a function, with a check against the end condition (pin state high/low), etc. Nice job though! :D
Can I add in one more button using gpio 18 and how to write the code.
hi i would like to control xbmc using these type of buttons any ideas on how to do this.
You would have to program the buttons to simulate key presses. Not terribly difficult but some minor programming is involved.
Loved this button video....thank you!
Great tutorials but your statement about tactile switches is incorrect. A tactile switch can be a momentary or latching / locking. By default a tactile switch isn't latching. That said you have specified that it needs to be momentary so user will get it right anyway. I only wanted to say this so that potentially another 16,000 people don't leave with wrong idea about the definitions. Thanks and keep up with the awesome tutorials :D
how do u connect your rasberry pi to your pc?
what kind of case do you use
how many buttons can we hook to the pi?
Doesn't dust kill circuit boards? Why do all the Pi cases leave it open and vulnerable to dust
Thanks! This has helped me alot. Quick question tho, where can I get that plastic casing you have for your Pi?
Thank you :)
On the raspberry pi store
Hello. Love the tutorial. Would be really nice to know how to replace the print command with a command line command. Thanks!
How ya mate? I ran that Tutorial and came up with the following error.
root@raspberrypi:/home/pi# sudo python gpiotest1.py
File "gpiotest1.py", line 9
while True:gpio.output(14, gpio.HIGH) time.sleep(1) gpio.output(14, gpio.LOW) time.sleep(1)
^
SyntaxError: invalid syntax
Confused as I typed it all out as per your tutorial am I missing something?
will this work from a touch sensor
Hello. Awesome tutorial.
When I close the program and then start it again, I get the following message:
runtimewarning this channel is already in use.
I have tried adding the following GPIO.cleanup().
But it does not help.
i know its been a lot of time like a lot lot of time on this video and i want to ask you something... soo the last part of the program when u said
while input_value == False:
input_value==gpio.input(17)
it means that whenever you press the button and get the false it goes back to the value it had before pressing? something like that? hope someone can answer me asap thanks!
+Kariey Zah well isn't this a blast from the past! I can't actually remember what I was thinking when I coded this (that's what comments are for, whoops!) but I do remember the crux of the code coming from a MagPi magazine. You're description of its function is right, however if I was to make this video again I would probably simplify my program!
Do you have recommendations for making a more permanent circuit based on this? I'm really new to electronic experimentation and I'm trying to make this button circuit to place into a Super Nintendo case. Breadboard doesn't really work in that situation. >.
awesome tutorials, you do a very good work. cya
Ace! You saved my life :D Thanks very much
wie muss ich es machen wen ich machen wil das es stat "the button has been brest " einen input auf einen anderen pin geben wil ??
Excellent tutorial, it helped me a lot
How can I interact with any python games through this button press
This is so cool!
i did everything that you did in the tutorial but i keep getting this error when I try to run the program.
pi@aaronpi ~ $ sudo python /button.py
File "/button.py", line 11
^
SyntaxError: invalid syntax
I am not sure what is is saying because there is no line 11.
Are you using GPIO pin 14?
hi there
can i have some help off some 1 how do i get it to work with a html to puss a button on a website plz
First of all, excellent tutorial!
I would like to know how I might modify the code so that I can have one action after one press of the button and a different action if I double-tap the button (maybe allowing 100ms to listen for a second press) Can anyone help?
Where can I buy this button?
You can buy these buttons on Ebay like this listing here: www.ebay.com/itm/100pcs-Tactile-Push-Button-Switch-Momentary-Tact-6x6x5mm-DIP-Through-Hole-4pin-/231123514137?pt=LH_DefaultDomain_0&hash=item35d008f319
import os
os.system('command here')
would like to show you the project i made using only the tools you provided for me here...
Can you get it to say light and flash a light
how if 3 buttons are pressed simultaneously?
i suscribed by the way ur a good teacher
It only works when I press it once.
Do you still use this?
Great tutorial !
i shared your video on facebook so it can get more views
Hello everyone
I was wondering if I can find any helpful materials or tutorials on how to read data and transfer data from one device to another Raspberry PI device using USB ports. In short it would be reading data through USB.
Please help me with this I would really appreciate your help
Many thanks
Usman
Can't you do that on C ?
Sometimes the button registers two presses, any idea why?
That's because of something called bouncing. Think of what would happen if you took a hammer and smacked it against a concrete floor, the hammer would make contact with the floor initially, and sort of bounce around, if it were a switch it would act as if it was flipped several times. There is a way of fixing it, a process called debouncing, instead of checking the input for a high pulse, it checks for a falling pulse and activates a small delay before it can be reactivated.
Why don't u put the button in between the resistor and gpio17? That way the circed is normally on zero v and only uses energy if pressed. Also calling the input will return True if the button is pressed witch is a bit more logical in my opinion.
hi getting error
AttributeError: 'module' object has no attribute 'setmode'
please help
Sounds like you either do not have GPIO library installed, have not imported it correctly, or you did not use it as a function like this: gpio.setmode(gpio.BCM). Linux and Python are case sensitive so make sure all lower case and upper case letters match the code above. Pause video at 9:22 to see how everything needs to be typed. In programming a single spelling or case sensitive error can throw the entire program off.
This is not working for me and I've done exactly as you've said. I need your help.
awesome work man! thanks
I figured it out when used my brain :P
Cheers!
~Novice electrician speaking~ I found the source of the problem. So apparently if you connect the 3.3 volts to ground, the pi restarts. (I assume this is a safety feature)
Great vid, but I kinda programmed a button that restarts my pi on accident... (Help me please!)
awesome video!
oyeah how much does a rasberry pi cost
Just a friendly message that "sudo" is pronounced "su do". It doesn't sound like the word "pseudo". It's from DO this as SuperUser. Also, thanks for the tutorial.
When changing directory, you already where in your home directory (/home/pi) and all you needed to type was: cd MTB/
hi
can i have this program in C...plz
great work I think you should be a teacher. could you work through a stepper motor please.
this comment has nothing to do with Raspberry Pi... just had to say.. you sound exactly like the kid from the original max headroom movie O-o.... he created an AI. you work with PI ^_^
You could have used a else statement, it looks nicer.
its reali cool that hes going
i am 10 plz can u use more simple language and what was the aim of the video? and i was wondering if u could teach us how to make a r.c(remote controlled) car using xbox controller/controller and rasberry pi to move the r.c car
Cool! Thanks a lot!
It's "if not input_value", not "if input_value == False".
Thanks! :)
only work when I touch it with my hand, wtf
While I understand the rationale behind why you'd pronounce it that way, I have heard very few people actually say it that way.
P.S. I've pressed it three times now.
I understand why you would say sue do, but I have only heard people say sue doh.
go to 12:55 to see it work
by the way the rasberry pi cost £90 including case andi live in england
You shuld shutdown the PI while you are doing those connections
*should
10 killer ohm
Too high? Just wondering...
Hi I am 14
Euw "if value == False:". I puked a little. "if not value:"?