hello world this is cs50 today we're not
gonna be talking too much about software
today we're gonna be working with
hardware in fact my particular favorite
type of hardware Arduinos good afternoon
my name is Robert Jamar malate this is
cs50 and today we're gonna be going pro
with Arduino so you might be asking what
is Arduino well physically I have it
here in my hand or do we know is this
microcontroller where essentially you
can put a bunch of electronic components
together and control them together it's
open source so the hardware and the
software is available for anyone to use
that's why there's a lot of knockoffs
available but these knockoffs have the
same exact quality as this Arduino board
so it doesn't really matter which type
which one you get but in the end the
greatest thing about our do we know is
that it's great for making electronics
projects so for instance here someone is
making some remote control circuit or
another person made this remote control
robot with a bunch of servos and an
ultrasonic sensor all of these is
possible with Arduino so you might be
wondering okay what are we going to
cover today
well Arduino is quite a large topic to
cover so up here in the board what is in
green ozone are gonna cover and red is
not what we're gonna cover so first what
we're not gonna cover
unfortunately Arduino involves some
electrical engineering as you can see
here with these bread boards you need to
make sure things are wired properly
we'll teach you the basic electrical
engineering but if you want to know more
advanced then I would highly recommend
checking out books or asking or checking
out online for more resources
next is working type with different
types of Arduino boards most Arduino
boards are about the same but the thing
is for now we'll just be working with
the Arduino Uno because this is pretty
much the standard board that people use
and finally integration or external
hardware it is possible for instance one
time I made a robot arm that could be
controlled through Python with voice
commands but the thing is that involves
a little bit more technicality and this
is kind of outside the scope of what we
want to come
today so unfortunately we won't be
integrating hardware with Arduino with
any other thing just with the laptop to
send in the new code that Arduino will
run but what we are going to cover is
one setting up because we got to start
somewhere and of course we got to start
with setting up and the next thing is we
also need to work with basic Arduino
components so strong here across the
table is various components I have here
is an LED circuit we also have a button
here we have this ultrasonic sensor
which is used for measuring distances we
have this servo which can be used for
many many things such as creating
movement moving objects here we have
this potentiometer and these components
will essentially form the basis of what
today's seminar is going to be about now
there are some extra components here
such as is this 9-volt battery this
voltage regulator but in the end what
really matters is these core components
and what we're going to learn because in
the end we want to make sure that
through working with the simple projects
and going through debugging common
issues we want you to have the
foundation to work on cool or do we know
projects so you guys can start to dream
about what cool projects you want to
tackle on next
okay so first this is gonna be the
agenda for today and we'll start and
here is also the materials so Arduino
does involve some materials you have to
get because it is a hardware project and
hardware is not as cheap a software but
it is highly rewarding and if you want
to follow it with today's seminar and
topics these are the materials you will
need so strong you're across the table
we have this Arduino Uno we have a lot
of wires and as you can see with these
bread boards there's a lot of wiring
involved we have several bread boards
but you just need one and then we have
this cables to transfer the code from
our laptop to the Arduino then we have
an LED a button several types of
resistors a potentiometer ultrasonic
sensor and a servo
first getting set up so or do we know
can't be run on the cs50 IDE because the
cs50 IDE doesn't have it however I would
highly recommend downloading and
installing the Arduino into your
computer because this is the best way
you can work with the hardware and also
you can work offline however if you are
inclined to work on the Internet Arduino
does have a web-based editor which is a
cloud version you can follow the link
there post it onto the screen and you
can see the cloud software they have
available to work with our do we know
code assuming that it gets set up let's
start with the IDE so here's a picture
of the IDE but better let's actually go
to the real IDE so as you can see here
this is Arduino IDE we're gonna put all
our code here it's gonna run between
these things and we're gonna have
several several things in here but the
important thing is we want to focus on
several key things first is this void
setup
so what void setup means is that when
the board is running for the first time
or whenever you press the reset button
on the board this is the batch of code
that's going to rerun so for instance
here it's going to set this pin to an
output pin every time this has been set
up the next major part of the code is
loop so or do we know is a
microcontroller and the purpose of one
of the huge advantages of our do we know
is that as long as it has power it can
continuously run the code until you
press reset button once you press reset
it just runs the setup again and then
you go back to the loop and what loop
does is it kind of runs like a while
true or a forever for loop and it just
keeps running and running the code
whatever is there so as long as the or
do we know has battery or power running
through it it'll keep running that batch
of code within the braces of the void
loop now the next thing is we also want
to be familiar with some of the things
we have to set up so over here if you
check out the tools tab
you can check out the different types of
boards available comma another common
bug that people face is dealing with is
sometimes choosing the wrong board
so to simplify today's lecture we're
just gonna use the Arduino Uno board and
also this port number now it's not
highlighted because Arduino is not
connected but allow me to connect this
to the computer and port basically is in
charge of making sure that all the
commands get sent at a specific port so
you can see over here that it's on comm
five and and of course you want to save
it just to make sure you resave all our
spaces let's get back okay now we've
finished the set up and we kind of see
how the arduino ide looks like now let's
get on with basic electrical engineering
now this is a huge simplification of the
whole electrical engineering field but
this is what I found helpful that helps
me understand and at least get my foot
off the ground working with hardware
projects and these are the five rules I
generally follow so rule one electricity
flows from positive to negative to
ground so we can see here with this
simple circuit that electricity flows
from the positive flows from the
positive wire down the negative wire and
from the negative wire goes to the
ground so you can imagine it as a
waterfall analogy where we have our
water source at top water or aka
electricity is flowing through a circuit
until it reaches the lowest point point
ak8 ground and all components in this
circuit have to obey this law so make
sure that you always make sure that your
components have this positive connection
are connected to this negative terminal
and flow to ground
so remember positive to negative to
ground second must flow to the same
ground so we're gonna see that soon see
here with this servo components that
things are not it's gonna get a little
bit Messier but the thing is we all want
to make sure we share the same ground
and for the sake of today's seminar the
same ground will be this Arduino board
so ground is basically where just
electricity flows because as we saw from
rule one it flows from positive to
negative to ground
all electricity has to flow to the same
ground or the same point and as I've
said before the Arduino is going to be
that ground
now the next one same wire same rail
same flow okay so we all know that
electricity conducts and it follows for
instance if I stick a metal wire into
the outlet obviously there's gonna be
electric flow flowing there and from
these wires what I mean by same wire
same rail same flow is that the same
quote-unquote electric signal is going
to be flowing between these wires so if
this comes from pin 9 of our do we know
the electric signal from pin 9 is gonna
flow from this wire now what do I mean
by same rail since we're dealing with
breadboards we're gonna need to make
sure we understand how things work with
breadboards so the breadboards on this
table follow this schematic and imagine
each of these green lines as one rail so
this entire column means that if I stick
let's say a signal from pin 9 there this
whole rail will contain the signal from
pin 9 because electricity as we've seen
from here flows from positive to
negative the ground it's gonna flow from
here and then it's just gonna sit there
until it continues flowing now also on
top it's kind of the exception here is
just these are continuous lines so this
means that if I plug something into here
that electric signal is gonna flow
through this entire rail here and same
for the red one down there
now for today's convention blue will
just mean ground and red will mean the
positive voltage you can do it whatever
way you want there's no rules against it
it's just that to make things simplify
or simplified and to work with kind of
convention we'll just be sticking with
blue as ground and red as by voltage
rule number four don't exceed the
voltage
okay so thing is we can break electronic
components pretty easily just by
sticking these things so for instance we
have this LED right over here this small
little beauty unfortunately can only
take
about two volts if I wish to stick it
directly to this 9-volt battery this
thing's gonna heat up and blow up and
it's also gonna die and I might get some
burns which we don't want any of those
things to happen because these are
precious components and we don't want to
hurt your fingers so to make sure with
that we want to make sure that the
voltage is controlled and we do this
either through resistors or separating
the type of voltage that flows so for
this you might be wondering okay why is
this the case
well final rule for today is Ohm's law V
equals IR so we're not going to be doing
any calculations all of these have been
done already for you with the
calculations but essentially it relates
voltage it means that voltage equals the
resistance times the current times the
current and if you do the math you can
see why sometime why you can see why
some components will blow up or
sometimes some components won't run at
all okay so next we're gonna start off
with LEDs so as we've seen here this is
an LED it has it's basically just a
fancy it's just a lot it's just
essentially a light bulb where there's a
positive terminal over here and negative
terminal and then here are some various
colors LEDs and here are the commands on
the screen that are most relevant to how
an LED will work so we're gonna have pin
mode and digital right so in order do we
know pin mode means setting the pin up
for something
by default it sets it to input however
for LEDs because we want to output
output a light where I have to set this
led to output and digital right
this is varying the voltage that's
flowing to the LED since the Arduino is
controlling how that electricity flows
we want to vary from high to low okay so
as you can see here we have the circuit
setup this small circuit we have a
resistor here this will protect the LED
from blow
because the Arduino provides about 5
volts directly to the LED and if we
didn't have this resistor this could
this could severely damage it it might
not look like it in the beginning but
it's gonna damage it so we want to make
sure we protect against that from
happening then also we have this then
we're gonna flow from the positive
terminal to the negative and then from
the negative is gonna flow to the ground
aka the ground pin on the Arduino okay
so let's set up these final connections
I'll just leave these white wire here
for now and I'm gonna set this black
wire because it shares the same railing
as the breadboard as the ground for the
LED I'm gonna stick it into the ground
pin over here okay now we got the
hardware kind of set up we need to
figure out okay what to do next
so let's actually go to the Arduino
terminal and check things out so first
line of code is this is a typical common
practice is first to clear your
libraries but in this case we're just
simply want to turn an LED on and off
make it blink a little bit so we don't
need any libraries but the first thing
we want to do is give the pin a name now
you could just use now for the sake here
we're gonna be using pin 9 as the main
circuit you could just use the number 9
directly but for good software
engineering practice and for good
documentation because when you look back
at your password we no projects you want
to know what this what your code is
saying or what it's going to do so what
we do is we declare here this line up
here is saying that ok pin 9 will be
known as LED now we're gonna run this
setup function now we're gonna set the
pin mode to output so what this is
saying we're gonna change LED aka pin
pin number 9 as an output so it's
there's also input and there's also
output but since we just want the
electricity to flow or the logic to flow
from the board Arduino board into the
circuit we that means we're gonna set it
as an output
next is the loop function okay so first
is we want to turn on the LED so we're
gonna do digital right which means okay
I'm gonna write some command to the to
the arduino and it's gonna tell that to
turn the LED on to hi
so what hi means is that because the
weight Arduino works between high and
low is that it goes from zero volts to
five volts and there's nothing really in
between there's this thing called pulse
width modulation but we will not be
covering that today but essentially by
high means okay let this thing receive
all the voltage we can provide and low
means okay let's cut off all the voltage
to that particular pin number so we're
here first
in order to turn on the LED we're gonna
do digital right right it to pin nine
and we're gonna set it set it to high
this bill a command just make sure that
things aren't going too fast so we're
just gonna set it to delay it for one
second here the way the delay command
works is that it uses microseconds so
just do some of the math we'll get this
means that delay 1,000 means delay for
one second
next is we're gonna turn off the LED so
as before here quite the opposite first
we write wrote it too high now we're
setting that same pin but we're setting
it too low aka we're cutting off all
voltage flowing to it or we're making
zero volts flow to the LED and then
we're gonna delay again just to make
sure that you know we got a nice pattern
of blink of a blinking LED okay so what
we're gonna do is we're gonna upload
this you can press this button right up
here in the corner the one with the
arrow pointing to the right click upload
useful thing about the Arduino is that
IDE is that it can catch mistakes before
compiling and we'll see that once we get
to the next circuit okay so right now
we're going to so we have this circuit
setup but the thing that's missing is
there's no electric flow flowing from
here to the LED so in order to do this
remember that we said we set pin 9 2
equal LED so we need to set this
breadboard wire into the pin 9 slot and
as you can see here this LED is blinking
quite brightly quite smoothly and it's
blinking and often turning it on and off
every one second
so that's it for the LEDs up next is
buttons now I would argue that this
segment is going to be a little bit more
difficult because of the logic involved
with buttons but essentially your button
is just you prep you just press this
it's essentially like this switch that
you press to send out commands so think
of a button as like a light switch but
instead of flipping flicking it on and
off you just push it or push it and push
it again to turn it on or off so the
relevant commands for button is going to
be digital Reed and this is the bounce
function so for digital Reed we want to
make sure that we're reading the state
of the button because this time the
button is going to be the input to the
board this is gonna be telling hey we're
receiving some signal from the outside
world this is what it's saying
so this digital regis interprets the
commands from the outside world and then
boolean the bounce
so the finicky thing with buttons is
that sometimes when you press them the
signal is not so clear and immediately
now before we get into that more in
depth let's just get started with making
this light bulb work with a nice button
because it's nice that it's blinking on
and off but maybe you want to give it
some control that hey if we push the
button it should be on and if we let go
of the button it should be off so let's
get to coding write that so we can see
up here we're going to be setting these
so we're gonna have LED we're gonna have
pin 9 set as LED and we're gonna set pin
2 as the buttons pin and our avoid setup
we're gonna set the LED as an output and
notice here that for the button this
time were you setting it as an input or
do we know by default just sets all pins
by as inputs but we just want to make
sure - for clarification set
clarifications sake that we set this as
input now in the void loop function
let's read through what's happening so
if digital read button equals low so
what this means is that if I press the
button right here and so if we press the
button it gives the signal high because
remember as I said a button is kind of
like a switch think of a button as a
switch that tell that connects this
electrical wires to continue the flow of
electricity now if we're gonna turn on
imagine that an LED is in between the
button imagine a buttons in between the
LED and a voltage source if the button
is not being pressed then there's no
voltage or current flowing to the LED
now if we push the button it completes
the circuit so current is allowed to
flow between the voltage source to the
LED so if the button is not being
pressed we just want the LED to be off
else if the button is is pressed then we
set it to high okay so we're going to
upload this code to the Arduino
and then let's see what happens so we
have right now here and here's the
breadboard we are going to connect this
red wire to the five bolts because this
time the button also needs to receive
some power and then we also need to send
the signal from the button to the
Arduino so we're gonna connect it right
between that resistor and this button
right here and as you can see when I
press the button it turns on when I let
go of the button it turns off now you
might be wondering and asking to
yourself okay what's up with this
resistor right here now I won't get in
too much detail about this but these
things are called pull-up resistors and
the reason why we include this resistor
in between is to prevent some weird
readings from happening because if we
were to remove that resistor the LED
would turn on and off because of mixed
electrical signals coming from around us
but the thing is we want this to be
controlled we want the button to turn on
only when the button is being pressed
and turned off when we let go of the
button
so that's a fun way to work with buttons
but when was the last time you've ever
had to hold down a button to keep the
lights on it's not a fun thing to do
when especially you got other things to
do such as your homework so we want to
make sure that whenever we pressed the
button once it turns on and when we let
it go it's still on and the only time we
turn it off is that when you push the
button again so back to what I was
saying about this the bouncing thing now
you can see as you can see here this is
some mixed electrical signal that's
coming from a button if I were to press
the button for a few micro seconds the
electric signal is going to be
fluctuating so we want to make sure we
prevent that from happening or we want
to make sure that okay we only read the
state of what happens after we press
that button after those five
milliseconds because in between the time
things get a little bit finicky so in
order to deal with this we have these
this debounce function now the logic is
a bit complex but essentially is we want
to read the state of the button wait for
five milliseconds read the state again
if it's on then we turn on the LED if
it's off we turn off the LED now let's
see this code in action so over here we
have the same thing we're setting up our
button and LED at preparing it for the
pin numbers and now we also have these
additional things we have previous state
current state and LED on so previous
state stores the last state of the
button current state will store the
current state of the button and right
under set to low because in the
beginning let's just assume that there
is no that there's no voltage flowing
that the lights in the room are
completely off and then the LED on is
keeps track of whether or not the LEDs
turn on or off so we're gonna go through
this setup again we're going to set the
LED as the output and the button as the
input and before we go to the void loop
function down here we have this function
inserted in between now the way or do we
know
IDE works this kind of follows the C C++
syntax so to make sure that the void
loop can actually use this bounce
function we need to make sure it's
elevated up or we hint that the or we do
offer early function call earlier on in
the code so here's the bully in the
bounce so it takes in a boolean so what
it does is first it reads the buttons
current state and if the last is not
equal to the current state if the last
date is not equal to the current state
of the button we're gonna delay for five
microseconds because as we've seen that
weird fluctuating thing that was
happening
we're fluctuating electric signal and
then we're gonna set the current to
whatever the button is reading so if it
was pushed it'll set the true if it was
pushed it'll set to high or true and if
it's not push it'll set to low or false
and then we're gonna return that output
so let's go through the void loop code
because this is what's gonna be running
on forever so first we're gonna check
the previous state and we're gonna set
that to the current state now if the
previous state equals low and the
current state equals high then what we
want to do is do the opposite of
whatever led is on so for instance in
the beginning of the circuit when we
haven't pushed the button when the LED
is off previous state and current state
is going to be both low but once we run
into this line that the bounce when I
press the button current state now is
going to be true or high but previous
state will still be low so if previous
state means low and current state means
high that means we push the button so
that means we're gonna turn we're gonna
do the opposite of whatever the LED has
been set to before so if it was set to
false or low it's going to be it's gonna
be turned off but if it's set to high or
true it's gonna be turned on then after
that we want to set the previous state
equal to the current state
and hearing the gentle right we want to
make sure that we all so that we write
out that output total led and I forgot
to mention that I did allude to before
but I didn't stated it explicitly in our
do we know true and hi are about the
same thing
false and low are also the same thing so
you notice that okay you might be
thinking to yourself what happens if the
LED is what happens if we don't press
the button now remember that this the
bounce is continuously reading that and
this the bounce is called within this
this void loop and it's void loop is
constantly running the functions within
here so we can see that it's constantly
running it so it's always going to
change the state automatically so that
way you see that when we press the
button we won't have to hold it down
just to keep the LED on now to make sure
that this works let's actually upload
this project so we're going to upload
this code
now let's see what happens I'm gonna
push the button I'm not holding the
button but the LED is currently right on
and that's amazing
think about it we just went despite the
little bit complexity of this function
we were able to create essentially what
works as a light bulb it might not be
amazing to you but building upon this
type of logic for the button you can
build multiple things for instance if
you were to work with a speaker and you
were to connect some LED to it and if I
were to press a button you could create
a mini piano so what you could do is
another thing about this project that I
want to make sure that you guys learn
yes so I could press the button again
and it goes off so here in the code is
okay let's say that we just press the
button now we let go of the button so
it's gonna render the bounce again we're
gonna pass in the previous state and the
previous state right now is true so we
just press a button previous state is
true so we're gonna run it through here
we're gonna read the button if the
current state the current state is gonna
be low because we're not pushing the
button anymore so since the last state
is not equal to the current state
because the last state is high and the
current state is low we're gonna delay
we're gonna delay it and then we're
gonna continue reading then we're gonna
set the current state to the digital
read button and then we're gonna return
the current state so what happens after
this is that the previous state is high
and the current state is low right now
so we go to this void loop if current
state we ran the current state function
so current state is low previous state
is high now we're gonna run this logic
now this logic won't check out because
as I've said previously it is high but
current state is low so the LED won't
switch it states so previous state is
gonna still equal the current state but
the key here is this LED on because
remember that this LED on is keeping
track whether or not we have turned the
LED on or off so in this case when we
press the button LED on will switch to
true
and here's the code where it actually
switches whether the LED turns on or off
but since it failed this condition the
previous state was high in the current
state was low the LEDs whether the LED
turns on on or off is still not still
hasn't changed it's still on so that's
why that even though we were to write
this digital function LED on is still
true or still high so it goes off is
when you press the button again so the
question in the Mike is how did it go on
and then the next question is okay how
does it go off again thank you for your
question so over here and that the it
goes through that it goes to loose this
loop again where that we've pushed the
button current state is now high what
previous state is now low because we
just we just change it right over here
before and then we do this check again
it passes the LEDs switches it's logic
from true to false which set the
previous state to the current state and
then we write it out so feel free to use
this as a boilerplate template because
this is a common thing you you you want
to be using for your Arduino and same
thing for the hardware where you have
the button here some voltage to the
rights to the left side and some output
to the left side with a resistor in
between okay so that covers buttons for
now let's move on to potentiometers or
also known as pots so potentiometers
might seem like this fancy word or
something a little bit obscure but
potentiometers are actually used in a
lot of places so what they are
essentially is variable resistors they
change their resistance that's flowing
through a circuit so imagine if you just
have a bunch of resistors and you're
continuously moving the wire from one
resistor set to another to another but
instead you have this knob that takes
care of it for you
the relevant commands for a
potentiometer are up here on the board
so we're gonna have this analog read
we're going to be reading what the
potentiometer is saying we're gonna be
mapping its outputs so map is basically
this function that takes one input and
spits it out to another so it takes in
the input then it needs to know what are
the ranges of the input values so the
minimum and the maximum and it allowed
maximum output values the minimum and
maximum output values now we'll see here
later how this will work and this will
especially be useful for the servo once
we get to that section and then another
thing also we want to start working with
is the Arduino serial monitor this is
essentially a way to see what's
happening and is a useful debugging
technique also and we also we want to
print out the output because print
debugging is useful in software but it's
also very useful in Hardware to see not
only what's happening but what kind of
inputs are we getting now let's move on
to the code so we have this random stuff
over here we have this voltage regulator
but for now we're just gonna ignore all
this stuff and just focus on this
potentiometer right here so it has this
knob here that you can turn back and
forth and potentiometers as I've said
before they've you they're using a lot
of things for instance in a speaker when
you turn the knob when you turn up to
the maximum you're turning the
resistance very low because sound is
essentially just a bunch of electric
signals and when you flow that electric
signal and there's no resistance full
maximum sound is hit but if you turn it
all the way up the resistance then
there's no electric flow flowing aka
minimum sound so we're gonna input we're
gonna plug that in right here we're
going to be using this board so I'm
going to connect this to the five volt
rail and this this wire here to the
ground and this entire rail here is
going to have that 5 volt 5 volts and
the graph and the negative volts now
let's look at the code for this so the
way potentiometers work is that there
are three in
there are three inputs over here and as
you can see in the picture there's three
prongs that's sticking out one of them
is for the on the either sides either
one of them could be positive or
negative it doesn't really matter you
can switch them but in the middle for
most potentiometers they're generally
the cone code input signal okay now
let's get on with the potentiometer okay
so we're gonna set this now so we're
gonna set the potentiometer to pin zero
and value will just or whatever the
value that it's reading from the
potentiometer and under doing on board
there's also this thing called analog in
these are the analog inputs that you're
going to be providing so these are
pretty much the signals that you'll send
from the outside world such as such as
turning this turning this potentiometer
nava round or getting some sensor values
and in this case the input will just be
from the potentiometer so we're gonna
stick that inside there serial dot begin
so what we're doing is we're setting up
this serial or communication rate
command and essentially what this does
is okay so the signal that we're gonna
be sending and receiving from the
Arduino the communication is gonna
travel at about nine thousand six
hundred nine thousand six hundred and
this is just a common value you can set
it said this anything but for
conventions sake it's nine thousand six
hundred now what we're gonna do here is
we're going to first read the value from
the pot so analog read from pot whatever
value is coming from the zeroth pin we
want to print that to our serial and
we're going to live from five hundred
seconds or five hundred microseconds
because we don't want to read too much
commands too fast okay so before we get
on to see what's happening I just want
to show you guys what the serial monitor
is
okay so does it look like it's popping
up right now which is all right we can
fix that
oh okay this is another educational
moment actually you see here that when I
try to open the serial thing the thing
is there's no communications and you can
see here this but this bug at the bottom
board at count five is not available so
what this means is that in order for the
serial command to open in the first
place there needs to be something
communicating with the computer okay
it's dard do we know so in order to make
sure that we can open it let's stick it
there now let's open the serial monitor
and as you can see here this is kind of
like some terminal that comes through
it's pretty much blank you can type in
some commands send them some things but
for now what we what I want to show you
guys is that this will be used for
reading inputs from the potentiometer so
we're going to close this we're going to
upload the code from - from this code
into the arduino and then let's go to
serial monitor
as you can see here numbers are starting
to pop up so this is 10 23 this is the
maximum value of this resistor now I'm
going to be turning this knob and see
what happens to the values and you can
see it's slowly starting to decrease now
these correspond to the various
resistance values because this is a 10k
resistor but with that map function that
I mentioned earlier this could be used
for many many things such as oh how
bright do I want my LED to be or what
color should I set it to or what
positions should I set my servo to we're
gonna cover that position actually
towards the last project of today but as
you can see here this is a good way to
debug because now you're seeing okay
what inputs are we getting so we're
gonna close that and that is it for
potentiometers now ultrasonic sensors so
the reason why I'm covering ultrasonic
sensors is these tend to be quite common
in a lot of our Duino kits and there are
tutorials online but I would like to
make sure that people learn how to use
these because these are surprisingly
useful so the purpose of ultrasonic
sensors is that they can measure
distance and this might not seem like
much but for instance if you're doing a
physics lab and you want to measure how
fast an object is traveling you want the
way this thing will work is it sends out
a sound signal from one end and it and
it measures how long it takes for that
signal to come back in the other and
here we have four pins this first pin
VCC is for the input voltage this other
pin at the end is for the ground and the
last two pins in between is for the
trigger and for the echo so trigger is
to send it to prepare to send a signal
and echo is to take to kind of measure
how long it takes to receive that signal
back and the relevant command here is
going to be pulse in so we're gonna we
need to know what echo pin we're gonna
be working with and we're gonna be
setting it to high okay so let's
disconnect these things from the board
so I have this nice ultrasonic sensor
over here I connected the five to five
volts and the ground to the board and I
also have these two trigger pins now
we're gonna check the code to see which
pins we're gonna put this in let's just
make the font a bit bigger before
continuing on okay so here in the code
it says trigger pin is seven and echo
pin is six you can put them in whatever
pin you want here except the analog
input you can put them in any pins you
want but for now let's just use seven
and six so the green wire here goes from
the trigger and since triggers at seven
I'm going to put this into seven and the
echo pin is this tannish wire it's gonna
go into six okay
now let's connect these stuff to the
five volts and ground respectively just
to make sure we get some current flowing
but the thing is no logic is being
loaded because actually the Arduino
takes it's kind of keeps in memory the
last thing that the code you uploaded to
it
okay so now let's walk through what's
happening here so keep in mind that the
are that the ultrasonic sensor uses
sound waves to measure distance and the
time it takes to travel so we're gonna
take the duration and the distance we're
going to set these variables to be used
later so the trigger print is the output
this is the one that's gonna send the
sound signal out the echo pin is going
to be the input which is going to
measure how long it takes for the signal
to be received and we're also gonna do
we're gonna use a serial monitor so we
can see what distance we're getting so
avoid loop first we want to clear the
trigger print to make sure that there is
no previous stuff that was used in that
sensor before now we want to set the
trigger pin on high state for 10
microseconds so what this does is we're
gonna send a signal now from the trigger
pin so we're gonna send a signal from
here we're gonna set out a sound wave
and we're gonna delay it and then we're
gonna turn it right off
now once that sound wave bounces off the
object and it's coming back and hits the
other pit other echo pin what we want to
do now is take okay how long does it
take for it to come back so we're gonna
do this pulse in and pulse in returns
how long the sound wave travels in
microseconds and because this speed of
sound is fixed in most situations about
348 meters per second we just have to
divide that by how long it takes how
long it took for the object we just have
to take that speed multiply it by the
time attack travels and then we can get
back how we can get back the measurement
of how far that object is here we're
gonna be using centimeters so which
explains why this is not what a number
is a bit is a bit low then after that
we're gonna print out this thing
distance this is simply gonna literally
print out distance inside the terminal
then we're going to print out okay what
was the actual value we got and to make
sure we understand the units we're gonna
use centimeters in this case and to make
sure that we're not gonna be receiving
too much commands I'll set this from 5
to 500 500 microseconds or about half a
second so every half a second we get a
different reading okay all looks well
let's upload this to the board
and looks look at the serial monitor so
we can see here that we're getting now
some measurements and the way this thing
is right now it's pointing towards the
ceiling so it's measuring that the
ceiling is about 133 centimeters
let's say I have this book over here and
I'll point it a bit right here and
notice that as I move closer that this
distance is getting smaller and that as
I move further the distance is getting
larger now keep in mind this is this
thing is actually is setting out sound
waves and it's measuring them so it will
get lost as it goes as you try to
measure out something further so there
are limits to what this thing can
measure and in addition you don't want
to measure things too close because once
in my hand is right now pressing on the
set on the ultrasonic sensor but notice
that the reading is saying it's 3000
centimeters away or 2,000 so you want to
make sure that you're not too close to
it let's close that for now
awesome so from ultrasonic sensors let's
move on to the final part servo is my
favourite components so servos are
essentially these motors that control
eyes motors that can move around from in
the fixed place so here we have this
servo library the relevant commands are
we need to include a servo library we
need to declare this object called my
servo we're gonna attach it we're gonna
write to it the angle and we're gonna
read it okay let's see this in action so
let's separate this and we're gonna pair
this with the potentiometer to
illustrate what's happening right here
and I'll just leave this right there
just for visual sakes so in this circuit
here I included a voltage regulator now
depending on your servo sometimes your
servo can take a higher load but the
thing is you don't want to connect a
servo directly to the Arduino because
it'll pull out a lot of power and you'll
and you'll notice that hey why is my
Arduino not connecting - no my computer
it's
because this servos draining out a lot
of power and Arduino can only provide a
fixed amount so what we're gonna need to
do is we need to provide an external
voltage source now let's be very careful
of how we wire this properly so this is
not the dangerous part but the dangerous
part is when you're combining voltages
so this entire rail in the breadboard
this is all going to be from the battery
do not ever put the same voltage right
here where the Arduino is going to be
things are gonna get really messed up
quite quickly however we do want to make
sure we share the same ground though so
we're gonna flow from the battery to
here and into the Arduino board now
notice this black component right here
this is a voltage regulator so what this
pin here on the left on the rightmost
pin is going to leftmost pin is going to
be the input the output voltage is just
gonna be ground and over already out
this is gonna be the ground and on the
rightmost pin is gonna be the output so
the good thing about a voltage regulator
is that it makes sure that we still have
this constant voltage flowing so whether
or not this thing drops from a nine to a
seven automatically we know who that
we're still gonna get five volts so
let's see this in action let's make the
font bigger and let's wire this up
quickly so because we want to use only 5
volts I'm going to connect this the way
to start this servo before I forget to
mention we can see here that servos have
these tricolor rings sometimes it count
comes in brown red and yellow but brown
or black corresponds to ground red
corresponds to the input voltage or five
volts whatever volts you want to provide
and white or yellow corresponds to the
signal so with those facts in mind I'm
gonna connect this red to the output of
the five volt regulator I'm gonna
connect this black wire to the ground
because remember we want to keep some
common ground and what I want to do next
is for the white wire we want to connect
it to the Arduino because remember the
Arduino is the
master the brain behind all of this so
we want to connect this to pin nine okay
now let's also connect this stuff from
the potentiometer into the Arduino so
we're gonna connect this to the ground
connect this little wire to the five
volts and we're gonna connect this pin
output to pin zero okay now let's see
what's happening here
the code is not that much as the
ultrasonic sensor or the the bounce one
with the button but this is pretty
powerful code I'd say so we included
this servo library up top here next
we're setting out the pins so we're
gonna set servo we're gonna set the
output for the servos pin nine and for
the potentiometers pin zero we're gonna
use the Potts value we want to measure
the pots value and because this is gonna
be outputted somehow we're gonna declare
this servo we're just gonna say it's my
servo now this circle dot H builds upon
a ton of abstractions which you don't
have to worry about so let's just work
with this we're gonna attach this line
here is gonna attach this servo to pin
nine so this is saying that okay I want
all the logic flowing from the board to
go to this to be out put it into this
pin and we're also gonna read from the
terminal just to see what happening
right now
okay so first in the void loop we're
gonna read the value from the
potentiometers so how far did we twist
this thing next we're gonna print it out
also then the key thing here is the map
function so the way servos work it can
only take values from zero to one
hundred seventy nine but the
potentiometer puts out values from 0 to
1023 so in order to do this we need to
map it so this is saying okay we know
that the pots value is gonna be between
zero and one thousand twenty three so we
want to make it map it to between zero
to one
79 so this map function essentially
takes care of all the logic you have to
do and just outputs the value you want
after that you're gonna write to that
servo so by right
it depends what servo you have in this
case we have a naught we have a
continuous rotation servo so dot write
sets how fast the servo is turning in
one direction if you have a ninety
degree servo it changes the angle of
what the service position at so since
we're working with a continuous rotation
servo
this is just going to tell okay how fast
are we making the servo turn and we're
gonna delay it for fifteen seconds okay
so let's upload this code now the next
thing we also want to keep in mind is
just making sure again we check all that
the components are located together and
as you can see here this thing is
already starting to spin just pause it
in for now let's just insert this so the
viewers at home can see
just add this right here
and let's open the tools the serial
monitor just to see what's happening on
the inside so notice that as I turn it
around for those not able to see
currently the servo spinning right now
unfortunately I don't have anything to
clearly display it but we can see over
here that the servo is currently turning
and if I turn the potentiometer the
other way around
it stops and spins the other direction
so we can have a lot of fun with this we
can keep switching it back and forth
back and forth just to see what's
happening yes here the potentiometer is
the input to control the speed so it's
kind of like the speaker where you set
the volume it sets the speed so that's
it for our wonderful projects now I just
want to get to some common debugging
issues and how to solve them
so first the circuits not wired properly
the most common thing I've ran into is
people forgetting to connect something
to the positive voltage or the ground so
make sure that each component is flowing
from positive to negative to ground next
is drawing too much current and this is
relevant for huge components like servos
because they're doing a can only provide
about 500 milliamps but this thing could
take a lot more than that and could take
a lot more voltage than the Arduino
could provide wrong board selected as we
saw in the Arduino IDE there are some
there are multiple boards and a common
thing is people sometimes if you work
with multiple boards they select the
wrong one accidentally and the next one
is the port not selected that is another
common issue that we can see over here
so we go to tools we just want to make
sure that one you're working with the
right board and the next one is that
there's actually a comm and it should be
it should just display right there
because it's a little bit too complex to
deal with yourself
but essentially comm is just whatever
this thing is connected to and don't
hesitate to use serial the serial
monitor as we saw we're we're able to
see the inputs from the potentiometer
but this will be really useful for once
you start using more complex
sensors to try to output the value
readings so this is the end for a
presentation but before we leave I just
want to leave you guys with extra
resources if you want to pursue more
with Arduino we just scratch the surface
of what we could do the number one book
I recommend is exploring Arduino by
Jeremy Blum I've used this book
personally and this is what made me love
Arduino because the explanations in the
book are great but it also shows it
makes you start to think okay what can I
do with these projects and build up upon
them and then it was also what inspired
me to kind of give this seminar is I
want you guys to work with these basic
components and build abstractions upon
them so with the button it might look
complex and the code there is a little
bit rough but you can build upon that to
create a mini piano or some fancy light
switch or this are connected to some LED
to change its colors next is Fritzing if
you're gonna work with our do we know
Fritzing is just this thing to lay out
the breadboard schematics so for here if
you want to document the process I would
use Fritzing I will be uploading the
schematics the code for all of these all
of these components so you viewers at
home and people here can view them and
recreate them yourselves and for our do
we know project ideas you can check out
my website down here in the link there
are there's going to be Arduino projects
currently there's nothing there yet but
I will be uploading some projects for
you guys to try out which I believe will
further your education so with that in
mind this is cs50 I hope you enjoyed
this seminar and start making stuff with
Arduino