Introduction to Arduino Digital I/O

logo_YT.png

The most common and highly used data transfer,  from and to (Input and Output) the micro-controller is DIGITAL.  Digital data means, signal is set at either logic HIGH (i.e. 5V or 3.3V or Vcc) or logic LOW ( i.e., ground or 0V)

SBS_ARDUINO2_DIGITAL_IO_THUMBNAIL_WIX.jp

Most of the times Digital signal may not exactly matching to the Vcc voltage and Ground voltage.  So, the voltage at a trigger voltage level is considered as High or Low.  The Trigger voltage is dependent on the micro-controller, in use.  Normally, for 5V power supplies, above 3.5V is considered as logic High and less than 2V is considered as logic Low. (This may vary slightly and depends on the micro-controller).

Another important point to note is, the micro-controller cannot handle high currents.  Any micro-controller generates current in milli-amperes only.  So, a series resistance to the load, must be connected wherever is required, to limit the current flowing through the micro-controller pin.

Understanding Digital Output :

A test circuit is to be connected as shown in the figure, using 4 LEDs and 4 resistors , on a bread board.

Here, they are connected to four pins of Arduino Nano, i.e. D2, D3, D4 and D5, for testing.  You may connect any number of LEDs, to any pins of Arduino Nano (of your choice).

 

Then, Connect the USB cable (USB type-A and micro-USB on either end) to the Arduino Nano and your system USB port.

TestCircuit_4LEDs_SMALL.jpg

Now, Open Arduino IDE, then select  through menu, File->New.  Then declare all the LED connected pins, as output in setup function, using pinMode function, as shown below.

void setup()

{
 
// put your setup code here, to run once:
  pinMode ( 2 , OUTPUT );  
  pinMode ( 3 , OUTPUT );  
  pinMode ( 4 , OUTPUT );  
  pinMode ( 5 , OUTPUT );  

} 
}

Then, send logic High and Low codes to each pin sequentially, to all the LED connected pins, using digitalWrite function (HIGH to glow and LOW to off), with a small delay,  as shown below.

void loop() {
 
// put your main code here, to run repeatedly:

  digitalWrite ( 2 , HIGH );
  delay(1000);
  digitalWrite ( 2 , LOW );

  digitalWrite ( 3 , HIGH );

  delay(1000);

  digitalWrite ( 3 , LOW );

 

  digitalWrite ( 4 , HIGH );

  delay(1000);

  digitalWrite ( 4 , LOW );

  digitalWrite ( 5 , HIGH );

  delay(1000);

  digitalWrite ( 5 , LOW );

}

The above code is written for easy understanding. You may use for loop, instead of writing code for each pin, which is shown in the next part (Understanding Digital Input).

Then, through menu, do necessary settings for connecting Arduino Nano:

Tools->Board->Arduino Nano

Tools->Programmer-.USBasp

Tools->Port->(select the port connected to Arduino Nano)

Then, press compile icon (or through menu, sketch->Verify/Compile) and check for any errors uin typing.

Then, press upload icon (or through menu, sketch->upload) and check the RX & TX LEDs on Arduino Nano are blinking, while uploading the sketch.

Once uploaded the sketch to Arduino Nano, the LEDs starts glowing one-by-one sequentially for one second each. Since, it was written in loop function,  the sequence continues.

Understanding Digital Input :

TestCircuit_4LEDs_2probes_SMALL.jpg

The previous test circuit is to be modified as shown in the above figure, by adding a positive probe (red) connected to 5V and a negative probe (blue) connected to GND (ground).  You may connect berg strip pins to A2, A3, A4 and A5, for easy identification as input pins. 

Now, in the setup function,  set the pins 2 to 5 as Digital OUTPUT, using pinMode function as earlier (same code is modified using for loop). Also, set the A2, A3, A4 and A5 pins as Digital INPUT, using pinMode function, as shown below.

void setup()

{
 
// put your setup code here, to run once:

  int i;   //variable declaration

  for ( int i=2; i<=5; i++ )   //for loop to select 2 to 5
    pinMode ( i , OUTPUT );   // set 2 to 5 as OUTPUT

     // set A2 to A5 as INPUT

  pinMode ( A2 , INPUT );

  pinMode ( A3 , INPUT );

  pinMode ( A4 , INPUT );

  pinMode ( A5 , INPUT );

} 

