HomeArduino TutorialsArduino digitalWrite() and digitalRead() Tutorial

Arduino digitalWrite() and digitalRead() Tutorial

Getting started with Arduino becomes much easier once you understand how to work with its GPIO pins. Two of the most important functions for beginners are digitalWrite() and digitalRead(). With digitalWrite(), you can send signals to control outputs like LEDs, buzzers, and relays. With digitalRead(), you can receive signals from inputs such as push buttons, switches, and sensors.

Arduino digitalWrite() and digitalRead() Tutorial

In this Arduino tutorial, we will explain how both functions work, their syntax, and how to use them in simple projects. By the end, you’ll know how to make your Arduino board read inputs and control outputs, which is the foundation for almost every Arduino project.

Arduino Digital Output

The Arduino GPIO pins can work as either inputs or outputs. When you set a pin as an output, you can use it to control devices like LEDs, buzzers, motors, or relays. To configure a pin as an output, you need the pinMode() function.

For example, if you want to set pin 13 as an output, the code looks like this:

pinMode(13, OUTPUT);

Once the pin is configured, you can use the digitalWrite() function to set it either HIGH (ON) or LOW (OFF). This makes it easy to control connected components.

Arduino Digital Output Example

void setup() {
  pinMode(13, OUTPUT);       // Set pin 13 as output
  digitalWrite(13, HIGH);    // Set pin 13 as HIGH (5V or 3.3V)
}
void loop(){
}

setup() runs once when the Arduino starts. pinMode(13, OUTPUT) will set the pin 13 as an output pin and digitalWrite(13, HIGH) will pull the pin to HIGH (5V or 3.3V). If you connect a voltmeter to measure the voltage of this pin, you should be able to see how much your Arduino outputs.


Key Points to Remember

  • Always set the pin as OUTPUT using pinMode() before calling digitalWrite().
  • HIGH = ON (5V or 3.3V), LOW = OFF (0V).
  • Use resistors when connecting LEDs to protect the pin.
  • Do not connect high-current devices directly; instead, use a relay, transistor, or MOSFET.

Arduino Output Voltage

On most Arduino boards, the digital output voltage is:

  • HIGH = 5V (or 3.3V on some boards)
  • LOW = 0V (Ground)

Important: Never connect one Arduino output pin directly to another output pin, or to the output of a sensor or IC. If two outputs with different voltages are connected, a large current may flow, which can damage the Arduino pin permanently.

Each GPIO pin also has protection diodes that help prevent damage from small voltage spikes. Still, it’s best to always match the voltage levels of your components. If you’re using a mix of 3.3V and 5V devices, you should use a level shifter for safety.


Arduino Output Current

Every Arduino digital pin can supply or receive a small amount of current.

  • The maximum current per pin is 40 mA, but this is the absolute limit.
  • The safe recommended current is about 20 mA per pin.

This is enough for driving small components like LEDs (with a resistor) or buzzers. But if you need to control devices that require more current—like motors, relays, or high-power LEDs—you must use an external driver circuit, transistor, or relay module.

Arduino digitalWrite() Function

The digitalWrite() function in Arduino is used to set the state of a digital pin. You can use it to make a pin HIGH (turn ON, send 5V or 3.3V) or LOW (turn OFF, send 0V). This function is one of the most basic yet powerful tools for controlling external devices with Arduino.

Syntax of digitalWrite()

digitalWrite(pin, value);
  • pin → The GPIO pin number you want to control.
  • value → Can be HIGH or LOW.

Wiring diagram for LED connected to Arduino

The image below shows how the LED is connected to the Arduino Uno.

LED connected to Arduino Uno. The Arduino pin will be set as output to control the LED.

Even though Arduino already has an onboard LED connected to pin 13, I have added an external LED as well to demonstrate how we can connect an LED to the Arduino.

  • Connect the anode (long leg) of the LED to digital pin 13 of the Arduino through a 330 Ω resistor.
    • The resistor is used to limit the current and protect the LED from burning out.
  • Connect the cathode (short leg) of the LED directly to the GND pin of the Arduino.

Arduino digitalWrite() code to blink the LED

Now that the LED is connected to the Arduino Uno, we will write a code to blink this LED at a defined rate. The digitalWrite function can only Set or Reset the pins, therefore, we will add a small delay between the Setting and Resetting of the pin to have a blinking effect.

The code below blinks the LED every 1 second.

