Digital Input and Output. (Thanks to Dan O'Sullican & Tom Igoe)

In many cases, we only need to know one thing about the physical world: Whether something is true or false. Is the viewer in the room or out? Are they touching the table or not? Is the door open or closed? In these cases, we can determine what we need to know using a digital input, or switch.

Digital inputs have two states: off and on. If voltage is flowing, the circuit is on. If it's not flowing, the circuit is off. To make a digital circuit, we need a circuit, and a movable conductor which can either complete the circuit, or not.

In the above diagram, we add the resistor to resist the current flow. When the switch is closed, the current will follow the path of least resistance, to the microcontroller pin, sending it a signal like we want it to do. We need the connection to ground as a reference point, and the resistor also prevents a power-to-ground short circuit.

Wiring/Arduino:

On the Arduino module, we declare the pin to be an input at the top of our program. Then we read it for the values 1 or 0, like so:

// give the pin numbers names:
         # define inputPin 2
         #define outputPin 3
       
void setup() {
         // declare inputPin to be an input:
         pinMode(inputPin, INPUT);
         pinMode(outputPin, OUTPUT);
         }
void loop() {
         if (digitalRead(inputPin) == 1) {
         digitalWrite(outputPin, HIGH);
         }
         }
      

Digital output

The simplest control we can use over an electrical device is digital output. In this case, we would either turn something off, or on. The diagram below is a digital output controlling an LED:

Digital outputs are often used to control other electrical devices, through transistors or relays. More detail on these components will follow later in the course.

Arduino:

On the Arduino module, we declare the pin an output at the top of the program, then in the body of the program we use the commands HIGH and LOW to set the pin high or low, as we've seen above.

Here's a simple blinking LED program in the Arduino syntax, for either module:

//give pin number a name:
         #define LEDpin 13
void setup() {
         pinMode(LEDPin, OUTPUT);
         }
void main() {
         digitalWrite(LEDpin, HIGH);
         delay(1000);
         digitalWrite(LEDpin, LOW);
         delay(1000);
         }

Physical computing interaction Design

Processing + Arduino

Sensor & components

Essential Viewing

Lectures