1 00:00:07,710 --> 00:00:11,120 In this video I'll introduce some new components 2 00:00:11,120 --> 00:00:13,630 that will be used to construct your first circuit. 3 00:00:13,630 --> 00:00:17,810 Afterwards we will step into the Arduino development environment 4 00:00:17,810 --> 00:00:21,250 and learn some of it's basic features. 5 00:00:21,250 --> 00:00:28,350 Finally we will code our first microcontroller program and upload it to our Arduino. 6 00:00:28,350 --> 00:00:30,400 Let's get started. 7 00:00:30,400 --> 00:00:37,500 >> The first component that we should familiarize ourselves with is the solderless breadboard. 8 00:00:37,500 --> 00:00:42,590 This breadboard allows us to prototype or test our circuits 9 00:00:45,190 --> 00:00:51,900 simply by placing the leads or component ends inside these tiny holes called sockets. 10 00:00:51,900 --> 00:00:58,000 It's important to note that letters and numbers run along the perimeter of the breadboard. 11 00:01:00,670 --> 00:01:04,760 This is because the sockets in each numbered row are connected 12 00:01:04,760 --> 00:01:13,260 which means row 1A to row 1E, for example, 13 00:01:13,260 --> 00:01:20,570 will receive the same current; however, the rows are not connected to each other. 14 00:01:23,920 --> 00:01:28,330 >> The next component is the resistor which has the primary puroposes 15 00:01:28,330 --> 00:01:31,280 of limiting current and dividing voltage. 16 00:01:31,280 --> 00:01:36,530 We use resistors because not all components accept the same level of voltage 17 00:01:36,530 --> 00:01:39,220 that the power source provides. 18 00:01:39,220 --> 00:01:45,190 When a steady voltage is applied to the leads of the resistor, 19 00:01:45,190 --> 00:01:51,040 the amount of current that allows to flow through it is determined by its resistance 20 00:01:51,040 --> 00:01:53,360 which is measured in ohms. 21 00:01:53,360 --> 00:01:57,520 So more ohms results to less current. 22 00:01:57,520 --> 00:02:01,720 In order to figure out how to calculate the amount of resistance in ohms 23 00:02:01,720 --> 00:02:05,900 that a resistor applies, we simply look at its color stripes 24 00:02:05,900 --> 00:02:08,500 which wrap around the outer casing. 25 00:02:08,500 --> 00:02:14,200 The resistance value can be read by the first 3 stripes of color. 26 00:02:14,200 --> 00:02:22,040 Each color has a specified value from 0, being black, to 9, being white. 27 00:02:22,040 --> 00:02:26,770 You could find more information about these values from the link provided. 28 00:02:26,770 --> 00:02:33,530 There is also a fourth stripe that comes in either gold, silver, or just blank. 29 00:02:33,530 --> 00:02:41,400 This gives the tolerance levels of the resistor, i.e. how closely it matches its rated resistance. 30 00:02:41,400 --> 00:02:47,790 For now we can ignore the fourth stripe and set our focus on the first 3. 31 00:02:47,790 --> 00:02:54,830 >> The first stripe, which is the opposite of the tolerance stripe, is the first digit. 32 00:02:54,830 --> 00:02:58,260 This value can be 0 to 9. 33 00:02:58,260 --> 00:03:05,130 Similarly, the second stripe is the second digit which can also have a value of 0 to 9. 34 00:03:05,130 --> 00:03:09,780 But the third digit is where it becomes different. 35 00:03:09,780 --> 00:03:16,730 The third digit is the number of 0's that are added to the end of the first 2 digits. 36 00:03:16,730 --> 00:03:20,920 The formal name of this stripe is the multiplor. 37 00:03:20,920 --> 00:03:23,800 Take for example this resistor. 38 00:03:23,800 --> 00:03:28,610 We currently have an orange, orange, brown resistor. 39 00:03:28,610 --> 00:03:35,120 Orange's value is 3, and brown's value is 1. 40 00:03:35,120 --> 00:03:42,400 Therefore, we have a 3, 3, 0 or 330 ohm resistor. 41 00:03:42,400 --> 00:03:48,960 Remember the third stripe, which is brown, is telling us only the number of 0's to be added 42 00:03:48,960 --> 00:03:52,200 onto the first and second digits. 43 00:03:52,200 --> 00:03:58,720 >> Finally our last component is the light-emitting diode or LED for short. 44 00:03:58,720 --> 00:04:04,250 The LED is a little light that we may find in most of our electronics. 45 00:04:04,250 --> 00:04:10,250 In order for an LED to emit light, current must pass through a lead in a specific direction. 46 00:04:10,250 --> 00:04:12,250 But we will come back to this shortly. 47 00:04:12,250 --> 00:04:16,209 For now, notice how 1 lead is longer than the other. 48 00:04:16,209 --> 00:04:22,860 The longer lead is called the anode, and this is the positive terminal for the LED. 49 00:04:22,860 --> 00:04:28,470 The shorter lead, which is the negative terminal, is called the cathode. 50 00:04:28,470 --> 00:04:31,810 >> Now that we have a general understanding of our components, 51 00:04:31,810 --> 00:04:33,950 let's build our first circuit. 52 00:04:33,950 --> 00:04:38,950 When you begin building a circuit you should always unplug your Arduino from the computer. 53 00:04:38,950 --> 00:04:44,790 So according to our schematic, we know that the resistor should be between 54 00:04:44,790 --> 00:04:50,490 the power source, i.e. one of the Arduino's digital pins, and the anode, 55 00:04:50,490 --> 00:04:53,550 the positive lead of the LED. 56 00:04:53,550 --> 00:04:58,380 While the cathode, negative lead, will be connected directly to ground, 57 00:04:58,380 --> 00:05:00,930 thus completing our circuit. 58 00:05:00,930 --> 00:05:07,040 Unlike the LED, the direction by which we place the resistor does not matter. 59 00:05:07,040 --> 00:05:13,310 Let's place one of the resistors leads in socket row 1A. 60 00:05:21,790 --> 00:05:25,830 Now let's place the other lead of the resistor in a separate circuit path. 61 00:05:25,830 --> 00:05:28,890 How about row 2A? 62 00:05:39,990 --> 00:05:43,410 >> Great. Halfway there. Let's move on to the LED. 63 00:05:43,410 --> 00:05:49,970 Per the schematic, our anode, the positive lead, must be connected to our resistor. 64 00:05:52,190 --> 00:05:57,910 This means that we should place the LEDs anode in a socket that is on the same 65 00:05:57,910 --> 00:06:00,510 circuit path as 1 of the resistors leads. 66 00:06:00,510 --> 00:06:03,760 Let's do row 2E. 67 00:06:09,440 --> 00:06:15,310 Per our schematic, we know that the cathode will go directly into the Arduinos ground pin. 68 00:06:15,310 --> 00:06:21,370 So we can place the cathode into row 3E. 69 00:06:24,480 --> 00:06:27,450 >> Great. The final part to our schematic is simply using these jumper cables 70 00:06:27,450 --> 00:06:32,190 to connect to our Arduino, thus completing the circuit. 71 00:06:32,190 --> 00:06:37,080 Let's start by making the connection from the cathode to the Arduinos ground. 72 00:06:37,080 --> 00:06:42,610 To do this, we simply plug the jumper cable into any of the sockets 73 00:06:42,610 --> 00:06:47,630 which share the same A to E row of the cathode. 74 00:06:47,630 --> 00:06:55,060 In this case we'll plug 1 end of the jumper cable directly into row 3A. 75 00:07:12,190 --> 00:07:18,580 The other plug will go into 1 of the grounded or GRD digital pins of the Arduino. 76 00:07:25,310 --> 00:07:29,550 As for the second cable, according to our schematic we will make a connection 77 00:07:29,550 --> 00:07:36,390 from our resistor to our power source which is 1 of the digital pins on the Arduino. 78 00:07:36,390 --> 00:07:42,150 We already know that 1 end of the resistor is connected to the LEDs anode. 79 00:07:42,150 --> 00:07:49,110 So this leaves us with only 1 option, row 1 sockets B through E. 80 00:07:49,110 --> 00:07:52,410 Let's give ourselves some room between our components. 81 00:07:52,410 --> 00:07:56,610 Let's plug 1 end of the jumper cable in row 1E. 82 00:08:07,670 --> 00:08:12,870 Finally, plug the other end of this jumper cable in digital pin 13. 83 00:08:12,870 --> 00:08:17,000 Remember this pin. It will be very important soon. 84 00:08:26,660 --> 00:08:29,860 >> Well the circuit looks pretty, but we want it to do something. 85 00:08:29,860 --> 00:08:31,860 Let's crack our knuckles and get down to business 86 00:08:31,860 --> 00:08:34,750 writing our first microcontroller program. 87 00:08:34,750 --> 00:08:38,730 First plug the square USB end into the Arduino. 88 00:08:42,870 --> 00:08:44,930 In order to start writing our own program, 89 00:08:44,930 --> 00:08:48,000 we will need to access the Arduino integrated development environment, 90 00:08:48,000 --> 00:08:51,570 which I will refer to as the IDE. 91 00:08:51,570 --> 00:08:55,890 To do this click on the appliance menu at the bottom lefthand of the screen. 92 00:08:55,890 --> 00:09:01,510 Go to programming and select Arduino from this menu. 93 00:09:01,510 --> 00:09:05,210 If the Arduino software is not currently installed you can easily install it by 94 00:09:05,210 --> 00:09:08,450 opening a terminal and typing the following command: 95 00:09:08,450 --> 00:09:13,450 Sudo yum install arduino. 96 00:09:13,450 --> 00:09:15,450 You will need to restart the appliance when it completes. 97 00:09:16,820 --> 00:09:20,070 So once you launch the IDE, the first thing you should check 98 00:09:20,070 --> 00:09:25,480 is if the Arduino IDE is registering or seeing your Arduino device. 99 00:09:25,480 --> 00:09:30,190 You can do this by simply going to the tools menu, hover over serial port, 100 00:09:30,190 --> 00:09:34,340 and there should be at least 3 devices listed. 101 00:09:34,840 --> 00:09:41,680 If it is not checked already, do make sure you check the /dev/ttyacm0 102 00:09:41,680 --> 00:09:44,990 as this is where you Arduino is plugged into. 103 00:09:44,990 --> 00:09:50,790 >> When you first open the Arduino IDE a new project, which is called a Sketch, 104 00:09:50,790 --> 00:09:53,250 opens up automatically. 105 00:09:53,250 --> 00:09:56,500 This area will be used to place our coding. 106 00:09:56,500 --> 00:10:00,700 At the bottom of the screen there is a terminal window responsible for outputing information 107 00:10:00,700 --> 00:10:06,180 such as complilation response codes or syntax errors in your code. 108 00:10:06,180 --> 00:10:10,340 At the top of the screen just below the file menu, there are a series of icons 109 00:10:10,340 --> 00:10:12,290 that we should be acquainted with. 110 00:10:12,290 --> 00:10:17,050 Starting from the far left, there is an icon that resembles a check. 111 00:10:17,050 --> 00:10:20,920 This button is called verify, and its responsible for compiling your code 112 00:10:20,920 --> 00:10:25,200 while validating the correctness of your program syntax. 113 00:10:25,200 --> 00:10:30,260 The button after verify, which resembles that of a sideways arrow pointing to the right, 114 00:10:30,260 --> 00:10:32,260 is the upload command. 115 00:10:32,260 --> 00:10:37,180 The upload command is resonsible for sending the programs compiled 1's and 0's 116 00:10:37,180 --> 00:10:41,010 over to your microcontroller for it to be saved on the board. 117 00:10:41,010 --> 00:10:45,810 Keep in mind that the verify button will not upload your code. 118 00:10:45,810 --> 00:10:50,280 The next 3 buttons are new, open, and save respectively. 119 00:10:50,280 --> 00:10:54,920 The final button to the far right of this menu is called the serial monitor, 120 00:10:54,920 --> 00:11:00,930 and it acts as a consult whereby programmers can configure the Arduino to read as the input 121 00:11:00,930 --> 00:11:05,730 or display as the output to and from the serial monitor. 122 00:11:05,730 --> 00:11:08,600 We'll come back to the serial monitor in another video. 123 00:11:08,600 --> 00:11:11,850 >> For now let's start writing our program. 124 00:11:11,850 --> 00:11:17,350 Now starting to write an Arduino program slightly differs from regular C programs. 125 00:11:17,350 --> 00:11:23,570 This is because an Arduino needs, at a bare minimum, 2 specific void funtions defined. 126 00:11:23,570 --> 00:11:26,310 Setup and loop. 127 00:11:26,310 --> 00:11:32,350 Arduino makes it very easy to get started by utilizing example code templates 128 00:11:32,350 --> 00:11:35,510 which come with the IDE. 129 00:11:35,510 --> 00:11:42,750 To load our bare minimum, simply go to the file menu, examples, choose number 1 basics, 130 00:11:42,750 --> 00:11:44,380 and click on bare minimum. 131 00:11:44,380 --> 00:11:46,770 A new sketch window should appear. 132 00:11:46,770 --> 00:11:48,770 Loading the templated code. 133 00:11:48,770 --> 00:11:51,510 Let's briefly go over these 2 functions. 134 00:11:51,510 --> 00:11:57,310 The setup function is similar to main as it is the first function to run, 135 00:11:57,310 --> 00:11:59,820 and it only runs once. 136 00:11:59,820 --> 00:12:04,160 Setup is used for defining which pins will be input or output. 137 00:12:04,160 --> 00:12:09,400 For example, this would be a great place to tell the Arduino that we want to output 138 00:12:09,400 --> 00:12:13,400 some electrical current over to pin number 13. 139 00:12:13,400 --> 00:12:19,370 Loop is a function that runs continuously on the microcontroller. 140 00:12:19,370 --> 00:12:22,130 Ever wonder why your alarm clock never stops? 141 00:12:22,130 --> 00:12:26,170 It's because most of the microcontrollers will loop through their program. 142 00:12:26,170 --> 00:12:31,650 In our current circuit this would be a great place to tell the Arduino that we want to make 143 00:12:31,650 --> 00:12:34,110 our light blink forever. 144 00:12:34,110 --> 00:12:41,550 So in pseudocode it would be something like turn light on, delay n seconds, turn light off, 145 00:12:41,550 --> 00:12:45,170 delay n seconds. 146 00:12:45,170 --> 00:12:50,460 >> Well instead of writing out that code we're just going to cheat. Just this time. 147 00:12:50,460 --> 00:12:55,640 This is actually already a code template for a blinking LED saved in our examples. 148 00:12:55,640 --> 00:13:03,350 To load it go to file, examples, choose number 1 basics, and choose blink. 149 00:13:03,350 --> 00:13:09,090 What happens here is that a new sketch window should appear with some code already inside. 150 00:13:09,090 --> 00:13:14,930 Inside of the setups body there is an Arduino helper function called pinMode. 151 00:13:14,930 --> 00:13:17,540 PinMode prepares the pin to be used. 152 00:13:17,540 --> 00:13:20,030 It accepts 2 parameters. 153 00:13:20,030 --> 00:13:24,390 First the IO pin number, which is the pin you want to utilize, 154 00:13:24,390 --> 00:13:29,910 and second, a value declaring whether the pin is used for input from the circuit 155 00:13:29,910 --> 00:13:36,050 constant value of INPUT in all capitals, or output to the circut, 156 00:13:36,050 --> 00:13:39,110 which is a constant value OUTPUT in all capitals. 157 00:13:39,110 --> 00:13:43,820 Inside of the loop there are 2 additional Arduino helper functions, 158 00:13:43,820 --> 00:13:48,840 digialWrite accepting 2 parameters and delay accepting 1 parameter. 159 00:13:48,840 --> 00:13:55,010 DigialWrite is used to interact with the pin that you configured using pinMode. 160 00:13:55,010 --> 00:13:59,730 >> The first argument is the pin number that you are interacting with. 161 00:13:59,730 --> 00:14:04,440 The second argument is a constant that is either high, meaning full voltage, 162 00:14:04,440 --> 00:14:07,080 or low, meaning no voltage. 163 00:14:07,080 --> 00:14:09,800 The second helper function is delay 164 00:14:09,800 --> 00:14:13,870 which will stop the code from running based on the amount of time in milliseconds. 165 00:14:13,870 --> 00:14:18,300 Remember 1 second is equal to 1,000 milliseconds. 166 00:14:18,300 --> 00:14:23,620 Based on our walkthrough we can deduce that if our circuit was set up correctly 167 00:14:23,620 --> 00:14:30,910 our LED should turn on and stay lit for 1 second and turn off and stay off for 1 second 168 00:14:30,910 --> 00:14:33,640 before turning it back on. 169 00:14:33,640 --> 00:14:38,580 This should repeat forever as it is currently in the loop function. 170 00:14:38,580 --> 00:14:42,340 Let's choose the upload to board button and find out. 171 00:14:48,060 --> 00:14:50,990 >> Great. So you might be wondering what's next. 172 00:14:50,990 --> 00:14:55,710 Well now that you have an understanding of everything that is needed to create 173 00:14:55,710 --> 00:15:01,030 an Arduino circuit, we can start applying knowledge gained from our lectures in CS50 174 00:15:01,030 --> 00:15:03,800 to sharpen our skills further. 175 00:15:03,800 --> 00:15:08,090 For example, what if I didn't want to use the Arduino loop function? 176 00:15:08,090 --> 00:15:11,760 What if instead I wanted to write my own type of loops and conditions 177 00:15:11,760 --> 00:15:15,870 or even create my own functions outside of the bare minimum? 178 00:15:15,870 --> 00:15:20,180 What if I wanted to play music or build a burglar alarm 179 00:15:20,180 --> 00:15:23,900 or even contact the internet with my Arduino? 180 00:15:23,900 --> 00:15:29,330 The answers to those questions are coming. So stick around. 181 00:15:29,330 --> 00:15:32,610 >> I'm Christoper Bartholomew. This is CS50.