#define LED_PIN 13  // LED is connectde to Pin 13

void setup() {
  pinMode(LED_PIN, OUTPUT);       // Set LED Pin as output
}

void loop() {
  digitalWrite(LED_PIN, HIGH);    // Set Led Pin High to turn the LED ON
  delay(1000);              // Wait 1 second (1000ms)
  digitalWrite(LED_PIN, LOW);     // Reset Led Pin to Low to turn the LED OFF
  delay(1000);              // Wait 1 second (1000ms)
}

Let’s break down this code in small parts to understand it better.

Pin Definitions

#define LED_PIN 13

#define LED_PIN 13 tells the Arduino that LED_PIN is just another name for pin 13. This makes your code easier to read. If you later move the LED to pin 12, you just change this line.


setup () function

void setup() {
  pinMode(LED_PIN, OUTPUT);       // Set LED Pin as output
}

setup() runs once when the Arduino starts. pinMode(LED_PIN, OUTPUT) tells Arduino: “Pin 13 will send out voltage, not read it.” Since an LED lights up when voltage flows through it, this pin needs to be an output.


loop () function

void loop() {
  digitalWrite(LED_PIN, HIGH);    // Set Led Pin High to turn the LED ON
  delay(1000);              // Wait 1 second (1000ms)
  digitalWrite(LED_PIN, LOW);     // Reset Led Pin to Low to turn the LED OFF
  delay(1000);              // Wait 1 second (1000ms)
}

The loop() function runs forever. Inside this function, we will set the LED_PIN (Pin 13) HIGH and LOW after every 1 second. This will have a blinking effect on the LED connected to the this pin. The LED will keep blinking forever as long as the Arduino is connected to the power supply.


Arduino LED blinking – Testing Results

After uploading the example code to your Arduino, here’s what you should observe:

The LED connected to pin 13 turns ON for 1 second, then OFF for 1 second. This cycle repeats continuously, showing that the digital output pin is working correctly. This confirms that your pin has been correctly configured as OUTPUT using pinMode().

Arduino Digital Input

Just like Arduino pins can act as outputs, they can also work as digital inputs. In this mode, the pins are used to read signals from external devices such as buttons, switches, and digital sensors. With digital input, Arduino can detect whether a pin is receiving a HIGH (ON) signal or a LOW (OFF) signal, which makes your projects interactive.

To use a GPIO pin as an input, you need the pinMode() function. For example, if you want to set pin 2 as an input:

pinMode(2, INPUT);  // Set pin as input

Once the pin is configured, you can use the digitalRead() function to Read it.

Arduino Digital Output Example

void setup() {
  pinMode(2, INPUT);  // Set pin as input
}
void loop(){
  int buttonState = digitalRead(2);
}

The setup()function runs once when the Arduino starts. pinMode(2, INPUT) will set the pin 2 as an input pin. We can read the state of this pin inside the loop(), which runs forever. The current state of the Pin 2 will be stored in the buttonState variable.

  • If the pin is receiving voltage, digitalRead() will return HIGH.
  • If the pin is connected to ground, it will return LOW.

Problem: If nothing is connected to the pin, it “floats”, meaning it can randomly read HIGH or LOW. This can make your program behave unpredictably.


Using INPUT_PULLUP

Arduino also provides an internal pull-up resistor that can be activated using:

pinMode(2, INPUT_PULLUP);  // set input with default state as HIGH

INPUT_PULLUP also sets the pin to read voltage, but it turns on a built-in pull-up resistor. This means the pin is internally connected to 5V (HIGH) through a resistor, so it stays HIGH when the button is not pressed.

When you press the button and connect the pin to GND, it reads LOW. This feature reduces wiring complexity because you don’t need an external resistor.


Why use INPUT_PULLUP instead of INPUT

FeatureINPUTINPUT_PULLUP
Pin state without inputFloating, randomHIGH (stable)
External resistor neededYesNo (built-in)
Button wiringConnect to GND, need resistorConnect to GND only, simpler wiring
Risk of errorHigh (false reads)Low (stable, predictable)

Arduino Input Voltage & Current

The Arduino reads digital inputs as either:

  • HIGH = 5V (or 3.3V on some boards)
  • LOW = 0V (GND)

Never apply voltage higher than the board’s rated voltage to an input pin, as this can permanently damage the Arduino.