Then, send logic High and Low codes to respective output pins (2, 3, 4 and 5), according to the status of input pins (A2, A3, A4 and A5). Here, digitalRead function is used to read the digital status (HIGH or LOW) of each input pin. Where as digitalWrite function (HIGH or LOW), is used to send output code to the respective pins.  You may observe in the loop function, four types of logic is used to show the status of input pins to respective output pins.

void loop() {
 
// put your main code here, to run repeatedly:

  bool pin2, pin3;

   // 1) read the input and act accordingly if it is HIGH

  pin2 = digitalRead( A2 );

  if ( pin2 == HIGH )
    digitalWrite ( 2 , HIGH );
  else
    digitalWrite ( 2 , LOW );

   // 2) read the input and act accordingly if it is LOW

  pin3 = digitalRead( A3 );
  if ( pin3 == LOW )
    digitalWrite ( 3 , LOW );
  else
    digitalWrite ( 3 , HIGH );

   // 3) read the input and directly act accordingly

  if ( digitalRead ( A4 ) )
    digitalWrite ( 4 , HIGH );
  else
    digitalWrite ( 4 , LOW );

   // 4) set output matches to the input value; single statement

  digitalWrite ( 5 , digitalRead ( A5 ) );
}

Again compile and upload the sketch to Arduino Nano.  Then, test the sketch using the circuit.  The LED connected to pin 2 glows, when A2 pin is connected to positive probe and goes off, when the A2 pin is connected to negative probe.  The same phenomenon is observed with input pin A3 to output pin 3, input pin A4 to output pin 4 and A5 to 5.

As the input pins are neither connected to positive nor negative power supply, by default, the A2 to A5 are in floating condition and sensitive to small signal change.  Some times, some inputs may be set to either  HIGH or LOW status, if not connected to any probe.

External Pull-up/ Pull-down to Digital Input(s) :

TestCircuit_4pulldown_SMALL.jpg
TestCircuit_4pullup_SMALL.jpg

To avoid floating state for digital input(s),  the open input(s) should be connected to either positive (5V) or negative (GND) of power supply, through resistors, by default. Now, test the output, using the probe connecting to the input pins for both the Pull-up and Pull-down circuits, which is shown above.  In case, the inputs are continuously  connected to any external signalling system, then there is no need of pull-up or pull-down resistors.  No change is required in the sketch to test the external pull-up or pull-down circuit. 

Activating Internal Pull-up Resistor(s) to Digital Input(s) :

TestCircuit_4pullupINT_SMALL.jpg

While using external pull-up or pull-down resistor(s) for digital inputs, the resistor(s) have to be tied to either positive or negative power supply.  To avoid using any internal resistor, the pull-up resistor(s) may be activated inside the micro-controller, through writing code in the sketch, as shown below.

pinMode ( x_pin_number , INPUT );  // set any pin as input

digitalWrite ( x_pin_number , OUTPUT );  // activates internal pull-up resistor 

Now, in the setup function,  set the pins A2 to A5 as Digital INPUT, using pinMode function as earlier, and then add digitalWrite function as OUTPUT, as shown below.

void setup()

{
 
// put your setup code here, to run once:

  int i;   //variable declaration

  for ( int i=2; i<=5; i++ )   //for loop to select 2 to 5
    pinMode ( i , OUTPUT );   // set 2 to 5 as OUTPUT

     // set A2 to A5 as INPUT

  pinMode ( A2 , INPUT );

  pinMode ( A3 , INPUT );

  pinMode ( A4 , INPUT );

  pinMode ( A5 , INPUT );

    // activate internal pull-up resistor for  A2 to A5

  digitalWrite ( A2 , OUTPUT );

  digitalWrite ( A3 , OUTPUT );

  digitalWrite ( A4 , OUTPUT );

  digitalWrite ( A5 , OUTPUT );

} 

There is no change in loop function.  compile and upload again.  Since, the inputs are pulled-up internally (means connected to positive through resistors), all the LEDs glow, by default. Now, connect each input pin to ground to observe the respective LED goes to OFF state.

So, this will avoid using resistor(s) and makes the circuit neat.  In case the outputs are to be in the off state, by default, you may change the logic in the loop function or may use pull-down resistors as shown in the earlier circuit.

Best of Luck.