Arduino digitalWrite() Digital Output (GPIO) Tutorial

This is a comprehensive guide for Arduino digitalWrite function and Arduino digital output in general. We’ll cover Arduino GPIO basics and Arduino digitalWrite function with examples. And we’ll also discuss some advanced topics like Arduino digitalWrite speed and how to implement a fast digitalWrite and Arduino port manipulation.

You’ll also learn how to use Arduino’s analog pins as digital output pins, Arduino toggle pin state, digitalWrite to multiple pins, and much more. Without further ado, let’s get right into it.

Table of Contents

  1. Arduino GPIO
  2. Arduino Digital Output
  3. Arduino pinMode() Function
  4. Arduino digitalWrite() Function
  5. Arduino Digital Output Examples
  6. Arduino digitalWrite Speed
  7. Arduino Toggle Pin
  8. Arduino Port Manipulation
  9. Arduino Analog Pins As Digital Output
  10. Download Attachments
  11. Arduino digitalWrite & GPIO Recap

Arduino GPIO

Arduino GPIO (General-Purpose Input Output) Pins are the IO pins of the Arduino board. Those pins can either be used as digital input or output pins depending on your application’s need.

If you need to read the state of a push button, you’d need to configure a GPIO pin as an input pin. On the other hand, if you need to drive an LED ON or OFF, you’d need to configure a GPIO pin as an output pin to control the LED.

Arduino GPIO pins can be used for various applications like sensor reading, module interfacing, and controlling output devices such as LED units, motors, or anything else.

Arduino-GPIO-Pin-Diagram
Pinout of ARDUINO Board and ATMega328PU” by pighixxx is licensed under Creative Commons Attribution-Share Alike 4.0 International


Arduino Digital Output

The Arduino GPIO (digital IO) pins can be configured as output pins to be used for driving output devices (such as LEDs, motors, relays, etc). In order to configure a digital IO pin as an output, we need to use the pinMode() function. Let’s say we want to configure Arduino’s pin number 8 to be an output pin. Here is how to do it in code.

And now, we can use the Arduino digitalWrite() function to control the pin state ( HIGH or LOW).

Arduino Output Voltage

The output voltage of Arduino digital pins is 5v (when the digital pin is HIGH) or 0v (when the digital pin is set to LOW). And make sure to not connect any output pin from a sensor or IC to an Arduino output pin. If there is a voltage difference, a significant amount of current will flow through the IO pin and potentially causing permanent damage to the Arduino’s output pin.

Arduino-INPUT_PULLUP-pin-diagram

There are two protection diodes for each IO pin that will clip excessive over/under voltage away from damaging any IO pin. However, you still need to be careful with the voltage levels of various components in your system and do level-shifting if needed.

Arduino Output Current

The Arduino output current for any IO pin can be up to 40mA. Any Arduino digital pin can source or sink up to 40mA of current. This is the maximum absolute current that can be sourced from or sunk to any Arduino’s IO pin at any given time. So make sure you’re not over-driving the IO pins in your application.


Arduino pinMode() Function

The Arduino pinMode() function sets the behavior of a specific digital IO pin to behave as an output pin or an input pin. It can also enable the internal pull-up resistor for input pins if the mode INPUT_PULLUP is selected. However, the mode INPUT will set your IO pin in input mode and explicitly disable the internal pull-up resistor.

The Arduino pinMode() function takes only 2 parameters:

  • pin: the Arduino pin number to be configured.
  • mode: it can be(  INPUT,  OUTPUT, or  INPUT_PULLUP).

For example, to set pin number 8 to be an output pin. Here is how to do it:

To learn more about the Arduino pinMode() function, INPUT_PULLUP, INPUT_PULLDOWN modes, pin floating state, and how to implement internal or external pull-up, it’s highly recommended that you check out the tutorial linked below.

💡 Also Read
Arduino pinMode Function & Arduino INPUT_PULLUP Explained

This article will give more in-depth information about the Arduino pinMode() function, INPUT_PULLUP, INPUT_PULLDOWN modes, pin floating state, and how to implement internal or external pull-up


Arduino digitalWrite() Function

The Arduino digitalWrite() function is used to set the pin state of a digital IO pin that’s configured as an output pin. The digital output state can either be HIGH or LOW.

The Arduino digitalWrite() function takes only 2 parameters:

  • pin: the Arduino pin number that we want to change its state.
  • value: The desired pin state HIGH or LOW

For example, to set pin number 8 to be HIGH, we’ll do it as follows.

You can also change the HIGH macro and use 1 instead and it’ll do the exact same thing.


Arduino Digital Output Examples