Arduino input pins draw a very small current (microamps), which is safe to connect to switches, sensors, and buttons directly. The pin only “listens” to the voltage and does not supply power.


Common Uses of Digital Input

Digital input is the basis of many Arduino projects. Some common examples include:

  • Buttons and Switches → Detecting user input
  • Motion Sensors (PIR) → Detecting movement
  • IR Sensors → Reading obstacle detection signals
  • Limit Switches → Used in robotics and CNC machines

Arduino digitalRead() function

The digitalRead() function is used to check the current state of a digital input pin. It returns either:

  • HIGH (1) → The pin receives voltage
  • LOW (0) → The pin receives 0V

This is essential for reading buttons, switches, sensors, or any device that sends a digital signal to the Arduino.

Syntax of digitalRead()

digitalRead(pin);
  • pin → The digital pin number you want to read.
  • The function returns HIGH or LOW, depending on the pin state.

Wiring diagram to connect a button to Arduino

The image below shows how a button can be added to the Arduino Uno.

LED and button connected to Arduino Uno pins. The button will be the input to the Arduino and LED is the output.

We are going to use the INPUT_PULLUP mode for reading the Pin 2. This means that the pin will be HIGH in the default state. The button the connected between the Pin 2 and ground of Arduino.

When the button is pressed, the Pin 2 will connect to the ground and the the pin state will change to LOW.


Arduino digitalRead() example (Reading a Button)

Before reading a pin, you must configure it as an input using pinMode(). You can also use INPUT_PULLUP to enable the internal pull-up resistor, which is useful for buttons.

const int buttonPin = 2;   // Button connected to pin 2
const int LED_PIN = 13;    // LED connected to pin 13

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);    // Button pin uses internal pull-up
  pinMode(LED_PIN, OUTPUT);            // LED pin is output
}

void loop() {
  int buttonState = digitalRead(buttonPin);  // Read button state

  if (buttonState == LOW) {                 // If button is pressed
    digitalWrite(LED_PIN, HIGH);           // Turn LED ON
  } else {                                  // If button is not pressed
    digitalWrite(LED_PIN, LOW);            // Turn LED OFF
  }
}

Let’s break down this code in small parts to understand it better.

Pin Definitions

const int buttonPin = 2;   // Button connected to pin 2
const int LED_PIN = 13;     // LED connected to pin 13

First define the pins used by the button and LED. This make it easier for us to change them later.


setup() Function

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);    // Button pin uses internal pull-up
  pinMode(LED_PIN, OUTPUT);            // LED pin is output
}

The setup() runs once when the Arduino starts. Here we will configure the buttonPin as Input with internal Pull-Up resistor and the LED pin as output. The Internal Pull-Up will keep the button state HIGH while the button is not pressed.


loop() Function

  int buttonState = digitalRead(buttonPin);  // Read button state

  if (buttonState == LOW) {                 // If button is pressed
    digitalWrite(LED_PIN, HIGH);           // Turn LED ON
  } else {                                  // If button is not pressed
    digitalWrite(LED_PIN, LOW);            // Turn LED OFF
  }

The loop () function runs forever. Here we will continuously read the state of the buttonPin using the function digitalRead(). Since this pin is connected to the 5V (or 3.3V) using the internal Pull-UP, the pin will read HIGH by default. In this situation the LED will be turned OFF.

When the button is pressed, the pin is pulled LOW to the ground and the function digitalRead()will also return LOW. At this point we will turn the LED ON.


Arduino Button Read – Testing Results

After uploading the example code to your Arduino, here’s what you should observe:

As you can see in the video, the LED is OFF by default. When the button is pressed, the LED turns ON and it stays ON as long as the button is pressed. On realeasing the button, the LED turns OFF again. This confirms that our code is working well, just as we programmed it.

Conclusion

Understanding Arduino’s digitalWrite() and digitalRead() functions is fundamental for controlling and interacting with digital devices. By configuring pins as outputs or inputs, you can easily turn LEDs, buzzers, and motors on or off, or read the state of buttons and sensors. Using INPUT_PULLUP helps avoid unstable readings from floating pins, while proper current management ensures your Arduino and components stay safe. Mastering these basics lays the foundation for more advanced projects and real-world electronics applications.

Browse More Arduino Tutorials

Arduino digitalRead Project Download

Info

You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.

Arduino digitalWrite and digitalRead FAQs

Subscribe
Notify of

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments