Language: EN

salidas-digitales-en-arduino

Digital Outputs in Arduino

In the previous entry, we saw how to use digital inputs to receive signals from the world. However, if we could only take readings, automation wouldn’t be very useful (at least, it would be much less useful than it is).

Now we are going to learn how to use Arduino outputs to perform actions in the world. We’ll start with digital outputs, as they are simpler than analog ones.

What is a digital output

Recall that a digital signal can only vary between two values, which we call -Vcc and +Vcc (for example, 0 and 5V).

A digital output is a device that allows its voltage to vary to one of these two values through programming. Therefore, we can use them 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 the Arduino Due.

All digital pins on Arduino can act as digital outputs (that’s why they are called I/O, input, and output).

It is less known that all analog pins can also be used as digital inputs and outputs.

The exact number of digital outputs depends on the board model we are using, as we saw in the entry What is Arduino? Which model to buy?.

In summary,

  • Arduino Uno and Nano have 22 pins that we can use as digital outputs
  • Arduino Mini has 20 digital outputs
  • Arduino Mega has up to 70 digital outputs

These are more than respectable figures, exceeding most industrial-type automation.

Maximum current of a digital output

In general, digital outputs of automata are not designed to provide power, but to interact with electronics or other automata.

The maximum current that a pin can provide is 40 mA, although the recommended value is 20mA. Additionally, there are further restrictions regarding power, such as

  • The total sum of all outputs must be less than 300 mA
  • Additionally, they cannot exceed 150 mA per port.

This power is sufficient to light an LED, a small 9g servo motor, or power some 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 (like a BJT transistor, MOSFET).

It is not advisable to push the power limits for extended periods as the board could overheat and be damaged. The limit of 20 mA per output means that, for a voltage of 5V, the resistance of the device we want to power should not be less than 200 ohms.

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

Digital output connection

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

arduino-salida-digital-esquema

Code

The code to turn 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 remember that pin 13 is a special pin, connected to the built-in LED on the board.

Thus, 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 one second
  digitalWrite(pin, LOW);    // set the Pin to LOW
  delay(1000);               // wait one second
}

Try it online

The following code, which we saw in the entry about the Arduino serial port, receives a character through the serial port to turn a digital signal on or off 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){
    //we 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 analog outputs and delve into more advanced aspects of digital outputs.

Download the code

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