are you trying to figure out how to
use arduino to build something cool well hey you're in luck because in this single
workshop you're going to learn everything you need to get started with arduino by the time
you're done here you're going to know what hardware to get and why you're going to
know the ins and outs of an arduino board you're going to understand which software to get
you're going to know the key arduino programming functions to control electronic stuff and if
you've never programmed before you're going to learn the key coding structures found in
all programming languages plus you'll learn the best follow-on training to this workshop
to really accelerate your skills let's go before we start please do subscribe to our
youtube channel it doesn't cost you anything but a click but it really helps us bring you
great content like this arduino workshop huge thank you to altium for sponsoring this training
if you want to get a free trial of the super powerful altium 365 software check the link in the
description thanks a ton i really appreciate that okay so what the heck is this thing called arduino
well arduino is a tool that helps you control electronic stuff with code so if you're building
a house you'd probably go grab a hammer well if you're gonna build a project using electronic
stuff grab yourself an arduino but what do i mean exactly by electronic stuff well let's make
two big general groups inputs and outputs inputs are devices that gather information and outputs or
devices that have some action in the world so for inputs think of things like temperature sensors
light sensors touch sensors flex sensors humidity sensors infrared sensor distance sensors just to
name a few then you have things like outputs dc motors stepper motors servo motors leds led strips
buzzers speakers lcd displays oled displays the list goes on what arduino can do is stand in
the middle of these input and output devices and control them with some cool logic that you get
to create so arduino can be used to read inputs and control outputs okay so now you have a
general idea of what arduino is it's a tool to control electronic stuff great okay but how do
you actually get started using arduino what do you have to do so here's what you're going to need
we're going to talk about the big three it's what i like to call the arduino trifecta you've got
arduino hardware the arduino ide and the actual arduino code that you're going to need to write
so first we have the physical component of arduino which is called an arduino board there's a bunch
of different types of arduino boards out there when someone says arduino board it could mean a
number of these different circuit boards in just a little bit i'm going to tell you exactly which
one to go buy if you haven't purchased one yet and here's some quick good news if you have bought
one already it's probably gonna work just fine now all arduino boards share one thing in
common they have a micro controller on them a microcontroller is basically a really small
computer so when you use an arduino you're really just using a microcontroller the microcontroller
is what enables us to read those different inputs and control those outputs so again when somebody
says arduino board they're talking about something physical it's this printed circuit board and it's
got some electrical components on it we'll dive into this in a little bit okay so you're gonna
need an arduino board the next thing you're going to need is the arduino ide that is the software
that you will use to actually write the code that is then going to get loaded onto the
arduino board itself ide stands for integrated development environment in just a moment we're
going to walk through downloading the arduino ide to your computer it's totally free it's
pretty darn easy to use and it's an amazing tool that's going to get you up and running
super fast alright the third part of this arduino trifecta is the arduino code itself you're going
to need to write some code inside the arduino ide that is ultimately going to get loaded onto the
microcontroller that's on the arduino board you're using so the arduino code that you write is called
a sketch so that's like a little bit of jargon there right so normally you write a program
well arduino they call it a sketch and this arduino code itself is basically c and c plus plus
programming languages but it's got some arduino specific functions and structure so when you
program an arduino you're basically programming in c and c plus plus programming languages now
c code is blazingly fast it is used all over the place from mission critical code on spaceships
to gpus running machine learning algorithms if you are planning on learning to program arduino
and this is your first programming language you should be super stoked because everything you
learn in arduino is going to help you understand other programming languages it's not like
arduino some weird esoteric coding language that has nothing similar to other languages and
it's going to give you a step up on learning to code other languages like python java or whatever
you want to take on so those are the three things you're going to need an arduino board you're going
to need the arduino ide and then you'll have to figure out how to write the code that's going to
control this electronic stuff so we are going to be diving into all three of those things i hope
you're pumped and excited to start learning a ton so up next we're going to talk about what kind
of arduino board you should get and why if you're looking for electronic parts for your design
you're going to want to check out octopart it is a search engine for electronic components
and i'm telling you it's pretty darn amazing it's like google for electronic components if you
want to find and compare parts quickly there is no comparison you know when you're designing
something you don't want to get dragged down by some clunky user interface trying to find the
right component octopart lets you keep your design momentum going through moments when you used to
stop to research components with every search you get super important supply chain information like
low stock levels and even life cycle validation checks octopart is even integrated with altium
365 so you can get real-time insight as you design make sure to check out octopart.com for
finding your next electronic part all right so you need to go buy an arduino board but there
are a ton of options which one or ones are you supposed to go buy to complicate things further
there's a ton of boards out there that say arduino compatible what does that even mean are they going
to work with the arduino ide in the arduino code okay if you have not yet bought an arduino board i
would recommend buying an arduino uno rev 3 if you already have an arduino board and it's not an uno
don't worry about it it's probably going to work just fine and we'll talk about why in a moment
so why do i recommend buying an arduino uno reb3 well here's the deal the arduino uno is the
best board for getting started with arduino it's not the best board for all applications but
when you are learning to use the arduino hardware the arduino ide and the code all at once then
sticking with the arduino uno is going to be your path of least resistance and here's why it's super
popular so you're going to find tons of code out there and training specifically for it it has 20
different general purpose input output pins called gpio for reading inputs and controlling outputs
and this is plenty pins for most applications plus the board layout itself is kind of the
standard that's used by most arduino accessories called shields the only real annoying thing
about the arduino uno is that you'll need a type a b usb cable like the kind you use with
lots of printers to connect it to your computer so if you don't have one yet go get an arduino
uno rev 3 and i would recommend for your first one actually getting it from the arduino website it's
a great way to support the arduino organization oh crud but wait i already bought an arduino and it's
not an arduino uno rev 3 i got a mega or a nano or this thing that doesn't even say arduino on it but
it sure looks like an arduino uno will these work well here's the good news the chances are whatever
board you bought is probably going to work just fine and that is one of the amazing things about
arduino and i want to take just a quick second to explain something that eluded me for like
ever but hopefully it'll make sense to you so arduino is the name of a company they started
making arduino boards and they made the arduino ide and they helped make the underlying arduino
language but everything they did was open source so the hardware like the actual design of the
board is open source hardware and what that means is that anybody or company can use the exact same
design and change it if they want to and start selling boards on their own so that's exactly what
has happened and tons of people and companies have started selling boards that do the exact same
thing as an arduino board does or sometimes they do additional things many times they do additional
interesting things so the arduino company and all types of other companies sell tons of different
boards that all do slightly different things like think of a car company that sells different types
of vehicles maybe it sells a minivan or an suvs sedans or trucks each one serves a different
use case but for the most part they can all get you where you want to go but it's not just the
hardware that's open source it's also the arduino ide the arduino ide again that's the software that
you use to program the arduino is totally open source and it's actively developed and supported
by the arduino company and the arduino community but here's what's crazy the arduino ide will
work with these other companies boards just fine so any board that you get that says it's arduino
compatible will very likely work just fine with the arduino ide but here's what's even crazier all
the code that you write for one board is in most cases going to work fine on another board with
maybe some slight modifications so if you wrote code for your arduino uno but later want to switch
it to a smaller form factor like an arduino nano it's not like you need to rewrite all of your
code at most you might need to adjust a couple lines of code and you're gonna be ready to roll
okay i know that was actually quite a bit there hopefully you've kind of got a bigger picture of
what arduino compatible actually means and again i recommend getting yourself an arduino uno r3 if
you're just getting started now what if you don't have any cash at all like zero cash but you still
want to start playing around with arduino well you actually have a free simulator option you can get
access to a really nice free arduino simulator at a website called tinkercad.com tinkercad is
created by autodesk and they have built a really nice arduino simulator so you can use an arduino
uno board you can build small circuits test them you can write code right inside the simulator it's
pretty darn cool a lot of people find they like it a ton now it does have some limits like you're
not going to find all the different parts in there and there's not a ton of different arduino boards
you can use but for just getting started it is a great place to go if you're strapped for cash so
now let's take a look at an arduino board and talk about the stuff that's on it like have you ever
rented a car and you check out its features like what side you put the fuel in where do you adjust
the mirrors like that kind of thing that's what we're gonna do here so what i've got is an arduino
uno here this is what we're going to use as our base for talking right first thing first the whole
thing together is called a circuit board yeah i know you probably know that but hey just in case
the most important thing on this circuit board is this big black rectangle that has a bunch of metal
prongs sticking out this is the micro controller and again it is the most important part of the
arduino board in fact an arduino board is a micro controller development board it's designed for
you to take advantage of all the cool features on that microcontroller now depending on the board
you have the micro controller might look a little different it could be smaller and the metal prongs
may be really tiny not to mention it could be a completely different microcontroller than the
one on the arduino uno and just as a reminder the microcontroller is the brains of the operation
here it's what's going to be taking the code you write and applying the logic so those metal prongs
i was talking about those are called pins these pins are how the microcontroller communicates to
the world many of the pins can be configured or like set up right to be inputs or outputs for
reading sensor values or for controlling stuff and those pins are called the gpio that stands
for general purpose input output now the way the output pins work is by adjusting the voltage at
the pin the way the input pins work is by reading a voltage applied at the pin now if you look at
the board like the periphery you'll notice these plastic columns filled with holes these are
called pin headers or just headers and they come in different shapes and sizes but a lot of them
look just like this each one of the holes makes an electrical connection to one of the pins that
we were just talking about on the microcontroller and those holes are designed to make an electrical
connection with wires and components so you can stick something into that hole like a wire or
resistor or something like that and you don't have to solder stuff to the microcontroller you can
just make a temporary connection using this pin header now let's say you have an input device like
a button you can hook it directly into one of the pins on the arduino board or if you have an output
device like an led you can connect that directly to the arduino board as well oftentimes people use
a breadboard when they're working with an arduino that's a place where you don't have to solder your
circuit together you can use these little wires called jumper wires to make different connections
they're super handy if you don't have one i would recommend getting one of those as well now
depending on the arduino board you have the pin headers may be labeled let's take a close look at
this arduino uno which has labeled headers on one side we have the digital pins labeled 0 through
13. these pins can be used for reading on off type inputs like if a button is being pressed or
if it's not being pressed we'd call that a binary input not only can they read inputs but they can
also act as outputs which means they can source voltage so if you turn a pin on which is called
setting it high then the pin can source 5 volts if you turn it off called setting the pin low then
it can source zero volts and what this allows you to do is control different electronics like leds
buzzers and it also allows you to communicate with other devices using different protocols now
some of these pins have special purposes for example pins 0 and 1 are used for communication
with the usb port they're marked with a tx for transmit and an rx for receive there's also two
tiny leds on the board marked tx and rx and those leds will flash on and off when signals are being
transmitted on those two pins some pins also have a little squiggly line next to them those little
squiggles mean that the pin is capable of pulse width modulation or pwm and what pwm does is turn
the voltage high and low at different frequencies and this is useful for driving inertial loads like
motors and also for fading leds on and off as well as controlling things like servo motors now if you
look at the other set of pin headers on the other side of the board you'll see two sections one
marked for analog in and one for power the analog in section has six holes and these are places
where you can connect and read analog inputs so before remember we said the button is either on
or off that binary input well analog is when you have a continuous signal and there's lots of
sensors out there that have analog outputs to read those analog outputs you can use these analog
input pins and that's because the microcontroller has an analog to digital converter on it called
an adc these pins right here are connected to the microcontroller's adc an example of a sensor that
might output a variable voltage is a temperature sensor so as the temperature changes in the room
or wherever the voltage at the output pin of the temperature sensor is going to adjust and if you
have that output pin connected to the analog input pin then you can read that variable voltage and
then use code to translate it from a voltage to an actual temperature next to the analog
in section we have the power section here you'll find two pins marked g and d gnd stands for
ground and that's the lowest voltage on the board you'll also see a 5v and a 3.3 v pin the v
stands for voltage which you probably guessed and you can use these as a voltage source for
small components you connect to the arduino board as long as they don't draw too much current now
we're going to skip all the other pins for now and talk briefly about how you can power this arduino
uno there's generally three recommended ways you can provide power through the usb port you know
just by like connecting the board to your computer or you can power it through the dc jack so that
dc jack there is a 2.1 millimeter center positive plug and you can connect a power supply to it
like say five double a batteries or one of those wall wart supplies the voltage limit on this dc
jack is about 7 to 20 volts but you're better off limiting your voltage to about 12 volts and
that's because there's a voltage regulator on the arduino board and if you supply a ton of
voltage then it has to dissipate a bunch of heat and that's just lost power and excess heat
that you don't need so i'd limit it to a 12 volt output i'd also recommend a minimum output
current of one amp that's a thousand milliamps more is okay but less can be an issue if you start
hooking up a bunch of stuff to your arduino board now if you look at that power section again
on the pin headers you'll see a pin marked v in this is where you can hook up an external
voltage source to the board in order to power it this has the same limits as the dc jack so we're
talking about that 7 to 20 volts again limiting it to 12 volts is a good idea the final pin i want
to talk about is the reset pin if you apply a low voltage to this pin it will reset the arduino
board same thing with that reset button when you press it that's going to reset the board as well
and that's like turning the power off on like some electric device right you just turn it off turn
it back on again which is totally fine to do with an arduino board all right i know that was a ton
you probably have more questions than answers the truth is there is a ton to learn we just barely
scratch the surface with all the stuff that's going on with an arduino board but hopefully that
gives you a taste of the most important things you need to know on the arduino board right now you're
going to have a basic footing so that we can start writing some code and actually make this arduino
board do some stuff now in order to get code onto this arduino board we're going to be using
the arduino ide next what we'll do is get the arduino ide set up so that we can start writing
code and get it loaded onto the arduino board we'll run through a couple example sketches just
to give you an idea of how this whole thing works all right so what i've done is navigate to the
arduino website that's just arduino.cc and then i clicked on this software navigation tab and
that takes me to this page where it shows kind of a bunch of different options for different
ides so as a reminder ide stands for integrated development environment and it's simply a software
tool that you're going to use to write your code in and it's also going to help you load the code
into an actual arduino board it's a really handy tool and all the options on the arduino website
are free and when i say options you actually have three different options on this website for
what ide you want to use they have the web editor they have the original arduino ide which is
arduino ide 1 and then they have the future version of the arduino ide which is arduino ide 2.
now arduino ide 2 is still in development as we're speaking but it's definitely working and you can
download it and check it out i think it's a great ide i'm not positive yet but i actually think
they're going to keep both of these ides around for a long time so i don't think ide 1 is going
away i think it's got a lot of great features it's really simple to use nothing really gets in your
way not to say that arduino ide 2 isn't great also but it has a bit more going on than arduino ide
one now arduino ide one and two are both downloads that you would download onto your computer you
know load onto your hard drive and use but they also have a web editor that you can set up and so
it doesn't matter what computer you're on you can write code for your arduino and you can upload the
code that you write in the web editor onto your arduino boards with your computer what's great
about the web editor is if you have a chromebook or maybe you're in an educational environment a
lot of people have chromebooks then you can use this web editor on those chromebooks so let's go
through the process of downloading this arduino ide one it's gonna be the same for arduino ide two
but let's just do this so i'm on a windows machine click this you have the option to donate
i'm just gonna download it for now i'm going to download it to downloads i'll double
click the exe file i want it to make changes something about selling your first kid
i'm just going to leave all these checked i'm just going to leave it as this default
destination folder under program files all right now it's completed i'll go ahead and
hit close all right now i've got a shortcut right on my desktop here if i click this it's going
to open up the arduino ide i'll allow access all right here we go this is not super duper
fancy but this is it right here so when you open up the arduino ide it opens up a new
sketch it creates a new sketch for you with the date in there and it fills it with
two functions void setup and void loop which you'll learn about here shortly but what's
really important is about where these things get saved so what i'm going to do is i'm just
going to go ahead and save this file save all right so i'm saving this the file name is
going to be sub sandwich i'm going to hit save so now what i want to do is show you where
that actually gets saved so i'm going to go to my documents and if we come to documents when
you install the arduino ide it creates a folder called arduino and if you open that folder
up there's going to be some files inside here so libraries is where all the libraries that you
save will go and all the other sketches you save it's going to create a folder with the name of
the sketch so we save sub sandwich and if you open that it's going to have a file in here and
you can see the file name is also sub sandwich now the file extension is dot eno old file extensions
are dot pde but dot eno is what you'll see in this folder named arduino this is called your
sketchbook folder so if you go to file sketchbook you'll see here's two programs in here now i had
already downloaded the arduino ide and i had a little simple write program so that's why this
shows up but if you want to get to the programs you've written which are called sketches in
arduino you would just go to sketchbook and there they would show up but let's go to file and let's
go down to preferences and i want to show you this right here this is where you set your sketchbook
location right now mine is under users mica documents and then arduino and you can change that
here if you want you can also change the font size like we could make the font size bigger which i
usually do so we'll make that 18. you can display line numbers if you want so you'll see this when
i save this here but there's some important stuff that takes place in here we won't talk about
all of it right now but i'm just gonna hit ok and now you can see we see the line numbers okay
cool all right so let's go ahead and check out one of the cool features of the arduino ide and
that is all the example sketches in here so i'm going to go to file and i highly recommend
you do this as well and i'm going to go to examples and in the examples they have a bunch
of example programs a bunch of example code that you can play around with so i'm going
to go to basics and i'm going to go to blink it's going to open up a new program new sketch
here and they've got some comments up here comments are things that tell you about the
program but they there it's not actually code it's just more like help or text for you to understand
what's going on and blank turns an led on and off so we won't talk about this code right
now but i just want to show you how to check to see if there's any errors in the
code and then how to actually upload it onto an arduino board so the first thing i'm going
to do is i'm going to take my arduino uno and i'm actually going to plug it in with that usb
cable to my computer so i'm going to plug it in now my computer made a little noise like oh hey i
recognized something yours may or may not but mine does and then i'm going to go up to tools board
and i'm going to select which board i have now i have an arduino uno so it already recognized
that for me so i'm just going to click that we also need to set the port so here's the port
it's going to show me i have com4 hooked up i have an arduino uno maybe i have different things
shown here right but there might be multiple ports listed here i'm just going to go with this
one right here so i'm going to click that so now that i have my board and my port selected
i'll be able to upload this code onto my arduino now if you look in the bottom right of the
arduino ide it'll tell you what board you have and what port it's on and then this space right
here in this black space this council space it's going to show you if you have any errors or
anything like that so the first thing i'm going to do is hit this little check mark this is
the verify and what it's doing is it's checking your sketch for any errors now since this is an
example sketch we're not going to see any errors so we get a little message down here about how
much space it's using and we're doing just fine so now i want to actually get it onto my arduino
board so i'm going to go ahead and hit upload now when i hit upload now it's actually
uploading it to my arduino board and if you look at your board while it's uploading you'll see
the tx and rx lights start blinking now what this program does is it blinks an on-board led on the
arduino uno so most arduinos they have an led on the board somewhere usually it's a pin 13 but not
always and as you can see right now we're blinking the led so we are able to successfully upload this
to the to the board and so it's on for a second and off for a second but if let's say we wanted to
speed that up we could mess around with this delay function and we'll talk more about this later so
don't get too worried but i just want to just show you how how this can work so i'm just going to
make that 100 and this let's make it 75 and then i'm gonna go ahead and upload it again so now i
can see the led is blinking much faster and it's not off as long as it is on so it's kind of a neat
little effect okay so that's uploading code to the board that's not too crazy right so that's about
everything you need to know on a very basic level about how to get code loaded onto your arduino
board i highly recommend going into file examples and just walk through each of these examples see
the circuit they talk about setting up and then playing around with those circuits just doing that
is really going to help you find your way around alright so this is arduino ide 1.0 for
kicks let's check out arduino ide 2.0 all right the license agreement i'm going
to install it for everybody uses a computer and i want to say yep that's
okay again selling my children now what's nice is you can have arduino
ide 1 and arduino ide 2 installed on your computer at the same time no
issues there so i'll get hit install and then i'll just click finish all right and
it automatically opens the arduino ide 2.0 for you now i had already had it installed you might
have to click to install some drivers while you go through that process but it's just acknowledging
that some drivers are getting installed and it looks pretty darn similar doesn't it
so you're going to have file and preferences and you'll notice that the sketchbook location
is the same for ide1 and for ide 2. so when you save a sketch in idd2 it's going to also
you'll also be able to access it from ide1 no problem at all okay and you can also you
can change the theme to a different you know colored theme in there too if you wanted
to do that kind of fun to do i'll just keep it with this theme now if i want to connect a
board i can just go to tools board and select my board notice this is arduino avr
boards there's different types of boards that you can have show up here we won't
cover how to show the different boards but got a really big list here and i'm just going down
saying yep arduino uno and then i go back to tools port and the only port is showing me is currently
this com4 arduino uno port so i'll select that and then you'll notice just right here it's going
to show me my options if i had other boards here it would show me those and then again down in
the bottom right it's telling me what board i'm connected to and the port that i'm on the verify
button is the exact same and the upload button is also the same so i can go to file sketchbook
i can see there was that sub sandwich sketch that we had saved i can go to examples and see
all those examples i'll just go to blink again now in arduino id 2.0 this is going to open up
a new window so go ahead and open this up it's the same sketch nothing different there and just
verify that yep this is the port i've got selected and i can go ahead and upload and the output
shows down here all right and that's pretty much arduino ide 2.0 so pretty darn similar huh not
too much different there now let's talk about the web editor so go ahead and click code
online and what you have to do is set up an account i've already done that all right so here
i am now i'm logged in to the arduino web editor but in order to actually upload a sketch with
the usb port you have to install some software it's the arduino agent so this little thing pops
up i'm just going to go ahead and click that and it's going to walk me through setting it up making
sure it's installed so i'm going to install the agent and then select for windows 32 or windows
64. i've got a win64 so i'll download for that i'll double click the installer just hit some next yep i'm gonna accept selling my kiddos that
looks like a good installation directory all right i am planning on using this with google
chrome or firefox so i'm i'm not gonna click yes this is these are the two i plan on working it
on so here we go and now i'll hit next it's going to go ahead and install that and then it launches
the agent it just kind of runs in the background finish it now when you look down at
your toolbar you'll see this little arduino looking thing and this is the agent right
here so you can actually click this it's going to take you right to the web ide if you go to
the create it'll open up a tab for you and what we're going to be working with is
the web editor all right so now i'm i'm all connected all right so now what i do if i
want to select the board is i just click here and it's going to show me the boards i
have so here's an arduino uno i select that cool and then over here it's got the different
things so i can go to examples basics blank here's that sketch that we just saw on our
desktop same thing here and then i'll click upload same thing happens the tx and rx lights turn on
and off and i have successfully uploaded the board so same thing here and when you save sketches they
get saved over in your sketchbook folder but this is an online sketchbook folder so you know i've
used this before so we've got some code in here but it just gets saved over in here and you can
organize it you can access these from anywhere so that's pretty cool so which one of these are
you supposed to use well here's the good news it doesn't really matter just pick one and go with
it now if you are on a chromebook computer then you're gonna have to go with that arduino web
editor but otherwise you know ide ide1 ide 2 it doesn't really matter there's also other editors
out there that you know we're not going to talk about right now that you could write this code on
for example vs code you could use platform io the code's going to remain the same it's just a matter
which ide you feel most comfortable programming in all right here we are in the arduino ide
and this is the stuff we're going to go over how every arduino program is laid out how to
use variables how to use control structures and we're going to be talking about all the
most important arduino specific functions as we go through this so the first thing we're
going to do is go ahead and open up a new sketch we're going to talk about how an arduino program
is laid out so here's a new sketch and what do you notice there's some words here some stuff well
this stuff these are functions and the reason we know their functions is they've got these
open and closing so here's an open and here's a closing parenthesis after a word so that kind
of setup when you see an open closing parenthesis maybe some stuff would be in here that's going
to give it away like hey this is a function so the name of this function is called setup it has
an opening curly brace and a closing curly brace same thing with this function this function is
called loop and it also has an opening and closing curly brace now you can forget this word void
don't worry about that but these two functions set up in loop need to be in every single
arduino program you write if we read the information that's in here it says put your setup
code here to run once and in loop it says put your main code here to run repeatedly well what does it
mean to run code what are they talking about well that just means that if we write any code in here
it's going to execute the code and it's going to execute the code from the top to the bottom so if
we have code 1 code two it's gonna execute code one first and then the next line of code code two
and it's gonna go on until it gets to the end and once it gets to the end setup is over and then
it moves on to the loop so setup runs once and the code you'll put in here is stuff that only
needs to run once it's like code that's going to set up the other part of your program now
the loop it's going to run over and over again so in loop it also starts at the top and it's
going to work its way down first it's going to execute this code on line 9 and then this code on
line 10. of course this is in code this is just gibberish but i'm just trying to give you an idea
then it gets to the bottom curly brace and when it gets here it starts back up at the top and it runs
this code step by step and it's going to do that forever that is it just repeats the same thing
over and over and over until either you remove power from the arduino board or i don't know
the universe ends in heat death now you might be thinking wait a second how can a program
that does the same thing over and over again actually do anything really useful doesn't
that kind of limit it to doing simple stuff now first blush you are totally right but what we
can do as programmers and where it really gets fun is we can use variables and control structures to
change how the program reacts and so you're able to get extremely creative diverse logic that can
control things from graphical user interfaces to countdowns to just about anything you can dream
up so these functions void setup and void loop are extremely important they are going to be
in every single one of your arduino programs in fact if you leave one of these
functions out you're going to get an error so now we don't have the loop function let me
verify and we should see an error down here and we get an undefined reference
to loop which is just saying hey man stick that loop in there okay so this is how
every arduino program is laid out with setup and loop all right so now let's talk about variables
these are super awesome tools that you're going to be using in just about every program you write a
variable is like a bucket that allows you to store information in it like say we wanted to measure
the temperature with a temperature sensor when you store the value from the temperature
sensor you would put it into a variable making a variable is simple and storing information in
a variable is really simple you need four things you need a type a name an assignment operator
which is just an equal sign and then the value that you're actually going to store into the
variable so let's go through each of these items again that's type a name an assignment
operator equal sign and then an actual value the type it's also called a data type is the kind
of information that you're actually going to store in the variable and there's a bunch of different
data types and i have them listed right down here if you want to pause the video you can take a look
at them so you've got boolean types those are like true false values so with a boolean there's only
two options it's either going to be true or false zero or one but technically zero is false and any
number other than zero would be evaluated to true and then you can also have high and low so high
is true and low is false so those are examples of boolean variables then you have a byte that's
just a small number from 0 to 255. so a lot of times when we're referring to those pin numbers on
the arduino i'll save those as a byte because it's just a small number and it doesn't take up a lot
of memory so the more the variable can store the more memory it takes and a byte is a pretty
darn small variable and that's why i use it for something like a pin number larger numbers
are can be integers and that type is just int just like up here boolean would be b-o-o-l so that
is the type you would write out before the name so integers can store negative values to negative 32
000 and some change up to 32 000 and some change and then for bigger numbers you can use a long
and you just type the word long and it's a longer number it's like all the way up to 2 billion and
some change and all the way down to negative 2 billion and some change that's a huge number and
then a float is going to be a number that has a decimal point and then if you want to store a
character something that's going to actually be displayed on text like say on an lcd screen or
a computer monitor you'd store it as a character and you store single characters as c-h-a-r
car stands for character and you use single quotes and then if you want to hold a bunch
of characters like let's say you're storing somebody's name or or maybe the name of your
wi-fi network you would use a character array and that's going to be name and then these
open and closing brackets these square brackets and then you use double quotes for storing that
whole line of text right there and this is what we'd call a string now it's different than capital
s string that you'll see in the arduino language but we're not going to be talking about that
now we're just gonna say uh we're just gonna be using character arrays to store strings so that's
what i mean when i say type what about the name well the name of the variable is just
how you're gonna refer to it in your code variable names can contain letters numbers and
underscores but they can't start with a number and you can't use emojis in your names bomber but
you can use descriptive names for your variables so using something like current temperature
would be more clear than just writing the letter c t or something like that now to actually
store a value into the variable you need to use an equal sign which is called the assignment operator
so here it is right here the assignment operator throw out all the algebra you know
because this has nothing to do with that when you see this it says take the value over
here and store it into this variable's bucket that's what it's saying so it's going to evaluate
this and this doesn't have to just be a value this could be an expression over here so it's going to
evaluate this expression and save it right here so let's say i had two variables here one is
called taserblast and it's set equal to eight and then the other one is called pain
threshold and notice i set it equal to this expression right so this is taser blast so
this is referring to this variable so this is the number eight and we're saying well what's eight
divided by two that would be 4 right so now pain threshold would be holding the value 4. now to set
up a variable the first time all you really need to use is a declaration so that is the type and
the name so we could do something like taser blast that would be a declaration and once you've
declared it then you can use it in your code just by using the name so then we could do something
like this we could say taserblast equals eight we don't have to include the type anymore because
we've already declared the type so now the program knows oh hey taserblast is an integer it's a
variable that stores an integer and here we are assigning the value 8 to it but you can also
do that all in one line and that's a declaration and initialization all in one so taser blast
equal eight so here we are defining it and we are initializing it to eight now finally one thing
you might be wondering here is what about this semicolon here you see a bunch of semicolons right
well any time you've finished with the statement in arduino that is you're like finished with your
line of code then you end it with a semicolon so a lot of programming languages they use spaces
between lines to delineate one statement of code from the next but arduino which is based on c
and c plus is not one of those languages it uses semicolons so for example we could break this line
up into this right here so notice how these are on two different lines but since the semicolon is
here when we verify the program the compiler which is part of the ide which is going to be checking
for our code looking at our code for errors it's going to start on line 5 but it's not going
to see an end of this statement until it gets to that semicolon so this is fine in fact this is
fine but normally you'd see it like this okay so variables they're like special purpose
buckets and they can hold and store values let's look at an example program that uses
some variables so i'm going to go to file examples basics fade and let me clean this up
real quick just so we can see a little bit better alright so check this out we've got three
variables up at the top above setup and when a variable is above setup it means it can be used
by any function inside the program so it's called global when it's up here so like i said here's
three variables and we see that they are declaring and initializing each one of these variables so
all of them are of what type integer right and then each one has a name so one's called led one's
called brightness and one is called fade amount they assign the value nine to led and here
it says the pwm pin the led is attached to so they have a circuit that might look
something like this they've got an led one side of the led is connected through
a resistor to pin 9 which is capable of doing pulse width modulation the other side of the
resistor with the short leg is connected to ground then they have a variable called brightness
set to zero and a variable called fade amount set to five so let's just take a look at the
rest of this program we're going to find some really interesting functions in here so here's
setup you'll recall setup runs once and there is a really important function inside setup here
called pin mode so if you'll recall the pins on the microcontroller are how they interact with
the outside world and we can access those pins through the headers on the arduino board right if
we want them to be inputs we need to use this pin mode function to set them as inputs if we want
those pins to be outputs like turning on an led we need to set those pins to be outputs and the
way we do that is in setup because it only needs to happen once and we use this function called
pin mode now you should notice that pin mode is a function right because it's got a name and
then it's got these open opening and closing parentheses you also notice that it changes to a
specific color and this means that it's an arduino specific function it's like a special function
that arduino is using and pin mode takes two values it said that you pass in values to this
function the first value is led so what was led well that's a variable name and we had set it
equal to nine so we're passing the value nine to pin mode and then we have a comma and
then we're passing this keyword output and what this is telling the arduino is hey pin 9
on that microcontroller needs to be set as an output that way it can source voltage so pin
mode super important arduino function so what's the first thing that happens in the loop well we
come to another really important arduino specific function called analog write we know it's arduino
specific because it turns orange like this or it highlights in a specific way now depending on the
ide you're using maybe it doesn't highlight but anyway you know it's a function because it's got
these open and closing parentheses and you'll notice we pass in some variables some values here
now what analog write does is for the pins that are capable of doing pwm pulse width modulation
it sets the duty cycle so if you recall with pwm we're adjusting the on and off time of that pin
very rapidly and we can adjust the average voltage of the pin between 0 and 5 volts but it's on a
scale and it goes from zero to 255. so this first value led is referring to the pin number that we
want to apply the pwm at so what was led oh that's right we set it to nine so this is the value nines
we're saying hey pin 9 we're going to pwmu and now it says hey well what is the amount you want to
set the pwm tube somewhere between 0 to 255. if we set it to 0 this would have an average voltage
of 0. if we set it to 255 the average voltage would be 5 volts and somewhere in between there
you know like what 127 or something like that that would be two and a half volts so you can use
anywhere in that range and again it's adjusting the actual duty cycle so the on and off cycles of
the pin all right again super important function analog write arduino specific you can use it
to set pwm for a pin okay so we've talked about pin mode and analog right so far now what's
this next line of code here well look at this we're doing another assignment so we're saying
hey brightness that was a variable we made right what was this brightness was equal to zero so it
says brightness is equal to brightness plus fade amount that doesn't that look like weird algebra
well remember it's not algebra the assignment operator is saying take the stuff on the right
side of the equal sign evaluate it and then store it in this variable so here we have brightness
well what is brightness well it was zero right so this is zero plus another variable fade
amount what's this well it was set at five so what's zero plus five that gives
us five right so now brightness is now five so brightness is five okay that's
interesting so on line thirteen we were pwming pin nine and we're saying the brightness was zero
so that led would be off so now what we need to do is start talking about control structures a
control structure allows you to adjust the flow of your code if we go to the arduino reference
page and we go to structure when i'm talking about control structures i'm talking about these
right here so we have an if statement force else whiles switch cases these are the control
structures that are going to allow you to have your code do different things depending on
different circumstances so let's go back to that code and we'll see we have this if statement so
this is some control code and notice it's followed by an opening and a closing parenthesis and inside
this is called our condition so if statements have a condition if the condition evaluates to
true then the code inside the curly braces gets run if the condition evaluates to false then
all the code in here just gets skipped over so you only do the stuff inside the curly braces of an if
statement if this is true now you might be looking at this and being like wait what is going on here
well we've got two things going on so this right here is actually an or symbol so this condition
is saying if the brightness variable is less than or equal to 0 or if the brightness variable is
greater than or equal to 255 then do something so well why don't we just evaluate well let's pretend
we're the arduino and let's evaluate this so what did we say we said brightness was 5. so if
brightness is 5 is 5 less than or equal to 0 nope it's not okay well is five greater than or equal
to 255 nope it's not so that means we're just going to skip over this line of code right here
the first time through the loop right okay so we've controlled our code flow because we
haven't used this based on based on a condition so then we get to this next line of code and it
says delay 30. here's another one of those super important arduino functions that you'll learn to
love and hate it's called delay and what it does is it stops the program in its tracks no other
code will run for the amount of time you put in here and this time is in milliseconds so for 30
milliseconds we're going to say stop don't do anything arduino board and when we say stop don't
do anything what we're saying is don't execute any more code because what we did up here on line 13
when we pwm'd pin 9 to a value of 0 that means the led was off right it's there's no brightness
there and we're just gonna hold that position so delay is kind of like hold it hold it okay go
that's what's going on here so after the delay we get to this final curly brace here's the end of
this loop function and what do we do we start back up at the top so we're back at the top we run
into our friendly function analog write again using the pwm feature of the microcontroller
and what is brightness this time well if you recall last time through we changed brightness to
five so now brightness is five so that increases the on time for the duty cycle and that means
this led is going to be on ever so slightly so now the led is going to turn on dimly now it's
not going to turn on dimly because we've applied a lower average voltage to it it's actually going to
come on dimly because of the ways our eyes work so the led is going to be flickering on and off at
a rapid pace it's not going to be like somewhat on and somewhat off it's going to be full on
and full off but the frequency of it coming on and off tricks our eyes into thinking that it's
actually dimly lit and that's how pwm works with leds you're able to make a fading effect of the
led by adjusting the duty cycle of that pwm signal all right so we can see the led dimly well down
here what do we do well now we're going to take brightness that was five right and we're setting
it equal to brightness plus fade amount well what is brightness well you might be tempted to think
well hey yeah we set brightness to zero up here well actually brightness got updated before so
we're you we're working with the newest the latest and greatest brightness which was five right so
five plus fade amount which was set to five so that's ten so now brightness is equal to ten now
we come back down to our if statement and we are checking the condition but the condition really
hasn't changed right because 10 isn't less than or equal to zero nor is it greater than or equal to
255. so again we're just going to skip this code now we delay we say it hold it everybody we come
back and you can see we're just going through this over and over again and what is happening
well what's happening is brightness this value because of line 16 it keeps increasing it
gets bigger and bigger and bigger and so we see this led fade on slowly it's going to get
brighter and then brighter and then brighter so we're going to see it fade on from dark to
brightness well what happens when this value gets to 255 well when this value gets to 255 then this
if statement this condition is going to be true because brightness will be equal to 255 which
means the code in here is going to run so our control structure is going to affect that flow
when brightness gets to 255 and what does it do well it flips the sign of fade amount so it's
this fade amount which was five it sets it equal to negative fade amount which makes it minus five
and then we delay so that's interesting minus five okay so we said brightness was 255 right
so we come up to this loop brightness is still 255 so that led is going to be like fully
bright fully lit we come down to brightness now brightness that's 255 plus negative 5. so what's
a positive number plus a negative number well you basically do a subtraction so now we're going
to go down to 250. so now we'll be going from a high brightness to a low brightness because fade
amount is negative so we'll be subtracting from brightness until we get down to zero and then this
if control statement is going to flip it again and then we'll add and it's just going to go back
and forth forever and ever and ever and that's just a basic idea of how this flow works so let's
look at another example of a control structure let's look at a switch case statement here's
an example of another control flow mechanism function called switch case and what we're
doing here is we have a sensor hooked up to one of our analog pins and if you recall
the analog pins are connected to the analog to digital converter on the microcontroller so
pins a0 through a5 6 pins on arduino uno using that adc and what that adc is able to do is take
an analog voltage and convert it into a number between 0 and 1023 so what this program is doing
is it's going to read the value of the sensor and then based on what that input in is it's going
to print out something different to the serial monitor now we haven't talked about the serial
monitor but we are in a second because it's super important when you're using the arduino ide but
let's start at the very top here look we've got two variables they're both integers one is called
sensor min and one is called sensor max sensor min was assigned to the value zero and since our max
you see we've initialized it to the value six hundred but what's this const in front well this
is constant and what this is saying is hey these variables aren't going to change throughout the
program this is called a qualifier and you'll see this pretty often you only use this for variables
that don't change okay here we are in the setup and we see a super important function called
serial dot begin so there is a code library called the serial library and begin is a function
that's part of that library and so to use the begin function first we write the name of the
library which is serial we have a little dot and then we follow with the name of the function
begin and we're passing into it this value 9600 which is the baud rate all seems kind of odd just
a lot of details here i'm kind of skimming over but essentially what this does is it allows us
to communicate on the serial port between our arduino board and our computer so there's a tool
built into the arduino ide called a serial monitor open it up right now and what it does is it allows
us to display text from the arduino board to our computer okay so serial begin initializes that
serial communication super important function don't worry about the 9600 now just go with it
again it's just the rate of communication okay so we go through setup then we jump into the loop
what's loop do it it goes over and over and over again and what are we doing here inside the loop
well it looks like we are declaring a variable and initializing it to a value so we're making
an integer we're calling it sensor reading and we set it equal to what what is this well this is
a function right we see the name and then we see these parentheses here analog read is another one
of those super duper important arduino functions analog read uses those analog pins so analog pins
zero through six and it allows us to use the adc to read a voltage but all it needs to know is hey
what pin number do i need to read that voltage at and so we're saying pin a0 so if you want to refer
to those analog pins you can use a 0 a 1 a 2 a 3 a 4 a 5 so you just put an a in front of the
number now we could have said we could have made a variable out of this we could have made a byte
called sensorpin and set it equal to a0 we could have made it a constant since it's not going to
change and then we could have taken sensorpin and put it right here just like that so now what's
going to happen on this line of code is that analog read is going to look at this sensor pin
pin a0 it's going to read the voltage and whatever the adc returns it's going to be a number between
0 and 1023 it is going to store it into sensor reading so now sensor reading is going to be equal
to whatever that value was line 14 a little bit confusing it's using a map function now this isn't
this is a arduino specific function it's not super duper important it's still useful what map does
is it takes one range and it converts it into another range basically what's going to happen
is you pass in the existing range and then you want to transform it into a new range not going to
go into it now but basically what it's going to do is it's going to squish down the number so as that
sensor value changes we're either going to get a 0 a 1 a 2 or a 3. and that's when we finally get
to this switch case here so the switch notice that there's a parenthesis following it and inside
there is a value and this value is going to determine which case we actually take on so here
we've got case zero so when the value is zero we're going to execute this code when the value is
1 we're going to execute this code when the value is 2 this code and when the value is 3 this code
notice the break statement after the case that says hey when you get to the break statement
it's going to jump us out of that switch case so you're only going to do one of these cases so
in this loop we're going to read the value at the sensor we're going to map it to a small condensed
range and then we're going to do something based on the value and in this case we're using
the serial library again but we're using the print line function from the serial library and this is
what is actually going to print something out to the serial monitor window so after we get down to
the end of the switch case we have a short delay just one millisecond and then we jump back up
to the top of the loop and what do we do well we read the sensor again well why would we read
the sensor we just read it didn't we well we're going to read the sensor because the idea is that
our arduino is constantly checking the sensor it's always on the lookout hey did the sensor change
did it did it change again is something different so every time through this loop which is running
rapidly the arduino is on the lookout for a change and when it sees a change this switch case can
react differently so i'll go ahead and upload this and i'm going to open up that serial monitor
window and you'll see it's low and the sensor i have attached i actually just have a potentiometer
attached a potentiometer is like a dial think of like on a amplifier or in an old school radio you
know as you turn that dial to change the station so i'm just gonna sli slowly change this
and you can see as i change that the value is changing sensor reading is changing because
the value at sensor pin is changing and as i adjust it lower it's doing that as well so you can
see again how this control structure is helping us adjust what happens in the sketch itself all
right we're going to look at one final sketch and that's going to cover some of those other
super important arduino functions so let's go to file examples button so now this is a sketch
that is going to check for a button press so this circuit looks something like this we're going
to have an led attached through a 220 ohm resistor to pin 9 the other side going to ground but then
we're adding in a button one side of the button is connected to pin two the other side of the button
is connected to ground so you can forget about the potentiometer just look at the led and the button
here so what we want this code to do is look at that button and we want to change how the led is
reacting based on the what the button is doing so in this case when we press the button the led
is going to turn off and when we're not pressing the button the led is going to be turned on all
right so what do we have at the top we've got some variables should be familiar with these by now
we've got button pin we set that equal to two and led pin we set that equal to nine
again that's where we've connected our hardware right the button at pin 2 and the led
at pin 9. these are set as integers they could be bytes but it's okay to have them integers and
then they're qualified as constants they're not going to change so that's why they're made as
constants but then we have a variable called an integer called button state and it's set equal to
zero now the button can either be on or off so we could have set this equal to a boolean but setting
it equal to an integer is fine too then we get to our setup function and we're using the pin mode
again so we can use the pin mode function to set those pins as outputs like we're doing
with the led we're setting it as an output or we can set a pin as an input so we could just
do input like this but we can also set it to an input pull up now i won't exactly get into what
pull-up resistors are and all that stuff right now but basically what it's saying is hey we're going
to set this pin to a high state and if you want it to change you need to pull it to a low state but
for all intents and purposes we're saying hey we want that button pin that pin 2 to be an input and
if we wanted we could just do input but in this case we're just going to go with input pull up all
right so in setup which only runs once we're going to use pin mode to set one pin as an output and
one pin as an input and then inside the loop what do we do let's see first line of code we come into
we've got our variable button state it's set equal to zero now and we're setting it equal to this
function digital read button pin so here's another super important arduino function digital read
we know it's a function because it's got those parentheses right it takes a value in here and
it's a reserved function name it changes to the color orange and what digital reads going to do is
it is going to read the voltage at that button pin and it's going to check whether the voltage is
high or whether the voltage is low now in our case when we are not pressing this button since
we made this an input pull up pin 2 will read high but when we are pressing that button what
we're doing is we're introducing ground voltage and so pin 2 is actually going to read ground
voltage when we press the button so that would be a low voltage state so when we don't press
the button this is going to be high and when we do press the button it's going to be low high
is represented by a 1 and low is represented by a zero so if we don't press the button
then digital read is going to return a value what's that value gonna be if we're not pressing
the button it's gonna be a one so then button state would be assigned the value one and then we
get to some control flow we've got our friendly if statement and we look at this condition in this
control flow and it's saying hey button state equal equal high hey what is this isn't that an
assignment operator well notice that it's an equal equal this is actually checking for equality so
it wants to know hey is button state equal to high well we said it was right because digital read it
was reading button pin if we're not pressing the button it would be a one it would be high right so
this is true and if that's true we would run the code inside the if statement and in this case we
come to another super important arduino function digital write so we had digital read a second
ago that's for reading digital inputs either on or off inputs and digital write is for creating
an output on those digital pins either a high voltage or a low voltage so digital right it's
going to take two values first it needs to know what pin to apply the voltage at and then either a
high voltage or a low voltage so in this case the voltage is going to be high we want that led to
be on if we are not pressing the button so now we see another part of this control flow it's called
an else so notice that the else comes right after the end of this if statement and it's got its own
curly braces and what this else is doing is saying hey if the if statement is false so if this is not
true go ahead and do this other thing so if this else do this right so if we're not pressing the
button then we're going to write the voltage to led pin low so what this control flow is going to
allow us to do is check the current button state and then whether or not it's pressed it's going
to do one thing or another thing now we could make this an else if and if we add the if there we had
another if we could add another condition in here like maybe we had two buttons and we said if
the other button is high then do this won't do that now but just wanna let you know that
that's something that you can do all right i know that was a lot we talked about how every
arduino program is laid out with setup and loop we talked about variables how they're buckets for
storing information how you can declare them and initialize them in the different data types there
are we talked about a couple control structures namely the if statement the if else and also the
switch case statement now there's a lot of other control structures that are really useful for
example a for loop and a while loop we won't get into those now and then throughout this whole
conversation we've been talking about the most important arduino specific functions the pin mode
function digital write digital read analog write analog read and then the serial
library function serial begin and serial print and serial print line and then
finally the delay function now i know that's a lot to take in but really that just laid most of it
out right there for you it's really these basics that allow you to do tons of stuff so all the
stuff we talked about here is core critical stuff that you need to kind of get into your brain and
practice play around with go into those example sketches see if you can see any of this stuff
see if you can follow some of the control flow now we've only scratched the surface though it's
going to get really exciting because we're going to start talking about arduino libraries and
what arduino libraries are going to let you do is start using all different types of hardware by
simply using other people's code so what exactly is an arduino library it's not like you know
a place with a bunch of books about arduino right you're right it's not that when you hear
the term arduino library what it's referring to is code that someone or some group of people
have written and they've packaged it up to work on a specific thing so an arduino library is
code that's developed for a specific purpose and is packaged in a way that allows you to access
all of the functionality without necessarily having to understand all the dirty details because
anytime you're writing code and learning about new stuff and figuring things out there's a lot
of complexity and what an arduino library does is it abstracts away some of that complexity by
giving us simpler ways to access the functionality so let me give you an example let's say you sit
in your car and you want to start it right maybe you have a key start or a button start or maybe i
don't know maybe there's like clapper start cars i don't know whatever anyway all you got to do is
something really simple right but what actually takes place inside the car is rather complex
it's got a starter in there or maybe i guess if you have an electric car maybe there isn't a
starter but you get the idea all types of things are happening by you simply turning a key pressing
a button whatever right you don't need to know the details of all that all you have to do is turn
the key so an arduino library is sort of like that there's a person or several people who have
worked together to write code to control some type of hardware or do some type of calculation
for example controlling a stepper motor or a servo motor or maybe lighting up some neopixel
leds so these people have put in the blood sweat and tears to make this code work and what you're
able to do is just use the functions or part of the code that they've written in order to invoke
all of the stuff that is going on in their library and what's amazing is you can get these arduino
libraries for completely free almost all of them are open source they're just being shared out
there just about any piece of hardware out there that you want to control there's probably going
to be a library that's already been written to help you control it and i want to try to drive
the point home that this is absolutely amazing you're basically being handed all these tools
that you can start using people are just like hey here use this tool hey use this tool it's
like i don't know to me it's really exciting now i'm not saying you're going to just blindly
use any library it's a good idea to have an idea of kind of some of the inner workings but when
you're just getting started and you really have no idea how the coding works at all it's fantastic to
be able to just use these libraries and bam there you're going you're like off to a running start so
hands down access to arduino libraries is stinking amazing so now i am going to show you how
to install an arduino library it's super simple so first here's a quick bonus when
you download the arduino ide they actually already include some really handy libraries
so you can just go to the arduino libraries from sketch include library and see the list
here now if you want to install a library it's pretty simple there's a couple ways to do it so
you can either go up to sketch include library and then go to manage libraries or you can go to
tools manage libraries and it's going to open up the library manager before i get to the library
manager though what i'll usually do is search for a library on the internet so i'll google something
like arduino servomotor library or arduino accelerometer library i'll find a library on
google i'll see what the name is and who wrote it and then i'll search for it in the library
manager so let's just run through that real quick all right several different listings here i'm just
going to click this one by electronic cats kind of a cool name so the mpu 6050 by electronic cats so
this looks like it's a specific accelerometer so if i have this specific accelerometer you know
i already have the hardware then i would have searched for this i probably would have gotten to
this page if i haven't purchased an accelerometer yet then maybe this is one of the accelerometers
i would consider buying because i know there's a library that i can use to control it
so now all i'm going to do is just grab this part right here mpu6050 i know it's by electronic
cats now i'm going to go back to the ide paste it right in there and now this is filtering and i'm
coming down here and then look at this see a bunch of them come up there's several different ones for
this mpu6050 but this one is by electronic cats so i know this is the one i want i'll go ahead
and click install that easy and now it tells me it's installed so what actually is happening
when you click install well what's happening is the arduino ide is going out to the internet
it is grabbing all the code files and it is saving them into the libraries folder in your arduino
folder so let me show you exactly where that is so i'm going to go ahead and open up my documents so
here i have documents you know my documents this is the arduino folder right and inside the arduino
folder you have a folder called libraries and this is where that folder is going to get saved all
that code is going to get saved so let's look for it oh there it is the mpu6050 and here is all
that code right there it just gets saved into this libraries folder if you wanted to you could just
download this library and paste it right inside this libraries folder but that's what it needs
to go inside this libraries folder okay so that is how you install an arduino library let's go
ahead and do the same thing in arduino ide 2.0 so here i am in arduino ide 2.0 and guess
what i got to do i can go up to tools manage libraries and instead of opening up a box over
here a new window it's just going to show the libraries right here so this is the same
thing you're going to search and filter and then a shortcut to get here instead of
going to tools is just clicking this little these uh shelf book of libraries right see it just
pops up like that and then you would do the same thing you could click install so it's really
pretty much the same thing okay so that is how you install an arduino library all right what i'm
about to show you isn't some type of secret but i have met so many people who don't realize that
this actually exists alright so here's the deal when you install a new library a very common
thing that comes with the library are example programs that demonstrate how to use the library
and this is how you get to them alright so i'm gonna go to file examples and now i'm gonna scroll
down and this is examples from custom libraries so these are libraries that i've installed
right that we just did like we showed there and i'm going to come down to that mpu6050 i'm
going to come over here and i can see that there's a bunch of different example files i'm just
going to go ahead and go to this mpu6050 raw and what this sketch does is it is a working
sketch on how to use this accelerometer sometimes they'll have comments in here to help you explain
different lines of code but they're going to give you some basic examples of actually how to use
the accelerometer now look at this this is great it says use the code below to change the excel
gyro offset values so they're giving you a bunch of code to kind of play around with to adjust the
accelerometer so you can literally take this code upload it to your arduino and just play around
like hey how can i make this thing work how can i make this thing adjust it gives you a known
thing to kind of start with and many times in these examples they're going to give you the types
of programs that you'd be looking for so anytime you get a new library first thing you should do
first thing i always do is i go to those examples and i just start reading through the examples and
see if i can make heads or tails of what's going on and i will play with the example sketch before
i ever start trying to write my own code from that library all right so that is the quickest
way to learn how to use an arduino library all right now what i want to do is i want to
show you two examples of a library in action so check this out here we are i'm going to go to
file examples and i'm going to come down to the servo library and i'm going to come over to knob
now the servo library is going to help us control a servo motor and we're going to control it using
a potentiometer so potentiometer is like a dial like you think of an amplifier you know you'd
like turn the volume up turn the volume down or like old-school radio dial right and so as
we turn the potentiometer it is going to move this servo motor and this is what the circuit
diagram would look like so here's our servomotor it's connected to power and ground on this
breadboard which is then connected to the arduino and then we have a potentiometer the middle pin is
connected to pin a0 and then we've got the outside pins the potentiometer one hook to ground and
one hook to power so i do want to mention servos can really draw a lot of current especially if
you have a load on the servo so like let's say you're trying to move some amount of weight even
if it's somewhat small that current requirement can surge and the voltage regulator on the arduino
can only provide so much current so generally it's a good idea to use a separate power supply to
actually power the servo but you control the servo with the arduino again lots of details in this
stuff i won't get into it but since we've got no load on our servo we'll be just fine so if we
look at this sketch we are including notice at the top here we say include servo.h this allows us
to use all that code in the servo library here this kind of looks like we're creating a variable
right what we're actually doing here is creating an object we're using a class that's part of this
servo library the type is servo and the name we're giving it is my servo then we've got some other
variables here this is for our potentiometer pin hooked in at pin a0 and then there's a variable to
hold the value that gets red from a0 now in setup we're going to do something that runs once and
so here we're calling the attach function that's provided by the servo library and we're going to
attach pin 9. that's where we've got the servo signal line plugged in start some serial
communication here then we get into the loop and notice what we're doing so we've got that variable
val so first we're using analog read at pin a0 right so if you'll recall that's using the analog
to digital converter it's going to take whatever position we have that potentiometer at it's going
to take it and store it in this variable val and then down here we're going to take that value and
we're going to map it to a new range from 0 to 180 because most servos you use they can move 180
degrees in either direction and then we're going to use this function myservo dot right and put the
value in and what this is going to do is tell the servo what position to move it so if val is a zero
it's going to move to the zeroth degree if val is 180 it's going to move to 180 degrees and anywhere
in there so if we go ahead and upload this code now as i move the potentiometer it moves the
servo motor it's pretty cool and it's just amazing how quick i was able to get that up
and running so let's look at another example i don't know if you have heard about individually
addressable leds a common name is called neopixels but they come in these strips and they're super
cool so one library that's used with those is the fast led library so i'm just going to come into
fast led and i'm going to check out this cylon now notice this example in the library it's giving
me all types of information about what i need to actually hook up now it might take me a bit of
time to kind of figure out exactly what this code is doing but at least i have a starting place to
work with all i have to do is make the connections so the strip i have has 12 leds i'll make the
connections as appropriate click upload and now i've got this cool effect on my led strip that's
pretty awesome all right well i hope you are as pumped as i am about using arduino libraries like
i said they open up so much opportunity for you as a new programmer because you can lean into
the expertise of all these developers all over the world i'm going to make a claim here maybe
i'm wrong but i would say arduino hands down has the most libraries available for all different
types of hardware out there it's just amazing now if you have any questions about this please
just ask the question in the comments i'm going to do my very best to answer all the questions
also while you're down there if you could like our video and if you haven't yet subscribed
to our channel it doesn't cost you anything but a click but it really helps us bring you great
content like this arduino programming workshop hey if you made it this far and you hear me talking
right now congratulations i'm hoping that you just learned a ton of stuff and it tells me if again
if you're this far into it you really want to try to learn this arduino stuff now if you enjoyed
this workshop you feel like you're kind of getting ahead a little bit then i really think you would
enjoy the training that we offer at programming electronics academy now our flagship training is
called the arduino course for absolute beginners and it lays out module by module a clear and
concise guide for learning the ins and outs of programming with arduino you're going to
start at the very beginning getting familiar with the arduino ecosystem and the fundamentals of
programming then you'll dive into programming for inputs and outputs you'll learn coding structure
to help build logic into your programs and you'll do a deep dive into coding libraries and how to
find and use the best ones for your application we'll cover using arrays and writing your
own functions and finally we'll cover using interrupts and timers to make your code react to
inputs exactly when you need them by the end of this training course you're going to have the
programming knowledge to start writing code to control your own projects you'll understand how to
lay out an arduino program the specific functions you'll need for reading inputs and creating
outputs and you're going to know how to structure your program so that it flows like you wanted
to now here's what's crazy this flagship course is only one of the courses that comes as part of
our program we have courses that cover connecting your project to the internet on how to power your
project course on basic electronics on using lcd displays on using audio boards for making noise
on using servo motors and more and we keep adding concise high quality training programs and here's
probably the best part of this program if you ever get stuck or confused on a lesson in our training
you can simply ask for help in our private forum and get a pleasant response thanks again to
altium for sponsoring this training check out the description to get a free trial of the super
powerful altium 365. if you want to learn more about this training program just click the link
in the description to get started thanks a lot again if you want to check out that program just
click the link in description have a great one bye