Arduino Digital Output and "if" Statements

Written by: Brandon Tsuge

|

|

Time to read min

In the previous post in this series, I showed how to use the Arduino digital pins to read digital values. In this tutorial, I will demonstrate how to output a digital signal and I'll introduce the "if" statement.

Parts Needed

For this exercise you'll need:

  • an Arduino Uno,

  • breadboard,

  • a 10kΩ resistor,

  • two pushbuttons

  • solid core jumper wires,

  • and stranded jumper wires.

These are all included in the Arduino starter kit.

Building the Circuit

The circuit for this lesson builds off of the previous lesson from the Introduction to Arduino series. This circuit included two separate push buttons circuits that were wired to pins 2 and 3. The goal of this exercise is to have the button wired to pin 2 turn an LED on when the button is being pressed and off when it isn't. I also want another LED to always be on and turn off when button attached to pin 3 is pressed.

  • Start by wiring a jumper wire to pin 8.

  • Connect the anode of an LED to this jumper in series.

  • Connect the cathode of the LED to ground with a 220Ω Resistor.

  • Repeat this process for another LED that is wired to pin 9.

Arduino Push button breadboard and circuit diagram

The point of this circuit is to have these two LED turn on based on the signals they receive from pins 8 and 9.

Arduino Code

As I mentioned above, I'm building off of the previous Arduino tutorial. The previous sketch can be found her. The complete sketch for this exercise can be found here. The goal of my sketch is to have the LED wired to pin 8 turn on only when the button wired to pin 2 is pressed. I also want the LED wired to pin 9 to always be on unless the other push button is being pressed. Lastly, I want both LED's to alternate flashing if both push buttons are being held down.

  • First define two new variables, LED1 and LED2, to pins 8 and 9. This goes above void setup(), in addition to the two pins that were already set up for the push buttons.

#define Button1 2
#define Button2 3
#define LED1 8
#define LED2 9
  • Then set the LED pins to OUTPUT and the buttons pins to INPUT and INPUT_PULLUP, with the pinMode command. This goes inside of the void setup().

Serial.begin(9600);
  pinMode(Button1, INPUT); 
  pinMode(Button2, INPUT_PULLUP); 
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  • Use an "if" statement, "else if" statement, and the digitalWrite commands to control the behavior of the LED's

  • The syntax for and "if" and "else if" statements are:

if (condition){
    instructions
}
else if (additional condition){
    additional instructions
}
  • The syntax from digitalWrite is:

digitalWrite(pin number, value)
  • For the first LED, write a "HIGH" value if the first button is pushed and a "LOW" value if it isn't.

  if (Button1State == HIGH){
    digitalWrite(LED1, HIGH);
  }
  else if (Button1State == LOW){
    digitalWrite(LED1, LOW);
  }
  • Repeat this for the second button and LED.

if (Button2State == HIGH){
    digitalWrite(LED2, HIGH);
  }
  else{
    digitalWrite(LED2, LOW);
  }
  • Note: I used an "else" statement just as an example. This covers any condition that isn't mentioned in the rest of the "if" statement.

  • The code up to this point causes the LED's to behave the way I want it, however, it doesn't cause the LED's to alternate and flash if both buttons are being pressed at the same time. An additional "if " statement is needed for this.

  • An "if" statement can have two conditions that need to be met using &&.

  • Setup an "if" so that if pin 2 is reading "HIGH" and pin 3 is reading "LOW," turn LED1 on and LED2 off for 250ms. Then turn LED1 off and LED2 on for 250ms, and repeat.

  if (Button1State == HIGH && Button2State == LOW){
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    delay(250);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    delay(250);
  }
Both Button Pushed

The Bored Robot LLC is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com