How to use button to start program

How to start Arduino program by pressing a button.? I want to run Arduino code when I press a button.

Answer

See and compare two below codes:

void setup() { /******************* * your setup code *******************/ } void loop() { /****************** * your loop code ******************/ }
#include <ezButton.h> ezButton button(7); // create ezButton object that attach to pin 7; void setup() { button.setDebounceTime(50); // set debounce time to 50 milliseconds while (!button.isPressed()) button.loop(); // MUST call the loop() function first /******************* * your setup code *******************/ } void loop() { /****************** * your loop code ******************/ }

The wiring diagram for above code:

How to use button to start program

This image is created using Fritzing. Click to enlarge image

If you want to use a button to start/stop program each time button is pressed, see how to use button to start/stop program

※ NOTE THAT:

The above code uses the ezButton library, you can see how to install the library

Hardware for above code

1 × Arduino UNO Buy on Amazon
1 × USB 2.0 cable type A/B Buy on Amazon
1 × Button Buy on Amazon
1 × Breadboard Buy on Amazon
1 × Jumper Wires Buy on Amazon
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

The Best Arduino Starter Kit

※ OUR MESSAGES