Toggle/Momentary Push Button with Arduino - Tutorial

preview_player
Показать описание
in this video, i have explained how to use a push button to blink an LED bulb with arduino. there are 2 ways to use a push button. toggle and momentary.

a toggle switch will turn the led on if pressed once,
and turn it off when pressed again.

a momentary button will keep the led on until the button is pressed.
and turn it of when released

we will require an arduino
push button
led bulb
4.k7 ohm resistor
and some wires

connect a pull down resistor from -ve leg of the button to the breadboard ground
connect the breadboard ground to arduino ground
connect breadboard vcc to arduino 5v
connect signal pin after first leg of the resistor to pin 2 of the arduino
connect the led to pin 13 and ground of arduino

for momentary switch, the code is already present in arduino examples.
go to examples/digital/button

upload the code and you can control the led via the button.

pressing the button will turn the led on, and releasing it will turn it off.

in the toggle switch code, we have a variable code called status, which store the
last state of the button.
if the state is true or on, it changes it to false or off, and vice versa.

upload the code and use the button.

when the button is pressed once, the led turns on
and when the button is pressed again, the led turns off.

hope you found the tutorial useful

also do check out these cool 3d Printers:

#Arduino #Button #Tutorial #SuperbTech
Рекомендации по теме
Комментарии
Автор

I was looking for something simpler than most of what I was seeing. Your code is elegant compared to a lot of others. Love the 'while' loop.

demiurgiac
Автор

Thanks so much for this tutorial! I was able to simplify my code significantly because of it!

I actually used this to toggle a motor on and off rather than just an LED, and I used an Arduino Nano. It was a pretty seamless change, however, I did have one slight hiccup.

I'm still a learning circuits and Arduino. For this project, I started working on a benchtop test circuit with a different motor than the one I would be using in my final design because that motor is already mounted onto something. When I got the test circuit to work, I switched to the motor I would be using for my final design. At that point, the press of the button would trigger the motor on, but it would stop after milliseconds. At first I thought it might be the resistor I was using, but eventually I realized it was the code itself. I did some research into true/false values and realized the "bool" data type is specifically made for true/false values. I changed the variable definition of "status" from int to bool, tried again, and it worked perfectly!

Thanks again for this tutorial!

lizzymceleney
Автор

Thank you so much! This one actually worked unlike the other 3 tutorials I just watched.

jaredo
Автор

best tutorial, i tried 3 diferents codes nothing works . this work amazing ..great to find you mate ..keep the best :)

junniorrT
Автор

you just saved my project this is the best code for toggle button

russellhertel
Автор

You need a digitalread(button) in the while loop. Otherwise it will get stuck in that loop forever.

luongmaihunggia
Автор

epic man only thing that i would improve in this video is a lil bit better explenation on the code :DDD but anyway rly nice

daveblackstudios
Автор

Thank you! This helped me a lot. I like how simple and effective the code is.

grantscott
Автор

Thanks! but how to make the LED blinking when momentary push button applied, then totally off when applied again?

ikhsanfarhanmaulana
Автор

You are the new God I pray to 🛐🛐🛐 Thank you so much!!!1!!

erickreychan
Автор

Sir, you deserve likes....am sorry I have only one like...

mohitarora
Автор

Thank u for post this but I have a problem the ]while(digitalRead(button) ==true); says there is an error can u please help me

chaklecharcoal
Автор

Very nice!! I needed this. Thanks so much!! :)

ajbeal
Автор

How can I can i get the LED to flash when its on?

talenjohnston
Автор

That is very helpful, thank you. An excellent and very clear video.

steveseabrook
Автор

SUPER VERY NICE COOL
IF CAN EXPLAINED THE ON AND OFF
CODE IT WILL NICE
THANKS

creatingcreations
Автор

Very good tutorial. The code windows are too small to be readable.

KW-eipi
Автор

hi.. Thanks! I am trying scale this up to more than one button and light.. I am stuck with defining status on each LED. Your help would be much appreciated

simonhead
Автор

The code for examples/digital/button has changed in Arduino IDE 2.1.0. Will it still work?

RonPiggott
Автор

I NEED 4 ON AND OFF OUTPUT USING BUTTONS HOW TO DO??

electronicsideas