1 00:00:00,000 --> 00:00:01,244 2 00:00:01,244 --> 00:00:02,660 ROBERT JOMAR MALATE: Hello, world. 3 00:00:02,660 --> 00:00:04,360 This is CS50. 4 00:00:04,360 --> 00:00:07,540 Today we're not going to be talking too much about software. 5 00:00:07,540 --> 00:00:10,280 Today we're going to be working with hardware, 6 00:00:10,280 --> 00:00:14,630 in fact, my particular favorite type of hardware, Arduinos. 7 00:00:14,630 --> 00:00:15,480 Good afternoon. 8 00:00:15,480 --> 00:00:17,360 My name is Robert Jomar Malate. 9 00:00:17,360 --> 00:00:21,140 This is CS50, and today we're going to be going pro with Arduino. 10 00:00:21,140 --> 00:00:23,800 11 00:00:23,800 --> 00:00:26,980 So you might be asking, what is Arduino? 12 00:00:26,980 --> 00:00:29,200 Well, physically I have it here in my hand. 13 00:00:29,200 --> 00:00:33,850 Arduino is this microcontroller where, essentially, you 14 00:00:33,850 --> 00:00:38,620 can put a bunch of electronic components together and control them together. 15 00:00:38,620 --> 00:00:42,280 It's open source, so the hardware and the software is available for anyone 16 00:00:42,280 --> 00:00:43,000 to use. 17 00:00:43,000 --> 00:00:45,280 That's why there's a lot of knockoffs available. 18 00:00:45,280 --> 00:00:49,120 But these knockoffs have the same exact quality as this Arduino board. 19 00:00:49,120 --> 00:00:52,480 So it doesn't really matter which one you get. 20 00:00:52,480 --> 00:00:55,270 But in the end, the greatest thing about Arduino 21 00:00:55,270 --> 00:00:58,190 is that it's great for making electronics projects. 22 00:00:58,190 --> 00:01:01,750 So for instance here, someone is making some remote-control circuit 23 00:01:01,750 --> 00:01:04,900 or another person made this remote-control robot with a bunch 24 00:01:04,900 --> 00:01:08,120 of servos and an ultrasonic sensor. 25 00:01:08,120 --> 00:01:11,850 All of these is possible with Arduino. 26 00:01:11,850 --> 00:01:15,640 So you might be wondering, OK, what are we going to cover today? 27 00:01:15,640 --> 00:01:18,990 Well, Arduino is quite a large topic to cover. 28 00:01:18,990 --> 00:01:22,200 So up here on the board, what is in green is what we're going to cover, 29 00:01:22,200 --> 00:01:24,370 and red is not what we're going to cover. 30 00:01:24,370 --> 00:01:26,820 So first, what we're not going to cover-- 31 00:01:26,820 --> 00:01:29,610 unfortunately, Arduino involves some electrical engineering, 32 00:01:29,610 --> 00:01:32,400 as you can see here with these breadboards. 33 00:01:32,400 --> 00:01:35,280 You need to make sure that things are wired properly. 34 00:01:35,280 --> 00:01:37,440 We'll teach you the basic electrical engineering. 35 00:01:37,440 --> 00:01:39,314 But if you want to know more advanced, then I 36 00:01:39,314 --> 00:01:43,050 would highly recommend checking out books or checking out 37 00:01:43,050 --> 00:01:45,320 online for more resources. 38 00:01:45,320 --> 00:01:48,750 Next is working with different types of Arduino boards. 39 00:01:48,750 --> 00:01:51,460 Most Arduino boards are about the same. 40 00:01:51,460 --> 00:01:54,510 But the thing is, for now we'll just be working with the Arduino Uno 41 00:01:54,510 --> 00:01:59,200 because this is pretty much the standard board that people use. 42 00:01:59,200 --> 00:02:02,500 And finally, integration or external hardware-- it is possible. 43 00:02:02,500 --> 00:02:04,770 For instance, one time I made a robot arm 44 00:02:04,770 --> 00:02:08,280 that could be controlled through Python with voice commands. 45 00:02:08,280 --> 00:02:11,580 But the thing is, that involves a little bit more technicality, 46 00:02:11,580 --> 00:02:15,010 and this is kind of outside the scope of what we want to cover today. 47 00:02:15,010 --> 00:02:17,680 So unfortunately, we won't be integrating 48 00:02:17,680 --> 00:02:22,680 Arduino with any other thing, just with the laptop to send in the new code 49 00:02:22,680 --> 00:02:25,430 that Arduino will run. 50 00:02:25,430 --> 00:02:27,590 But what we are going to cover is, one, setting up 51 00:02:27,590 --> 00:02:30,140 because we've got to start somewhere and, of course, 52 00:02:30,140 --> 00:02:31,860 we've got to start with setting up. 53 00:02:31,860 --> 00:02:35,420 And the next thing is we also need to work with basic Arduino components. 54 00:02:35,420 --> 00:02:36,080 So 55 00:02:36,080 --> 00:02:39,380 Strewn here across the table is various components I have. 56 00:02:39,380 --> 00:02:41,700 Here is an LED circuit. 57 00:02:41,700 --> 00:02:44,920 We also have a button. 58 00:02:44,920 --> 00:02:49,760 Here we have this ultrasonic sensor, which is used for measuring distances. 59 00:02:49,760 --> 00:02:53,160 We have this servo, which can be used for many, many things, 60 00:02:53,160 --> 00:02:56,780 such as creating movement, moving objects. 61 00:02:56,780 --> 00:03:00,980 Here we have this potentiometer, and these components 62 00:03:00,980 --> 00:03:04,631 will essentially form the basis of what today's seminar is going to be about. 63 00:03:04,631 --> 00:03:06,380 Now, there are some extra components here, 64 00:03:06,380 --> 00:03:09,830 such as this 9-volt battery, this voltage regulator. 65 00:03:09,830 --> 00:03:13,477 But in the end, what really matters is these core components 66 00:03:13,477 --> 00:03:15,560 and what we're going to learn because, in the end, 67 00:03:15,560 --> 00:03:18,860 we want to make sure that, through working with the simple projects 68 00:03:18,860 --> 00:03:22,850 and going through debugging common issues, 69 00:03:22,850 --> 00:03:26,390 we want you to have the foundation to work on cool Arduino projects 70 00:03:26,390 --> 00:03:29,150 so you guys can start to dream about what cool projects 71 00:03:29,150 --> 00:03:30,280 you want to tackle on next. 72 00:03:30,280 --> 00:03:33,480 73 00:03:33,480 --> 00:03:36,290 So first, this is going to be the agenda for today. 74 00:03:36,290 --> 00:03:39,760 And we'll start-- and here is also the materials. 75 00:03:39,760 --> 00:03:41,770 So Arduino does involve some materials you 76 00:03:41,770 --> 00:03:44,410 have to get because it is a hardware project, 77 00:03:44,410 --> 00:03:47,110 and hardware is not as cheap as software. 78 00:03:47,110 --> 00:03:48,700 But it is highly rewarding. 79 00:03:48,700 --> 00:03:51,680 And if you want to follow with today's seminar and topics, 80 00:03:51,680 --> 00:03:54,170 these are the materials you will need. 81 00:03:54,170 --> 00:03:57,680 So strewn here across the table we have this Arduino Uno. 82 00:03:57,680 --> 00:03:58,750 We have a lot of wires. 83 00:03:58,750 --> 00:04:00,500 And as you can see with these breadboards, 84 00:04:00,500 --> 00:04:03,140 there is a lot of wiring involved. 85 00:04:03,140 --> 00:04:06,440 We have several breadboards, but you just need one. 86 00:04:06,440 --> 00:04:09,530 And then we have this cable to transfer the code from our laptop 87 00:04:09,530 --> 00:04:11,180 to the Arduino. 88 00:04:11,180 --> 00:04:17,380 Then we have an LED, a button, several types of resistors, 89 00:04:17,380 --> 00:04:21,079 a potentiometer, ultrasonic sensor, and a servo. 90 00:04:21,079 --> 00:04:25,010 91 00:04:25,010 --> 00:04:27,210 First, getting set up-- 92 00:04:27,210 --> 00:04:33,710 so Arduino can't be run on the CS50 IDE because the CS50 IDE doesn't have it. 93 00:04:33,710 --> 00:04:37,610 However, I would highly recommend downloading and installing the Arduino 94 00:04:37,610 --> 00:04:39,872 into your computer because this is the best 95 00:04:39,872 --> 00:04:41,330 way you can work with the hardware. 96 00:04:41,330 --> 00:04:43,262 And also, you can work offline. 97 00:04:43,262 --> 00:04:45,470 However, if you are inclined to work on the internet, 98 00:04:45,470 --> 00:04:49,040 Arduino does have a web-based editor, which is a cloud version. 99 00:04:49,040 --> 00:04:52,260 You can follow the link there posted onto the screen, 100 00:04:52,260 --> 00:04:56,630 and you can see the cloud software they have available to work with Arduino 101 00:04:56,630 --> 00:04:59,260 code. 102 00:04:59,260 --> 00:05:03,010 Assuming that you get set up, let's start with the IDE. 103 00:05:03,010 --> 00:05:04,660 So here's a picture of the IDE. 104 00:05:04,660 --> 00:05:08,730 But better, let's actually go to the real IDE. 105 00:05:08,730 --> 00:05:11,850 So as you can see here, this is Arduino IDE. 106 00:05:11,850 --> 00:05:13,826 We're going to put all our code here. 107 00:05:13,826 --> 00:05:15,450 It's going to run between these things. 108 00:05:15,450 --> 00:05:18,280 And we're going to have several things in here. 109 00:05:18,280 --> 00:05:21,880 But the important thing is we want to focus on several key things. 110 00:05:21,880 --> 00:05:25,070 First is this void setup. 111 00:05:25,070 --> 00:05:29,540 So what void setup means is that when the board is running for the first time 112 00:05:29,540 --> 00:05:32,560 or whenever you press the Reset button on the board, 113 00:05:32,560 --> 00:05:35,320 this is the batch of code that's going to rerun. 114 00:05:35,320 --> 00:05:41,410 So for instance, here it's going to set this pin to an output pin every time 115 00:05:41,410 --> 00:05:44,080 this has been set up. 116 00:05:44,080 --> 00:05:46,840 The next major part of the code is loop. 117 00:05:46,840 --> 00:05:52,390 So Arduino is a microcontroller, and one of the huge advantages of Arduino 118 00:05:52,390 --> 00:05:55,240 is that, as long as it has power, it can continuously 119 00:05:55,240 --> 00:05:58,452 run the code until you press Reset. 120 00:05:58,452 --> 00:06:00,910 But the once you press Reset, it just runs the setup again, 121 00:06:00,910 --> 00:06:02,440 and then you go back to the loop. 122 00:06:02,440 --> 00:06:06,790 And what loop does is it kind of runs like a while true or a forever 123 00:06:06,790 --> 00:06:07,770 for loop. 124 00:06:07,770 --> 00:06:10,520 And it just keeps running and running the code, whatever is there. 125 00:06:10,520 --> 00:06:14,720 So as long as the Arduino has battery or power running through it, 126 00:06:14,720 --> 00:06:20,410 it'll keep running that batch of code within the braces of the void loop. 127 00:06:20,410 --> 00:06:23,520 Now, the next thing is we also want to be familiar with some 128 00:06:23,520 --> 00:06:25,170 of the things we have to set up. 129 00:06:25,170 --> 00:06:28,890 So over here, if you check out the Tools tab, 130 00:06:28,890 --> 00:06:32,420 you can check out the different types of boards available. 131 00:06:32,420 --> 00:06:37,790 Another common bug that people face is sometimes choosing the wrong board. 132 00:06:37,790 --> 00:06:39,740 So to simplify today's lecture, we're just 133 00:06:39,740 --> 00:06:44,430 going to use the Arduino Uno board and, also, this port number. 134 00:06:44,430 --> 00:06:47,540 Now, it's not highlighted because Arduino's not connected, but allow 135 00:06:47,540 --> 00:06:51,160 me to connect this to the computer. 136 00:06:51,160 --> 00:06:56,290 And port basically is in charge of making sure that all the commands get 137 00:06:56,290 --> 00:06:58,010 sent to the specific port. 138 00:06:58,010 --> 00:07:00,220 So you can see over here that it's on COM5. 139 00:07:00,220 --> 00:07:04,596 140 00:07:04,596 --> 00:07:06,470 And then, of course, you want to save it just 141 00:07:06,470 --> 00:07:07,970 to make sure we save all our spaces. 142 00:07:07,970 --> 00:07:10,946 143 00:07:10,946 --> 00:07:14,280 Let's get back. 144 00:07:14,280 --> 00:07:15,040 OK. 145 00:07:15,040 --> 00:07:17,200 Now we've finished the setup, and we kind of 146 00:07:17,200 --> 00:07:19,780 see how the Arduino IDE looks like. 147 00:07:19,780 --> 00:07:22,360 Now let's get on with basic electrical engineering. 148 00:07:22,360 --> 00:07:26,671 Now, this is a huge simplification of the whole electrical engineering field. 149 00:07:26,671 --> 00:07:29,170 But this is what I've found helpful that helps me understand 150 00:07:29,170 --> 00:07:32,810 and at least get my foot off the ground working with hardware projects. 151 00:07:32,810 --> 00:07:35,230 And these are the five rules I generally follow. 152 00:07:35,230 --> 00:07:41,420 So rule 1, electricity flows from positive to negative to ground. 153 00:07:41,420 --> 00:07:43,640 So we can see here with this simple circuit 154 00:07:43,640 --> 00:07:49,810 that electricity flows from the positive, flows from the positive wire, 155 00:07:49,810 --> 00:07:53,710 down the negative wire, and from the negative wire goes to the ground. 156 00:07:53,710 --> 00:07:57,070 So you can imagine it as a waterfall analogy 157 00:07:57,070 --> 00:08:02,000 where we have our water source at top, water, or a.k.a. 158 00:08:02,000 --> 00:08:05,390 Electricity, is flowing through the circuit until it reaches 159 00:08:05,390 --> 00:08:07,220 the lowest point, a.k.a. 160 00:08:07,220 --> 00:08:08,510 ground. 161 00:08:08,510 --> 00:08:12,860 And all components in this circuit have to obey this law. 162 00:08:12,860 --> 00:08:17,600 So make sure that you always make sure that your components have 163 00:08:17,600 --> 00:08:22,020 this positive connection, are connected to this negative terminal, 164 00:08:22,020 --> 00:08:23,100 and flow to ground. 165 00:08:23,100 --> 00:08:27,650 So remember, positive to negative to ground. 166 00:08:27,650 --> 00:08:31,330 Second, must flow to the same ground-- 167 00:08:31,330 --> 00:08:34,260 so we're going to soon see here with the servo components 168 00:08:34,260 --> 00:08:37,289 that things are not-- it's going to get a little bit messier. 169 00:08:37,289 --> 00:08:40,260 But the thing is, we all want to make sure we share the same ground. 170 00:08:40,260 --> 00:08:44,500 And for the sake of today's seminar, the same ground will be this Arduino board. 171 00:08:44,500 --> 00:08:46,710 So ground is basically where just the electricity 172 00:08:46,710 --> 00:08:49,410 flows because, as we saw from rule 1, it flows 173 00:08:49,410 --> 00:08:51,360 from positive to negative to ground. 174 00:08:51,360 --> 00:08:54,960 All electricity has to flow to the same ground or the same point. 175 00:08:54,960 --> 00:08:59,720 And as I've said before, the Arduino is going to be that ground. 176 00:08:59,720 --> 00:09:00,770 Now, the next one-- 177 00:09:00,770 --> 00:09:04,370 same wire, same rail, same flow. 178 00:09:04,370 --> 00:09:04,870 OK. 179 00:09:04,870 --> 00:09:09,560 So we all know that electricity conducts and it follows-- for instance, 180 00:09:09,560 --> 00:09:12,930 if I stick a metal wire into the outlet, obviously 181 00:09:12,930 --> 00:09:15,220 there's going to be electric flow flowing there. 182 00:09:15,220 --> 00:09:19,210 And from these wires, what I mean by same wire, same rail, 183 00:09:19,210 --> 00:09:22,690 same flow is that the same, quote, unquote, "electric signal" 184 00:09:22,690 --> 00:09:26,080 is going to be flowing between these wires. 185 00:09:26,080 --> 00:09:29,260 So if this comes from pin nine of Arduino, 186 00:09:29,260 --> 00:09:33,670 the electric signal from pin nine is going to flow from this wire. 187 00:09:33,670 --> 00:09:36,370 Now, what do I mean by same rail? 188 00:09:36,370 --> 00:09:38,640 Since we're dealing with breadboards, we're 189 00:09:38,640 --> 00:09:42,460 going to need to make sure we understand how things work with breadboards. 190 00:09:42,460 --> 00:09:46,480 So the breadboards on this table follow this schematic, and imagine 191 00:09:46,480 --> 00:09:50,830 each of these green lines as one rail. 192 00:09:50,830 --> 00:09:55,330 So this entire column means that if I stick, let's say, 193 00:09:55,330 --> 00:09:58,390 a signal from pin nine there, this whole rail 194 00:09:58,390 --> 00:10:02,860 will contain the signal from pin nine because electricity, 195 00:10:02,860 --> 00:10:06,139 as we've seen from here, flows from positive to negative to ground. 196 00:10:06,139 --> 00:10:07,930 It's going to flow from here, and then it's 197 00:10:07,930 --> 00:10:10,510 just going to sit there until it continues flowing. 198 00:10:10,510 --> 00:10:13,040 199 00:10:13,040 --> 00:10:16,720 Now, also on top, it's kind of the exception. 200 00:10:16,720 --> 00:10:19,840 Here is just these are continuous lines. 201 00:10:19,840 --> 00:10:22,870 So this means that if I plug something into here, 202 00:10:22,870 --> 00:10:25,810 that electric signal is going to flow through this entire rail 203 00:10:25,810 --> 00:10:29,660 here and same for the red one down there. 204 00:10:29,660 --> 00:10:32,210 Now, for today's convention, blue will just 205 00:10:32,210 --> 00:10:36,230 mean ground and red will mean the positive voltage. 206 00:10:36,230 --> 00:10:38,060 You can do it whatever way you want. 207 00:10:38,060 --> 00:10:39,500 There's no rules against it. 208 00:10:39,500 --> 00:10:42,170 It's just that, to make things simplified 209 00:10:42,170 --> 00:10:44,060 and to work with kind of convention, we'll 210 00:10:44,060 --> 00:10:47,190 just be sticking with blue as ground and red as voltage. 211 00:10:47,190 --> 00:10:50,290 212 00:10:50,290 --> 00:10:52,731 Rule number 4, don't exceed the voltage. 213 00:10:52,731 --> 00:10:53,230 OK. 214 00:10:53,230 --> 00:10:56,920 So the thing is, we can break electronic components pretty easily 215 00:10:56,920 --> 00:10:58,760 just by sticking these things. 216 00:10:58,760 --> 00:11:02,472 So for instance, we have this LED right over here. 217 00:11:02,472 --> 00:11:04,180 This small, little beauty, unfortunately, 218 00:11:04,180 --> 00:11:07,380 can only take about 2 volts. 219 00:11:07,380 --> 00:11:10,620 If I was to stick it directly to this 9-volt battery, 220 00:11:10,620 --> 00:11:14,970 this thing's going to heat up and blow up, and it's also going to die, 221 00:11:14,970 --> 00:11:18,270 and I might get some burns, which we don't want any of those things 222 00:11:18,270 --> 00:11:20,580 to happen because these are precious components 223 00:11:20,580 --> 00:11:22,940 and we don't want to hurt your fingers. 224 00:11:22,940 --> 00:11:24,920 So to make sure with that, we want to make sure 225 00:11:24,920 --> 00:11:29,660 that the voltage is controlled, and we do this either through resistors 226 00:11:29,660 --> 00:11:32,060 or separating the type of voltage that flows. 227 00:11:32,060 --> 00:11:35,250 228 00:11:35,250 --> 00:11:40,230 So for this, you might be wondering, OK, why is this the case? 229 00:11:40,230 --> 00:11:44,540 Well, final rule for today is Ohm's law, V equals IR. 230 00:11:44,540 --> 00:11:47,210 So we're not going to be doing any calculations. 231 00:11:47,210 --> 00:11:51,200 All of these have been done already for you with the calculations. 232 00:11:51,200 --> 00:11:53,090 But essentially, it relates voltage. 233 00:11:53,090 --> 00:11:58,370 It means that voltage equals the resistance times the current. 234 00:11:58,370 --> 00:12:01,240 235 00:12:01,240 --> 00:12:06,870 And if you do the math, you can see why some components will blow up 236 00:12:06,870 --> 00:12:09,000 or sometimes some components won't run at all. 237 00:12:09,000 --> 00:12:11,958 238 00:12:11,958 --> 00:12:13,440 OK. 239 00:12:13,440 --> 00:12:16,680 So next we're going to start off with LEDs. 240 00:12:16,680 --> 00:12:20,550 So as we've seen here, this is an LED. 241 00:12:20,550 --> 00:12:22,600 It's basically just a fancy-- 242 00:12:22,600 --> 00:12:25,380 it's just essentially a light bulb where there's 243 00:12:25,380 --> 00:12:29,250 is a positive terminal, over here, and a negative terminal. 244 00:12:29,250 --> 00:12:33,240 And then here are some various colors LEDs, and here are 245 00:12:33,240 --> 00:12:37,890 the commands on the screen that are most relevant to how an LED will work. 246 00:12:37,890 --> 00:12:40,590 So we're going to have pinMode and digitalWrite. 247 00:12:40,590 --> 00:12:44,550 So in Arduino, pinMode means setting the pin up for something. 248 00:12:44,550 --> 00:12:46,920 By default, it sets it to input. 249 00:12:46,920 --> 00:12:51,400 However, for LEDs, because we want to output a light, 250 00:12:51,400 --> 00:12:55,450 we're going to have to set this LED to output. 251 00:12:55,450 --> 00:13:00,250 And digitalWrite, this is varying the voltage that's flowing to the LED. 252 00:13:00,250 --> 00:13:03,990 Since the Arduino's controlling how the electricity flows, 253 00:13:03,990 --> 00:13:06,992 we want to vary from high to low. 254 00:13:06,992 --> 00:13:08,770 OK. 255 00:13:08,770 --> 00:13:14,670 So as we can see here, we have the circuit set up, this small circuit. 256 00:13:14,670 --> 00:13:16,740 We have a resistor here. 257 00:13:16,740 --> 00:13:20,070 This will protect the LED from blowing up because the Arduino provides 258 00:13:20,070 --> 00:13:23,250 about 5 volts directly to the LED. 259 00:13:23,250 --> 00:13:28,350 And if we didn't have this resistor, this could severely damage it. 260 00:13:28,350 --> 00:13:31,420 It might not look like it in the beginning, but it's going to damage it. 261 00:13:31,420 --> 00:13:35,810 So we want to make sure we protect against that from happening. 262 00:13:35,810 --> 00:13:37,400 Then, also, we have this-- 263 00:13:37,400 --> 00:13:41,490 then we're going to flow from the positive terminal to the negative, 264 00:13:41,490 --> 00:13:44,490 and then from the negative, it's going to flow to the ground, a.k.a. 265 00:13:44,490 --> 00:13:47,180 the ground pin on the Arduino. 266 00:13:47,180 --> 00:13:47,680 OK. 267 00:13:47,680 --> 00:13:51,080 So let's set up these final connections. 268 00:13:51,080 --> 00:13:54,032 I'll just leave this white wire here for now. 269 00:13:54,032 --> 00:13:55,990 And I'm going to set this black wire because it 270 00:13:55,990 --> 00:14:00,940 shares the same railing as the breadboard, as the ground for the LED. 271 00:14:00,940 --> 00:14:03,220 I'm going to stick it into the ground pin over here. 272 00:14:03,220 --> 00:14:06,538 273 00:14:06,538 --> 00:14:07,960 OK. 274 00:14:07,960 --> 00:14:09,760 Now we got the hardware kind of set up. 275 00:14:09,760 --> 00:14:13,770 We need to figure out what to do next. 276 00:14:13,770 --> 00:14:19,172 So let's actually go to the Arduino terminal and check things out. 277 00:14:19,172 --> 00:14:21,880 So the first line of code is-- this is a typical common practice. 278 00:14:21,880 --> 00:14:23,490 It's first declare your libraries. 279 00:14:23,490 --> 00:14:27,030 But in this case, we just simply want to turn an LED on and off, 280 00:14:27,030 --> 00:14:28,800 make it blink a little bit. 281 00:14:28,800 --> 00:14:30,480 So we don't need any libraries. 282 00:14:30,480 --> 00:14:33,660 But the first thing we want to do is give the pin a name. 283 00:14:33,660 --> 00:14:35,670 Now, you could just use-- 284 00:14:35,670 --> 00:14:42,840 now for the sake here, we're going to be using pin nine as the main circuit. 285 00:14:42,840 --> 00:14:45,030 You could just use the number nine directly. 286 00:14:45,030 --> 00:14:47,700 But for good software engineering practice 287 00:14:47,700 --> 00:14:51,660 and for good documentation, because when you look back at your past Arduino 288 00:14:51,660 --> 00:14:55,350 projects, you want to know what your code is saying 289 00:14:55,350 --> 00:14:57,280 or what it's going to do. 290 00:14:57,280 --> 00:15:00,780 So what we do is we declare here-- this line up here 291 00:15:00,780 --> 00:15:07,040 is saying that, OK, pin nine will be known as LED. 292 00:15:07,040 --> 00:15:08,790 Now we're going to run the setup function. 293 00:15:08,790 --> 00:15:11,220 Now we're going to set the pin mode to output. 294 00:15:11,220 --> 00:15:15,090 So what this is saying, we're going to change the LED, a.k.a. 295 00:15:15,090 --> 00:15:19,020 pin number nine, as an output. 296 00:15:19,020 --> 00:15:21,990 So there's also input and there's also output. 297 00:15:21,990 --> 00:15:25,440 But since we just want the electricity to flow or the logic 298 00:15:25,440 --> 00:15:29,344 to flow from the board, Arduino board, into the circuit, 299 00:15:29,344 --> 00:15:31,260 that means we're going to set it as an output. 300 00:15:31,260 --> 00:15:34,470 301 00:15:34,470 --> 00:15:37,730 Next is the loop function. 302 00:15:37,730 --> 00:15:38,800 OK. 303 00:15:38,800 --> 00:15:40,930 So first is we want to turn on the LED. 304 00:15:40,930 --> 00:15:43,770 305 00:15:43,770 --> 00:15:46,440 So we're going to do digitalWrite, which means, 306 00:15:46,440 --> 00:15:52,600 OK, I'm going to write some command to the Arduino, 307 00:15:52,600 --> 00:15:55,600 and it's going to tell that to turn the LED onto high. 308 00:15:55,600 --> 00:15:57,580 So what high means is that-- 309 00:15:57,580 --> 00:16:00,100 because the way that Arduino works between high and low 310 00:16:00,100 --> 00:16:05,150 is that it goes from 0 volts to 5 volts, and there's nothing really in between. 311 00:16:05,150 --> 00:16:07,150 There's this thing called pulse with modulation, 312 00:16:07,150 --> 00:16:09,290 but we will not be covering that today. 313 00:16:09,290 --> 00:16:11,710 But essentially, by high means, OK, let this thing 314 00:16:11,710 --> 00:16:14,360 receive all the voltage we can provide. 315 00:16:14,360 --> 00:16:19,520 And low means, OK, let's cut off all the voltage to that particular pin number. 316 00:16:19,520 --> 00:16:20,470 So we're here first. 317 00:16:20,470 --> 00:16:24,730 In order to turn on the LED, we're going to do digitalWrite, write 318 00:16:24,730 --> 00:16:27,070 it to pin nine, and we're going to set it to high. 319 00:16:27,070 --> 00:16:30,520 320 00:16:30,520 --> 00:16:34,080 This delay command just makes sure that things aren't going too fast. 321 00:16:34,080 --> 00:16:38,280 So we're just going to set it to delay it for one second. 322 00:16:38,280 --> 00:16:41,580 Here, the way the delay command works is that it uses microseconds. 323 00:16:41,580 --> 00:16:44,820 So just do some of the math. 324 00:16:44,820 --> 00:16:50,730 This means that delay 1,000 means delay for one second. 325 00:16:50,730 --> 00:16:54,010 Next is we're going to turn off the LED. 326 00:16:54,010 --> 00:16:58,380 So as before here, quite the opposite, first we wrote it to high. 327 00:16:58,380 --> 00:17:01,980 Now we're setting that same pin, but we're setting it to low, a.k.a. 328 00:17:01,980 --> 00:17:04,349 we're cutting off all voltage flowing through it 329 00:17:04,349 --> 00:17:07,785 or we're making 0 volts flow to the LED. 330 00:17:07,785 --> 00:17:11,720 331 00:17:11,720 --> 00:17:14,450 And then we're going to delay again just to make sure that we 332 00:17:14,450 --> 00:17:17,030 got a nice pattern of a blinking LED. 333 00:17:17,030 --> 00:17:21,310 334 00:17:21,310 --> 00:17:24,367 So what we're going to do is we're going to upload this. 335 00:17:24,367 --> 00:17:26,950 You can press this button right up here in the corner, the one 336 00:17:26,950 --> 00:17:29,140 with the arrow pointing to the right. 337 00:17:29,140 --> 00:17:30,970 Click Upload. 338 00:17:30,970 --> 00:17:34,510 Useful thing about the Arduino IDE is that it 339 00:17:34,510 --> 00:17:36,635 can catch mistakes before compiling. 340 00:17:36,635 --> 00:17:38,760 And we'll see that once we get to the next circuit. 341 00:17:38,760 --> 00:17:41,260 342 00:17:41,260 --> 00:17:41,760 OK. 343 00:17:41,760 --> 00:17:45,820 So right now we're going to-- 344 00:17:45,820 --> 00:17:47,350 so we have this circuit set up. 345 00:17:47,350 --> 00:17:50,320 But the thing that's missing is there's no electric flow flowing 346 00:17:50,320 --> 00:17:53,420 from here to the LED. 347 00:17:53,420 --> 00:17:58,490 So in order to do this, remember that we set pin nine to equal LED. 348 00:17:58,490 --> 00:18:03,770 So we need to set this breadboard wire into the pin nine slot. 349 00:18:03,770 --> 00:18:07,550 And as you can see here, this LED is blinking quite brightly, 350 00:18:07,550 --> 00:18:12,530 quite smoothly, and it's blinking often, turning on and off every one second. 351 00:18:12,530 --> 00:18:22,960 352 00:18:22,960 --> 00:18:25,120 So that's it for the LEDs. 353 00:18:25,120 --> 00:18:27,250 Up next is buttons. 354 00:18:27,250 --> 00:18:30,280 Now, I would argue that this segment is going 355 00:18:30,280 --> 00:18:32,620 to be a little bit more difficult because 356 00:18:32,620 --> 00:18:35,200 of the logic involved with buttons. 357 00:18:35,200 --> 00:18:38,680 But essentially, a button is you just press 358 00:18:38,680 --> 00:18:42,190 this-- it's essentially like this switch that you press to send out commands. 359 00:18:42,190 --> 00:18:44,260 So think of a button as a light switch. 360 00:18:44,260 --> 00:18:46,510 But instead of flicking it on and off, you 361 00:18:46,510 --> 00:18:51,660 just push it and push it again to turn it on or off. 362 00:18:51,660 --> 00:18:55,070 So the relevant commands for button is going to be digitalRead 363 00:18:55,070 --> 00:18:57,750 and this debounce function. 364 00:18:57,750 --> 00:19:00,634 So for digitalRead, we want to make sure that we're 365 00:19:00,634 --> 00:19:03,300 reading the state of the button, because this time the button is 366 00:19:03,300 --> 00:19:05,970 going to be the input to the board. 367 00:19:05,970 --> 00:19:09,090 This is going to be telling, hey, we're receiving some signal 368 00:19:09,090 --> 00:19:11,640 from the outside world. 369 00:19:11,640 --> 00:19:14,080 This is what it's saying. 370 00:19:14,080 --> 00:19:19,260 So digitalRead just interprets the commands from the outside world. 371 00:19:19,260 --> 00:19:21,300 And then boolean debounce-- 372 00:19:21,300 --> 00:19:26,720 sort of finicky thing with buttons is that sometimes when you press them, 373 00:19:26,720 --> 00:19:30,630 the signal is not so clear in the immediately. 374 00:19:30,630 --> 00:19:33,060 Now, before we get into that more in depth, 375 00:19:33,060 --> 00:19:37,180 let's just get started with making this light bulb work with a nice button, 376 00:19:37,180 --> 00:19:39,180 because it's nice that it's blinking on and off, 377 00:19:39,180 --> 00:19:41,250 but maybe you want to give it some control 378 00:19:41,250 --> 00:19:44,410 that, hey, if we push the button, it should be on, 379 00:19:44,410 --> 00:19:47,310 and if we let go of the button, it should be off. 380 00:19:47,310 --> 00:19:51,150 So let's get to coding write that. 381 00:19:51,150 --> 00:19:56,850 So we can see up here, we're going to be setting these. 382 00:19:56,850 --> 00:19:58,670 So we're going to have LED. 383 00:19:58,670 --> 00:20:01,190 We're going to have pin nine set as LED. 384 00:20:01,190 --> 00:20:03,770 And we're going to set pin two as the buttons pin. 385 00:20:03,770 --> 00:20:06,500 386 00:20:06,500 --> 00:20:11,570 In our void setup, we're going to set the LED as an output. 387 00:20:11,570 --> 00:20:13,840 And notice here that, for the button this time, 388 00:20:13,840 --> 00:20:15,810 we're setting it as an input. 389 00:20:15,810 --> 00:20:20,440 Arduino, by default, just sets all pins as inputs. 390 00:20:20,440 --> 00:20:25,990 But we just want to make sure, for clarification's sake, that we set this 391 00:20:25,990 --> 00:20:26,980 as input. 392 00:20:26,980 --> 00:20:30,610 393 00:20:30,610 --> 00:20:34,330 Now, in the void loop function, let's read through what's happening. 394 00:20:34,330 --> 00:20:38,530 So if digitalRead button equals low-- 395 00:20:38,530 --> 00:20:43,000 so what this means is that if I press the button right here and-- 396 00:20:43,000 --> 00:20:45,640 397 00:20:45,640 --> 00:20:48,010 so if we press the button, it gives the signal high 398 00:20:48,010 --> 00:20:51,670 because, remember, as I said, a button is kind of like a switch. 399 00:20:51,670 --> 00:20:55,690 Think of a button as a switch that connects these electrical wires 400 00:20:55,690 --> 00:20:58,990 to continue the flow of electricity. 401 00:20:58,990 --> 00:21:04,350 Now, if we're going to turn on-- imagine an LED is in between the button. 402 00:21:04,350 --> 00:21:08,050 Imagine the button's in between the LED and the voltage source. 403 00:21:08,050 --> 00:21:12,210 If the button is not being pressed, then there's no voltage or current 404 00:21:12,210 --> 00:21:16,350 flowing to the LED. 405 00:21:16,350 --> 00:21:20,160 Now, if we push the button, it completes the circuit. 406 00:21:20,160 --> 00:21:25,260 So current is allowed to flow between the voltage source to the LED. 407 00:21:25,260 --> 00:21:28,050 408 00:21:28,050 --> 00:21:33,880 So if the button's not being pressed, we just want the LED to be off, 409 00:21:33,880 --> 00:21:40,450 else if the button is pressed, then we set it to high. 410 00:21:40,450 --> 00:21:40,950 OK. 411 00:21:40,950 --> 00:21:43,033 So we're going to upload this code to the Arduino. 412 00:21:43,033 --> 00:21:55,650 413 00:21:55,650 --> 00:21:58,150 And then let's see what happens. 414 00:21:58,150 --> 00:22:02,840 So we have right now, here's the breadboard. 415 00:22:02,840 --> 00:22:06,500 We are going to connect this red wire to the white volt 416 00:22:06,500 --> 00:22:09,690 because, this time, the button also needs to receive some power. 417 00:22:09,690 --> 00:22:12,490 418 00:22:12,490 --> 00:22:17,020 And then we also need to send the signal from the button to the Arduino. 419 00:22:17,020 --> 00:22:21,370 So we're going to connect it right between that resistor and this button 420 00:22:21,370 --> 00:22:22,080 right here. 421 00:22:22,080 --> 00:22:27,920 422 00:22:27,920 --> 00:22:31,330 And as you can see, when I press the button, it turns on. 423 00:22:31,330 --> 00:22:35,327 When I let go of the button, it turns off. 424 00:22:35,327 --> 00:22:37,910 Now, you might be wondering and asking to yourself, OK, what's 425 00:22:37,910 --> 00:22:40,500 up with this resistor right here? 426 00:22:40,500 --> 00:22:43,720 Now, I won't get into too much detail about this, 427 00:22:43,720 --> 00:22:46,260 but these things are called pull-up resistors. 428 00:22:46,260 --> 00:22:49,230 And the reason why we include this resistor in between 429 00:22:49,230 --> 00:22:53,070 is to prevent some weird readings from happening 430 00:22:53,070 --> 00:22:55,800 because if we were to remove that resistor, 431 00:22:55,800 --> 00:22:59,580 the LED would turn on and off because of mixed electrical signals coming 432 00:22:59,580 --> 00:23:00,870 from around us. 433 00:23:00,870 --> 00:23:03,090 But the thing is, we want this to be controlled. 434 00:23:03,090 --> 00:23:07,470 We want the button to turn on only when the button is being pressed 435 00:23:07,470 --> 00:23:09,530 and turn off when we let go of the button. 436 00:23:09,530 --> 00:23:16,122 437 00:23:16,122 --> 00:23:17,830 So that's a fun way to work with buttons. 438 00:23:17,830 --> 00:23:20,800 But when was the last time you've ever had to hold down 439 00:23:20,800 --> 00:23:23,030 a button to keep the lights on? 440 00:23:23,030 --> 00:23:26,030 It's not a fun thing to do when, especially, you got other things to do, 441 00:23:26,030 --> 00:23:27,400 such as your homework. 442 00:23:27,400 --> 00:23:33,370 So we want to make sure that whenever we press the button once, it turns on, 443 00:23:33,370 --> 00:23:36,730 and when we let it go, it's still on, and the only time we turn it off 444 00:23:36,730 --> 00:23:39,920 is that when we push the button again. 445 00:23:39,920 --> 00:23:42,950 So back to what I was saying about this debouncing thing. 446 00:23:42,950 --> 00:23:45,980 Now, as you can see here, this is some mixed electrical signal 447 00:23:45,980 --> 00:23:47,840 that's coming from a button. 448 00:23:47,840 --> 00:23:52,550 If I were to press the button for a few microseconds, 449 00:23:52,550 --> 00:23:55,160 the electric signal is going to be fluctuating. 450 00:23:55,160 --> 00:24:00,320 So we want to make sure we prevent that from happening or we want to make sure 451 00:24:00,320 --> 00:24:04,400 that we only read the state of what happens after we press that button, 452 00:24:04,400 --> 00:24:07,220 after those five milliseconds, because in between that time, 453 00:24:07,220 --> 00:24:09,990 things get a little bit finicky. 454 00:24:09,990 --> 00:24:13,580 So in order to deal with this, we have this debounce function. 455 00:24:13,580 --> 00:24:17,240 Now, the logic is a bit complex, but essentially it's 456 00:24:17,240 --> 00:24:22,170 we want to read the state of the button, wait for five milliseconds, 457 00:24:22,170 --> 00:24:24,000 read the state again. 458 00:24:24,000 --> 00:24:27,660 If it's on, then we turn on the LED. 459 00:24:27,660 --> 00:24:31,510 If it's off, we turn off the LED. 460 00:24:31,510 --> 00:24:33,240 Now, let's see this code in action. 461 00:24:33,240 --> 00:24:37,150 462 00:24:37,150 --> 00:24:39,100 So over here we have the same thing. 463 00:24:39,100 --> 00:24:42,550 We're setting up our button and LED at-- 464 00:24:42,550 --> 00:24:45,270 preparing it for the pin numbers. 465 00:24:45,270 --> 00:24:47,530 And now we also have these additional things. 466 00:24:47,530 --> 00:24:51,910 We have previous state, current state, and LED on. 467 00:24:51,910 --> 00:24:55,400 So previous state stores the last state of the button. 468 00:24:55,400 --> 00:24:58,670 Current state will store the current state of the button and right under set 469 00:24:58,670 --> 00:25:00,545 to load because, in the beginning, let's just 470 00:25:00,545 --> 00:25:04,310 assume that there's no voltage flowing, that the lights in the room 471 00:25:04,310 --> 00:25:06,580 are completely off. 472 00:25:06,580 --> 00:25:09,250 And then the LED on keeps track of whether or not 473 00:25:09,250 --> 00:25:10,750 the LED's turned on or off. 474 00:25:10,750 --> 00:25:12,845 475 00:25:12,845 --> 00:25:14,720 So we're going to go through the setup again. 476 00:25:14,720 --> 00:25:21,210 We're going to set the LED as the output and the button as the input. 477 00:25:21,210 --> 00:25:23,940 And before we go to the void loop function down here, 478 00:25:23,940 --> 00:25:28,010 we have this function inserted in between. 479 00:25:28,010 --> 00:25:34,110 Now, the way Arduino IDE works kind of follows the C, C++ syntax. 480 00:25:34,110 --> 00:25:37,730 So to make sure that the void loop can actually use this debounce function, 481 00:25:37,730 --> 00:25:43,580 we need to make sure it's elevated up or we do an early function call 482 00:25:43,580 --> 00:25:47,020 early on in the code. 483 00:25:47,020 --> 00:25:49,890 So here's the boolean debounce, which takes in a Boolean. 484 00:25:49,890 --> 00:25:54,470 So what it does is first it reads the button's current state. 485 00:25:54,470 --> 00:25:58,190 And if the last is not equal to the current state-- 486 00:25:58,190 --> 00:26:01,010 if the last state is not equal to the current state of the button, 487 00:26:01,010 --> 00:26:02,810 we're going to delay for five microseconds 488 00:26:02,810 --> 00:26:06,420 because, as we've seen, that weird fluctuating thing that was happening, 489 00:26:06,420 --> 00:26:09,000 weird fluctuating electric signal. 490 00:26:09,000 --> 00:26:12,370 And then we're going to set the current to whatever the button is reading. 491 00:26:12,370 --> 00:26:16,260 So if it was pushed, it'll set to true. 492 00:26:16,260 --> 00:26:19,680 If it was pushed, it'll set to high or true. 493 00:26:19,680 --> 00:26:22,499 And if it's not pushed, it'll set to low or false. 494 00:26:22,499 --> 00:26:24,290 And then we're going to return that output. 495 00:26:24,290 --> 00:26:26,850 496 00:26:26,850 --> 00:26:29,450 So let's go through the void loop code because this is 497 00:26:29,450 --> 00:26:32,000 what's going to be running on forever. 498 00:26:32,000 --> 00:26:34,810 So first, we're going to check the previous state, 499 00:26:34,810 --> 00:26:37,710 and we're going to set that to the current state. 500 00:26:37,710 --> 00:26:43,550 Now, if the previous state equals low and the current state equals high, 501 00:26:43,550 --> 00:26:48,480 then what we want to do is do the opposite of whatever LED is on. 502 00:26:48,480 --> 00:26:51,820 So for instance, in the beginning of the circuit, 503 00:26:51,820 --> 00:26:55,080 when we haven't pushed a button, when the LED is off, 504 00:26:55,080 --> 00:26:59,490 previous state and current state is going to be both low. 505 00:26:59,490 --> 00:27:03,270 But once we run into this line, the debounce, when I press the button, 506 00:27:03,270 --> 00:27:06,720 current state now is going to be true or high. 507 00:27:06,720 --> 00:27:10,210 But previous state will still be low. 508 00:27:10,210 --> 00:27:13,320 So if previous state means low and current state means high, 509 00:27:13,320 --> 00:27:16,030 that means we push the button. 510 00:27:16,030 --> 00:27:21,640 So that means we're going to do the opposite of whatever the LED has 511 00:27:21,640 --> 00:27:23,090 been set to before. 512 00:27:23,090 --> 00:27:28,540 So if it was set to false or low, it's going to be turned off. 513 00:27:28,540 --> 00:27:32,817 But if it's set to high or true, it's going to be turned on. 514 00:27:32,817 --> 00:27:34,900 Then after that, we want to set the previous state 515 00:27:34,900 --> 00:27:36,024 equal to the current state. 516 00:27:36,024 --> 00:27:40,350 517 00:27:40,350 --> 00:27:42,320 And here in digitalWrite, we want to make sure 518 00:27:42,320 --> 00:27:47,930 that we write out that output to the LED. 519 00:27:47,930 --> 00:27:49,640 And I forgot to mention that-- 520 00:27:49,640 --> 00:27:53,120 I did allude to it before, but I didn't state it explicitly. 521 00:27:53,120 --> 00:27:57,170 In Arduino, true and high are about the same thing. 522 00:27:57,170 --> 00:28:00,240 False and low are also the same thing. 523 00:28:00,240 --> 00:28:03,990 524 00:28:03,990 --> 00:28:05,430 So you notice that-- 525 00:28:05,430 --> 00:28:09,030 OK, you might be thinking to yourself, what happens if the LED is-- 526 00:28:09,030 --> 00:28:11,280 what happens if we don't press the button? 527 00:28:11,280 --> 00:28:15,840 Now, remember that this debounce is continuously reading, 528 00:28:15,840 --> 00:28:18,860 and this debounce is called within this void loop. 529 00:28:18,860 --> 00:28:23,040 And this void loop is constantly running the functions within here. 530 00:28:23,040 --> 00:28:25,610 So we can see that it's constantly running it. 531 00:28:25,610 --> 00:28:28,040 So it's always going to change the state automatically. 532 00:28:28,040 --> 00:28:31,190 So that way you see that when you press the button, 533 00:28:31,190 --> 00:28:35,600 we won't have to hold it down just to keep the LED on. 534 00:28:35,600 --> 00:28:38,862 Now, to make sure that this works, let's actually upload this project. 535 00:28:38,862 --> 00:28:40,320 So we're going to upload this code. 536 00:28:40,320 --> 00:28:53,050 537 00:28:53,050 --> 00:28:54,510 Now let's see what happens. 538 00:28:54,510 --> 00:28:58,060 I'm going to push the button. 539 00:28:58,060 --> 00:29:02,300 I am not holding the button, but the LED is currently right on. 540 00:29:02,300 --> 00:29:03,980 And that's amazing. 541 00:29:03,980 --> 00:29:04,620 Think about it. 542 00:29:04,620 --> 00:29:08,180 We just went-- despite the little bit complexity of this function, 543 00:29:08,180 --> 00:29:12,849 we were able to create, essentially, what works as a light bulb. 544 00:29:12,849 --> 00:29:14,140 It might not be amazing to you. 545 00:29:14,140 --> 00:29:17,980 But building upon this type of logic for the button, 546 00:29:17,980 --> 00:29:19,840 you can build multiple things. 547 00:29:19,840 --> 00:29:22,780 For instance, if you were to work with a speaker 548 00:29:22,780 --> 00:29:26,200 and you were to connect some LED to it and if I were to press a button, 549 00:29:26,200 --> 00:29:29,767 you could create a mini piano. 550 00:29:29,767 --> 00:29:32,850 So what you could do is-- and another thing about this project that I want 551 00:29:32,850 --> 00:29:34,886 to make sure that you guys learn-- yes? 552 00:29:34,886 --> 00:29:38,522 SPEAKER: How does it now go from on to off? 553 00:29:38,522 --> 00:29:41,480 ROBERT JOMAR MALATE: So I could press the button again and it goes off. 554 00:29:41,480 --> 00:29:43,720 SPEAKER: But how is it represented in code? 555 00:29:43,720 --> 00:29:45,620 ROBERT JOMAR MALATE: So here in the code is-- 556 00:29:45,620 --> 00:29:47,620 OK, let's say that we just press the button. 557 00:29:47,620 --> 00:29:49,840 Now, we let go of the button. 558 00:29:49,840 --> 00:29:51,870 So it's going to run the debounce again. 559 00:29:51,870 --> 00:29:53,620 We're going to pass in the previous state, 560 00:29:53,620 --> 00:29:56,034 and the previous state right now is true. 561 00:29:56,034 --> 00:29:57,200 So we just press the button. 562 00:29:57,200 --> 00:29:58,867 Previous state is true. 563 00:29:58,867 --> 00:30:00,450 So we're going to run it through here. 564 00:30:00,450 --> 00:30:02,010 We're going to read the button. 565 00:30:02,010 --> 00:30:03,222 If the current state-- 566 00:30:03,222 --> 00:30:05,430 the current state's going to be low because we're not 567 00:30:05,430 --> 00:30:07,450 pushing the button anymore. 568 00:30:07,450 --> 00:30:09,900 So since the last state is not equal to the current state, 569 00:30:09,900 --> 00:30:14,300 because the last state is high and the current state is low, 570 00:30:14,300 --> 00:30:17,750 we're going to delay it and then we're going to continue reading, 571 00:30:17,750 --> 00:30:21,649 then we're going to the current state to the digitalRead button, 572 00:30:21,649 --> 00:30:23,690 and then we're going to return the current state. 573 00:30:23,690 --> 00:30:27,420 So what happens after this is that the previous state is high 574 00:30:27,420 --> 00:30:29,950 and the current state is low right now. 575 00:30:29,950 --> 00:30:31,380 So we go to this void loop. 576 00:30:31,380 --> 00:30:34,750 If current state-- we run the current state function. 577 00:30:34,750 --> 00:30:36,060 So current state is low. 578 00:30:36,060 --> 00:30:38,130 Previous state is high. 579 00:30:38,130 --> 00:30:40,000 Now we're going to run this logic. 580 00:30:40,000 --> 00:30:43,430 Now, this logic won't check out because, as I've said, previous state is high, 581 00:30:43,430 --> 00:30:45,780 but current state is low. 582 00:30:45,780 --> 00:30:49,850 So the LED won't switch its states. 583 00:30:49,850 --> 00:30:53,030 So previous state is going to still equal the current state, 584 00:30:53,030 --> 00:30:55,340 but the key here is this LED on, because remember 585 00:30:55,340 --> 00:30:58,430 that this LED on is keeping track whether or not 586 00:30:58,430 --> 00:31:00,600 we have turned the LED on or off. 587 00:31:00,600 --> 00:31:06,700 So in this case, when we press the button, LED on will switch to true. 588 00:31:06,700 --> 00:31:10,010 And here is the code where it actually switches 589 00:31:10,010 --> 00:31:12,100 whether the LED turns on or off. 590 00:31:12,100 --> 00:31:15,430 But since it failed this condition, the previous state was high 591 00:31:15,430 --> 00:31:23,360 and the current state was low, whether the LED turns on or off 592 00:31:23,360 --> 00:31:24,780 still hasn't changed. 593 00:31:24,780 --> 00:31:26,460 It's still on. 594 00:31:26,460 --> 00:31:30,305 So that's why that even though we were to write this digital function, 595 00:31:30,305 --> 00:31:34,165 LED on is still true or still high. 596 00:31:34,165 --> 00:31:35,910 SPEAKER: How does it go off, then? 597 00:31:35,910 --> 00:31:39,090 ROBERT JOMAR MALATE: So it goes off is when you press the button again. 598 00:31:39,090 --> 00:31:42,321 So the question in the mic is, how did it go on? 599 00:31:42,321 --> 00:31:44,820 And then the next question is, OK, how does it go off again? 600 00:31:44,820 --> 00:31:46,140 Thank you for your question. 601 00:31:46,140 --> 00:31:52,350 So over here, it goes through this loop again where that we push the button, 602 00:31:52,350 --> 00:31:54,540 current state is now high, but previous state 603 00:31:54,540 --> 00:31:59,807 is now low because we just change it right over here before. 604 00:31:59,807 --> 00:32:01,140 And then we do this check again. 605 00:32:01,140 --> 00:32:01,640 It passes. 606 00:32:01,640 --> 00:32:05,670 The LED switches its logic from true to false. 607 00:32:05,670 --> 00:32:10,580 We set the previous state to the current state, and then we write it out. 608 00:32:10,580 --> 00:32:13,820 So feel free to use this as a boilerplate template 609 00:32:13,820 --> 00:32:18,170 because this is a common thing you want to be using for your Arduino, 610 00:32:18,170 --> 00:32:21,230 and same thing for the hardware where you have the button here, 611 00:32:21,230 --> 00:32:25,160 some voltage to the left side, and some output to the left side 612 00:32:25,160 --> 00:32:26,450 with a resistor in between. 613 00:32:26,450 --> 00:32:30,450 614 00:32:30,450 --> 00:32:30,950 OK. 615 00:32:30,950 --> 00:32:32,760 So that covers buttons for now. 616 00:32:32,760 --> 00:32:36,200 Let's move on to potentiometers or also known as pots. 617 00:32:36,200 --> 00:32:39,920 So potentiometers might seem like this fancy word or something 618 00:32:39,920 --> 00:32:44,160 a little bit obscure, but potentiometers are actually used in a lot of places. 619 00:32:44,160 --> 00:32:47,300 So what they are, essentially, is variable resistors. 620 00:32:47,300 --> 00:32:52,390 They change their resistance that's flowing through a circuit. 621 00:32:52,390 --> 00:32:54,390 So imagine if you just have a bunch of resistors 622 00:32:54,390 --> 00:32:57,180 and you're continuously moving the wire from one resistor set 623 00:32:57,180 --> 00:32:58,810 to another to another. 624 00:32:58,810 --> 00:33:03,090 Well, instead, you have this knob that takes care of it for you. 625 00:33:03,090 --> 00:33:06,270 The relevant commands for a potentiometer are up here on the board. 626 00:33:06,270 --> 00:33:09,030 So we're going to have this analogRead. 627 00:33:09,030 --> 00:33:12,800 We're going to be reading what the potentiometer's saying. 628 00:33:12,800 --> 00:33:15,660 We're going to be mapping its outputs. 629 00:33:15,660 --> 00:33:19,620 So map is basically this function that takes one input 630 00:33:19,620 --> 00:33:21,400 and spits it out to another. 631 00:33:21,400 --> 00:33:24,530 So it takes in the input. 632 00:33:24,530 --> 00:33:28,610 Then it needs to know what are the ranges of the input values, so 633 00:33:28,610 --> 00:33:33,620 the minimum and the maximum, and then the maximum output values, the minimum 634 00:33:33,620 --> 00:33:35,870 and maximum output values. 635 00:33:35,870 --> 00:33:40,320 Now, we'll see here later how this will work. 636 00:33:40,320 --> 00:33:45,619 And this will especially be useful for the servo once we get to that section. 637 00:33:45,619 --> 00:33:47,660 And another thing, also, we want to start working 638 00:33:47,660 --> 00:33:51,080 with is the Arduino Serial Monitor. 639 00:33:51,080 --> 00:33:53,480 This is essentially a way to see what's happening and is 640 00:33:53,480 --> 00:33:55,870 a useful debugging technique, also. 641 00:33:55,870 --> 00:33:58,130 And then we also want to print out the output 642 00:33:58,130 --> 00:34:01,190 because print debugging is useful in software, 643 00:34:01,190 --> 00:34:05,370 but it's also very useful in hardware to see not only what's happening, 644 00:34:05,370 --> 00:34:09,489 but what kind of inputs are we getting. 645 00:34:09,489 --> 00:34:10,980 Now, let's move onto the code. 646 00:34:10,980 --> 00:34:18,570 647 00:34:18,570 --> 00:34:20,429 So we have this random stuff over here. 648 00:34:20,429 --> 00:34:21,840 We have this voltage regulator. 649 00:34:21,840 --> 00:34:24,089 But for now, we're just going to ignore all this stuff 650 00:34:24,089 --> 00:34:27,900 and just focus on this potentiometer right here. 651 00:34:27,900 --> 00:34:30,989 So it has this knob here that you can turn back and forth. 652 00:34:30,989 --> 00:34:34,420 And potentiometers, as I've said before, they're used in a lot of things, 653 00:34:34,420 --> 00:34:36,330 for instance, in a speaker. 654 00:34:36,330 --> 00:34:39,090 When you turn the knob, when you turn up to the maximum, 655 00:34:39,090 --> 00:34:42,750 you're turning the resistance very low, because sound is essentially 656 00:34:42,750 --> 00:34:45,960 just a bunch of electric signals, and when you flow that electric signal 657 00:34:45,960 --> 00:34:49,179 and there's no resistance, full maximum sound is hit. 658 00:34:49,179 --> 00:34:51,540 But if you turn it all the way up the resistance, 659 00:34:51,540 --> 00:34:54,270 then there's no electric flow flowing, a.k.a. 660 00:34:54,270 --> 00:34:56,580 minimum sound. 661 00:34:56,580 --> 00:35:00,242 So we're going to plug that in right here. 662 00:35:00,242 --> 00:35:01,700 We're going to be using this board. 663 00:35:01,700 --> 00:35:06,740 664 00:35:06,740 --> 00:35:10,300 So I'm going to connect this to the 5-volt rail 665 00:35:10,300 --> 00:35:13,840 and this wire here to the ground. 666 00:35:13,840 --> 00:35:16,420 And this entire rail here is going to have 667 00:35:16,420 --> 00:35:21,865 that 5 volts and the negative volts. 668 00:35:21,865 --> 00:35:26,140 669 00:35:26,140 --> 00:35:28,400 Now, let's look at the code for this. 670 00:35:28,400 --> 00:35:35,510 So the way potentiometers work is that there are three inputs over here. 671 00:35:35,510 --> 00:35:37,610 And as you can see in the picture, there's 672 00:35:37,610 --> 00:35:39,960 three prongs that's sticking out. 673 00:35:39,960 --> 00:35:43,010 One of them is for the-- 674 00:35:43,010 --> 00:35:46,259 on the either sides, either one of them could be positive or negative. 675 00:35:46,259 --> 00:35:47,300 It doesn't really matter. 676 00:35:47,300 --> 00:35:48,260 You can switch them. 677 00:35:48,260 --> 00:35:51,590 But in the middle, for most potentiometers, 678 00:35:51,590 --> 00:35:54,236 they're generally the, quote, unquote, "input signal." 679 00:35:54,236 --> 00:35:59,100 680 00:35:59,100 --> 00:36:00,150 OK. 681 00:36:00,150 --> 00:36:04,000 Now let's get on with the potentiometer. 682 00:36:04,000 --> 00:36:04,500 OK. 683 00:36:04,500 --> 00:36:06,360 So we're going to set this now-- 684 00:36:06,360 --> 00:36:08,794 so we're going to set the potentiometers to pin zero. 685 00:36:08,794 --> 00:36:10,710 And value, we'll just store whatever the value 686 00:36:10,710 --> 00:36:13,700 that it's reading from the potentiometers. 687 00:36:13,700 --> 00:36:17,690 And on the Arduino board, there's also this thing called analog in. 688 00:36:17,690 --> 00:36:20,900 These are the analog inputs that you're going to be providing. 689 00:36:20,900 --> 00:36:24,560 So these are pretty much the signals that you'll 690 00:36:24,560 --> 00:36:32,570 send from the outside world, such as turning this potentiometers knob around 691 00:36:32,570 --> 00:36:36,270 or getting some sensor values. 692 00:36:36,270 --> 00:36:39,950 And in this case, the input will just be from the potentiometers. 693 00:36:39,950 --> 00:36:42,860 So we're going to stick that inside there-- 694 00:36:42,860 --> 00:36:45,080 serial.begin. 695 00:36:45,080 --> 00:36:50,540 So what we're doing is we're setting up this serial or communication rate 696 00:36:50,540 --> 00:36:51,410 command. 697 00:36:51,410 --> 00:36:53,120 And essentially what this does is-- 698 00:36:53,120 --> 00:36:56,450 OK, so the signal that we're going to be sending and receiving 699 00:36:56,450 --> 00:37:02,887 from the Arduino, the communication, is going to travel at about 9,600. 700 00:37:02,887 --> 00:37:04,220 And this is just a common value. 701 00:37:04,220 --> 00:37:12,262 You can set this to anything, but for convention's sake, it's 9,600. 702 00:37:12,262 --> 00:37:13,970 Now, what we're going to do here is we're 703 00:37:13,970 --> 00:37:19,550 going to first read the value from the pot-- so analogRead from pot. 704 00:37:19,550 --> 00:37:25,260 Whatever value is coming from the zeroth pin, 705 00:37:25,260 --> 00:37:27,640 we want to print that to our serial. 706 00:37:27,640 --> 00:37:30,990 And we're going to leave it for 500 seconds or 500 microseconds 707 00:37:30,990 --> 00:37:35,370 because we don't want to read too much commands too fast. 708 00:37:35,370 --> 00:37:35,870 OK. 709 00:37:35,870 --> 00:37:38,340 So before we get on to see what's happening, 710 00:37:38,340 --> 00:37:40,650 I just want to show you guys what a Serial Monitor is. 711 00:37:40,650 --> 00:37:48,031 712 00:37:48,031 --> 00:37:48,530 OK. 713 00:37:48,530 --> 00:37:52,390 So it doesn't look like it's popping up right now, which is all right. 714 00:37:52,390 --> 00:37:53,290 We can fix that. 715 00:37:53,290 --> 00:38:02,270 716 00:38:02,270 --> 00:38:03,920 Oh, OK. 717 00:38:03,920 --> 00:38:06,800 This is another educational moment, actually. 718 00:38:06,800 --> 00:38:09,410 You see here that when I tried to open the serial thing, 719 00:38:09,410 --> 00:38:11,570 the thing is there's no communications. 720 00:38:11,570 --> 00:38:14,780 And you can see here this bug at the bottom-- board at COM5 721 00:38:14,780 --> 00:38:16,200 is not available. 722 00:38:16,200 --> 00:38:19,970 So what this means is that in order for the serial command 723 00:38:19,970 --> 00:38:22,310 to open in the first place, there needs to be 724 00:38:22,310 --> 00:38:27,220 something communicating with the computer, a.k.a. it's the Arduino. 725 00:38:27,220 --> 00:38:30,670 So in order to make sure that we can open it, let's stick it there. 726 00:38:30,670 --> 00:38:33,790 727 00:38:33,790 --> 00:38:36,480 Now let's open the Serial Monitor. 728 00:38:36,480 --> 00:38:40,060 And as you can see here, this is kind of like some terminal that comes through. 729 00:38:40,060 --> 00:38:41,320 It's pretty much blank. 730 00:38:41,320 --> 00:38:44,760 You can type in some commands, send in some things. 731 00:38:44,760 --> 00:38:47,160 But for now, what I want to show you guys 732 00:38:47,160 --> 00:38:51,660 is that this will be used for reading inputs from the potentiometer. 733 00:38:51,660 --> 00:38:53,420 So we're going to close this. 734 00:38:53,420 --> 00:38:57,680 We're going to upload the code, this code, into the Arduino. 735 00:38:57,680 --> 00:39:02,050 736 00:39:02,050 --> 00:39:03,550 And then let's go to Serial Monitor. 737 00:39:03,550 --> 00:39:08,210 738 00:39:08,210 --> 00:39:10,560 As you can see here, numbers are starting to pop up. 739 00:39:10,560 --> 00:39:12,170 So this is 1023. 740 00:39:12,170 --> 00:39:14,490 This is the maximum value of this resistor. 741 00:39:14,490 --> 00:39:17,615 Now, I'm going to be turning this knob, and see what happens to the values. 742 00:39:17,615 --> 00:39:20,730 743 00:39:20,730 --> 00:39:23,670 And you can see, it's slowly starting to decrease. 744 00:39:23,670 --> 00:39:27,050 Now, these correspond to the various resistance 745 00:39:27,050 --> 00:39:29,810 values because this is a 10K resistor. 746 00:39:29,810 --> 00:39:32,360 But with that map function that I mentioned earlier, 747 00:39:32,360 --> 00:39:36,320 this could be used for many, many things, such as, oh, how bright 748 00:39:36,320 --> 00:39:39,800 do I want my LED to be or what color should I set it to 749 00:39:39,800 --> 00:39:42,606 or what position should I set my servo to. 750 00:39:42,606 --> 00:39:44,480 We're going to cover that position, actually, 751 00:39:44,480 --> 00:39:46,469 towards the last project of today. 752 00:39:46,469 --> 00:39:48,260 But as you can see here, this is a good way 753 00:39:48,260 --> 00:39:51,560 to debug because now you're seeing, OK, what inputs are we getting? 754 00:39:51,560 --> 00:39:54,082 755 00:39:54,082 --> 00:39:55,290 So we're going to close that. 756 00:39:55,290 --> 00:39:59,550 757 00:39:59,550 --> 00:40:02,360 And that is it for potentiometers. 758 00:40:02,360 --> 00:40:07,130 Now, ultrasonic sensors-- so the reason why I'm covering ultrasonic sensors 759 00:40:07,130 --> 00:40:10,610 is these tend to be quite common in a lot of Arduino kits. 760 00:40:10,610 --> 00:40:12,644 And there are tutorials online, but I would 761 00:40:12,644 --> 00:40:14,810 like to make sure that people learn how to use these 762 00:40:14,810 --> 00:40:17,910 because these are surprisingly useful. 763 00:40:17,910 --> 00:40:21,530 So the purpose of ultrasonic sensors is that they can measure distance. 764 00:40:21,530 --> 00:40:23,950 And this might not seem like much, but, for instance, 765 00:40:23,950 --> 00:40:25,700 if you're doing a physics lab and you want 766 00:40:25,700 --> 00:40:30,230 to measure how fast an object is traveling, the way this thing will work 767 00:40:30,230 --> 00:40:34,060 is it sends out a sound signal from one end 768 00:40:34,060 --> 00:40:39,860 and measures how long it takes for that signal to come back in the other. 769 00:40:39,860 --> 00:40:42,040 And here we have four pins. 770 00:40:42,040 --> 00:40:47,200 This first one, VCC, is for the input voltage. 771 00:40:47,200 --> 00:40:50,120 This other pin at the end is for the ground. 772 00:40:50,120 --> 00:40:53,960 And the last two pins in between is for the trigger and for the echo. 773 00:40:53,960 --> 00:41:00,770 So trigger is to prepare to send a signal, and echo is to kind of measure 774 00:41:00,770 --> 00:41:04,330 how long it takes to receive that signal back. 775 00:41:04,330 --> 00:41:07,680 And the relevant command here is going to be pulseIn. 776 00:41:07,680 --> 00:41:11,590 So we need to know what echo pin we're going to be working with. 777 00:41:11,590 --> 00:41:13,980 And we're going to be setting it to high. 778 00:41:13,980 --> 00:41:17,280 779 00:41:17,280 --> 00:41:17,780 OK. 780 00:41:17,780 --> 00:41:21,360 So let's disconnect these things from the board. 781 00:41:21,360 --> 00:41:24,350 So I have this nice ultrasonic sensor over here. 782 00:41:24,350 --> 00:41:30,700 I connected the 5 volts and the ground to the board. 783 00:41:30,700 --> 00:41:32,800 And I also have these two trigger pins. 784 00:41:32,800 --> 00:41:34,930 Now, we're going to check the code to see 785 00:41:34,930 --> 00:41:38,980 which pins we are going to put this in. 786 00:41:38,980 --> 00:41:42,350 Let's just make the font a bit bigger before continuing on. 787 00:41:42,350 --> 00:41:43,010 OK. 788 00:41:43,010 --> 00:41:47,150 So here in the code it says trigger pin is seven and echo pin is six. 789 00:41:47,150 --> 00:41:51,410 You can put them in whatever pin you want here except the analog input. 790 00:41:51,410 --> 00:41:54,460 You can put them in any pins you want. 791 00:41:54,460 --> 00:41:57,740 But for now, let's just use seven and six. 792 00:41:57,740 --> 00:42:01,120 So the green wire here goes from the trigger. 793 00:42:01,120 --> 00:42:05,230 And since trigger's at seven, I'm going to put this into seven. 794 00:42:05,230 --> 00:42:09,130 And the echo pin is this tanish wire. 795 00:42:09,130 --> 00:42:10,240 It's going to go into six. 796 00:42:10,240 --> 00:42:12,976 797 00:42:12,976 --> 00:42:14,350 OK. 798 00:42:14,350 --> 00:42:19,840 Now, let's connect these stuff to the 5 volt and ground 799 00:42:19,840 --> 00:42:24,550 respectively, just to make sure we get some current flowing. 800 00:42:24,550 --> 00:42:27,100 But the thing is no logic is being loaded because, actually, 801 00:42:27,100 --> 00:42:31,600 the Arduino kind of keeps in memory the last thing that-- 802 00:42:31,600 --> 00:42:35,370 the code you uploaded to it. 803 00:42:35,370 --> 00:42:35,870 OK. 804 00:42:35,870 --> 00:42:38,810 So now let's walk through what's happening here. 805 00:42:38,810 --> 00:42:41,620 So keep in mind that the ultrasonic sensor 806 00:42:41,620 --> 00:42:46,552 uses sound waves to measure distance and the time it takes to travel. 807 00:42:46,552 --> 00:42:48,760 So we're going to take the duration and the distance. 808 00:42:48,760 --> 00:42:52,830 We're going to set these variables to be used later. 809 00:42:52,830 --> 00:42:54,270 So the trigger pin is the output. 810 00:42:54,270 --> 00:42:57,470 This is the one that's going to send the sound single out. 811 00:42:57,470 --> 00:42:59,450 The echo pin is going to be the input, which 812 00:42:59,450 --> 00:43:03,740 is going to measure how long it takes for the signal to be received. 813 00:43:03,740 --> 00:43:06,470 And we're also going to use a Serial Monitor so we 814 00:43:06,470 --> 00:43:08,536 can see what distance we're getting. 815 00:43:08,536 --> 00:43:11,790 So in the void loop, first we want to clear the trigger pin 816 00:43:11,790 --> 00:43:15,580 to make sure that there's no previous stuff that was used in that sensor 817 00:43:15,580 --> 00:43:16,080 before. 818 00:43:16,080 --> 00:43:18,900 819 00:43:18,900 --> 00:43:22,530 Now, we want to set the trigger pin on a high state for 10 microseconds. 820 00:43:22,530 --> 00:43:27,210 So what this does is we're going to send a signal now from the trigger pin. 821 00:43:27,210 --> 00:43:29,490 So we're going to send a signal from here. 822 00:43:29,490 --> 00:43:32,040 We're going to send out a sound wave. 823 00:43:32,040 --> 00:43:35,810 And we're going delay it, and then we're going to turn it right off. 824 00:43:35,810 --> 00:43:40,640 Now, once that sound wave bounces off the object and it's coming back 825 00:43:40,640 --> 00:43:46,390 and hits the other echo pin, what we want to do now is take-- 826 00:43:46,390 --> 00:43:49,540 OK, how long does it take for it to come back? 827 00:43:49,540 --> 00:43:52,380 So we're going to do this pulseIn. 828 00:43:52,380 --> 00:43:57,420 And pulseIn returns how long the sound wave travels in microseconds. 829 00:43:57,420 --> 00:44:04,290 And because the speed of sound is fixed in most situations, about 348 meters 830 00:44:04,290 --> 00:44:10,660 per second, we just have to divide that by how long it took for the object-- 831 00:44:10,660 --> 00:44:14,900 we just have to take that speed, multiply it by the time 832 00:44:14,900 --> 00:44:23,822 it travels, and then we can get back the measurement of how far that object is. 833 00:44:23,822 --> 00:44:25,780 Here we're going to be using centimeters, which 834 00:44:25,780 --> 00:44:31,460 explains why the number is a bit low. 835 00:44:31,460 --> 00:44:35,090 Then after that, we're going to print out this thing, distance. 836 00:44:35,090 --> 00:44:41,370 This is simply going to literally print out distance inside the terminal. 837 00:44:41,370 --> 00:44:43,000 Then we're going to print out-- 838 00:44:43,000 --> 00:44:45,412 OK, what was the actual value we got? 839 00:44:45,412 --> 00:44:47,370 And then, to make sure we understand the units, 840 00:44:47,370 --> 00:44:50,310 we're going to use centimeters in this case. 841 00:44:50,310 --> 00:44:54,850 And to make sure that we are not going to be receiving too much commands, 842 00:44:54,850 --> 00:45:00,245 I'll set this from five to 500, 500 microseconds or about half a second. 843 00:45:00,245 --> 00:45:02,370 So every half a second, we get a different reading. 844 00:45:02,370 --> 00:45:05,597 845 00:45:05,597 --> 00:45:06,955 OK. 846 00:45:06,955 --> 00:45:07,580 All looks well. 847 00:45:07,580 --> 00:45:08,871 Let's upload this to the board. 848 00:45:08,871 --> 00:45:15,650 849 00:45:15,650 --> 00:45:17,070 And look at the Serial Monitor. 850 00:45:17,070 --> 00:45:22,130 851 00:45:22,130 --> 00:45:24,820 So we can see here that we're getting, now, some measurements. 852 00:45:24,820 --> 00:45:28,040 And the way this thing is, right now it's pointing towards the ceiling. 853 00:45:28,040 --> 00:45:32,640 So it's measuring that the ceiling is about 133 centimeters. 854 00:45:32,640 --> 00:45:38,750 Let's say I have this book over here, and I'll point it a bit right here. 855 00:45:38,750 --> 00:45:43,790 And notice that, as I move closer, that this distance is getting smaller, 856 00:45:43,790 --> 00:45:47,920 and that as I move further, the distance is getting larger. 857 00:45:47,920 --> 00:45:52,830 858 00:45:52,830 --> 00:45:55,980 Now, keep in mind, this thing actually is sending out sound waves 859 00:45:55,980 --> 00:45:57,240 and it's measuring them. 860 00:45:57,240 --> 00:46:01,930 So it will get lost as you try to measure out something further, 861 00:46:01,930 --> 00:46:04,679 so there are limits to what this thing can measure. 862 00:46:04,679 --> 00:46:06,720 And in addition, you don't want to measure things 863 00:46:06,720 --> 00:46:13,760 too close because my hand is right now pressing on the ultrasonic sensor, 864 00:46:13,760 --> 00:46:17,525 but notice that the reading is saying it's 3,000 centimeters away or 2,000. 865 00:46:17,525 --> 00:46:19,900 So you want to make sure that you're not too close to it. 866 00:46:19,900 --> 00:46:23,910 867 00:46:23,910 --> 00:46:26,628 Let's close that for now. 868 00:46:26,628 --> 00:46:28,420 Awesome. 869 00:46:28,420 --> 00:46:31,400 So from ultrasonic sensors, let's move on to the final part, 870 00:46:31,400 --> 00:46:34,030 servos, my favorite components. 871 00:46:34,030 --> 00:46:38,170 So servos are essentially these controllized motors that 872 00:46:38,170 --> 00:46:40,580 can move around in the fixed place. 873 00:46:40,580 --> 00:46:42,600 So here we have the servo library. 874 00:46:42,600 --> 00:46:46,090 The relevant commands are we need to include a servo library. 875 00:46:46,090 --> 00:46:48,610 We need to declare this object called myServo. 876 00:46:48,610 --> 00:46:51,370 We're going to attach it, we're going to write to it the angle, 877 00:46:51,370 --> 00:46:52,520 and we're going to read it. 878 00:46:52,520 --> 00:46:53,020 OK. 879 00:46:53,020 --> 00:46:56,460 Let's see this in action. 880 00:46:56,460 --> 00:46:57,740 So let's separate this. 881 00:46:57,740 --> 00:47:00,964 882 00:47:00,964 --> 00:47:03,130 And we're going to pair this with the potentiometers 883 00:47:03,130 --> 00:47:07,290 to illustrate what's happening right here. 884 00:47:07,290 --> 00:47:10,080 And I'll just leave this right there just for visual sakes. 885 00:47:10,080 --> 00:47:13,140 So in this circuit here, I included a voltage regulator. 886 00:47:13,140 --> 00:47:16,736 Now, depending on your servo, sometimes your servo can take a higher load. 887 00:47:16,736 --> 00:47:19,860 But the thing is, you don't want to connect a servo directly to the Arduino 888 00:47:19,860 --> 00:47:24,210 because it'll pull out a lot of power, and you'll notice that, 889 00:47:24,210 --> 00:47:26,970 hey, why is my Arduino not connecting to my computer? 890 00:47:26,970 --> 00:47:29,700 It's because this servo is draining out a lot of power, 891 00:47:29,700 --> 00:47:32,200 and the Arduino can only provide a fixed amount. 892 00:47:32,200 --> 00:47:34,033 So what we're going to need to do is we need 893 00:47:34,033 --> 00:47:35,800 to provide an external voltage source. 894 00:47:35,800 --> 00:47:38,380 Now, let's be very careful of how we wire this properly. 895 00:47:38,380 --> 00:47:40,920 So this is not the dangerous part. 896 00:47:40,920 --> 00:47:44,380 But the dangerous part is when you're combining voltages. 897 00:47:44,380 --> 00:47:48,090 So this entire rail in the breadboard, this 898 00:47:48,090 --> 00:47:50,910 is all going to be from the battery. 899 00:47:50,910 --> 00:47:55,620 Do not ever put the same voltage right here where the Arduino's going to be. 900 00:47:55,620 --> 00:47:58,960 Things are going to get really messed up quite quickly. 901 00:47:58,960 --> 00:48:01,710 However, we do want to make sure we share the same ground, though. 902 00:48:01,710 --> 00:48:08,190 So we're going to flow from the battery to here and into the Arduino board. 903 00:48:08,190 --> 00:48:10,780 Now, notice this black component right here. 904 00:48:10,780 --> 00:48:13,650 This is a voltage regulator. 905 00:48:13,650 --> 00:48:20,830 So what this pin here on the leftmost pin is going to be the input. 906 00:48:20,830 --> 00:48:24,429 Now, output voltage is just going to be ground. 907 00:48:24,429 --> 00:48:25,720 This is going to be the ground. 908 00:48:25,720 --> 00:48:29,159 And on the rightmost pin is going to be the output. 909 00:48:29,159 --> 00:48:30,950 So the good thing about a voltage regulator 910 00:48:30,950 --> 00:48:36,060 is that it makes sure that we still have this constant voltage flowing. 911 00:48:36,060 --> 00:48:40,280 So whether or not this thing drops from a nine to a seven automatically, 912 00:48:40,280 --> 00:48:43,950 we know that we're still going to get 5 volts. 913 00:48:43,950 --> 00:48:47,450 So let's see this in action. 914 00:48:47,450 --> 00:48:50,620 Let's make the font bigger. 915 00:48:50,620 --> 00:48:53,050 And let's wire this up quickly. 916 00:48:53,050 --> 00:48:57,880 So because we want to use only 5 volts, I'm going to connect this-- 917 00:48:57,880 --> 00:49:01,390 the way this servo, before I forget to mention. 918 00:49:01,390 --> 00:49:04,120 We can see here that servos have these tri-colorings. 919 00:49:04,120 --> 00:49:08,750 Sometimes it comes in brown, red, and yellow. 920 00:49:08,750 --> 00:49:14,260 But brown or black corresponds to ground, red corresponds to the input 921 00:49:14,260 --> 00:49:17,050 voltage or 5 volts, whatever volts you want to provide, 922 00:49:17,050 --> 00:49:21,500 and white or yellow corresponds to the signal. 923 00:49:21,500 --> 00:49:25,400 So with those facts in mind, I'm going to connect this red 924 00:49:25,400 --> 00:49:29,120 to the output of the 5-volt regulator. 925 00:49:29,120 --> 00:49:32,360 I'm going to connect this black wire to the ground because, remember, 926 00:49:32,360 --> 00:49:35,010 we want to keep some common ground. 927 00:49:35,010 --> 00:49:37,860 And what I want to do next is for the white wire, 928 00:49:37,860 --> 00:49:40,170 we want to connect it to the Arduino because remember 929 00:49:40,170 --> 00:49:43,530 that the Arduino is the master, the brain behind all of this. 930 00:49:43,530 --> 00:49:45,780 So we want to connect this to pin nine. 931 00:49:45,780 --> 00:49:51,016 932 00:49:51,016 --> 00:49:52,930 OK. 933 00:49:52,930 --> 00:49:57,180 Now, let's also connect this stuff from the potentiometers into the Arduino. 934 00:49:57,180 --> 00:50:01,630 So we're going to connect this to the ground. 935 00:50:01,630 --> 00:50:04,090 Connect this little wire to the 5 volts. 936 00:50:04,090 --> 00:50:08,000 937 00:50:08,000 --> 00:50:11,210 And we're going to connect this pin output to pin zero. 938 00:50:11,210 --> 00:50:13,890 939 00:50:13,890 --> 00:50:14,390 OK. 940 00:50:14,390 --> 00:50:17,310 941 00:50:17,310 --> 00:50:19,040 Now let's see what's happening here. 942 00:50:19,040 --> 00:50:21,430 The code is not that much as the ultrasonic sensor 943 00:50:21,430 --> 00:50:24,080 or the debounce one or the button. 944 00:50:24,080 --> 00:50:26,320 But this is pretty powerful code, I'd say. 945 00:50:26,320 --> 00:50:30,920 So we included the servo library up top here. 946 00:50:30,920 --> 00:50:32,450 Next, we're sending out the pins. 947 00:50:32,450 --> 00:50:34,640 So we're going to set servo-- 948 00:50:34,640 --> 00:50:36,980 we're going to set the output for the servo as pin nine 949 00:50:36,980 --> 00:50:40,270 and for the potentiometers is pin zero. 950 00:50:40,270 --> 00:50:43,390 We're going to use the pots value-- 951 00:50:43,390 --> 00:50:45,460 we want to measure the pots value because this 952 00:50:45,460 --> 00:50:48,410 is going to be outputted somehow. 953 00:50:48,410 --> 00:50:50,060 We're going to declare this servo. 954 00:50:50,060 --> 00:50:52,520 We're just going to say it's my servo. 955 00:50:52,520 --> 00:50:55,640 Now, this servo.h builds upon a ton of abstractions, 956 00:50:55,640 --> 00:50:57,650 which you don't have to worry about. 957 00:50:57,650 --> 00:51:00,680 So let's just work with this. 958 00:51:00,680 --> 00:51:01,809 We're going to attach-- 959 00:51:01,809 --> 00:51:04,100 this line here's going to attach the servo to pin nine. 960 00:51:04,100 --> 00:51:09,770 So this is saying that, OK, I want all the logic flowing from the board 961 00:51:09,770 --> 00:51:13,524 to be outputted into this pin. 962 00:51:13,524 --> 00:51:15,440 And we're also going to read from the terminal 963 00:51:15,440 --> 00:51:18,400 just to see what's happening right now. 964 00:51:18,400 --> 00:51:18,900 OK. 965 00:51:18,900 --> 00:51:21,430 So first in the void loop, we're going to read 966 00:51:21,430 --> 00:51:23,310 the value from the potentiometer. 967 00:51:23,310 --> 00:51:26,980 So how far did we twist this thing? 968 00:51:26,980 --> 00:51:29,260 Next we're going to print it out, also. 969 00:51:29,260 --> 00:51:32,250 Then the key thing here is the map function. 970 00:51:32,250 --> 00:51:37,080 So the way servos work, it can only take values from zero to 179. 971 00:51:37,080 --> 00:51:41,590 But the potentiometers puts out values from zero to 1,023. 972 00:51:41,590 --> 00:51:44,200 So in order to do this, we need to map it. 973 00:51:44,200 --> 00:51:47,840 So this is saying, OK, we know that the pots value's 974 00:51:47,840 --> 00:51:52,010 going to be between zero and 1,023. 975 00:51:52,010 --> 00:51:56,130 So we want to map it to between zero through 179. 976 00:51:56,130 --> 00:51:59,660 So this map function essentially takes care of all the logic you have to do 977 00:51:59,660 --> 00:52:03,010 and just outputs the value you want. 978 00:52:03,010 --> 00:52:05,710 After that, you're going to write to that servo. 979 00:52:05,710 --> 00:52:09,070 So by write, it depends what servo you have. 980 00:52:09,070 --> 00:52:12,920 In this case, we have a continuous rotation servo. 981 00:52:12,920 --> 00:52:19,340 So .write sets how fast the servo is turning in one direction. 982 00:52:19,340 --> 00:52:21,580 If you have a 90-degree servo, it changes the angle 983 00:52:21,580 --> 00:52:23,530 of what the servo's positioned at. 984 00:52:23,530 --> 00:52:26,200 So since we're working with a continuous rotation servo, 985 00:52:26,200 --> 00:52:30,670 this is just going to tell, OK, how fast are we making the servo turn? 986 00:52:30,670 --> 00:52:33,260 And we're going to leave it for 15 seconds. 987 00:52:33,260 --> 00:52:33,760 OK. 988 00:52:33,760 --> 00:52:35,010 So let's upload this code. 989 00:52:35,010 --> 00:52:44,650 990 00:52:44,650 --> 00:52:46,650 Now, the next thing we also want to keep in mind 991 00:52:46,650 --> 00:52:51,010 is just making sure, again, we check all of the components are located together. 992 00:52:51,010 --> 00:52:54,760 And as you can see here, this thing is already starting to spin. 993 00:52:54,760 --> 00:52:57,510 Just pausing it for now. 994 00:52:57,510 --> 00:53:01,050 Let's just insert this so the viewers at home can see. 995 00:53:01,050 --> 00:53:10,660 996 00:53:10,660 --> 00:53:11,980 Just add this right here. 997 00:53:11,980 --> 00:53:18,510 998 00:53:18,510 --> 00:53:22,360 And let's open the Serial Monitor just to see what's happening on the inside. 999 00:53:22,360 --> 00:53:26,960 1000 00:53:26,960 --> 00:53:32,790 So notice that, as I turn it around, for those not able to see, 1001 00:53:32,790 --> 00:53:34,980 currently the servo's spinning right now. 1002 00:53:34,980 --> 00:53:38,330 Unfortunately, I don't have anything to clearly display it. 1003 00:53:38,330 --> 00:53:41,790 1004 00:53:41,790 --> 00:53:44,730 But we can see over here that the servo is currently turning. 1005 00:53:44,730 --> 00:53:47,880 And if I turn the potentiometers the other way around, 1006 00:53:47,880 --> 00:53:52,647 it stops and spins the other direction. 1007 00:53:52,647 --> 00:53:54,230 So we can have a lot of fun with this. 1008 00:53:54,230 --> 00:53:56,330 We can keep switching it back and forth, back and forth just 1009 00:53:56,330 --> 00:53:57,598 to see what's happening. 1010 00:53:57,598 --> 00:54:00,659 SPEAKER: So the potentiometer already can process speed? 1011 00:54:00,659 --> 00:54:02,700 ROBERT JOMAR MALATE: Yes, here the potentiometers 1012 00:54:02,700 --> 00:54:05,002 is the input to control the speed. 1013 00:54:05,002 --> 00:54:08,210 So it's kind of like the speaker where you set the volume, it sets the speed. 1014 00:54:08,210 --> 00:54:12,140 1015 00:54:12,140 --> 00:54:13,900 So that's it for our wonderful projects. 1016 00:54:13,900 --> 00:54:16,400 Now, I just want to get through some common debugging issues 1017 00:54:16,400 --> 00:54:17,610 and how to solve them. 1018 00:54:17,610 --> 00:54:19,460 So first, the circuit's not wired properly. 1019 00:54:19,460 --> 00:54:23,120 The most common thing I've ran into is people forgetting to connect something 1020 00:54:23,120 --> 00:54:25,310 to the positive voltage or to ground. 1021 00:54:25,310 --> 00:54:27,830 So make sure that each component is flowing 1022 00:54:27,830 --> 00:54:30,100 from positive to negative to ground. 1023 00:54:30,100 --> 00:54:32,230 Next is drawing too much current, and this 1024 00:54:32,230 --> 00:54:34,480 is relevant for huge components like servos 1025 00:54:34,480 --> 00:54:37,540 because Arduino can only provide about 500 milliamps, 1026 00:54:37,540 --> 00:54:39,730 but this thing could take a lot more than that 1027 00:54:39,730 --> 00:54:44,460 and could take a lot more voltage than the Arduino could provide. 1028 00:54:44,460 --> 00:54:51,469 Wrong board selected-- as we saw in the Arduino IDE, there are multiple boards, 1029 00:54:51,469 --> 00:54:54,510 and a common thing is people sometimes, if you work with multiple boards, 1030 00:54:54,510 --> 00:54:57,420 they select the wrong one accidentally. 1031 00:54:57,420 --> 00:54:59,790 And the next one is the port not selected. 1032 00:54:59,790 --> 00:55:03,192 That is another common issue that we can see over here. 1033 00:55:03,192 --> 00:55:05,900 So if we go to Tools, we just want to make sure that, one, you're 1034 00:55:05,900 --> 00:55:07,850 working with the right board. 1035 00:55:07,850 --> 00:55:09,890 And the next one is that there's actually a COM. 1036 00:55:09,890 --> 00:55:14,540 And it should just display right there because it's a little bit too 1037 00:55:14,540 --> 00:55:16,152 complex to deal with yourself. 1038 00:55:16,152 --> 00:55:18,860 But essentially, COM is just whatever this thing is connected to. 1039 00:55:18,860 --> 00:55:21,610 1040 00:55:21,610 --> 00:55:25,000 And don't hesitate to use the Serial Monitor. 1041 00:55:25,000 --> 00:55:28,609 As we saw, we were able to see the inputs from the potentiometer. 1042 00:55:28,609 --> 00:55:30,400 But this will be really useful for once you 1043 00:55:30,400 --> 00:55:34,495 start using more complex sensors to try to output the value readings. 1044 00:55:34,495 --> 00:55:38,960 1045 00:55:38,960 --> 00:55:40,740 So this is the end for presentation. 1046 00:55:40,740 --> 00:55:44,922 But before we leave, I just want to leave you guys with extra resources. 1047 00:55:44,922 --> 00:55:46,630 If you want to pursue more with Arduino-- 1048 00:55:46,630 --> 00:55:49,240 we just scratched the surface of what we could do. 1049 00:55:49,240 --> 00:55:53,110 The number one book I'd recommend is Exploring Arduino by Jeremy Blum. 1050 00:55:53,110 --> 00:55:54,910 I've used this book personally, and this is 1051 00:55:54,910 --> 00:55:59,140 what made me love Arduino because the explanations in the book are great, 1052 00:55:59,140 --> 00:56:03,250 but it also makes you start to think, OK, what can I do with these projects 1053 00:56:03,250 --> 00:56:04,840 and build up upon them? 1054 00:56:04,840 --> 00:56:07,120 And then it was also what inspired me to kind of give 1055 00:56:07,120 --> 00:56:10,510 this seminar is I want you guys to work with these basic components 1056 00:56:10,510 --> 00:56:11,900 and build abstractions upon them. 1057 00:56:11,900 --> 00:56:13,990 So with the button, it might look complex 1058 00:56:13,990 --> 00:56:16,000 and the code there is a little bit rough, 1059 00:56:16,000 --> 00:56:19,990 but you can build upon that to create a mini piano or some fancy light switch 1060 00:56:19,990 --> 00:56:24,190 or connected to some LED to change its colors. 1061 00:56:24,190 --> 00:56:25,114 Next is fritzing. 1062 00:56:25,114 --> 00:56:27,030 If you're going to work with Arduino, fritzing 1063 00:56:27,030 --> 00:56:30,330 is just this thing to lay out the breadboard schematics. 1064 00:56:30,330 --> 00:56:34,560 So for here, if you want to document the process, I would use fritzing. 1065 00:56:34,560 --> 00:56:40,690 I will be uploading the schematics, the code for all of these components 1066 00:56:40,690 --> 00:56:43,440 so you viewers at home and people here can 1067 00:56:43,440 --> 00:56:46,570 view them and recreate them yourselves. 1068 00:56:46,570 --> 00:56:49,530 And for Arduino project ideas, you can check out my website down here 1069 00:56:49,530 --> 00:56:50,490 in the link. 1070 00:56:50,490 --> 00:56:52,830 There's going to be projects. 1071 00:56:52,830 --> 00:56:54,480 Currently there's nothing there yet. 1072 00:56:54,480 --> 00:56:56,700 But I will be uploading some projects for you guys 1073 00:56:56,700 --> 00:57:01,110 to trial, which I believe will further your education. 1074 00:57:01,110 --> 00:57:03,680 So with that in mind, this is CS50. 1075 00:57:03,680 --> 00:57:07,780 I hope you enjoyed this seminar, and start making stuff with Arduino. 1076 00:57:07,780 --> 00:57:09,934