Language: EN

salidas-digitales-en-arduino

Digital Outputs in Arduino

In previous tutorials we have seen how to use digital inputs and analog inputs to receive signals from the world. This way, we can read the state of a push button, read sensors, read the value of a resistor or the position of a potentiometer.

However, if we could only read, the automations would have much less utility than they really have. Now we are going to learn to use the outputs of Arduino to be able to perform actions in the world. Similar to how we did with the inputs, we are going to start with the digital outputs since they are simpler than the analog ones.

What is a digital output

We will remember that a digital signal can only vary between two values, which we call -Vcc and +Vcc. A digital output is a device that allows its voltage to vary to one of these two values through programming, and therefore allows us to perform actions with the environment.

In Arduino, in general, the voltages -Vcc and +Vcc correspond to 0V (GND) and 5V. However, some Arduino models operate at 3.3V, such as some Mini, Nano, and boards based on ARM processors like Arduino Due.

All of Arduino’s digital pins can act as digital outputs (which is why they are called I/O, input and output). But it is worth noting that analog pins can also be used as digital inputs and outputs.

The exact number of digital outputs depends on the model of the board we are using, as we saw in the entry What is Arduino? What model to buy?. In summary, Arduino Uno and Nano have 22 pins that we can use as digital outputs, Arduino Mini has 20, and we have up to 70 digital outputs in the Mega model. These are more than respectable figures, superior to most industrial automation systems.

Maximum current of a digital output

In general the digital outputs of automation systems are not designed to provide power, but to interact with electronics or other automations.

The maximum current that a pin can provide is 40 mA, although the recommended value is 20mA. In addition, there are additional restrictions in terms of power, such as the total sum of all outputs must be less than 300 mA, and in turn cannot exceed 150 mA per port.

This power is enough to turn on an LED, a small 9g servo motor, or turn on a sensor, but it is not enough to power larger loads. If we want to move a larger load, such as a DC motor, a servo, or even a relay, we will have to use an amplification stage, such as a BJT transistor.

It is not convenient to force the power limits for a prolonged period, the board could heat up and get damaged. The 20 mA limit per output means that, for a voltage of 5V, the resistance of the device we want to power must not be less than 200 ohms.

As a general rule, unless we know what we are doing, whenever we are going to connect a device to any output we will do it through a resistance of at least 300 ohms.

Assembly

For this tutorial no assembly is necessary. However, we can verify the correct operation of the digital outputs simply by measuring the voltage between the digital output and GND with a multimeter.

arduino-salida-digital-esquema

Code

The code for turning on is similar to what we saw in the tutorial Our first program in Arduino. In fact, it is exactly the same, changing 13 for the pin of the output we want to activate. We will remember that pin 13 is a special pin, which is connected to the LED integrated in the board.

So the following code, which is a modification of the Blink example file, turns on and off a digital output.

const int pin = 2;
 
void setup() {
  Serial.begin(9600);    //start serial port
  pinMode(pin, OUTPUT);  //define pin as output
}
 
void loop(){
  digitalWrite(pin, HIGH);   // set the pin to HIGH
  delay(1000);               // wait for a second
  digitalWrite(pin, LOW);    // set the pin to LOW
  delay(1000);               // wait for a second
}

Try it online

The following code, which we saw in the entry of the Arduino serial port, receives a character through the serial port to turn on or off a digital signal from the computer.

Through the serial port we send a character. If we write 0 the output turns off, and if we write 1 it turns on.

const int pin = 2;
int option;

void setup(){
  Serial.begin(9600);
  pinMode(pin, OUTPUT); 
}
 
void loop(){
  //if there is pending information
  if (Serial.available()>0){
    //read the option
    char option = Serial.read();
    if (option == '0' )  //if the value is 0
    {
         digitalWrite(pin, LOW);  //turn off the pin
    }
    else if (option == '1' )
    {
         digitalWrite(pin, HIGH;  //turn on the pin
    }
    delay(200);
  }
}

Try it online

So far the basic aspects of digital outputs in Arduino. In future entries we will see the analog outputs, and delve into more advanced aspects of digital outputs.

Download the code

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