Language: EN

entradas-digitales-en-arduino

Digital Inputs in Arduino

One of the most interesting functions (if not the most) of Arduino and generally of all automata is their ability to interact with the physical world.

We can, for example, take voltage measurements, obtain readings from a wide variety of sensors, turn on devices, or control motors and actuators.

This interaction is largely carried out through the use of both digital and analog inputs and outputs. In the following entries, we will learn to use these functions.

Let’s start with digital inputs because they are the simplest (although we will see that the other functions are not much more complicated).

Although we are using Arduino as a platform, it is important to emphasize that most concepts are applicable to any general automaton. In the end, we will see the code and assembly in Arduino, but first, we will briefly review some general theory.

What is a digital input?

A digital signal is a variation of voltage between -Vcc and +Vcc without passing through intermediate values (for example, 0 and 5V).

Therefore, a digital signal has only two states,

  • The lower value -Vcc is associated with a logical value LOW or logical 0
  • The upper value +Vcc is associated with HIGH or logical 1.

Of course, in the physical world, voltage signals are actually continuous. There is no signal that “magically switches from one voltage to another”.

The process of digital reading is a discretization process. We convert an analog measurement (the voltage value we measure) into a digital signal (“fictitious”).

Making this conversion is not very difficult. A digital input compares the real measurement with a threshold voltage value.

  • If the measured value is greater than the threshold voltage, it returns HIGH
  • If the measured value is lower, it returns LOW.

The threshold voltage value varies from one automaton to another, and it does not necessarily have to remain constant over time. But, in general, it is normal that the threshold voltage is close to the midpoint between -Vcc and +Vcc.

However, we must avoid measuring voltages near the threshold voltage because they can cause incorrect readings.

Digital inputs in Arduino

In Arduino, digital inputs and outputs share PINs. For this reason, they are referred to as digital I/O (input / output).

This means that the same pin can perform both input and output functions, although, logically, not simultaneously. It is necessary to configure an I/O pin as either input or output (in our program).

Arduino has a different number of digital I/O depending on the model, as we saw in the entry What is Arduino? What model to buy?. For example, Arduino UNO has 16 digital I/O and Arduino MEGA has 54.

In Arduino, the usual supply voltages are 0V and 5V. In this case, the threshold voltage will be very close to 2.5V. Therefore:

  • If we measure a voltage with an intermediate value between 0 to 2.5V, Arduino will return a LOW reading
  • If we measure a value between 2.5V and 5V, it will return HIGH.

Never introduce a voltage outside the range of 0V to 5V into a digital or analog input or we risk damaging the corresponding pin and rendering it permanently unusable.

Connecting digital inputs

Let’s assume we want to use Arduino to connect it with a sensor, or any other device, that has a continuous voltage output between 0V to 5V.

For now, we will not consider the possibility of the digital input being completely disconnected, something we will address in the next entry “Reading a button with Arduino”.

We can read the voltage value from the sensor with a schematic like the following.

entrada-digital-arduino-1

The reading will yield a value:

  • HIGH if the voltage value is greater than the threshold voltage (2.5V)
  • LOW if the voltage value is lower

Code in Arduino

The code to perform the reading is really simple. We just need to configure a digital I/O as input with pinMode() and perform the reading with digitalRead().

int pin = 2;
int value = 0;

void setup() {
  Serial.begin(9600);   //start serial port
  pinMode(pin, INPUT);  //define pin as input
}

void loop(){
  value = digitalRead(pin);  //digital reading of pin

  //send message to serial port based on the read value
  if (value == `HIGH`) {
      Serial.println("On");
  }
  else {
      Serial.println("Off");
  }
  delay(1000);
}

Try it online

Pins configured as inputs are in a high-impedance state, meaning they behave like very high value resistors (on the order of 100 megaohms). Therefore, a negligible current flows through them.

In reality, the Arduino pins (Atmega) start by default as inputs, so it would not be strictly necessary to configure them as inputs, although it is a convenient practice.

Reading values greater than 5V

We have mentioned that under no circumstances should we introduce a voltage outside the range of 0 to 5V into an Arduino pin, or we risk permanently damaging it.

If we want to measure a voltage level higher than the supply limits, the most convenient way is to use a simple voltage divider.

For example, to read a digital signal between 0 to 12V, we can use a schematic like the following.

entrada-digital-divisor-tension-arduino-1

With this configuration, the Arduino digital pin will receive a voltage that varies between 0 to 3.84V, enough to trigger the threshold voltage, and below the supply limit.

The values of the resistors to be used depend on the voltage we want to read, and on the sensor’s impedance.

In general, they must meet the following conditions

  • Provide a voltage greater than the threshold voltage
  • Be much greater than the equivalent impedance of the device being measured.
  • Be negligible compared to the impedance of the Arduino input.
  • Limit the current flowing through them to minimize losses.
  • Be capable of dissipating the power they will support.

You can use the voltage divider calculator to calculate resistor values that meet these requirements.

In the next entry, we will see how to use the digital input to read the state of a button.

Do not use this system to read voltages greater than 35V, or for alternating current devices unless you are very sure of what you are doing. It is very likely that the resistors will not hold up.

Download the code

All the code from this entry is available for download on Github. github-full