How to Control Digital Output with Digital Input on Arduino Board

One of the first things to learn when beginning to build projects with various development boards is how to control simple outputs with simple inputs. Accordingly, the digital inputs and outputs are considered the easiest to start with because there is not much to worry about except the zeros and ones or rather the binaries. In contrast, analog I/Os require you to worry about the continuous variable states of data. In this simple arduino beginner project, you will learn how to control LEDs (as digital outputs) with a push button (digital input). In addition, you will also learn how to calculate the values of components used to build the circuit.

Required components

  • Three LEDs (Red, Yellow, Green)
  • Three current-limiting resistors (values are calculated below)
  • One pull up resistor (value calculated below)
  • Arduino Uno
  • One pushbutton
  • Jumper wires

Design

There is not much in the design except to calculate the values of the current limiting resistors for the LEDs and the value of the pull-up resistor.

Calculating the value of pull up resistors

When connecting an LED to a circuit, you need to be careful about the amount of current to avoid frying it. For this reason, you need a current limiting resistor whose value can be determined by the following equation:

R = (Vs – Vf)/If where Vs is the voltage source, Vf is the forward voltage, and If is the forward current. The forward current and voltage can be found in the datasheet and are the minimum current and voltage needed to turn on an LED. The typical forward voltage and current for the visible LEDs is 1.8V and 20mA respectively. In this project, the ideal values of the resistors were follows: R = (5V-1.8V)/20mA = 160Ω but 220Ω resistors were used because they were the close values that were readily available. The downside of using very large values is that the forward current might be too small to turn on the LEDs while very small values might lead to large currents that may burn the LEDs.

Calculating the Pull-up resistors

The pullup resistor value needs to be high enough so that the input pin is pulled low when the button is pressed and high when the button is not pressed. Ideally, it needs to be 1/10 the input impedance of the input pin. A value of 10kΩ works well.

schematic-diagram-of-digital-input-and-digital-output-interface
Schematic diagram

Code

const int red =    2;
const int yellow = 4;
const int green  = 6;
const int button = 7;
void setup() {
  // put your setup code here, to run once:
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(button) == HIGH){
    for(int i = 0; i<3; i++){
      if (i == 0){
        digitalWrite(red, HIGH);
        delay (1000);
        digitalWrite(red, LOW);
          }else if (i == 1){
        digitalWrite(yellow, HIGH);
        delay (1000);
        digitalWrite(yellow, LOW);
        }else if (i == 2){
          digitalWrite(green, HIGH);
          delay (1000);
          digitalWrite(green, LOW);
          }
       }
  }else{
    delay(1000);
  }
}  

Build

The following is a video demo of the simple digital I/O arduino project.