Maker.io main logo

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

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.

Videos

 

 

 

 

制造商零件编号 HLMP-4700-C0002
LED RED DIFFUSED T-1 3/4 T/H
Broadcom Limited
制造商零件编号 CF14JT2K20
RES 2.2K OHM 5% 1/4W AXIAL
Stackpole Electronics Inc
制造商零件编号 A000066
ARDUINO UNO R3 ATMEGA328P BOARD
Arduino
制造商零件编号 TW-E41-1020
BREADBRD TERM STRP 6.5X2.14 70PC
Twin Industries
制造商零件编号 153
JUMPER WIRE M TO M VARIOUS
Adafruit Industries LLC
制造商零件编号 GPTS203211B
SWITCH PUSHBUTTON SPST 1A 30V
CW Industries
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum