Using an Arduino’s Digital I/O
2018-01-09 | By All About Circuits
License: See Original Project Arduino
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.
Functions
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.
digitalRead(pin_number)
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
- 1 x LED
- 1 x 220 ohm resistor
- 1 x Arduino UNO
- 1 x breadboard
- 2 x jumper wires
Wiring Diagram
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
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
- 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.
- Connect the cathode of the LED to the Arduino GND pin.
- Using the Arduino USB cable, connect the Arduino to the PC. Then, transfer the program to Arduino using Arduino IDE software.
- Using a power supply, battery, or USB cable, provide power to the Arduino board.
- 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
- 1 x LED
- 2 x 220 ohm resistor
- 1x pushbutton switch
- 1 x Arduino UNO
- 1 x breadboard
- 6 x jumper wires
Wiring Diagram
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.
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
- Connect the circuit as shown in the diagram.
- Use the Arduino USB cable connect the Arduino. Then, program the Arduino with Arduino IDE software.
- Using a power supply, battery, or USB cable, provide power to the Arduino board.
- Press the button to turn the LED on, then release the button to turn the LED off.
Download the code for this project here.
Videos
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum