ROBERT JOMAR MALATE: Hello, world.
This is CS50.
Today we're not going to be talking too much about software.
Today we're going to be working with hardware,
in fact, my particular favorite type of hardware, Arduinos.
Good afternoon.
My name is Robert Jomar Malate.
This is CS50, and today we're going to be going pro with Arduino.
So you might be asking, what is Arduino?
Well, physically I have it here in my hand.
Arduino 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 one you get.
But in the end, the greatest thing about Arduino
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, OK, what are we going to cover today?
Well, Arduino is quite a large topic to cover.
So up here on the board, what is in green is what we're going to cover,
and red is not what we're going to cover.
So first, what we're not going to cover--
unfortunately, Arduino involves some electrical engineering,
as you can see here with these breadboards.
You need to make sure that 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 checking out
online for more resources.
Next is working 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 cover today.
So unfortunately, we won't be integrating
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've got to start somewhere and, of course,
we've got to start with setting up.
And the next thing is we also need to work with basic Arduino components.
So
Strewn 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 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 Arduino projects
so you guys can start to dream about what cool projects
you want to tackle on next.
So first, this is going to 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 as software.
But it is highly rewarding.
And if you want to follow with today's seminar and topics,
these are the materials you will need.
So strewn here across the table we have this Arduino Uno.
We have a lot of wires.
And as you can see with these breadboards,
there is a lot of wiring involved.
We have several breadboards, but you just need one.
And then we have this cable 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 Arduino 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 posted onto the screen,
and you can see the cloud software they have available to work with Arduino
code.
Assuming that you get 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 going to put all our code here.
It's going to run between these things.
And we're going to have 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 Arduino is a microcontroller, and one of the huge advantages of Arduino
is that, as long as it has power, it can continuously
run the code until you press Reset.
But the 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 Arduino 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.
Another common bug that people face is sometimes choosing the wrong board.
So to simplify today's lecture, we're just
going to use the Arduino Uno board and, also, this port number.
Now, it's not highlighted because Arduino's 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 to the specific port.
So you can see over here that it's on COM5.
And then, of course, you want to save it just
to make sure we save all our spaces.
Let's get back.
OK.
Now we've finished the setup, 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've 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 1, 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 a.k.a.
Electricity, is flowing through the circuit until it reaches
the lowest point, a.k.a.
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 going to soon see here with the servo components
that things are not-- it's going to 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 the electricity
flows because, as we saw from rule 1, 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.
OK.
So we all know that electricity conducts and it follows-- for instance,
if I stick a metal wire into the outlet, obviously
there's going to 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 nine of Arduino,
the electric signal from pin nine is going to flow from this wire.
Now, what do I mean by same rail?
Since we're dealing with breadboards, we're
going to 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 nine there, this whole rail
will contain the signal from pin nine because electricity,
as we've seen from here, flows from positive to negative to ground.
It's going to flow from here, and then it's
just going to 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 going to 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 simplified
and to work with kind of convention, we'll
just be sticking with blue as ground and red as voltage.
Rule number 4, don't exceed the voltage.
OK.
So the 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 2 volts.
If I was to stick it directly to this 9-volt battery,
this thing's going to heat up and blow up, and it's also going to 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, OK, 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.
And if you do the math, you can see why some components will blow up
or sometimes some components won't run at all.
OK.
So next we're going to start off with LEDs.
So as we've seen here, this is an LED.
It's basically just a fancy--
it's just essentially a light bulb where there's
is a positive terminal, over here, and a 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 going to have pinMode and digitalWrite.
So in Arduino, pinMode means setting the pin up for something.
By default, it sets it to input.
However, for LEDs, because we want to output a light,
we're going to have to set this LED to output.
And digitalWrite, this is varying the voltage that's flowing to the LED.
Since the Arduino's controlling how the electricity flows,
we want to vary from high to low.
OK.
So as we can see here, we have the circuit set up, this small circuit.
We have a resistor here.
This will protect the LED from blowing up because the Arduino provides
about 5 volts directly to the LED.
And if we didn't have this resistor, this could severely damage it.
It might not look like it in the beginning, but it's going to damage it.
So we want to make sure we protect against that from happening.
Then, also, we have this--
then we're going to flow from the positive terminal to the negative,
and then from the negative, it's going to flow to the ground, a.k.a.
the ground pin on the Arduino.
OK.
So let's set up these final connections.
I'll just leave this white wire here for now.
And I'm going to set this black wire because it
shares the same railing as the breadboard, as the ground for the LED.
I'm going to stick it into the ground pin over here.
OK.
Now we got the hardware kind of set up.
We need to figure out what to do next.
So let's actually go to the Arduino terminal and check things out.
So the first line of code is-- this is a typical common practice.
It's first declare your libraries.
But in this case, we 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 going to be using pin nine as the main circuit.
You could just use the number nine directly.
But for good software engineering practice
and for good documentation, because when you look back at your past Arduino
projects, you want to know 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 nine will be known as LED.
Now we're going to run the setup function.
Now we're going to set the pin mode to output.
So what this is saying, we're going to change the LED, a.k.a.
pin number nine, as an output.
So 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,
that means we're going to set it as an output.
Next is the loop function.
OK.
So first is we want to turn on the LED.
So we're going to do digitalWrite, which means,
OK, I'm going to write some command to the Arduino,
and it's going to tell that to turn the LED onto high.
So what high means is that--
because the way that Arduino works between high and low
is that it goes from 0 volts to 5 volts, and there's nothing really in between.
There's this thing called pulse with modulation,
but we will not be covering that today.
But essentially, by high means, OK, let this thing
receive all the voltage we can provide.
And low means, OK, 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 going to do digitalWrite, write
it to pin nine, and we're going to set it to high.
This delay command just makes sure that things aren't going too fast.
So we're just going to 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.
This means that delay 1,000 means delay for one second.
Next is we're going to turn off the LED.
So as before here, quite the opposite, first we wrote it to high.
Now we're setting that same pin, but we're setting it to low, a.k.a.
we're cutting off all voltage flowing through it
or we're making 0 volts flow to the LED.
And then we're going to delay again just to make sure that we
got a nice pattern of a blinking LED.
So what we're going to do is we're going to 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 IDE is that it
can catch mistakes before compiling.
And we'll see that once we get to the next circuit.
OK.
So right now we're going to--
so we have this circuit set up.
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 set pin nine to equal LED.
So we need to set this breadboard wire into the pin nine slot.
And as you can see here, this LED is blinking quite brightly,
quite smoothly, and it's blinking often, turning 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, a button is you just press
this-- it's essentially like this switch that you press to send out commands.
So think of a button as a light switch.
But instead of flicking it on and off, you
just push it and push it again to turn it on or off.
So the relevant commands for button is going to be digitalRead
and this debounce function.
So for digitalRead, 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 going to be telling, hey, we're receiving some signal
from the outside world.
This is what it's saying.
So digitalRead just interprets the commands from the outside world.
And then boolean debounce--
sort of finicky thing with buttons is that sometimes when you press them,
the signal is not so clear in the 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 going to have LED.
We're going to have pin nine set as LED.
And we're going to set pin two as the buttons pin.
In our void setup, we're going to set the LED as an output.
And notice here that, for the button this time,
we're setting it as an input.
Arduino, by default, just sets all pins as inputs.
But we just want to make sure, for clarification's sake, that we set this
as input.
Now, in the void loop function, let's read through what's happening.
So if digitalRead 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 connects these electrical wires
to continue the flow of electricity.
Now, if we're going to turn on-- imagine an LED is in between the button.
Imagine the button's in between the LED and the 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's not being pressed, we just want the LED to be off,
else if the button is pressed, then we set it to high.
OK.
So we're going to upload this code to the Arduino.
And then let's see what happens.
So we have right now, here's the breadboard.
We are going to connect this red wire to the white volt
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 going to 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, OK, what's
up with this resistor right here?
Now, I won't get into 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 turn 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 press 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 we push the button again.
So back to what I was saying about this debouncing thing.
Now, 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 microseconds,
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 we only read the state of what happens after we press that button,
after those five milliseconds, because in between that time,
things get a little bit finicky.
So in order to deal with this, we have this debounce function.
Now, the logic is a bit complex, but essentially it's
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 load because, in the beginning, let's just
assume that there's no voltage flowing, that the lights in the room
are completely off.
And then the LED on keeps track of whether or not
the LED's turned on or off.
So we're going to go through the 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 Arduino IDE works kind of follows the C, C++ syntax.
So to make sure that the void loop can actually use this debounce function,
we need to make sure it's elevated up or we do an early function call
early on in the code.
So here's the boolean debounce, which takes in a Boolean.
So what it does is first it reads the button's current state.
And if the last is not equal to the current state--
if the last state is not equal to the current state of the button,
we're going to delay for five microseconds
because, as we've seen, that weird fluctuating thing that was happening,
weird fluctuating electric signal.
And then we're going to set the current to whatever the button is reading.
So if it was pushed, it'll set to true.
If it was pushed, it'll set to high or true.
And if it's not pushed, it'll set to low or false.
And then we're going to return that output.
So let's go through the void loop code because this is
what's going to be running on forever.
So first, we're going to check the previous state,
and we're going to 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 a button, when the LED is off,
previous state and current state is going to be both low.
But once we run into this line, the debounce, 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 going to 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 turned off.
But if it's set to high or true, it's going to be turned on.
Then after that, we want to set the previous state
equal to the current state.
And here in digitalWrite, we want to make sure
that we write out that output to the LED.
And I forgot to mention that--
I did allude to it before, but I didn't state it explicitly.
In Arduino, true and high are about the same thing.
False and low are also the same thing.
So you notice that--
OK, 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 debounce is continuously reading,
and this debounce is called within this void loop.
And this 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 you 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 going to push the button.
I am 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-- and another thing about this project that I want
to make sure that you guys learn-- yes?
SPEAKER: How does it now go from on to off?
ROBERT JOMAR MALATE: So I could press the button again and it goes off.
SPEAKER: But how is it represented in code?
ROBERT JOMAR MALATE: So here in the code is--
OK, let's say that we just press the button.
Now, we let go of the button.
So it's going to run the debounce again.
We're going to pass in the previous state,
and the previous state right now is true.
So we just press the button.
Previous state is true.
So we're going to run it through here.
We're going to read the button.
If the current state--
the current state's going to 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 going to delay it and then we're going to continue reading,
then we're going to the current state to the digitalRead button,
and then we're going to 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 run the current state function.
So current state is low.
Previous state is high.
Now we're going to run this logic.
Now, this logic won't check out because, as I've said, previous state is high,
but current state is low.
So the LED won't switch its states.
So previous state is going to 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 is the code where it actually switches
whether the LED turns on or off.
But since it failed this condition, the previous state was high
and the current state was low, whether the LED turns on or off
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.
SPEAKER: How does it go off, then?
ROBERT JOMAR MALATE: So it goes off is when you press the button again.
So the question in the mic is, how did it go on?
And then the next question is, OK, how does it go off again?
Thank you for your question.
So over here, it goes through this loop again where that we push the button,
current state is now high, but previous state
is now low because we just change it right over here before.
And then we do this check again.
It passes.
The LED switches its logic from true to false.
We 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 want to be using for your Arduino,
and same thing for the hardware where you have the button here,
some voltage to the left side, and some output to the left side
with a resistor in between.
OK.
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.
Well, 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 going to have this analogRead.
We're going to be reading what the potentiometer's saying.
We're going to 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 then the 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 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 then we also 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 onto the code.
So we have this random stuff over here.
We have this voltage regulator.
But for now, we're just going to 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're used in 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, a.k.a.
minimum sound.
So we're going to plug that in right here.
We're going to be using this board.
So I'm going to connect this to the 5-volt rail
and this wire here to the ground.
And this entire rail here is going to have
that 5 volts and the negative volts.
Now, let's look at the code for this.
So the way potentiometers work is that 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, quote, unquote, "input signal."
OK.
Now let's get on with the potentiometer.
OK.
So we're going to set this now--
so we're going to set the potentiometers to pin zero.
And value, we'll just store whatever the value
that it's reading from the potentiometers.
And on the Arduino 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 turning this potentiometers knob around
or getting some sensor values.
And in this case, the input will just be from the potentiometers.
So we're going to stick that inside there--
serial.begin.
So what we're doing is we're setting up this serial or communication rate
command.
And essentially what this does is--
OK, so the signal that we're going to be sending and receiving
from the Arduino, the communication, is going to travel at about 9,600.
And this is just a common value.
You can set this to anything, but for convention's sake, it's 9,600.
Now, what we're going to do here is we're
going to first read the value from the pot-- so analogRead from pot.
Whatever value is coming from the zeroth pin,
we want to print that to our serial.
And we're going to leave it for 500 seconds or 500 microseconds
because we don't want to read too much commands too fast.
OK.
So before we get on to see what's happening,
I just want to show you guys what a Serial Monitor is.
OK.
So it doesn't look like it's popping up right now, which is all right.
We can fix that.
Oh, OK.
This is another educational moment, actually.
You see here that when I tried to open the serial thing,
the thing is there's no communications.
And you can see here this bug at the bottom-- board at COM5
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, a.k.a. it's the Arduino.
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 in some things.
But for now, 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, 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 1023.
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 position should I set my servo to.
We're going to 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, OK, what inputs are we getting?
So we're going to 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 Arduino 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, the way this thing will work
is it sends out a sound signal from one end
and measures how long it takes for that signal to come back in the other.
And here we have four pins.
This first one, 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 prepare to send a signal, and echo is to kind of measure
how long it takes to receive that signal back.
And the relevant command here is going to be pulseIn.
So we need to know what echo pin we're going to be working with.
And we're going to be setting it to high.
OK.
So let's disconnect these things from the board.
So I have this nice ultrasonic sensor over here.
I connected the 5 volts and the ground to the board.
And I also have these two trigger pins.
Now, we're going to check the code to see
which pins we are going to put this in.
Let's just make the font a bit bigger before continuing on.
OK.
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 trigger's at seven, I'm going to put this into seven.
And the echo pin is this tanish wire.
It's going to go into six.
OK.
Now, let's connect these stuff to the 5 volt 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 kind of keeps in memory the last thing that--
the code you uploaded to it.
OK.
So now let's walk through what's happening here.
So keep in mind that the ultrasonic sensor
uses sound waves to measure distance and the time it takes to travel.
So we're going to take the duration and the distance.
We're going to set these variables to be used later.
So the trigger pin is the output.
This is the one that's going to send the sound single 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 going to use a Serial Monitor so we
can see what distance we're getting.
So in the void loop, first we want to clear the trigger pin
to make sure that there's no previous stuff that was used in that sensor
before.
Now, we want to set the trigger pin on a high state for 10 microseconds.
So what this does is we're going to send a signal now from the trigger pin.
So we're going to send a signal from here.
We're going to send out a sound wave.
And we're going delay it, and then we're going to turn it right off.
Now, once that sound wave bounces off the object and it's coming back
and hits the other echo pin, what we want to do now is take--
OK, how long does it take for it to come back?
So we're going to do this pulseIn.
And pulseIn returns how long the sound wave travels in microseconds.
And because the speed of sound is fixed in most situations, about 348 meters
per second, we just have to divide that by how long it took for the object--
we just have to take that speed, multiply it by the time
it travels, and then we can get back the measurement of how far that object is.
Here we're going to be using centimeters, which
explains why the number is a bit low.
Then after that, we're going to print out this thing, distance.
This is simply going to literally print out distance inside the terminal.
Then we're going to print out--
OK, what was the actual value we got?
And then, to make sure we understand the units,
we're going to use centimeters in this case.
And to make sure that we are not going to be receiving too much commands,
I'll set this from five to 500, 500 microseconds or about half a second.
So every half a second, we get a different reading.
OK.
All looks well.
Let's upload this to the board.
And 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 thing actually is sending out sound waves
and it's measuring them.
So it will get lost 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 my hand is right now pressing on the ultrasonic sensor,
but notice that the reading is saying it's 3,000 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,
servos, my favorite components.
So servos are essentially these controllized motors that
can move around in the fixed place.
So here we have the servo library.
The relevant commands are we need to include a servo library.
We need to declare this object called myServo.
We're going to attach it, we're going to write to it the angle,
and we're going to read it.
OK.
Let's see this in action.
So let's separate this.
And we're going to pair this with the potentiometers
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 notice that,
hey, why is my Arduino not connecting to my computer?
It's because this servo is draining out a lot of power,
and the Arduino can only provide a fixed amount.
So what we're going to 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's going to be.
Things are going to get really messed up quite quickly.
However, we do want to make sure we share the same ground, though.
So we're going to 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 leftmost pin is going to be the input.
Now, output voltage is just going to be ground.
This is going to be the ground.
And on the rightmost pin is going to 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 that we're still going to get 5 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 this servo, before I forget to mention.
We can see here that servos have these tri-colorings.
Sometimes it comes in brown, red, and yellow.
But brown or black corresponds to ground, red corresponds to the input
voltage or 5 volts, whatever volts you want to provide,
and white or yellow corresponds to the signal.
So with those facts in mind, I'm going to connect this red
to the output of the 5-volt regulator.
I'm going to 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
that the Arduino is the master, the brain behind all of this.
So we want to connect this to pin nine.
OK.
Now, let's also connect this stuff from the potentiometers into the Arduino.
So we're going to connect this to the ground.
Connect this little wire to the 5 volts.
And we're going to connect this pin output to pin zero.
OK.
Now let's see what's happening here.
The code is not that much as the ultrasonic sensor
or the debounce one or the button.
But this is pretty powerful code, I'd say.
So we included the servo library up top here.
Next, we're sending out the pins.
So we're going to set servo--
we're going to set the output for the servo as pin nine
and for the potentiometers is pin zero.
We're going to use the pots value--
we want to measure the pots value because this
is going to be outputted somehow.
We're going to declare this servo.
We're just going to say it's my servo.
Now, this servo.h builds upon a ton of abstractions,
which you don't have to worry about.
So let's just work with this.
We're going to attach--
this line here's going to attach the servo to pin nine.
So this is saying that, OK, I want all the logic flowing from the board
to be outputted into this pin.
And we're also going to read from the terminal
just to see what's happening right now.
OK.
So first in the void loop, we're going to read
the value from the potentiometer.
So how far did we twist this thing?
Next we're going to 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 179.
But the potentiometers puts out values from zero to 1,023.
So in order to do this, we need to map it.
So this is saying, OK, we know that the pots value's
going to be between zero and 1,023.
So we want to map it to between zero through 179.
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 going to write to that servo.
So by write, it depends what servo you have.
In this case, we have a continuous rotation servo.
So .write sets how fast the servo is turning in one direction.
If you have a 90-degree servo, it changes the angle
of what the servo's positioned at.
So since we're working with a continuous rotation servo,
this is just going to tell, OK, how fast are we making the servo turn?
And we're going to leave it for 15 seconds.
OK.
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 of the components are located together.
And as you can see here, this thing is already starting to spin.
Just pausing it for now.
Let's just insert this so the viewers at home can see.
Just add this right here.
And let's open 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's 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 potentiometers 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.
SPEAKER: So the potentiometer already can process speed?
ROBERT JOMAR MALATE: Yes, here the potentiometers
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 through some common debugging issues
and how to solve them.
So first, the circuit's not wired properly.
The most common thing I've ran into is people forgetting to connect something
to the positive voltage or to 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 Arduino 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 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 if 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 COM.
And it should just display right there because it's a little bit too
complex to deal with yourself.
But essentially, COM is just whatever this thing is connected to.
And don't hesitate to use the Serial Monitor.
As we saw, we were 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 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 scratched the surface of what we could do.
The number one book I'd 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 makes you start to think, OK, 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 connected to some LED to change its colors.
Next is fritzing.
If you're going to work with Arduino, 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 components
so you viewers at home and people here can
view them and recreate them yourselves.
And for Arduino project ideas, you can check out my website down here
in the link.
There's going to be projects.
Currently there's nothing there yet.
But I will be uploading some projects for you guys
to trial, 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.