In this video I'll introduce some new components
that will be used to construct your first circuit.
Afterwards we will step into the Arduino development environment
and learn some of it's basic features.
Finally we will code our first microcontroller program and upload it to our Arduino.
Let's get started.
>> The first component that we should familiarize ourselves with is the solderless breadboard.
This breadboard allows us to prototype or test our circuits
simply by placing the leads or component ends inside these tiny holes called sockets.
It's important to note that letters and numbers run along the perimeter of the breadboard.
This is because the sockets in each numbered row are connected
which means row 1A to row 1E, for example,
will receive the same current; however, the rows are not connected to each other.
>> The next component is the resistor which has the primary puroposes
of limiting current and dividing voltage.
We use resistors because not all components accept the same level of voltage
that the power source provides.
When a steady voltage is applied to the leads of the resistor,
the amount of current that allows to flow through it is determined by its resistance
which is measured in ohms.
So more ohms results to less current.
In order to figure out how to calculate the amount of resistance in ohms
that a resistor applies, we simply look at its color stripes
which wrap around the outer casing.
The resistance value can be read by the first 3 stripes of color.
Each color has a specified value from 0, being black, to 9, being white.
You could find more information about these values from the link provided.
There is also a fourth stripe that comes in either gold, silver, or just blank.
This gives the tolerance levels of the resistor, i.e. how closely it matches its rated resistance.
For now we can ignore the fourth stripe and set our focus on the first 3.
>> The first stripe, which is the opposite of the tolerance stripe, is the first digit.
This value can be 0 to 9.
Similarly, the second stripe is the second digit which can also have a value of 0 to 9.
But the third digit is where it becomes different.
The third digit is the number of 0's that are added to the end of the first 2 digits.
The formal name of this stripe is the multiplor.
Take for example this resistor.
We currently have an orange, orange, brown resistor.
Orange's value is 3, and brown's value is 1.
Therefore, we have a 3, 3, 0 or 330 ohm resistor.
Remember the third stripe, which is brown, is telling us only the number of 0's to be added
onto the first and second digits.
>> Finally our last component is the light-emitting diode or LED for short.
The LED is a little light that we may find in most of our electronics.
In order for an LED to emit light, current must pass through a lead in a specific direction.
But we will come back to this shortly.
For now, notice how 1 lead is longer than the other.
The longer lead is called the anode, and this is the positive terminal for the LED.
The shorter lead, which is the negative terminal, is called the cathode.
>> Now that we have a general understanding of our components,
let's build our first circuit.
When you begin building a circuit you should always unplug your Arduino from the computer.
So according to our schematic, we know that the resistor should be between
the power source, i.e. one of the Arduino's digital pins, and the anode,
the positive lead of the LED.
While the cathode, negative lead, will be connected directly to ground,
thus completing our circuit.
Unlike the LED, the direction by which we place the resistor does not matter.
Let's place one of the resistors leads in socket row 1A.
Now let's place the other lead of the resistor in a separate circuit path.
How about row 2A?
>> Great. Halfway there. Let's move on to the LED.
Per the schematic, our anode, the positive lead, must be connected to our resistor.
This means that we should place the LEDs anode in a socket that is on the same
circuit path as 1 of the resistors leads.
Let's do row 2E.
Per our schematic, we know that the cathode will go directly into the Arduinos ground pin.
So we can place the cathode into row 3E.
>> Great. The final part to our schematic is simply using these jumper cables
to connect to our Arduino, thus completing the circuit.
Let's start by making the connection from the cathode to the Arduinos ground.
To do this, we simply plug the jumper cable into any of the sockets
which share the same A to E row of the cathode.
In this case we'll plug 1 end of the jumper cable directly into row 3A.
The other plug will go into 1 of the grounded or GRD digital pins of the Arduino.
As for the second cable, according to our schematic we will make a connection
from our resistor to our power source which is 1 of the digital pins on the Arduino.
We already know that 1 end of the resistor is connected to the LEDs anode.
So this leaves us with only 1 option, row 1 sockets B through E.
Let's give ourselves some room between our components.
Let's plug 1 end of the jumper cable in row 1E.
Finally, plug the other end of this jumper cable in digital pin 13.
Remember this pin. It will be very important soon.
>> Well the circuit looks pretty, but we want it to do something.
Let's crack our knuckles and get down to business
writing our first microcontroller program.
First plug the square USB end into the Arduino.
In order to start writing our own program,
we will need to access the Arduino integrated development environment,
which I will refer to as the IDE.
To do this click on the appliance menu at the bottom lefthand of the screen.
Go to programming and select Arduino from this menu.
If the Arduino software is not currently installed you can easily install it by
opening a terminal and typing the following command:
Sudo yum install arduino.
You will need to restart the appliance when it completes.
So once you launch the IDE, the first thing you should check
is if the Arduino IDE is registering or seeing your Arduino device.
You can do this by simply going to the tools menu, hover over serial port,
and there should be at least 3 devices listed.
If it is not checked already, do make sure you check the /dev/ttyacm0
as this is where you Arduino is plugged into.
>> When you first open the Arduino IDE a new project, which is called a Sketch,
opens up automatically.
This area will be used to place our coding.
At the bottom of the screen there is a terminal window responsible for outputing information
such as complilation response codes or syntax errors in your code.
At the top of the screen just below the file menu, there are a series of icons
that we should be acquainted with.
Starting from the far left, there is an icon that resembles a check.
This button is called verify, and its responsible for compiling your code
while validating the correctness of your program syntax.
The button after verify, which resembles that of a sideways arrow pointing to the right,
is the upload command.
The upload command is resonsible for sending the programs compiled 1's and 0's
over to your microcontroller for it to be saved on the board.
Keep in mind that the verify button will not upload your code.
The next 3 buttons are new, open, and save respectively.
The final button to the far right of this menu is called the serial monitor,
and it acts as a consult whereby programmers can configure the Arduino to read as the input
or display as the output to and from the serial monitor.
We'll come back to the serial monitor in another video.
>> For now let's start writing our program.
Now starting to write an Arduino program slightly differs from regular C programs.
This is because an Arduino needs, at a bare minimum, 2 specific void funtions defined.
Setup and loop.
Arduino makes it very easy to get started by utilizing example code templates
which come with the IDE.
To load our bare minimum, simply go to the file menu, examples, choose number 1 basics,
and click on bare minimum.
A new sketch window should appear.
Loading the templated code.
Let's briefly go over these 2 functions.
The setup function is similar to main as it is the first function to run,
and it only runs once.
Setup is used for defining which pins will be input or output.
For example, this would be a great place to tell the Arduino that we want to output
some electrical current over to pin number 13.
Loop is a function that runs continuously on the microcontroller.
Ever wonder why your alarm clock never stops?
It's because most of the microcontrollers will loop through their program.
In our current circuit this would be a great place to tell the Arduino that we want to make
our light blink forever.
So in pseudocode it would be something like turn light on, delay n seconds, turn light off,
delay n seconds.
>> Well instead of writing out that code we're just going to cheat. Just this time.
This is actually already a code template for a blinking LED saved in our examples.
To load it go to file, examples, choose number 1 basics, and choose blink.
What happens here is that a new sketch window should appear with some code already inside.
Inside of the setups body there is an Arduino helper function called pinMode.
PinMode prepares the pin to be used.
It accepts 2 parameters.
First the IO pin number, which is the pin you want to utilize,
and second, a value declaring whether the pin is used for input from the circuit
constant value of INPUT in all capitals, or output to the circut,
which is a constant value OUTPUT in all capitals.
Inside of the loop there are 2 additional Arduino helper functions,
digialWrite accepting 2 parameters and delay accepting 1 parameter.
DigialWrite is used to interact with the pin that you configured using pinMode.
>> The first argument is the pin number that you are interacting with.
The second argument is a constant that is either high, meaning full voltage,
or low, meaning no voltage.
The second helper function is delay
which will stop the code from running based on the amount of time in milliseconds.
Remember 1 second is equal to 1,000 milliseconds.
Based on our walkthrough we can deduce that if our circuit was set up correctly
our LED should turn on and stay lit for 1 second and turn off and stay off for 1 second
before turning it back on.
This should repeat forever as it is currently in the loop function.
Let's choose the upload to board button and find out.
>> Great. So you might be wondering what's next.
Well now that you have an understanding of everything that is needed to create
an Arduino circuit, we can start applying knowledge gained from our lectures in CS50
to sharpen our skills further.
For example, what if I didn't want to use the Arduino loop function?
What if instead I wanted to write my own type of loops and conditions
or even create my own functions outside of the bare minimum?
What if I wanted to play music or build a burglar alarm
or even contact the internet with my Arduino?
The answers to those questions are coming. So stick around.
>> I'm Christoper Bartholomew. This is CS50.