Next, we’ll put everything we’ve learned so far together to create a couple of Arduino example projects. In the first example, we’ll configure an output pin to control an LED. In the second example, we’ll add a push button to use it as an input device to control the output LED state.

1. Arduino LED Blinking Example

This is the first project for anyone just getting started with Arduino shall do. We’ll turn an LED on and off for a second each time. And will be using the built-in LED which is connected to Arduino’s IO pin13. But as it’s not fun enough, we’ll hook the pin to another external LED on a breadboard to show the same blinking effect on an external LED.

Wiring

Here is how to hook up the external LED output. (note that the LED current limiting resistor is 330Ω)

Arduino-LED-Blinking-Project-Wiring

Example Code

Here is the full code listing for this example.

Code Explanation

The code example simply set the Builtin-LED IO pin to be output and sends output signal High and Low with 1 second time delay after each state change.

setup()

in the setup() function, we initialize the digital IO pin ( LED_BUILTIN or pin 13) to be an OUTPUT pin.

loop()

in the loop() function, we use the digitalWrite() function to set the pin state to high and low. And we also use the delay() function to insert a 1000ms (1sec) time delay after each state change.

Simulation

Here is the simulation result for this project on the TinkerCAD simulator.

You can check this simulation project on TinkerCAD using this link.

Testing Results

Here is a demo video for testing this project on my Arduino UNO board.

2. Arduino LED + Button Example

In this second project, we’ll add a push button to the circuit and use it to control the LED. What we’ll implement is called a “momentary button action” in which the LED will turn on as long the button is held down and it’ll go off whenever the button is released. The button will be connected to Arduino’s IO pin 4, and the LED will stay on the same pin 13.

Wiring

Here is how to hook up the external LED output. (note that the LED current limiting resistor is 330Ω). The push button here is connected in a pull-down configuration with a pull-down resistor of 10kΩ.

Arduino-LED_Button-Example-Wiring

Example Code

Here is the full code listing for this example.

Code Explanation

The code example simply set the Builtin-LED IO pin to be output and defines BTN_PIN to be IO pin4 and sets it as an input pin. Then we read the digital state of the Button pin and turn on the LED while the button is pressed, when the button is released, the LED will be driven low again.

This is the button pin definition (assignation)

setup()

in the setup() function, we initialize the digital IO pin ( LED_BUILTIN or pin 13) to be an OUTPUT pin. And the BTN_PIN to be an input pin.

loop()

in the loop() function, we continuously read the state of the button’s pin. While it’s held down (state=1), the LED will be turned ON. When the button is released, the while loop condition breaks and the LED will be driven back to the LOW state.

Simulation

Here is the simulation result for this project on the TinkerCAD simulator.

You can check this simulation project on TinkerCAD using this link.

Testing Results

Here is a demo video for testing this project on my Arduino UNO board.


Arduino digitalWrite Speed

Changing an IO pin state using the Arduino digitalWrite function is not immediate, it takes some time which is the execution time of the digitalWrite() function itself.

We can measure the Arduino digitalWrite function’s speed (execution time) using a timer or the timer-based micros() function. Which takes a timestamp of the timer register and converts it to a time unit in µS (microsecond).

Here is the test code that I’ve come up with, which takes a T1 timestamp before calling the digitalWrite() function 4 times, then it takes another timestamp T2. Therefore, the total execution time for the 4 function calls = T2 – T1. And to get the execution time for a single digitalWrite() function’s call, we’ll divide the (T2-T1)/4 to get the result.

After running the code above on my Arduino UNO board, it turned out that the built-in Arduino digitalWrite() function’s speed is 4µS exactly. Which is quite a lot of time for just changing a single IO pin’s state.

This moderately high execution time can be justified if you take a look at the digitalWrite() function’s implementation (which is available on GitHub). This turns out to be doing some extra logic, bit mask creation, validity checks, and other operations the reason why it takes this slightly high execution time.


Arduino Toggle Pin

Arduino pin toggle is the act of changing an output pin state from HIGH to LOW or from LOW to HIGH. In other words, it’s flipping the state of the pin from HIGH to LOW and vice versa.

This can be achieved by a couple of methods but I’ll show you the easiest one among them all. Which is to read the current pin state, invert it, and write back the inverted state using the digitalWrite() function.

Let’s say you’d like to toggle the Arduino pin number 8, here is how to do it.


Arduino Port Manipulation

Arduino can also be programmed with register-level addressing like Embedded-C programming for microcontrollers, it’s called Arduino Port Manipulation. Which is basically writing directly to the port registers of the GPIO ports in the Arduino’s microcontroller.

Arduino Fast digitalWrite

