Arduino MASTERCLASS | How to Use Arduino Libraries PART 7

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
- [Instructor] Okay. I got a feeling that what you really wanna do is start controlling all types of cool electronic stuff. Maybe you got some cool ideas for some projects and you just wanna get rolling. The quickest way to do that is to learn and understand how to use Arduino Libraries. They are gonna help you get up and running super fast with all types of different electronic stuff, by leveraging the hard work and expertise of an amazing group of programmers from all over the world. In this video, you are gonna learn exactly what an Arduino Library is and how it's gonna catapult you in your prototyping efforts. You are gonna learn the quickest way to learn how to use an Arduino Library. It's what I've been doing for years, and it always pays off. And I'll show you two examples of a library in action. Let's go. (ethereal music) Before we start, if you haven't yet, I would really appreciate if you'd take a moment to subscribe to our channel. It doesn't cost you but a click, but it really helps us bring you great content, like this Arduino Programming Workshop. So what exactly is in Arduino Library? It's not like 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 and 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 wanna 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 gotta 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 gonna be a library that's already been written to help you control it, and I wanna 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 gonna 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 gonna 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 gonna 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 gonna 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, I already have the hardware, then I would've searched for this. I probably would've 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 gonna do is just grab this part right here. MPU6050. I know it's by Electronic Cats. Now, I'm gonna go back to the IDE, paste it right in there, and now this is filtering. And I'm coming down here, and then look at this. See a bunch of them come up. There's several different ones for this MPU6050, but this one is by Electronic Cats. So I know this is the one I want. I'll go ahead and click Install. That easy. And now, it tells me it's installed. So what actually is happening when you click Install? Well, what's happening is the Arduino IDE is going out to the internet, it is grabbing all the code files, and it is saving them into the library's folder in your Arduino folder. So let me show you exactly where that is. So I'm gonna go ahead and open up my documents. So here I have documents, 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... Oh, there it is, the MPU6050, and here is all that code right there. It just gets saved into this library's folder. If you wanted to, you could just download this library and paste it right inside this library's folder, but that's what it needs to go inside this library's folder. Okay. So that is how you install on Arduino Library. Let's go ahead and do the same thing in Arduino IDE 2.0. (mellow music) 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 gonna show the libraries right here. So this is the same thing. You're gonna search and filter, and then a shortcut to get here instead of going to Tools is just clicking 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 in 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 gonna go to File, Examples, and now I'm gonna scroll down and this is Examples from Custom Libraries. So these are libraries that I've installed, right? That we just did, like we showed there, and I'm gonna come down to that MPU6050. I'm gonna come over here, and I can see that there's a bunch of different example files. I'm just gonna go ahead and go to this MPU6050_raw. And what this sketch does is it is a working sketch on how to use this accelerometer. Sometimes, they'll have comments in here to help you explain different lines of code, but they're gonna 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 accel/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 gonna 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 wanna do is I wanna show you two examples of a library in action. So check this out. Here we are. I'm gonna go to File, Examples, and I'm gonna come down to the Servo library, and I'm gonna come over to Knob. Now the Servo library's gonna help us control a servo motor, and we're gonna control it using a potentiometer. So potentiometer is like a dial, like you think of an amplifier. 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 gonna 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, the potentiometer one hooked to ground and one hooked to power. So I do wanna mention, Servos can really draw a lot of current, especially if you have a load on the servo. So, 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 will 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 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 gonna do something that runs once. And so here, we're calling the attach function that's provided by the Servo library and we're gonna 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 analogRead at Pin A0, right? So if you'll recall, that's using the analog to digital converter. It's gonna take whatever position we have that potentiometer at. It's gonna take it and store it in this variable val. And then down here, we're gonna take that value and we're gonna 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 gonna use this function, myservo.write, and put the value in. And what this is gonna do is tell the servo what position to move in. So if val is a zero, it's gonna move to the zeroth degree. If val is 180, it's gonna move to 180 degrees in... 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, a common name is called NeoPixels, but they come in these strips and they're super cool. So one library that's used with those is the FastLED library. So I'm just gonna come into FastLED and I'm gonna 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 'cause you can lean into the expertise of all these developers all over the world. I'm gonna 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 gonna 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. (ethereal music)
Info
Channel: Programming Electronics Academy
Views: 35,418
Rating: undefined out of 5
Keywords: Arduino, Arduino(Brand), Arduino Tutorial, Arduino Lesson, Open Source Hardware Group, Learning Arduino, Microcontrollers, Electronics, Arduino IDE, Arduino Sketch, Computer programming, C++, Programming Electronics Academy, arduino basics, electronics full course, electronics for beginners, electronics components
Id: uLRa-9hHK_Y
Channel Id: undefined
Length: 14min 26sec (866 seconds)
Published: Fri Jul 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.