You are on page 1of 30

Digital Signals and

Digital Input

Wiring buttons and using digitalRead()


Data or a MESSAGE
Life Is:
can be ANALOG or DIGITAL
ANALOG
Digital and Analog
Signals and Messages
Signal vs. Message

All signals are ANALOG Messages are EXACT and can be


digital or analog.
Due to the REAL LIFE nature of
signal transmission… digital Digital - numbers 0 and 1
messages are transmitted over:
Analog (exists on a continuum) -
● light (fluctuations in temperature, distance, light level,
color/brightness) color, sound / tone, volume
● sound
● touch
● electrical pulses
● radio waves
What message did you
get?
001011011001

00101001100

001000011001

00111001100

???

Why so many different messages?


Signal was unclear

ANGLE 1 0 ???

90

0
How to make digital output

In code, use the digitalWrite() function call to send a digital


signal through a digital output pin 0-13

You can send HIGH (+5V) or LOW (0V) signal.

digitalWrite(13,HIGH); // turns pin 13 on

digitalWrite(9,LOW); // turns pin 9 off


digital message through analog
signal
If the transmission method is analog, the message is up for
interpretation.
Digital Input
push a button
Digital Input Summary

● Any pin can perform digital input A0-A5 and 0-13


● pinMode() must be set to INPUT or INPUT_PULLUP
● Digital input state is read with the digitalRead() function
call
● Digital Input Range for LOW is 0-1.5V
● Digital Input Range for HIGH is 3-5V
Button State
Pushed or Unpushed
Button State

Pushed Unpushed (default state)

Down Up

Circuit Closed Circuit Opened

When wired in INPUT_PULLUP: When wired in INPUT_PULLUP:

LOW HIGH
HIGH and LOW input voltages
in INPUT_PULLUP mode
HIGH LOW

Greater than 3.0 V Less than 1.5 V

5V

3V

???

1.5V

0V
Wiring a button
2 Modes:

INPUT
INPUT_PULLUP
Full explanation of different wiring methods is
demonstrated here: http://gammon.com.au/switches
INPUT Wire the button with two branches:
This is the schematic symbol
for a button or switch

+5V

Pin 2 GND
The button / circuit is OPEN

Button is NOT being pushed

+5V

Pin receives a HIGH signal


Pin 2 GND
The button / circuit is CLOSED

Button IS being pushed down

+5V

Think of the current as


being diverted AWAY
from the input pin

Pin receives a LOW signal GND


Pin 2
INPUT_PULLUP

Uses built-in pull-up 20kΩ resistor

+5V Pin 2
Opposite of your intuition

When detecting input, remember:

HIGH

HIGH signal means button is NOT being pushed

LOW signal means button IS being pushed

LOW
Coding for a button
The Arduino will read the button state only
when the code says to.
Sample

A digital reading of the analog input state (taking an analog


input and digitizing/giving it a digital value)

Sampling Rate - how often it takes a digital reading

Usually measured in Hertz (Hz)

Hertz - samples per second


Absolutely First: Declare Your Variables

In global space: Constant - this value may not ever change in this program

const int button1Pin=2; int is the type - integers / whole numbers

const int button2Pin=3; Each variable has a name and value

In loop function:

int button1State; These variables are “local” to the loop function, their
int button2State; values will be assigned later - they’re just given a type
and a name
Next: Set your digital input pin to
INPUT_PULLUP mode
void setup() {

pinMode( button1Pin, INPUT_PULLUP );

pinMode( button2Pin, INPUT_PULLUP );

}
In your code for circuit 5:
read both button states and assign to the
button1State and button2State variables.
void loop()
{
int button1State; digitalRead command
int button2State; On this button pin

button1State = digitalRead(button1Pin); And the result


button2State = digitalRead(button2Pin);

Gets assigned to the button1State variable


Each time the loop()
function executes
these lines, a
sample is taken of
the input state
If one is LOW and the other is HIGH...

if (((button1State == LOW) && (button2State == HIGH)) ||


((button1State == HIGH) && (button2State == LOW)))
{
digitalWrite(ledPin, HIGH); // turn the LED on
}
else
{
digitalWrite(ledPin, LOW); // turn the LED off
}
}
If This Then do That…
Otherwise do the that other thing.
If This then do That...

if ( this statement is true )

do that

}
Otherwise do that other thing

else

do that other thing

}
if( LOW && HIGH || HIGH && LOW )
“AND” “OR”
“AND”

button1 button2 button1 button2

{ THEN...

digitalWrite(ledPin,HIGH);
} else { OTHERWISE
digitalWrite(ledPin,LOW);
}

You might also like