By doing Arduino Port Manipulation, we can create a lot simpler implementation for pin state change by directly accessing the port register and ignoring any input verification or other logic. It’s definitely not going to be as safe as the built-in digitalWrite() function and you should do this only if needed.

This is going to be way faster than the digitalWrite() function but it requires additional macros definitions. The Arduino Port Manipulation can provide a digital pin state change as fast as 0.25µS per operation. Which is almost 16x times faster than the Arduino built-in digitalWrite() function.

Arduino digitalWrite Multiple Pins

You can directly access multiple pins by using the Arduino Port manipulation as well. This is rarely needed but in case you’ve got an application for it and you need to update the state of digital port pins all at once, it’s the only way to do it.

It’s highly recommended to check out the following tutorial below for Arduino Port Manipulation which discusses the topic in more detail with example use cases like (Fast digitalWrite implementation and digitalWrite for multiple pins at the same time).

💡 Also Read

This article will give more in-depth information about Arduino port manipulation and direct register access to control IO ports. How to use DDR, PORT, and PIN registers to control IO ports/pins. And also the implementation of fast digitalWrite & digitalRead macros in Arduino.


Arduino Analog Pins As Digital Output

Arduino Uno has a total of 14 GPIO pins, out of which 6 pins (from A0 to A5) are analog pins.

The question is can we use Arduino analog pins as digital output pins? The answer is YES, and here is how to do it.

First of all, the ADC is a valuable resource in any microcontroller. Under certain circumstances, you’d sacrifice the analog input channels just to get extra digital IO pins. Sometimes an external IO expander would be a better solution instead of sacrificing the ADC analog input channels.

But let’s say you’ve made your decision and you need to have an extra digital pin or two. And the analog input pins are the only hope you’ve got. Here is how you’d go about implementing this.

Just deal with the analog input pins (A0 – A5) as if they’re normal digital IO pins. This means you first need to call the pinMode() function to set the pin mode to OUTPUT. And then call the digitalWrite() function to change the pin state to HIGH or LOW. Here is an example code:

And similarly, if you need to set an analog pin to be a digital input pin, you’ll call the pinMode function and set the mode to INPUT. Then, you can read the digital pin state using the digitalRead() function as you’d do for any digital IO pin (0-13).

Here is an example of using an Arduino analog input pin as a digital input pin. Note: here the A0 pin is assumed to be externally pulled-up to Vcc with a 10kΩ resistor.

💡 Also Read
Arduino Analog Pins As Digital Output Input Pins

This guide will give more in-depth information about Arduino analog pins and how to use them as digital IO pins for digitalWrite & digitalRead operations.


Parts List

Here is the full components list for all parts that you’d need in order to perform the practical LABs mentioned here in this article and for the whole Arduino Programming series of tutorials found here on DeepBlueMbedded. Please, note that those are affiliate links and we’ll receive a small commission on your purchase at no additional cost to you, and it’d definitely support our work.

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting my work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports our community.


Arduino digitalWrite & GPIO Recap

To conclude this tutorial, we’ll highlight the fact that the Arduino digitalWrite function is used to change the digital state of an output pin (HIGH or LOW). And it can take around 4µS of execution time which sets an upper limit for the maximum speed at which you can update an IO pin. Acceleration is possible by using Arduino Port Manipulation but it’s a more risky business and there should be an urgent need before attempting this kind of optimization.

If you’re just getting started with Arduino, you need to check out the Arduino Getting Started [Ultimate Guide] here. And follow this Arduino series of tutorials to learn more about Arduino Programming.

💡 Also Read
Getting Started With Arduino Programming For Beginners

This is the ultimate guide for getting started with Arduino for beginners. It’ll help you learn the Arduino fundamentals for Hardware & Software and understand the basics required to accelerate your learning journey with Arduino Programming.


FAQ & Answers

What is digitalWrite() in Arduino?

It’s a built-in function in Arduino core that is used to set the digital output pin state to HIGH or LOW.

Can I digitalWrite to an analog pin in Arduino?

Yes, you can. Just treat it as any digital IO pin and use the (A0-A5) as the pin number when you can the pinMode or digitalWrite functions.

How Fast is the digitalWrite() function?

It takes around 4µS to complete execution. Acceleration is possible by using Arduino Port Manipulation but it’s a more risky business and there should be an urgent need before attempting this kind of optimization.

What is the difference between analogWrite and digitalWrite?

The Arduino digitalWrite function gives out a binary digital state to the output pin either a HIGH or LOW. On the other hand, the analogWrite function sets the duty cycle of a PWM channel which is a continuous HIGH_LOW signal with a controllable duty cycle.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?