Arduino - Turn LED On and Off With Push Button

  Рет қаралды 135,841

Robotics Back-End

Robotics Back-End

Күн бұрын

Пікірлер: 67
@nataliagrabowieckialetti5034
@nataliagrabowieckialetti5034 Жыл бұрын
Thank you so much. I needed to program something for the school where I had to set up the button as you explained. In no video I have found a programming that detects when you press the button to turn on a light and then another press to turn it off. Thanks again. Keep explaining this kind of stuff, it's worth it.
@aofddofa6661
@aofddofa6661 Жыл бұрын
i logged in to hit like and thank you .. not one of best but the far best of all
@Pewpewz
@Pewpewz 2 жыл бұрын
Hey, fyi to make it even simpler instead of your if -> then -> else you could do ledState = !ledState
@smalburg1
@smalburg1 Жыл бұрын
Would delay(50); at the end of the if-condition not have done the job instead of the extra variables?
@navb0tactual
@navb0tactual 2 жыл бұрын
THANK YOU SO MUCH! Instead of an LED, I used this on an 8 part Relay to power my LED Strips. I do have a problem where if I accidentally jiggle the wires it thinks a button is pressed, but that's likely the cheap breadboard/crappy duponts. If you're doing the same I had to put my delay to 1000 milis otherwise it was going crazy. (It's interesting at first, but a great way to damage them.)
@FlockofSmeagles
@FlockofSmeagles 2 жыл бұрын
I did the same thing, but they take way longer to actuate now, lol.
@navb0tactual
@navb0tactual 2 жыл бұрын
@@FlockofSmeagles Yeah the next day mine just turned on and off without pressing the button... I love coding... I'll see if I can figure out why, but I have very little faith in my coding capabilities (or lack thereof lol) Also I actually put mine on 1250 milliseconds, which is 1.25 Seconds... I actually like the delay for some reason, but I can see that being incredibly annoying.
@navb0tactual
@navb0tactual 2 жыл бұрын
@@FlockofSmeagles I figured it out... I'm wildly dumb, I also didn't touch it since my last comment. I skipped the circuitry section because I was eager to get this to work that day. My coding wasn't the problem, it was the wiring. I forgot to add the 5v to the button lol, curiously it worked the first time for 5 minutes, but never again after that. Now it is fixed and I have it set to 500 milliseconds, however I'll edit this to get the lowest one without any bouncing. The reason it first but then wasn't working is because I'm in a small room surrounded by electronics, the interference caused by them made the button go crazy. I'm very new to this so I had no clue. I was dealing with a "floating pin" problem I'm using an Elegoo 8 Part relay so I'm not sure if this will help with yours but I'll link what I found and hopefully KZbin doesn't get mad at me. Like the guy in the GIF, I was able to actuate the button without pressing it. This stuff is so interesting. makeabilitylab.github.io/physcomp/arduino/buttons.html#the-floating-pin-problem kzbin.info/www/bejne/ranNlqV5fah4g8k&ab_channel=AddOhms Hope you got yours to work better otherwise. Hopefully this helps if you haven't! Edit: I got it down to 10 Milliseconds with it only bouncing once from 10 presses. I can give you my code if you want. Idk if that'll work on here but I can try
@CoderDad
@CoderDad 10 ай бұрын
You may also have grounding or power supply problems. Try running on batteries for a clean DC supply.
@v1bonoxd
@v1bonoxd 10 ай бұрын
yo man if you still can, can you send me a code?@@navb0tactual
@TJenga
@TJenga 2 жыл бұрын
Very nice, could you make one where you have 2 leds and 2 buttons where button 1 changes led1 state and button 2 changes changes button 1 to change state for led 2 ?
@weltroyt1270
@weltroyt1270 Жыл бұрын
that would be nice for me also
@lokyilau3126
@lokyilau3126 8 ай бұрын
Hi, thank you so much for your detailed instruction! But I don't know why it even lights up without me pressing the button. When I press it, it gets lighter. How can I solve this issue tho? Thank you so much!
@ahmedahmed-rd2lj
@ahmedahmed-rd2lj 4 ай бұрын
More helpful than the paid course
@ShivrajKarkera91
@ShivrajKarkera91 11 ай бұрын
I still don't understand why do we need that 10k resistor, while the pushbutton simply breaks the circuit when it's not pressed.
@modular_warfare_remastered3657
@modular_warfare_remastered3657 11 ай бұрын
Pull down resistor
@AashirwadTyagi-ch4me
@AashirwadTyagi-ch4me 2 ай бұрын
Yes bro i am also have this question
@ShivrajKarkera91
@ShivrajKarkera91 2 ай бұрын
@@AashirwadTyagi-ch4me I figured it out bro. It's for making sure that arduino receives only 2 values, either 5v or 0v, from the pushbutton. Without the resistor, arduino might receive "floating" states or in-between values due to electrical noise, that can cause confusion and misbehaving of the circuit. When the button is not pressed, the resistor PULLS DOWN the reading to 0 volts, preventing false reading. Also it is said that the resistor prevents short circuiting by limiting the current, when button is pressed, coz it's directly connected to both terminals.
@lastchance045
@lastchance045 7 ай бұрын
I enjoyed the video and was finally sucessful in running the code on my Arduino Uno. I think it was unnecessarly difficult to extract the code from the video considering the many "back and forth" journeys in the coding. One set-back was caused by the use of different coding styles, e.g. (#define led #define button) & (#define LED_PIN #define BUTTON_PIN) Here is the code I ended up with Thanks Turn on the LED when the button is pressed & keep the LED on unless the button is pressed again. If the button is pressed again the LED will remain off unless the button is pressed again. The on & off state of the LED will toggle each time the button is pressed. #define LED_PIN 8 #define BUTTON_PIN 7 byte lastButtonState; byte ledState = LOW; unsigned long lastTimeButtonStateChanged = millis (); unsigned long debounceDuration = 50; //millis void setup () { pinMode(LED_PIN, OUTPUT); pinMode (BUTTON_PIN, INPUT); lastButtonState = digitalRead(BUTTON_PIN); } void loop () { if (millis () - lastTimeButtonStateChanged >= debounceDuration) { byte buttonState = digitalRead (BUTTON_PIN); if (buttonState != lastButtonState) { lastTimeButtonStateChanged = millis(); lastButtonState = buttonState; if (buttonState == LOW) { //released if (ledState == HIGH) { ledState = LOW; } else { ledState = HIGH; } digitalWrite(LED_PIN, ledState); } } } }
@sas-inventions7969
@sas-inventions7969 2 жыл бұрын
Thanks bro i have issue and you solved it on minutes🧡
@FlockofSmeagles
@FlockofSmeagles 2 жыл бұрын
I've done this. Except with 5 buttons and LEDS. The debounce doesn't seem to work consistently a crossed all of them. is this because of how they are being called in the code? Is there a way to call each button and led block within an array? If you see this. I would appreciate your help, thank you.
@bloodlinks7137
@bloodlinks7137 10 ай бұрын
Well, i use another debounce system that i think would work better for your case than the on in the video. Instead of using millis(), i use a bool system, let me give you an example: if (debounce == false) { // Checks if debounce is on or off debounce = true; // If its off, sets it true digitalWrite(13, ledState); // Turns led on or off delay(1000); // This is the debounce duration, i put 1000 miliseconds debounce = false; // Sets the debounce off after the designated time has passed } I am not sure if you can or not but i believe that you can implement this to multiple LEDs, probably you'd have to create 5 different bools
@oliverjanssen1700
@oliverjanssen1700 2 жыл бұрын
Very well explained. Thank you.
@xdevil8382
@xdevil8382 11 ай бұрын
Can we use IR sensor instead of push button?
@elaprimo1
@elaprimo1 2 жыл бұрын
This helped me out, thanks.
@nerdtowncity5930
@nerdtowncity5930 Жыл бұрын
If I wanted to wire this up without the bread board how can I do it in regards to the ground? Would I split the ground on the switch? Or how can I do that?
@RoboticsBackEnd
@RoboticsBackEnd Жыл бұрын
in this case you need to find a way to connect all grounds (maybe by soldering?)
@lxgb01
@lxgb01 7 ай бұрын
What a brain power ! Great stuff
@Zeu_99
@Zeu_99 Жыл бұрын
I replaced the push button with an electric wire, but when I short-circuited the electric wire, it turns on, and when I don't, the led turns off, but once I short-circuited, the led keeps turning on, so can't I replace it with an electric wire?
@CoderDad
@CoderDad 10 ай бұрын
The button is to act as a "momentary" contact, that is to say you push it down for a fraction of a second and let it go again. This code is not designed for the buttong to be held down continuously. To use a wire for continuous contact try this code in the Loop: if(digitalRead(BUTTON_PIN) == HIGH){ digitalWrite(LED_PIN, HIGH); } else { digitalWrite(LED_PIN, LOW); }
@GoogleGoogle-ni9ww
@GoogleGoogle-ni9ww 6 ай бұрын
why the green led light is at d3 and d4 in the diagram at 4:11 and at 5:39, the actual green led light is at different place?. I'm a beginner and super confused....
@Initial_Depressed
@Initial_Depressed Жыл бұрын
how would we change the code to where the led would only flash five times?
@sudenazerdogan5258
@sudenazerdogan5258 11 ай бұрын
It should light up when the user presses the button once, and then go out when he presses it again. However, if the user presses the button again within 5 seconds, the LED should turn on and turn off after 5 seconds. how can I do that
@CoderDad
@CoderDad 10 ай бұрын
You have store the time of the first button press in a variable. You also have to store the number of presses in another variable. Then you test with (psuedo code) "If (ButtonPressCount >= 2 AND ButtonTimer
@Chaos_Nova
@Chaos_Nova Жыл бұрын
Why not use state machine and task implementation?
@jakerooni5542
@jakerooni5542 Жыл бұрын
what do i do when there's two buttons and two LEDs?
@CoderDad
@CoderDad 10 ай бұрын
copy the code and add two more ports. Change the copied code to reference the two new ports.
@CoderDad
@CoderDad 10 ай бұрын
//My code version: #include #define LED_PIN 8 #define BUTTON_PIN 3 bool LedState = false; //bool - the state can only be ON or OFF. void setup() { pinMode(LED_PIN, OUTPUT); pinMode(BUTTON_PIN, INPUT); } void loop() { if(digitalRead(BUTTON_PIN) == HIGH){ //if the button is pressed enter if# delay(250); //small delay for button bounce. LedState = !LedState; //set LedState to be not what it is. } digitalWrite(LED_PIN, LedState); }
@eliasnedelea232
@eliasnedelea232 Ай бұрын
how could i put a countdown with milis. so that the led goes off after 60 seconds?
@tythy-.-9914
@tythy-.-9914 Жыл бұрын
I have a question about the button pin? why the button Pin is 7? How to define which number of pin is for the button?
@RoboticsBackEnd
@RoboticsBackEnd Жыл бұрын
the button pin is 7 just because I decided to plug the wire from the button to the pin 7 on the circuit :) you could use any other valid pin, then make sure you use the same number in your code.
@husseinali8412
@husseinali8412 2 жыл бұрын
Nice video, good explenation but a bit slow. KEEP GOING!!! JUST DO IT ;)
@bilalalissa6789
@bilalalissa6789 2 жыл бұрын
Thank you Great video Very good explained.
@Sam-ll4tl
@Sam-ll4tl Жыл бұрын
on your diagram resistor, it's seems 10k right ? not 100k on your video. its brown,black, yellow, gold
@RoboticsBackEnd
@RoboticsBackEnd Жыл бұрын
Yes it's 10k Ohm (4 band resistor) Note that for the real circuit I use a 5 band resistor, so the colors are a bit different (but still 10k)
@Jannat_Rocks
@Jannat_Rocks Жыл бұрын
what are the 2 resistors used there which ohm ?
@bloodlinks7137
@bloodlinks7137 10 ай бұрын
Read their color codes
@stevewilson8267
@stevewilson8267 2 жыл бұрын
Great, hey thank you.
@pablogutierrez4642
@pablogutierrez4642 2 жыл бұрын
what libraries do I need? it does not work... byte remains white
@calebappiagyei4007
@calebappiagyei4007 Жыл бұрын
Thanks very very much sir 🙏
@ShoTime017
@ShoTime017 8 ай бұрын
TYSM!
@spectrumtrend2328
@spectrumtrend2328 2 жыл бұрын
Thanks Man👌
@mohamedahmedelsinety231
@mohamedahmedelsinety231 Жыл бұрын
Why is not working? int buttonPin=13; int buzzer=2; byte LastButtonState; byte BuzzerState=LOW; unsigned long lasttimebuttonstatechanged = millis(); unsigned long debounceduration = 50; void setup() { pinMode(buttonPin,INPUT); pinMode(buzzer,OUTPUT); LastButtonState= digitalRead(buttonPin); } void loop() { if (millis() - lasttimebuttonstatechanged >= debounceduration) { byte buttonstate =digitalRead(buttonPin); if (buttonstate != LastButtonState) { lasttimebuttonstatechanged = millis(); LastButtonState = buttonstate; if(buttonstate == LOW) { if (BuzzerState == HIGH) { BuzzerState = LOW ; } else { BuzzerState == HIGH; } digitalWrite(buzzer,BuzzerState); } } } }
@mohamedahmedelsinety231
@mohamedahmedelsinety231 Жыл бұрын
I have known what is wrong the fking = after else just remove one
@raypizer8037
@raypizer8037 Ай бұрын
But why, if all you want to do is turn an led on and off, just use s power supply and switch
@herschelvijae7201
@herschelvijae7201 2 ай бұрын
thnk you
@tawfeqal-jaber5544
@tawfeqal-jaber5544 2 жыл бұрын
thanks pro
@technomattanbadbollisch1113
@technomattanbadbollisch1113 9 ай бұрын
Compilation error: expected '}' at end of input - Line 17 😩
@RoboticsBackEnd
@RoboticsBackEnd 9 ай бұрын
That's a syntax error, triple check your code to find the error :)
@nulzcage
@nulzcage 2 жыл бұрын
thank youu
@LucasRizzotto
@LucasRizzotto 3 ай бұрын
lol your board is totally different from your cad
@RoboticsBackEnd
@RoboticsBackEnd 3 ай бұрын
Just different colors, but the pin layout is the same.
@imranfoodtech
@imranfoodtech Жыл бұрын
I am finding error collect2.exe
@tahabelghalem447
@tahabelghalem447 Жыл бұрын
ty brother
@Echelon_Sky_Denver
@Echelon_Sky_Denver 10 ай бұрын
First project followed to the 't,' but get "Compilation error: 'digitalRead' was not declared in this scope" Pretty dissapointing.
@user-lo8td9uw4t
@user-lo8td9uw4t 3 ай бұрын
This video is very useful. Thank you.
Arduino Tutorial 28: Using a Pushbutton as a Toggle Switch
21:58
Paul McWhorter
Рет қаралды 297 М.
Arduino - Debounce a Push Button (+ Visual Explanation)
9:21
Robotics Back-End
Рет қаралды 15 М.
This Game Is Wild...
00:19
MrBeast
Рет қаралды 154 МЛН
World’s strongest WOMAN vs regular GIRLS
00:56
A4
Рет қаралды 51 МЛН
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2 МЛН
What type of pedestrian are you?😄 #tiktok #elsarca
00:28
Elsa Arca
Рет қаралды 32 МЛН
You can learn Arduino in 15 minutes.
16:34
Afrotechmods
Рет қаралды 10 МЛН
How to Use a Button with an Arduino (Lesson #5)
20:57
Science Buddies
Рет қаралды 99 М.
Arduino MASTERCLASS | Full Programming Workshop in 90 Minutes!
1:25:31
Programming Electronics Academy
Рет қаралды 2,7 МЛН
How To Wire It! Buttons & Switches
9:52
ItKindaWorks
Рет қаралды 141 М.
Arduino Tutorial 29: Using Push Buttons to Create Dimmable LED
41:38
Paul McWhorter
Рет қаралды 144 М.
MOSFETs and Transistors with Arduino
40:50
DroneBot Workshop
Рет қаралды 1,1 МЛН
PlatformIO: All you need to know in 10 Minutes!
10:56
J's e-shack
Рет қаралды 333 М.
Arduino Basics Handling Multiple States
10:35
learnelectronics
Рет қаралды 121 М.
This Game Is Wild...
00:19
MrBeast
Рет қаралды 154 МЛН