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