Amazon Store

Monday, 23 March 2015

How To Use Digital Input And Digital Output Of Arduino

 

 

There is no other tool available which helps in easy prototyping like the arduino does. Any AVR microcontroller based board which follows the standard arduino schematic and is flashed with the arduino boot-loader can be called an arduino board. The arduino can be used as a stand-alone board of which the output or inputs can be taken from the boards or given to the board using convenient connectors. Both digital and analog inputs and outputs are available in all arduino boards. When it comes to programming the arduino board anyone who have basic knowledge of c programming can quickly get started with the arduino IDE.

Since the arduino board can act as a stand-alone system it should have capabilities to take inputs process the input and then generate a corresponding output. It is through these inputs and outputs that the arduino as a system can communicate with the environment. The arduino boards can communicate with other devices using digital input/output analog input/output standard communication ports like USART, IIC, and USB etc. 

This article explains how to use the digital input and output of the arduino board with the help of a simple push button as a digital input device and a LED as a digital output device.

 

Circuit Diagram:



It is assumed that the reader has gone through the project Getting started with arduino and done all the things discussed in it.

This project also uses the arduino pro-mini board and the same version of the arduino IDE 1.0.3.



The digital output device in this particular project is an LED which is connected to the pin number 5 of the arduino board through a 1K current limiting resistor. A push button is connected to the pin number 13 using a pull down resistor. The code continuously reads the status of the pin number 13 which has been configured as digital output. Due to the presence of the pull down resistor the default value will be zero (the value when the key is not pressed). When the key is pressed the value at the pin 13 suddenly becomes logic high and the code detects the change and glows the LED connected to the pin number 5.


THE CODE

const int buttonPin = 13;             // the number of the pushbutton pin

const int ledPin =  5;                    // the number of the LED pin

// variables will change:

int buttonState = 0;                      // variable for reading the pushbutton status

void setup()

{

  // initialize the LED pin as an output:

  pinMode(ledPin, OUTPUT);     

  // initialize the pushbutton pin as an input:

  pinMode(buttonPin, INPUT);    

}

void loop()

{

  // read the state of the pushbutton value:

  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.

  // if it is, the buttonState is HIGH:

  if (buttonState == HIGH)

  {    

    // turn LED on:   

    digitalWrite(ledPin, HIGH); 

    delay(100);

  }

  else

  {

    // turn LED off:

    digitalWrite(ledPin, LOW);

  }

}

All the arduino code has two basic functions namely setup() and loop() explained in the project Getting started with arduino. Apart from these functions there are four other library functions used in this project namely pinMode (), digitalRead (), digitalWrite () and delay().

pinMode ()

The function pinMode() is a built-in function used to set a particular pin as input or output. The first parameter is pin number and the second parameter suggests whether the pin should be input or output.

For example to make pin number 5 as output

pinMode (5, OUTPUT);

To make pin number 6 as input

pinMode(6, INPUT);

digitalWrite ()

The digitalWrite() is another function which can be used to write a digital value (logic 0 or logic high) to a particular pin which has already been made as output using the pinMode() function.

For example to make the pin number 5 as logic high

digitalWrite(5, HIGH);

And to make the same pin as logic low

digitalWrite(5, LOW);

digitalRead ()

The digitalRead is a function which is used to read the value (logic0 or logic1) at a pin which has already been made input using the pinMode() function. Suppose if the pin number 13 is an input pin then one can use the pinMode () function to store the value of the pin number 13 into a variable 'var' as shown in the following example;

var = digitalRead(13);

The function returns the value 0 or 1 according to the voltage existing at the pin number 13 into the variable 'var' which can be an integer or character type.

delay()

The function delay () is a very useful function for almost all the projects and it can generate a delay in milliseconds between the code steps.

For example to generate a delay of 5 seconds,

delay(5000);

Once the coding has been done verify the code and upload the code to the arduino board as mentioned in the project how to get started with the arduino.

 

 

Coding:

*================================= EG LABS =======================================

 

 Turns on and off a light emitting diode(LED) connected to digital  

 pin 5, when pressing a pushbutton attached to pin 13. 

 

 The circuit:

 * LED attached from pin 5 to ground through a 1K resistor

 * pushbutton attached to pin 13 from +5V

 * 10K resistor attached to pin 13 from ground

//================================= EG LABS =======================================*/

 

// constants won't change. They're used here to 

// set pin numbers:

 

const int buttonPin = 13;             // the number of the pushbutton pin

const int ledPin =  5;                    // the number of the LED pin

 

// variables will change:

int buttonState = 0;                      // variable for reading the pushbutton status

 

void setup()

{

  // initialize the LED pin as an output:

  pinMode(ledPin, OUTPUT);      

  // initialize the pushbutton pin as an input:

  pinMode(buttonPin, INPUT);     

}

 

void loop()

{

  // read the state of the pushbutton value:

  buttonState = digitalRead(buttonPin);

 

  // check if the pushbutton is pressed.

  // if it is, the buttonState is HIGH:

  if (buttonState == HIGH) 

  {     

    // turn LED on:    

    digitalWrite(ledPin, HIGH);  

    delay(100);

  } 

  else 

  {

    // turn LED off:

    digitalWrite(ledPin, LOW); 

  }

}

 

 

No comments:

Post a Comment