Digital Input and Output

We use computers every day that have inputs and outputs. On a computer, the inputs might include keyboard and a mouse. The outputs are a screen and speakers. On the Arduino, a digital input might be a switch or button, the output an LED. Inputs send messages to the microcontroller/Arduino. Outputs receive messages from the microcontroller/Arduino.

ch5-inputs-01

ch5-outputs-01

Why do we want to add an input? Interactivity! So far, we have been able to control the LED by code, but it is not interactive, the Arduino turns the LED on and off with no user input. We will learn how to turn the LED with a button.

It’s important to understand that a digital input or output can only have 2 possible values, on and off. In digital inputs and outputs, HIGH = On = 1. LOW = Off = 0.
ch5-high-low-01

Adding a button to a circuit

Let’s start with the circuit we built to run the Blink sketch. Make sure that power and ground are connected to the busses on the breadboard. Pin 13 should be attached to a row of tie-points that has one lead of a 220 ohm resistor. The other end of the resistor should be attached to the anode (positive end) of the LED. The cathode (negative end) of the LED should be attached to a jumper going to ground.

ch5-basic-circuit-review-01

Once you have made sure that your circuit is built properly, we will move on the adding a button.

Buttons and switches

There are a million different ways to trigger electronic devices or turn something on. Switches and similar on/off devices activate everything from televisions, to music equipment, lights, even your kitchen appliances. How does a switch work?

All switches work on the same basic principle: it either “closes the circuit” and turns something on, or “opens the circuit” and turns something off. When the switch is closed, electricity can flow through; it cannot flow through when the switch is open. The schematic symbols for a switch illustrate how this works. (A schematic is a simplified drawing that uses symbols to represent the electrical relationships in a circuit.)

ch5-button-schematics-labelled-01

We are using a momentary pushbutton switch in our circuit. When you hold down the button, it completes the circuit. When you let go, the switch opens up and the circuit is no longer complete.

Here is a drawing and a schematic of the pushbutton:
ch5-button-pins-schematic-01

Step by step: add button

Now let’s place the button on the board. It will go over the trench in the breadboard.

ch5-add-button-detail-01

Next we will add a jumper from the power bus to one of the leads of the pushbutton.

ch5-button-jumper-power-detail-01

Add a 10k resistor from the ground bus to the other side of the pushbutton.

ch5-button-add-resistor-01

The final step is to connect the pushbutton to the Arduino, Attach a jumper from pin 2 to the same row of tie-points that as the resistor and one of the leads of the pushbutton.

ch5-jumper-pin-button-01

Once you have added the jumper connecting the Arduino to the breadboard, you are ready to connect the Arduino to a computer and upload a script. Connect with a USB A-B cable.

ch5-open-verify-upload-sequence-01

Open up the Arduino IDE. Open this script: from the File Menu > Examples > 02.Digital > Button. Save as, with a name like MyButton.

Click the Verify Button to make sure there aren’t any problems with your code or the connection or settings in the software. If all is well, then click the Upload button. Now try pressing the button. Your LED should turn on and off.

While there may be adjustments depending on what type of device you are turning on or off, this procedure should work generally for any kind of digital input or output.

Further reading/resources