The best tools to make your project dreams come true

Login or Signup

By All About Circuits

Using an Arduino’s Digital I/O

Courtesy of All About Circuits

You can connect Arduino sensors, actuators, and other ICs with the digital inputs and outputs (digital I/O) on the Arduino. Learning how to use them will allow you to use the Arduino to do some really useful things, such as reading switch inputs, lighting indicators, and controlling relay outputs.

Digital signals

Digital signals have two distinct values—HIGH (1) or LOW (0)—unlike analog signals, which may take on any value within a range of values. In situations where the input or output will have one of those two values, you use digital signals. Turning an LED on or off is one example of how you might use a digital signal.


The Arduino functions associated with digital signals that we will be using in this tutorial are:

  • pinMode()
  • digitalRead()
  • digitalWrite()

pinMode (pin_number, mode)

The Arduino digital I/O pins can be set for either output or input. So, be sure to configure the pins you want to use for digital I/O first. The number of the pin you want to configure is pin, while mode must be one of three values: INPUT, OUTPUT, or INPUT_PULLUP. When mode is set to INPUT_PULLUP, a 20 kohm pullup resistor is internally connected to the pin to force the input HIGH if there is nothing connected to the pin.digitalWrite(pin_number,value)

This function assigns a digital value to a particular pin. pin shows which Arduino pin the digital value will be written to, and value is the digital value that the pin is set to. value must be either HIGH or LOW.


The function above reads a digital value from a certain pin. pin is the number of the digital I/O pin you want to read. This function returns one of two values: HIGH or LOW.

Experiment 1: Blinking LED using delay

We’ll start by switching an LED on and off using a digital output in this first experiment.

Hardware Required

Wiring Diagram

Image 1

The figure above shows how to connect the Arduino to the 220 ohm resistor and the LED. As shown above, digital I/O pin 8 of the Arduino connects to the LED through the resistor. The resistor controls the current through the LED. The program below configures pin 8 to be an OUTPUT first. It then sets the digital I/O pin to HIGH for 1000 ms, and then it sets it to LOW for another 1000 ms.

Program for Experiment #1

Copy Code
                   const int led  =  8;     //use digital I/O pin 8
void setup()
pinMode(led,OUTPUT); //set pin 8 to be an output output

void loop()
delay(1000); //delay 1000 milliseconds
digitalWrite(led,HIGH); //set pin 8 HIGH, turning on LED
delay(1000); //delay 1000 milliseconds
digitalWrite(led,LOW); //set pin 8 LOW, turning off LED


Running the experiment

  1. Connect the anode of the LED to one end of the resistor, then connect the other end of the resistor to digital I/O pin 8 on the Arduino board.
  2. Connect the cathode of the LED to the Arduino GND pin.
  3. Using the Arduino USB cable, connect the Arduino to the PC. Then, transfer the program to Arduino using Arduino IDE software.
  4. Using a power supply, battery, or USB cable, provide power to the Arduino board.
  5. LED should start to blink.

Experiment 2: Blinking LED using Button

This experiment demonstrates not only how to use a digital output, but also a how to use a digital input. An LED will turn on or off when the pushbutton connected to a digital input is pressed. The program uses both the digitalWrite() and digitalRead() functions.

Hardware Required

Wiring Diagram

Image 2

Note the diagram above. We are using two Arduino digital I/O pins now. An LED connects to pin 8, which is configured as an OUTPUT. A pushbutton connects to pin 9. It is then configured as an INPUT. When someone presses the pushbutton switch, pin 9 is set to HIGH. Next, the program will set the output of pin 8 to HIGH and turn on the LED. Pin 9 is set to LOW after releasing the pushbutton. The LED is turned off when the program sets pin 8 to LOW.

Copy Code
                   const int led    =   8;     //name pin 8 as led
const int button = 9; //name pin 9 as button
void setup()
pinMode(led,OUTPUT); //set pin 8 as OUTPUT
pinMode(button,INPUT) ; //set pin 9 as INPUT
void loop()
int reads = digitalRead(button); //read the digital value on pin 9
digitalWrite(led,reads); //set the digital output value of pin 8 to that value

Running the experiment

  1. Connect the circuit as shown in the diagram.
  2. Use the Arduino USB cable connect the Arduino. Then, program the Arduino with Arduino IDE software.
  3. Using a power supply, battery, or USB cable, provide power to the Arduino board.
  4. Press the button to turn the LED on, then release the button to turn the LED off.

Download the code for this project here.






Key Parts and Components

Add all Digi-Key Parts to Cart
  • 516-2483-1-ND
  • CF14JT2K20CT-ND
  • 1050-1024-ND
  • 438-1046-ND
  • 1528-2185-ND
  • CW181-ND