Hi, I'm Becky Stern.
And today we're going to talk about basic sensor input with Arduino.
Last episode, we covered controlling LEDs with code,
Which is one way to use Arduino’s outputs.
Well, this time we're adding input.
The Arduino board can be programed
to listen to electrical signals and take actions based on those inputs.
We’ll cover digital input with switches and analog input with variable resistors.
A switch is a mechanical device that connects or breaks the circuit,
often using a lever or a button.
This tiny push button is just one member of a huge variety of switches.
A variable resistor will change its electrical resistance when acted upon,
like turning the knob on this potentiometer, pressing or bending these FSRs,
Or changing the light exposure to this photo resistor.
Like last time, I'm going to start with my Arduino UNO board
and solderless breadboard together on a mounting plate.
With five volts and ground wired up from the Arduino to The breadboard rails
as well as over to the rails on the other side of the breadboard.
This is the basic setup any time I want to prototype a new circuit.
If you need advice on what supplies, you'll need to get started,
Check out the link in the description.
First, I'll wire up an output LED with the longer positive leg connected to PIN 13,
And the negative leg connected to ground through a small value resistor.
Next, I'll add the push button to the breadboard,
which is designed for parts like this to straddle the center dividing line.
If it doesn't click into place and stay snugly, you may have to bend
The legs a little bit.
One side of the push button is connected
To both pin 2 and also to power
through a large value resistor.
This is a 10K.
The other side of the switch is
Connected to ground.
I use the diagonal pin from the other connections
because it's a sure fire way to find the correct wiring
no matter how the switch is wired inside.
You can also
just check it with a multimeter.
Once the circuit is put together, it's time to plug in the USB cable.
And upload a new program.
I'm using the built in example
under 02 > Digital > Button.
The LCD lights up, and turns off
whenever I press the button.
The first lines of this program introduce some constants.
Which are similar to variables in that they store a piece of information.
However, as you might guess, constants don't change throughout the program,
and are, therefore perfect for storing pin numbers.
Row 36 configures pin 2
as an input so we can
listen to the electrical state of the push button.
In the main loop,
A function called digitalRead() checks the state of pin 2,
which will either be five volts a.k.a. HIGH,
Or ground, a.k.a. LOW.
And stores that state
in a variable called buttonState.
Row 44 contains an if() statement,
which executes a condition using comparison operators,
like greater than less than,
or in our case, equivalent to,
noted with two equals signs.
If the condition is met, the code inside
the curly braces executes, turning the LED on.
And if not, the code inside
the else statement executes, turning the LED off.
At rest, the switch leads Are not connected to one another.
Pin 2 is connected through a beefy 10K resistor to five volts.
When the button is pressed, the switch leads are connected.
Which allows pin 2 to be connected to ground with no resistor.
Since electricity
takes the path of least resistance, the pin will sense the connection to
ground strongly and ignore the weak 10K connection to five volts.
But when no other signal is present,
like when the switch is not pressed,
the weak connection to five volts is all the pin can sense.
So the resistor is pulling the pin up.
To five volts.
So it's called a pull up resistor.
Without one pin to would.
Be not connected to anything at all until the button is pressed.
This is called a floating pin,
and it can result in random noise
From static electricity, and electromagnetic interference.
Similarly, a resistor can be used to tie a pin to ground,
which is called a pull down resistor.
Then, you'd connect to the other side of the switch to power, instead of ground.
So to change the function of the button, you can either change
the wiring of your circuit or change the code.
The latter is less work in this case, but it might not always be,
when you're building projects on your own.
Edit lines 43 and 44,l the if() statement and its comment,
to say LOW instead of HIGH.
Upload the updated sketch to your board
and check that the button now turns the led on instead of off.
Arduino pins have built in pull up resistors
on many of the pins, tiny ones inside the chip just for this purpose.
And you can access one
by enabling it in the setup.
Then you don't need the resistor
on the breadboard anymore.
Next, I want to introduce you to the Serial Monitor,
which is a way to check in on different spots in your code,
by reporting back to the computer over the USB cable.
Using the same push button circuit,
I'm going to load up a different example program.
This one is in 01 > Basics called DigitalReadSerial.
To create a serial connection,
you should use Serial.begin() in your setup.
The number 9600 is the baud rate, or data speed in bits per second (bps).
Inside the main program loop,
You can use Serial.print() to send information to the serial port.
Serial.println() does the same thing, but prints on a new line.
Line 27 in our code prints
the current value of button state to the serial port.
So with the serial monitor open, we have a live scrolling view of whatever
the Arduino senses at pin 2.
The serial monitor is exceptionally handy when troubleshooting
since you can easily compare what you think should be happening to what
The Arduino is doing.
You can also use serial communication to talk between devices and much more,
which you can read about in the Arduino reference.
Moving on to using analog inputs,
which are these pins labeled with letter
A's across the board from the digital input output pins.
These special pins.
Are connected to the Arduino’s analog-to-digital converter or ADC,
equipped to convert an analog signal between zero volts and five volts
into a range of numbers from 0 to 1023.
I've got a small breadboard potentiometer,
Plugged into three rows on the breadboard.
The outer pins are wired to power and ground,
And the center pin To analog pin A0.
I've got an LED wired up to pin 9 so I can use PWM.
The built-in example to use for this circuit is under O3 > Analog > AnalogInOutSerial
After uploading the code, I'm going to open up the Serial Monitor
and also observe the LED
while twisting the knob on the potentiometer.
The values read by the analog input
are printed in the first column
and the brightness values applied to the LED are printed in the second column.
This sketch uses the map() function on line 39.
Which takes one range of numbers and massages it into another range.
It takes five arguments:
The value to be changed,
The lower bound of the values’ current range,
The upper bound of the values current range,
the lower bound of the target range, and the upper bound of the target range.
So this line of code sets a variable
outputValue to a number between zero and 255,
depending on the position of the potentiometer.
The serial printing commands on line 44 through 47
print text labels.
Whatever's inside the quotes, and then the values
Incoming from the sensor, and outgoingto the LED.
Seeing these numbers together in the serial monitor
Can really help you understand how functions like map() work.
Keep this in mind whenever working on your own sketches.
There are a multitude of
Sensors that act like digital switches or analog inputs
and can therefore use the same code
we just went over.
Try swapping your push buttons out for a
PIR Motion sensor, or your potentiometer for a photo resistor, for instance.
If you watched the last episode and made it this far,
You are now equipped with the basic building blocks of most Arduino programs.
When you think of project ideas, ask yourself what the inputs are,
what the outputs are, and whether they are analog or digital.
Then you can mash up the
built-in examples accordingly and get a good start on your prototype.
I recommend trying out more of the built in examples on your own.
Next time we'll cover Arduino libraries and some of the devices
you can use with them.
I've put links to some resources in the description.
If you've got more insight and suggestions for beginner Arduino experimenters,
please leave them in the comments, and we can all learn together.
Check out the series playlist
and don't forget to subscribe,
so you don’t miss the next episode.