Skip to main content

How to Use Arduino Digital Pins as Inputs and Outputs

On Arduino board there are many digital inputs and outputs (digital I/O), which allow you to connect the Arduino to various sensors, actuators, and other ICs. Learning use of them allows you to use the Arduino to do many interesting and useful things, such as reading switches / inputs, lighting LED/indicators, and controlling relays.

Analog & Digital signals

Analog signals may take on any value within a range of values, while digital signals can have only two distinct values: HIGH (1) or LOW (0). You can use digital signals in various situations.

Functions

The below options are Arduino functions associated with digital signals that we will use in this tutorial:

pinMode()

digitalRead()

digitalWrite()

 

pinMode (pin_number, mode)

Arduino digital I/O pins can be used as input or output; but first you have to configure the pins you wish to use as digital Input or Output with this function.

You just have to tell the pin number or name and mode: INPUT or OUTPUT.

You can also enable internal pull-up by using INPUT_PULLUP, When mode is set to INPUT_PULLUP, a 20 kohm pull-up resistor is internally connected to the pin to force the input HIGH if there is nothing connected to the pin.

 

Example:

Suppose you want to use pin no.7 as Input, then you will write the code as given below

pinMode (7, INPUT);

If you want to use internal pull up, then code will be like this

pinMode(7, INPUT_PULLUP);

 

If you want to use pin 7 as output, the code will be as given below.

pinMode(12,OUTPUT);

 

Function: digitalWrite(pin_number,value)

When you want to control and digital output then this function is used.

First mention the pin number or name, and then after comma, its value, HIGH or LOW.

Example:

Suppose you want to use pin 12 as output, then you will write the value as given below

digitalWrite(12, HIGH);                                           //( to make it high)

digitalWrite(12, LOW);                                           //( to make it low)

 

Function: digitalRead(pin_number)

This function is use to read a digital value from a pin. ‘pin_number’ is the number of the digital Input pin you want to read. This function returns one of two values: HIGH or LOW.

Example: if you want to read digital pin 12 then you will write code as…

digitalRead(12);

 

 

Experiment 1: Blinking LED with some delay

In this experiment, we will make an LED ON and OFF , for this we will use digitalWrite() function, but first we have to set the mode of the pin as output.

 

Hardware Required

1 x LED

1 x 330 ohm resistor

1 x Arduino UNO

1 x breadboard

2 x jumper Wires


The figure above shows how to connect an LED through 330 ohm resistor to the Arduino. As shown, the LED is connected to digital I/O pin 12 of the Arduino through the resistor. The resistor limits the current through the LED and it protect the Arduino also from overloading.

The program below first configures pin 12 to be an OUTPUT, then sets the digital output pin to HIGH for 500 ms, then to LOW for another 500 ms.

‘delay()’ is an inbuilt function, which is used to give some delay in the code.

‘delay(500)’ means it provides 500ms delay.

  

 

Program for Experiment #1

int outputLed = 12;

void setup()

{

pinMode(outputLed, OUTPUT);   // sets the pin as output

digitalWrite (outputLed, LOW);    // sets the output pin low

}

 

void loop()

{

digitalWrite(outputLed, HIGH);      //sets the output led high

delay(500);            //500ms delay

digitalWrite(outputLed, LOW);   //sets the output led low

delay(500);             //500ms delay       

}

 

Run the experiment

Connect the anode of the LED to one end of the resistor 330 ohm and the other end of the resistor to digital I/O pin 12 on the Arduino board.

Connect the cathode of the LED to the Arduino GND pin.

Connect the Arduino to the PC using Arduino USB cable and transfer the program to Arduino using Arduino IDE software.

USB cable is also providing the power to the Arduino board so, no need to supply separately to the Arduino.

 




 

 

Experiment 2: Control an LED By a Button

This experiment will demonstrate how to control a digital output by a digital input. When you press the pushbutton connected to a digital input will turn the LED ON or OFF. The program uses both the functions digitalWrite() and digitalRead().

 

Hardware Required

1 x LED

1 x 330 ohm resistor

1 x 10 Kohm resistor

1x pushbutton switch/micro switch

1 x Arduino UNO

1 x breadboard

6 x jumper Wires

 

 

As you can see from the diagram above, we are now using two Arduino digital I/O pins. An LED is connected to pin 12, which is configured as an OUTPUT. A pushbutton is connected to pin 7, which is configured as an INPUT. When you press the pushbutton switch, pin 7 is set to HIGH, and the program will then set the output of pin 12 to HIGH and turns on the LED. On Release of the pushbutton resets pin 7 to LOW. The program then sets pin 12 to LOW, which turns off the LED.

 

Program for Experiment #2

int inputButton = 7;

int outputLed = 12;

 

void setup()

{

pinMode(inputButton, INPUT);    // sets the pin as input

pinMode(outputLed, OUTPUT);   // sets the pin as output

digitalWrite (outputLed, LOW);    // sets the output pin low

}

 

void loop()

{

int buttonStatus = digitalRead (inputButton);  //variable to store input pin status

if(buttonStatus == HIGH)

{

digitalWrite(outputLed, HIGH);      //sets the output led high

delay(10);            //10ms delay

}

else

{

delay(10)

digitalWrite(outputLed, LOW);   //sets the output led low

}

}

 

Run the experiment

Connect the circuit as shown in the above diagram.

Connect the Arduino using Arduino USB cable.

Connect the Arduino to the PC using Arduino USB cable and transfer the program to Arduino using Arduino IDE software.

USB cable is providing also providing the power to the Arduino so, no need to provide the power separately.

Press the button LED should be ON and on release of the button LED should be OFF.


 






Comments

Popular posts from this blog

How to Make Automatic Room Light Controller Without Microcontroller

You must have noticed in some offices or hotels, when nobody is in gallery or washroom, the light remains OFF but when somebody enters the place, light switches ON automatically. In this post I am going to teach you how to make this circuit. Before going ahead I would like to tell you that this is VERY EASY circuit. For this circuit the material we need is… PIR Motion sensor General Purpose PCB - 5x5 cm. Transistor 2222N – 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Relay Circuit Concept : We can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed). So I am using 5V relay. PIR sensor has three terminals, One for 5Vdc Second for Gnd (0V). Third for ...

How to drive high voltage/current load by small voltage signal from a microcontroller?

Sometimes we need to control or drive a high voltage and heavy current load by a small voltage signal. For example, if you want to control water motor with your microcontroller output. We know that microcontroller gives only 5v output which is not sufficient to drive a heavy motor. This circuit, about which this post is, is very-very useful for electronics engineer and hobbyist. So pay attention! For this circuit the material we need is… General Purpose PCB - 5x5 cm. Transistor KN 2222A (TO-92) - 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Tools. Concept: Weak signal triggers the transistor and transistor acts as a switch for the relay. You can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed)....

How to control digital output with serial monitor in Arduino

Hello Friends, in this blog we will be controlling digital output with serial monitor command. First let’s understand the working of serial monitor. Serial monitor in Arduino IDE is a tool which allows communication between the computer and Arduino board via a serial connection, normally we use USB cable for connection. What are the features of Serial Monitor? It shows the data sent from the Arduino board by using the functions like Serial.print() or Serial.println(). It allows to send text or numeric data to the Arduino board, which can be read by function like, Serial.read() or Serial.parseInt(), thereafter you can use this data for further analysis and action. We can use this tool for debugging and monitoring the function of the sketch. There is a procedure to use the serial monitor, below are the steps given. First initialize the serial communication in the sketch as given below. Normally baud rate is set 9600.  Void setup(){          Serial.begin(9600)...