Transcript for:
Introduction to Arduino for Beginners

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 going to 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 are 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 going to work just fine. Now, all Arduino boards share one thing in common. They have a microcontroller 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 going to 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. All right, 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++ programming languages, but it's got some Arduino specific functions and structure. So when you program on Arduino, you're basically programming in C and C++ 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 is 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 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 lifecycle 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 and 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 REV3? 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 AB 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 Rev3, 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 Rev3. 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 SUV, sedans or trucks. Each one serves a different use case, but for the most part, they can all get you where you wanna 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 your 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 going to 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 cache at all, like zero cache, 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 do you put the fuel in? Where do you adjust the mirrors? Like, that kind of thing. That's what we're going to 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 microcontroller. And again, it is the most important part of the Arduino board. In fact, an Arduino board is a microcontroller 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 microcontroller 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 a resistor, 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 will use a breadboard when they're working with an Arduino, but, you know... 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 boards 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 five 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 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 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 GND. GND stands for ground, and that's the lowest voltage on the board. You'll also see a 5V and a 3.3V 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 gonna 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 AA batteries, or one of those wall wart supplies, the voltage limit on this DC jack is about seven 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 seven 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 scratched 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 are going to use to write your code in. And it's also going to help you load the code into an actual Arduino board. So 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 1. Now, Arduino IDE 1 and 2 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 1. It's gonna be the same for Arduino IDE 2, 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 gonna 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 subsandwich. 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 SubSandwich. And if you open that, it's going to have a file in here. and you can see the file name is also SubSandwich. Now, the file extension is .ino. Old file extensions are .pde, but .ino is what you'll see. And 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 SimpleWrite 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, Micah, 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 going to hit OK. And now you can see we see the line numbers. OK, 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 it's not actually code. It's just more like help or text for you to understand what's going on. And Blink 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, it made a little noise like, oh, hey, I recognize 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 console 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 onboard 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 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 this can work. So I'm just going to make that 100 and this. Let's make it 75. And then I'm going to 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. All right, 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 who 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 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 IDE 1 and for IDE 2. So when you save a sketch in IDE2, you'll also be able to access it from IDE1. No problem at all. Okay, and you can also change the theme to a different 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 says 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 and saying, yep. Arduino Uno. And then I go back to tools, port. And the only port it's 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 on Arduino IDE 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 I'll 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 a Windows 32 or a 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 going to 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 not going to click yes. 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 and 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 going to have to go with that Arduino web editor. But otherwise, you know, IDE1, IDE2, 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 of 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 they're functions is they've got these open and closing. So here's an open and here's a closing parentheses after a word. So that kind of setup, when you see an open closing parentheses, 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, setup and 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 one, code two, it's going to execute code one first and then the next line of code, code two, and it's going to go on till 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 nine and then this code on line 10. Of course, this isn't 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 at 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 zero 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 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 going to say, we're just going to 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 going to 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. bummer. But you can use descriptive names for your variables. So using something like current temperature would be more clear than just writing the letter ct 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 gonna evaluate this and this doesn't have to just be a value. This could be an expression over here. So it's gonna evaluate this expression and save it right here. So let's say I had two variables here. One is called taser blast 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. 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 four, right? So now pain threshold would be holding the value four. 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 taser blast 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, taser blast is an integer. It's a variable that stores an integer. And here we are assigning the value eight 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, anytime you've finished with a 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++ 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 semicolons here, when we verify the program, the compiler, which is part of the IDE, which is gonna be checking for our code, looking at our code for errors, it's gonna start on line five, but it's not gonna 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 it a little bit better. All right, 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 0 and a variable called fade amount set to 5. 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 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 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's 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, pin nine 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 analogWrite. 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 analogWrite does is for the pins that are capable of doing PWM, pulse, with modulation it sets the duty cycle so if you recall with pdwm we're adjusting the on and off time of that pin very rapidly and we can adjust the average voltage of the pin between zero and five 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 9. So this is the value 9. We're saying, hey, pin 9, we're going to PWM you. And now it says, hey, well, what is the amount you want to set the PWM to? 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. 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 cycles to 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 write 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 13, 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, fors, 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 parentheses. 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 zero, 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 five. So if brightness is five, is five less than or equal to zero? 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 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? There's no brightness there. And we're just going to 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 right, 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 up. updated before. So 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 10. So now brightness is equal to 10. 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 says 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 five. 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, six pins on our 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 zero and 1,023. So what this program is doing is it's gonna read the value of the sensor, and then based on what that input is, it's gonna 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 600. 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.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 it 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 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 0 through 6. 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 A0, A1, A2, A3, A4, A5. 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 sensor pin 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 sensor pin 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 zero 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 is an 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. I'm 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 parentheses 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 0. So when the value is 0, 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. But 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 change again? Is something different? So every time through this loop, which is running rapidly, the Arduino's 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 an amplifier or on an old school radio, you know, as you turn that dial to change the station. So I'm just going to 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 2, and the other side is connected to pin 2. 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 what the button's doing. So in this case, when we press the button, the LED is gonna 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 two and the LED at pin nine. 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 gonna 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 two, to be an input. And if we wanted, we could just do input. But in this case, we're just gonna go with input pull-up. All right, so in setup, which only runs once, we're gonna 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 read is 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 one 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 going to be? If we're not pressing the button, it's going to 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 high voltage or a low voltage. So digital write, 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 add 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 want to 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 and 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 is 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 Servo Motor 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 MPU6050 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 that. 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 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. All right, 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. All right, so I'm going to go to File, Examples, and now I'm going to 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 MPU 6050. 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 MPU 6050 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 servo motor. It's connected to power and ground on the spread board, 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 of the potentiometer, one hooked to ground and one hooked 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 up 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 a class that's part of this servo library. The type is servo and the name we're giving it is myServo. 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 read 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 zero 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.write, and put the value in. And what this is going to do is tell the servo what position to move in. 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. That'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. The 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 FastLED library. So I'm just going to come into FastLED 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 want it 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 the description. Have a great one. Bye.