Placeholder Image

Subtitles section Play video

  • Hello, world.

  • This is CS 50 today.

  • We're not gonna be talking too much about software today.

  • We're gonna be working with hardware.

  • In fact, my particulary favorite type of hardware are doing those good afternoon.

  • My name is Robert Joe Marmalade.

  • This is CS 50 and today we're gonna be going pro with are doing no.

  • So you might be asking, what is our doing?

  • Oh, well, physically I have it here in my hand Or do we know is this microprocessor 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 are doing aboard, 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 ultrasonic sensor.

  • All of these is possible with our doing?

  • Oh, so you wouldn't be wondering.

  • Ok, what are we gonna cover today?

  • Well, Arduino is quite a large topic to cover.

  • So appearing aboard what is going green owes order gonna cover and red is not what we're gonna cover.

  • So first, what we're not gonna cover unfortunately are doing to involve some electrical engineering.

  • As you can see here, with these bread boards, you have to make sure things are wired properly.

  • We'll teach you the basic electrical engineering, but if you want to know more advanced than I would highly recommend checking out books are asking or taking out online for more.

  • Resource is next is working type of different types of are doing a boards most are doing awards are about the same.

  • But the thing is, for now, we'll just be working with the Arduino, you know, because this is pretty much the standard board that people use and finally, its aggression or external hardware.

  • It is possible, for instance, one time they 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, won't be integrating hardware with or do we know with any other thing just with the laptop to send in the new code that are doing a well run?

  • But what we are gonna cover is one setting up because we 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 are doing, oh, 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 serval which can be used for many, many things, such as creating movement, moving objects.

  • Here we have this potential ometer, and these components will essentially form the basis of what today's seminar is going to be about.

  • Now there's some extra components here.

  • Such asses.

  • This nine volt battery is voltage regulator.

  • But Indian, what really matters is these core components and what we're gonna learn because Indian we want to make sure that through working with the simple projects and going to the bug in common issues.

  • We want you to have the foundation toe work on cool or doing a project so you guys can start to dream about what cool projects you want.

  • Attack along next.

  • Okay, so first, this is gonna be the agenda for today, and we'll start.

  • And here is also the materials so are doing.

  • It does involve some materials you have to get because it is a hardware project and hardware is not as cheaper software, but it is highly rewarding.

  • And if you want to follow with today's seminar and topics, these art of materials you will need so strong here across the table, we have this or do we know?

  • You know, 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 cable to transfer the code from our laptop to the R.

  • Do we know?

  • Then we haven't led a button.

  • Several types of resistors, a potential ometer ultrasonic sensor and a serval first getting settled.

  • So are doing oh, can't be run on the CS 50 e because the CS 50 i d 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 off line.

  • However, if you are inclined to work on the Internet are doing Oh, 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 are doing a code.

  • Assuming that you get set up, let's start with the I D.

  • So here's a picture of the I d e.

  • But better let's actually go to the real I d.

  • So as you can see here, this is our Do we?

  • No, I d 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 set up?

  • So what void set up 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 cold that's going to rerun.

  • So, for instance, here is 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 are doing no is a micro controller, and the purpose of one of the huge advantages of our doing Oh, is that as long as it has power, it can continuously run the code until you press reset.

  • But once you press reset, it just runs a set up again.

  • And then you go back to the loop.

  • And what Luke does is it kind of runs like a wild, true or a forever for Luke.

  • And it just keeps running and running to cold.

  • Whatever is there.

  • So as long as they are doing oh, has battery or power running through it, it'll keep running that batch of code within the braces off the void loop.

  • Now, the next thing is, we also wanna 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 snowboard and also this port number.

  • Now it's not highlighted because are doing is not connected.

  • But allow me to connect this to the Peter and Port basically is in charge of making sure that all the commands get scented a specific port.

  • So you can see over here that it's on com five and And, of course you want to save it just to make sure you receive all their spaces back.

  • Okay, now we finish the set up and we kind of see howto are doing No, I d 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 hard work projects.

  • And these are the five rules I generally follow.

  • The 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 wired goes to the ground so you can imagine it as a waterfall analogy where we have our water source a top water or a K A.

  • Electricity is flowing through a circuit until he reaches the lowest point point, a k eight 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 floated ground.

  • So remember positive to negative to ground Second must float to the same ground, so we're going to see that soon.

  • See here with this serval components that things are not 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 seven are the same ground will be.

  • This are doing aboard.

  • The ground is basically we're 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, nor do we know 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 nine of our doing Oh, the electrics to go from pin nine is gonna flow from this wire now.

  • What do you mean by seem real?

  • Since we're dealing with bread boards, we're gonna need to make sure we understand how things work with bread boards.

  • So the bread words 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 the ground.

  • It's gonna 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 air continuous lines.

  • So this means that if I plug something into here, that electric signal is gonna flow through his 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 simpler, Fire simplified and dork with kind of convention was just be sticking with blue as ground and read as vice 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 unfortunate can only take about to volts.

  • If I was to stick it directly to this nine volt battery, this thing's going to 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 that 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 to 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 alms Law vehicles, I R.

  • So we're not gonna 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 some time why you can see why some components will blow up.

  • Or sometimes some components won't run at all.

  • Okay, so next we're going to start off with ladies.

  • So as we've seen here, this is an led.

  • It has is basically just a fancy.

  • Just look.

  • 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, eh, ladies And here are the commands on the screen that are most relevant to how and led will work.

  • So we're gonna have pin mode and digital, right?

  • So in our doing, open mode means setting the pin up for something by default.

  • It sets it to input.

  • However, for led is 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 you are doing is controlling how that electricity flows.

  • We want a very from high to low, Kate.

  • So, as you 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 they are doing oh, provides about five volts directly to the led.

  • And if we didn't have this resistor this good, this could severely damage it.

  • It might not look like it in the beginning, but is 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 float to the ground A k a.

  • The ground pin on.

  • Nor do we know.

  • Okay, so let's set of 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 bread board 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 or do we know terminal and check things out?

  • So first line of code is this is a typical common practices first to clear your libraries, but in this case, we're just simply want to turn on 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 stake here.

  • We're gonna 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 or do we know 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 appear is saying that Okay, Pin nine will be known as led.

  • Now we're gonna run this set of function.

  • Now we're gonna set the pin moto output.

  • So what?

  • This is saying we're gonna change led a k a pin pin number nine as an output.

  • So it's there's also input, and there's also output.

  • But since we're just want the electricity to flow or logic to flow from the board are doing aboard into the circuit.

  • 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 two of the are doing.

  • Oh, and it's gonna tell that to turn the led on too high.

  • So what high means is that because the weight are doing it works between high and low, is that it goes from 0 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.

  • What is density by hiding means.

  • Okay, let this thing receive all the voltage we can provide and low means.

  • Okay, that'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?

  • Write it to pin nine, and we're gonna set to set it too high this delay 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 that the lake a man works is that it uses microsecond.

  • So just do some of the math we'll get.

  • This means that delay 1000 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 a k We're cutting off all voltage flowing to it, or we're making zero volts flow toothy 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?

  • Appear in the corner, the one with the arrow pointing to the right click upload useful thing about the or do we know is that I d.

  • 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 the 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 said we set pin nine to equal led.

  • So we need you set this bread board wire into the pin nine 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 led ease 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 just you prep, you just press.

  • This is essentially like this switch that you pressed 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 command for Button is going to be digital read and this the bounce function.

  • So for Digital read, 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 bullying 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, right?

  • 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 nine set us led, and we're gonna set up into as the button spin and avoid set up.

  • 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 are doing Oh, by the fault just sets opens by as inputs.

  • But we just want to make sure to for clarification, set clarifications sake that we set this as now in a void Luke function.

  • Let's read to what's happening.

  • So if the jewelry 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 an led is in between the button.

  • Imagine the buttons in between the led and the voltage source.

  • If the button is not being pressed, then there's no voltage or current flowing too 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 buttons not being pressed we just want led to be off else If the button is his breast, then we said it too high.

  • Okay, so we're gonna upload this code to Dar, Do we know?

  • And then let's see what happens.

  • So we have right now.

  • Here, here's the red board.

  • We are going to connect this red wire to divide bolts because this time the button also needs to receive some power.

  • And then we also need to send the signal from the button.

  • So they are doing Oh, so we're gonna connect it right between that resistor and this button right here.

  • And as you can see when they 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 pullup 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 the 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 whatever we press the button once it turns on, and when we let it go, it's still on, and only time we turn it off.

  • Is that when you pushed the button again so back to what I was saying about this.

  • The bouncing thing.

  • Now you can, 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 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 bounce 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 TV.

  • 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 too low.

  • Because in the beginning, let's just assume that there's no that there's no voltage flowing, that the lights in the room are completely off and then led on keeps tracts of whether or not the ladies turn on or off.

  • So we're gonna go through the set up again.

  • We're going to set the led as the output and the button as the input.

  • And before we go to the void Luke function down here we have this function inserted in between now the way we're doing No, I d works is kind of follows the C C plus plus in tax.

  • So to make sure that the void Luke can actually use this bounce function, we need to make sure it's elevated up or we hit that or we do off early function call earlier on in the code.

  • So here's the bullying, the bounce that it takes on a bullion.

  • So what it does this first it reads the buttons current state and if the last is not equal to the current state of the last eight 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 going to set the current tow whatever the button is reading.

  • So if it was pushed, it'll set the truth.

  • If it was pushed, it'll set too high or true.

  • And if it's not, push will set too low or false, and then we're gonna return that out.

  • So let's go through the void, Luke code, because this is what's gonna be running on forever.

  • So first we're gonna check the previous state, and we're gonna send that to the current state.

  • No.

  • 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, off 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 pressed the button current state now is going to be true or high, but previous state will still be low, so it previously it 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 is going to turn off.

  • But if it set too high or true is 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 also that we write out that output total led.

  • And I forgot to mention that I did allude to before, but he didn't stay, did explicitly in our do we know true and high are about the same thing.

  • False and lo are also the same thing.

  • So you noticed that, OK, you might be thinking to yourself what happens if the led is what happens if you 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 his voice loop is constantly running the functions within here so we can see that it's constantly running it, so it's always gonna 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 gonna blow 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 dysfunction, 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 run to the bounce again.

  • We're gonna 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 gonna run it through here.

  • We're gonna read the button if the current state, the current state's 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 status high and the current status 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 status low previous state is high.

  • Now we're gonna run this logic now.

  • This logic won't check out because I've I've said previous status high, but current state is low, so the led won't switch.

  • It 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 was switched to truth.

  • And here's the cold.

  • Where it actually switch is 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 ladies, whether the led turns on honor off it's 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 mic 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 then it goes through that it goes to reduce this loop again.

  • Where that we've pushed the button.

  • Current state is now high, but 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 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, you you want to be using for your are doing Oh, 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 resister in between.

  • Okay, so that covers buttons.

  • For now, let's move on to potential meters or also known as pots.

  • So potential bombers might seem like this fancy word or something a little bit obscure, but potentially ometer is actually using 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 resister set to another to another.

  • But instead you have this knob that takes care of it for you.

  • The relevant commands for a potential murder are appear on the board.

  • So we're gonna have this analog read.

  • We're going to be reading what?

  • The potential for 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 out 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 serval once we get to that section.

  • And another thing, also we want to start working with is the are doing no serial monitor.

  • This is essentially a way to see what's happening and is a useful debugging technique also, and we also were we want to put out the output because print the bugging is useful in software, but it's also very useful and hardware to see not only what's happening well 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 potential murder right here.

  • So it has this knob here that you could turn back and forth and potential bombers.

  • As I've said before, they've they're using a lot of things.

  • For instance, in a speaker, when you turn the knob when he turned up to the maximum, you're turning the resistance very low because sound is essentially just a bunch of electric signals.

  • And when you float 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 minimum sound.

  • So we're gonna input.

  • We're gonna plug that in right here.

  • I'm gonna be using this board.

  • So I'm gonna connect this to the five volt rail And this This why we're here to the ground.

  • And this entire rail here is going to have that five volts, five volts and the ground and the negative bolts.

  • Now, let's look at the code for this.

  • So the way potential mothers work is that there are three.

  • 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 potential commoners, they're generally the quote unquote input signal.

  • Okay, Now, let's get on with the potential number.

  • Okay, so we're gonna set this now, so we're gonna set the potential murder to pin zero, and value will distort whatever the value that it's reading from the potential ometer.

  • And under doing aboard, there's also this thing called analog in.

  • These are the analog inputs that you're gonna be providing.

  • So these are pretty much the signals that you'll send for the outside world.

  • Such as that is turning this turning this potential murder nav around or getting some sensor values.

  • And in this case, the input will just be for the from the potential murder.

  • So we're gonna stick that inside there.

  • Cereal, duck, begin.

  • So what we're doing is we're setting up this cereal or communication rate command.

  • And essentially, what this does is okay, so the signal that we're gonna be sending and receiving from, nor do we know the communication is gonna travel at about 9600 9600.

  • And this is just a common value you can set.

  • It said this anything.

  • But for conventions sake, it's 9600.

  • 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 values coming from the zero spin, we want to print that door cereal, and we're gonna live for over 500 seconds or 500 microseconds because we don't want to too much commands too fast.

  • Okay, So before we get on to see what's happening, I just want to show you guys what a serial monitor is.

  • Okay, so does it look like it's popping up right now?

  • Which is all right, We can fix that.

  • Okay.

  • 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, but this bug of the bottom boarded Con five is not available.

  • So what this means is that in order for the serial command toe open in the first place, there needs to be something communicating with the computer.

  • Okay, it's nor 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 want, I want to show you guys, is that this will be used for reading inputs from the potential murder.

  • So we're gonna close this.

  • We're going to upload the code to from this code into the are doing.

  • And then let's go to Cyril Monitor.

  • As you can see here, numbers are starting to pop up.

  • So this is 10 23.

  • This is the maximum value of this.

  • Your sister.

  • 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 10 K resistor.

  • But with that map function that he 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 said it too?

  • Or what position should I set my servo, too?

  • 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 the bug because now you're seeing Okay, what inputs are we getting?

  • We're gonna close that.

  • And that is it for potential motors now.

  • Ultrasonic sensors.

  • So the reason why I'm covering ultrasonic sensors is thes tend to be quite common in a lot of our doing.

  • Oh, 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 the object is traveling, you want the way this thing will work is it sends out a sound signal from one end, and and it measures how long it takes for that signal to come back in the other.

  • And here we have four pins.

  • This 1st 1 V C.

  • C is for the input voltage.

  • This other pin at the end is for the ground.

  • And the last two prints in between is for the trigger and for the echo.

  • So trigger is to send it to prepared to send a signal.

  • And echo is too.

  • They do 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 open we're gonna be working with, and we're gonna be setting it too high.

  • Okay, so let's disconnect these things from the board.

  • So we have this nice ultrasonic sensor over here.

  • I connected the five, the five volts and the ground to the board, and I also have these to trigger pins.

  • Now we're gonna take the kowtow, see which pins we're gonna put this in.

  • Just make the fun a bit bigger for 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 gonna put this into seven.

  • And Echo Pin is this 10 ish wire is gonna go into six.

  • Okay, Now, let's connect these stuff to the five Wilton Ground, respectively, just to make sure we get some current flowing.

  • But the thing is, no logics is being loaded because actually, the are doing.

  • Oh, takes its 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 ultrasonic sensor uses sound waves to measure distance and the time it takes to travel.

  • So we're gonna take a duration and the distance we're gonna set these variables to be used later.

  • So the trigger pin is output.

  • This is the one that's gonna send the sound single out the echo pin is going to be the input which is gonna measure how long it takes for that single 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 in the voice 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 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 gonna send a signal from here.

  • We're gonna send out a sound wave, and we're gonna be late, 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 other echo open, 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 Poulsen returns.

  • How long the sound wave travels in microseconds.

  • And because the speed of sound is fixed in most situations about 340 eight 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.

  • But they got speed multiplied by the time the tap 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 numbers a bit is a bit low.

  • Then after that, we're gonna print out distinct 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, Looks well, let's upload this to the board and look, 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 they 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 a, my hand is right now pressing on the on the ultrasonic sensor.

  • But notice that the reading and saying it's 3007 meters away or 2000 so you want to make sure that you're not too close to it.

  • That's close.

  • That for now.

  • Awesome.

  • So from ultrasonic sensors, let's move on to the final part.

  • Server was my favorite components, So servos are essentially these motors that control eyes motor that can move around from in the fixed place.

  • So here we have the serval library.

  • Irrelevant commands are we need to include a serval library.

  • We need to declare this object called my serval.

  • We're gonna attach it.

  • We're gonna right toe with the angle, and we're going to read it.

  • Okay, let's see this in action.

  • So let's separate this.

  • And we're gonna pair this with a potential murder to illustrate what's happening right here.

  • And I'll just leave this right there, just for visual sakes.

  • So in the circuit here, I included a voltage regulator.

  • Now, depending on your service, sometimes your servo can take a higher load.

  • But the thing is, you don't want to connect a serval directly to our doing.

  • Oh, because it'll pull out a lot of power and you'll and you'll notice that Hey, why is my are doing No, not connecting to know mine computer.

  • It's because this several is draining out a lot of power and are doing it can only provide a fixed amount.

  • So 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 bread board, this is all going to be from the battery.

  • Do not ever put the same voltage right here.

  • Where you are doing 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.

  • Oh, so we're gonna flow from the battery to here and into the are doing aboard.

  • Now, notice this black component right here.

  • This is a voltage regulator.

  • So what?

  • This Finn here on the left, on the right.

  • Most pin is going left.

  • Most friends going to be the input output.

  • Voltage is just gonna be ground and over are the up.

  • This is gonna be the ground and on the right, most pin is gonna be the output.

  • So the good thing about a voltage regulator is that would make sure that we still have this constant bolted flowing.

  • So whether or not distinct drops from a nine to a seven automatically, we know that we're still gonna get five volts.

  • So let's see this in action.

  • Make the front bigger and let's wire this up quickly.

  • So because we want to use only five volts.

  • I'm gonna connect this the way to sort this serval before I forget to mention we can see here that servos have these try colorings.

  • Sometimes it come comes in brown, red and yellow.

  • Yeah, but brown or black corresponds to ground red corresponds to the input voltage or five volts.

  • Whatever bolts you want provide and white or yellow corresponds to the signal.

  • So with those facts in mind, I'm gonna connect this red to the output off the five volt regulator.

  • I'm gonna connect this black wire to the ground, 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 are doing.

  • No, because remember that the order we know is the master the brain behind all of this.

  • So we want to connect this to pin nine, Kate.

  • Now, let's also connect this stuff from the potential ometer into the Arduino.

  • So we're gonna connect this to the ground, connect this little wire 2 to 5 volts, and we're gonna collect this pin up with 20 Okay, let's see what's happening here.

  • The code is not that much of the ultrasonic sensor or the bounce one with the button.

  • But this is pretty powerful code, I'd say so included the several library up top here.

  • Next, we're setting out the pins, so we're gonna set serval.

  • We're gonna set the output for the servos pin nine.

  • And for the potential, Amber's been zero.

  • We're gonna use the pots value.

  • We want to measure the pots value and because this is gonna be output it somehow we're gonna declare this serval.

  • We're just going to say it's my serval.

  • Now this several doubt h builds upon a ton of abstractions which you don't have to really worry about.

  • So let's just work with this.

  • We're gonna touch this line here, is gonna touch the servant to pin nine.

  • So this is saying that, OK, I want all the logic flowing from the board to go to this Toby out, put it into this pin, and we're also gonna read from the terminal just to see what happened.

  • What's happening right now?

  • Okay, so first in the void, we're gonna read the value for from the potential murder.

  • 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 think values from 0 to 179.

  • But the potential murder 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 parts values gonna be between zero and 1023.

  • So we want to make it map it to between 0 to 179.

  • So this map function essentially takes here off all the logic you have to do and just outputs the value want.

  • After that, you're gonna right through that serval.

  • So by right, it depends what serval you have.

  • In this case, we have not.

  • We have a continuous rotation servo, so dot right sets how fast serval is turning in one direction.

  • If you have a 90 degree serval, it changes the angle of what the service position at.

  • So since we're working with a continuous rotation serval, this is just going to tell.

  • Okay.

  • How fast are you making the serval turn and we're gonna be late for 15 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 posited for now that 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 service spinning right now, unfo

Hello, world.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it