
ARDUINO WHILE LOOP BUTTON CODE
Now you can test the code by compiling it and uploading it to your Arduino. After we exit the void loop(), it is called again, etc etc. Just after reading the button’s state and printing it, we add a delay() of 100 milliseconds, which means that we’re going to do this action roughly 10 times per second.
ARDUINO WHILE LOOP BUTTON SERIAL
Then, we put the result of digitalRead() into the function Serial.println(), which will simply print the data on the Serial Monitor. When the button is pressed you will read LOW. Here, because we are in a pull up configuration (with INPUT_PULLUP), when the button is not pressed you will read HIGH. The result of that function is going to be either HIGH or LOW. To read the button’s state, we use the digitalRead() function, with one argument: the button’s pin. Now the pin for the button is initialized as input, and we enter the void loop(), which will be called again and again an infinite number of times. Code to read the push button’s state void loop() Note that in this tutorial I’m not going to dive deep into the explanation of INPUT_PULLUP, but if you want to know more, checkout this Arduino INPUT_PULLUP tutorial. As we don’t have any resistor in our circuit, we will use INPUT_PULLUP to use the internal pull up resistor of the Arduino board. And then we still have 2 options: INPUT or INPUT_PULLUP. In the void setup(), you use the pinMode() function with 2 arguments: first the button’s pin – here BUTTON_PIN will be replaced by “4” – and then the mode we want for the pin.Īs we are going to read data from the button – not write – we choose an input mode. This is how you initialize the push button in your code. What we do here is to initialize Serial communication, so that we can use the Serial Monitor to print the data we got from the button.

The void setup() function will be called first, only once. This way you just need to modify one line of the code, which is at the top of the program – and thus easy to find. This will make your life easier, especially if you want to plug the button to another pin later on. Code to setup the push button #define BUTTON_PIN 4įirst we create a #define for the button pin, so we don’t need to write “4” every time we want to use this pin in our code. Here is the code to print the button’s state 10 times a second. > Watch this video as an additional resource to this tutorial section:Īfter watching the video, subscribe to the Robotics Back-End Youtube channel so you don’t miss the next tutorials! Print the push button’s state What we’re trying to do here is to simply read the state from the button and print it 10 times per second. Also, if you want to know more about Arduino pins, check out this Arduino Uno pinout guide. In the following of this post we’ll talk more about this. This other wire goes to a digital pin, for example 4.Īs for now there is no resistor involved in the circuit.

If you have plugged the GND wire on the left side, then plug another wire on the right side, so they are not connected together.

Plug the push button in the middle of the breadboard, like on the picture.
ARDUINO WHILE LOOP BUTTON HOW TO
You are learning how to use Arduino to build your own projects?Ĭheck out Arduino For Beginners and learn step by step.

