The site for those who love tinkering with Arduino, Raspberry Pi & Co.
Arduino digitalRead, digital input and buttons

Arduino digitalRead, digital input and buttons


digitalRead is the Arduino function to know the status of a digital input

With our first project we were able to switch on or off some LEDs at our own pace, so we took care of activating or deactivating an external device via an OUTPUT signal.

If, on the other hand, we want to receive information from the devices we connect to Arduino we have to talk about inputs and therefore about INPUTs.

We have already seen that Arduino has 14 digital I / O pins: each of these can be configured as desired as a digital input (INPUT) or output (OUTPUT) in the setup phase of our program.

In our next program we will therefore take care of reading the status of a button (pressed or released) and we will use this information to turn on a led, normally off, when we press the button.

The instruction we will use for this purpose is digitalRead which allows us to know the status of one of the Arduino digital inputs.

For our circuit we will need in addition to the Arduino board:

  • 1 Led
  • 1 Resistor to configure the led
  • 1 button
  • 1 Pulldown resistor (10 KOhm)

The code we will use is this:

int led = 12;
int button = 11;
int button_state;

void setup () { 
 pinMode (led, OUTPUT); 
 pinMode (button, INPUT);

void loop () {
 button_state = digitalRead (button); 
 if (button_state == HIGH) {
   digitalWrite (led, HIGH);
 } else { 
   digitalWrite (led, LOW);

The program is very simple and for many it will be obvious how it works at first glance.

For the newest to programming in addition to the new digitalRead instruction we find another new and important instruction: the IF instruction.

The IF statement is fundamental in almost all programming languages existing in the world and is the instruction that allows us to perform certain operations (instructions), instead of others, whether certain conditions are met or not.

One of these conditions that we can test is precisely the state of our button and we will do it through the digitalRead function that allows us to know if there is a voltage on the arduino pin or not.

The operation of the program can be summarized as follows: “If the button is pressed then turn on the LED, otherwise turn it off”.

In the setup () function we took care not only to set an output pin, as in our first project, but also to set a second in input to read the status of the button.

The loop function is summarized as follows:

  1. Read the button status
  2. Is the button pressed?
    1. YES: Turn on the led
    2. NO: Turn off the led
  3. Go back to step 1
Arduino scheme with push button
Arduino scheme with push button

As for the connection of the button we have to connect one end to + 5V, the second one must be connected both to the pull down resistor which then ends up to ground and to our Arduino input pin.

For the resistance a 10KOhm one is fine.

Easy too, isn’t it?

Unfortunately, in reality, reading a button is a bit more difficult because the signal sent by our button is not very precise, but we will deal with this in the next article.

Arduino starter Kit
Arduino Starter Kit Ufficiale Per iniziare alla grande con Arduino

Ti è rimasta una domanda o un dubbio?

Iscriviti e chiedi nella community

Leave a Reply

Your email address will not be published. Required fields are marked *