how arduino libraries work
A couple we’re going to talk about some of the things that can mess up if you don’t do a couple things when you’re installing a library so I’m doing this, because a friend of mine, Roman asked me to do it. You know he just emailed me and hey hey, said: hey I’m, having some trouble with some libraries. Can you show me what the deal is? So you know, and as I looked into it you know there are a couple things that really can kind of hold you up so we’re going to talk about those things too. Alright. So here I am in here, do we know ite and you know maybe I was on the internet and I was looking around and it said, hey you, you should use this library to do. You know XYZ, like maybe you want to run a matrix or something and there’s a library out there running all matrices you’re like okay, hey. I need that library. So you know the first thing you might want to do is check. Do I already have you know? I’M, getting ahead of myself. Well, what is a library and Arduino library is just a group of functions that are packaged together that are similar, okay, so there’s a bunch of different functions. They all do the same kind of thing like they, you know, run a four by four matrix and then you pack them together and that’s called a library that’s.
Really it that’s it. And then you know, a library makes it easy to share these functions with one another. So the Arduino IDE already comes with pre installed libraries. So if you go up to tools and you go to I’m, sorry, if you go to sketch you go to import library here right here. This big list right here these are libraries that are already installed on your dwee, no IDE. So if you were to click, let’s go ahead and click the servo alright. So if I click servo see how it adds that code right, there include servo dot H that line of code right there saying hey, you want to look through the servo library before you go ahead and execute the rest of this code. Okay, that’ll make sense here in a second but that’s when you go to again when you go to sketch import library, all that’s going to do is add a line of code to your sketch. Now, if you know the name of the lie library, you can type it in yourself right there you don’t, need you know, that’s, all that really does now notice down here. I have contributed – and I have a couple libraries down here so this first one that is a AF motor that’s Adafruit motor. So if you ever heard of Adafruit that’s a company that makes hardware stuff like that open source hardware, and so I bought one of the things and I used that one of the libraries they made with it in frequency time or two.
I think that’s also an Adafruit one I’m, not sure so I’ve used. Those are the two libraries that I have on this installation. So a contributed library is just a library that anybody else is written, that doesn’t come prepackaged in your Arduino IDE there’s, tons of them out there and it’s a really good idea before you start a project to start looking for libraries that might help you with a Project so let’s say you wanted to interface with a certain piece of hardware. Maybe you have some temperature sensor well and you’re, looking at it you’re like man, this temperature sensor, the status sheets, like you, know, 40 pages long and I’m, not really sure. I get this and it’s going to take me. You know four years to figure this out. Well, you know get online and do a search for a library in that component. Name like Arduino library, slash temperature sensor sensor and you might find, like forty people have written libraries for a you know. The specific temperature sensor and essentially they’ve, really done the work for you and then all you have to do is kind of familiarize yourself with the library and get going from there. So it really libraries really help you kind of jumpstart your project, okay, so highly recommend. Looking for libraries and using them so let’s go ahead now: okay, so we’ve kind of looked at it. You know where the libraries are here, but now I want to show you what’s in a library file for ttle, folder, alright, and that might seem a little higher level like you know.
What do I really care? Well, this is knowing what I’m about to show. You is going to help you down the road when you start installing libraries, okay, so what I’m going to do is. I am going to go ahead and I’m going to navigate to where I have my arduino installation all right, so here’s arduino, you have this same. Folder on your computer and now see these are all the sketches that i’ve written on this computer, okay and in here there is a folder called libraries, and that is where all your library folders are. So here look at this here’s AF motor. So remember when I was on the Arduino IDE, you saw AF motor in frequency time or two so I’m going to go ahead and click here and look. We’Ve got a couple files in here, so we’ve got frequency time or two dot, CPP and frequency time or 2 h. Ok, this file that dot CPP file and this file, the dot H file, are the two required files you’re going to need to see in any Arduino library. Let me say that one more time there’s two mandatory must have files and one is going to end in dot H and one is going to end in dot CPP. Alright, I think CPP stands for c and dot H stands for header, okay, so the header file is like a list of all the names of the function and the dot CPP file is the source coder that’s, like the actual line by line programming of the functions? Okay, it doesn’t I’m not going to get nitty gritty on what these are it doesn’t really matter.
You just need to make sure that there’s a dot CPP and a dot H file. Now what you could also find in here is a key word file and that’s. A text document, and all that does, is that, like you know, when you type in a function in Arduino, and it turns a color like it turns you know, digital read, I think, is orange. Well, this file defines different colors so like that you could set a keyword for the different functions and then an examples, folder that gives you that will allow you to have access to examples when you’re in the Arduino IDE so here’s one. It says test and there’s test PDE. Ok, so let me show you where this is real, quick. If we come to Arduino IDE, when we go to sketch I’m sorry, we go to file examples, see here frequency timer. Do you see that here’s an example of this library? Ok, so I hit test, should open up a little fold. It opens up a thing here and now here is a test here’s an example of using this library. Ok, so most libraries have examples that come in them alright. So here we are on this library folder. This is what I want to talk about here, notice that there are several files in a library. So what that means? When somebody wants to deliver a library to you, it has to be or it’s most likely going to be in a zip file.
Ok, because all the zip file does I’m sure you’re familiar with it. Is it packaged packages together, a bunch of different file types in a cozy little blanket okay, so by necessity every library that you download is going to be in a zip file. Now you’re, you know I I’m not trying to. You know pretend that you’re ignorant I’m sure you notice it file is, but I just want to talk through it, real, quick, so we’re, both on the same page. So when you get a zip file, you’re going to have to unzip it and then it’s going to have a folder okay, and so when you download a library generally when you unzip it you’re, going to see a folder and it’s going to be the name of That library, so when I downloaded to AF motor it was, you know, AF motor dot, zip and I unzipped it, and then it makes this folder AF motor and then here’s all these files in there. Okay, so let’s go ahead and just do an example. Alright of downloading and setting up a folder so or a library rather so go to the arduino webpage, go to reference references, my favorite page on the Arduino website and let’s click on the libraries tab. Now I am going to go to this link here. It’S called tutorial on writing your own libraries it’s, a really good tutorial, now we’re not going to talk about writing our own library, but what I want to do here is give you an example of a library that’s made okay.
So if you scroll down all the way down to the bottom, there is a library called Morse, zip, so I’m going to go ahead and click on this and it’s. Going to ask me: where do I want to save it notice that there’s that dot one okay I’ve already downloaded this before so I’m, going to go ahead and save it under the name Morse in oops I’m, going to go ahead and hit save here, it’ll ask Me, yes, I do want to replace that so it’s downloaded and so now what I’m going to do is I’m going to go back to all my files I’m going to find out what you know where I had downloaded that so it’s let’s see here downloads programs To install theirs here’s my file now this is what’s important, so it doesn’t matter where you save the zip file doesn’t matter a bit. What matters is where, when you open, when you unzip that file where the files go, so what I’m going to do? First is I’m going to show you how to do it from the Arduino IDE. So here we have there’s more stat, zip we’ve, downloaded so I’m going to go to my arduino ide and i’m going to go and we get rid of this here. I don’t want that i’m going to go up to tools. I’M, sorry, sketch import library and see you says, add library, so I’m gon na go add library and now I’m going to navigate to where I just downloaded that alright so let’s see downloads programs to install I’m going to look for more so I’m going to kind Of navigate there’s, more stat, zip, the zip file and now I’m going to say, choose and look down here.
It says libraries, Edie library, added to your libraries. Ok, so now I’m going to go up here to sketch import library, and I want to look down here – a look at that contributed there’s Morse, so it just added it. So when I click this, all it does is adds that little include Morse, dot, H, okay and that’s the header file and that basically allows the program to access that library. Now, when you compile this code, so let’s go ahead and take a look at the Arduino. You know the Arduino file folder at Moore’s and take a look at what that is so documents o programmer. Arduino. Ok, let’s come down to the library, folder, ok everybody’s, going to have a libraries folder in their Arduino installation and there it is hey, there’s, more so I’m gon na go ahead and click on this and look at this there’s. Those two files that you had to have more stat CPP or more stats, CPP and more stage and they’ve got some examples, all right, there’s SOS and then there’s a keywords. Ok. So if I so just basically what happened when I use that the Arduino way to do this they’re the built in thing, I go to tools, an import library and I hit add library what it’s going to do. You’Re going to point it to the zip file that you download and then behind the scenes, Arduino is going to unzip that file and it’s going to put it in the libraries folder in your Arduino directory.
Okay. So you need to know where your Arduino directory is on. Your computer should be pretty easy. You know just find out where the Arduino folder is all your sketches are going to be in there like mine. Remember so, I’ll just open this up one more time, just in case, so you know I go. I happen to have it in my documents for some particular reason and see here’s my Arduino folder here’s, all the sketches that I ever write they get saved in folders and then in here you are going to find a libraries folder and then this is where all Your libraries, the individual folders for your libraries, end up being so now, let’s talk about some issues. We can run into I’m going to talk about it right here first, this is what this is. What issue you will? Probably you can run into at times the name of the dot CP pile file, the dot H file in the folder all need to be the same, so see how this is Morse, the name of that folders Morse and the name of CP is Morse and the Name of dot H is Morse that those all have to be the same. Let’S go on to AF motor notice, this AF motor AF motor and then the folders name is AF motor frequency timer to look they’re all the same frequency timer to frequency, timer to dot CPP frequency timer. To H, it is absolutely necessary that all of those names are the exact same so let’s give it a real life example of where this is becomes an issue.
Okay, let’s go back to the Arduino website, I’m going to go to the home page here. Actually, you know what I can search right here, and this is one that Roman had mentioned so I’m just going to go to search for timer one and here’s, so let’s click on this timer one – and here is a library talks about a library, timer 3. All right here it is timer 1 Google code download so I’m going to go ahead and click on this link – and Here I am takes me to a code repository that Google has ok and look there’s, two different, zip files – all right. So here I’m, already at a point where I’m like wait a sec, well, which one do I get? You know, I’ve kind of was weird like which one do I want to take. Well, if you read the name, one is called V 9 and 1 is called R 11, so I’m guessing v, 9 stands for version 9 and R. Eleven stands for revision, 11, all right, so there’s really no similar naming convention there. That makes you wonder, like well what’s the deal, and I could be completely wrong about what v 9 and hour 11 stand for, but or you could look at like the release date. So this one was February of 2012. This was October, so the most recent one is in October. So no another thing you could look at is choose the download count.
This one man, this one’s, been downloaded a lot this one not so much so maybe this one is the more popular one. Well, I’m going to go ahead and go with timer, one so I’m, just going to click on it and it’s. Going to ask me to it’s going to take me here and now. I want to download that alright I’ll just click this download so it’s going to ask me: where do I want to download it you’ll notice I’ve already downloaded it. I am going to actually I’m going to name it this right here so timer, 1, R, 1, 1 and I’ll save that ok. Now what I will do is so I’ve saved it. Alright, so let’s go ahead and go into our dueo and see what happens when i try to import that library so i’m going to go to tools? Oh man, sketch import library, add library now i’m going to navigate to where i did that. So i will go to where i downloaded that library, here’s downloads and i’m trying to remember what the name of that library was that i just installed. It was timer 1 that’s right, ok, timer, 1, alright, so I’m going to choose that library! Oh hey what’s! This! I get a little caution down here says the library timer 1rr cannot be used. Library names must contain only basic letters and numbers ascii. Only in no spaces it cannot start with a number. So there is an a problem with the naming convention of this library.
So if I come up, let’s just verify if I come to sketch import library notice that it didn’t get added here, so what we need to do so this is it. This is actually a pretty common problem. So this is what we need to do. Let’S go find out where we let’s go, find out where we downloaded that file downloads programs to install timer1 alright. So what I’m going to do is I am going to open this file with my archive utility and windows would be like WinZip, probably ok, I’m going to click on this and it’s going to expand it out right. There ok so notice that look I’ve got examples, dot, CP and timer dot; H, ok, so here’s all the files right here, here’s the examples and here’s that collective folder that those are all in so what I’m going to do now is. I am gon na I’m going to copy that alright and I’m going to put it in my Arduino directory so I’m going to come to where my Arduino installation is so. I go to documents programmer Arduino I’m, going to come down to that libraries, folder, okay, here’s, the libraries folder and now I’m going to paste that item so here’s timer 1a it’s – all in here, but wait a second. Do you notice what the issue is? Look? These do not match that folder name and those names, the dot CP and the CPP and the dot H file they have are not the same name.
So let me change the name of this folder to timer dot 1 or just timer 1. Now they all match. Don’T they that’s easy enough. I mean that was nothing crazy. I just made sure that all those files were the same name. I could name this, you know Arduino 1, 2, 3 or I could name it or 42. It doesn’t really matter as long as it’s. The same name, okay, so that is taken care of so now let’s go ahead, here’s our Arduino IDE, and I just want to show you something real quick. If I go, if I go to sketch import library notice that it’s not under contributed, it did not get added here. The reason that is is we have to restart Arduino so I’m going to ahead and quit Arduino. I don’t need to save that and I will go ahead and open up your dwee, no IDE again and now let’s go to sketch import library and there it is look at that timer one. Now it got added okay, so here’s timer, one and we’re again all it does is say it includes timer. One well let’s go ahead and take a look at the example. Examples: timer one read receiver, so here’s an example of the read receiver and you know if we wanted to use this, it could be installed so that’s, basically how you install a library, okay, so there’s two ways to do it. The first way is you just go to sketch import library, add library all right, and then you navigate to the zip file that you downloaded for that library and then arduino behind the scenes is going to automatically pull that or it’s going to unzip that file and It’S going to put it in your libraries folder, so there’s option one option: two: is you download that zip file you locate it on your own? You know you find out where you installed it, you unzip it.
You know, and then you copy that folder and then you you yourself, move that folder over to wherever you have your arduino installation at again, and the arduino installation is just going to say. Arduino and you know, you’ve come down to the libraries folder which will you will have, and then you just paste it in there yourself and then you just got to make sure that those names are the same and that’s really how to set up a library it’s. That easy, okay, now using the library, is something a little different. We can talk about that later, but that’s kind of getting a library set up so that’s that now one quick thing I will do will show you real quick here. If you go to, if you’re wondering where your Arduino installation is set up, maybe you just you don’t happen to know. If you go to file and I’m sorry, if you go to our dwee no preferences, this is going to show you where you need to go. So mine is users, my name documents programmer Arduino okay. So this is going to tell you the file path to where that our dueo folder is that we just talked about and if you hit browse it’ll, take you right there. Okay, so you can find it easily. If you don’t happen to know off the top of your head, okay, well, that is that I hope that was helpful and I’m going to be doing a lot more of these in this coming year.
So, if you have a question about Arduino shoot me, an email. Go to the website, it’s open source hardware group comm go to the website. You should definitely sign up for the course the Arduino course if you haven’t yet it’s a free course, it’ll get you spun up on Arduino sign up for that there you can get my contact information. Send me an email. If you have a specific question and I’ll do my best to make a tutorial, hopefully it will be helpful.
how arduino libraries work Video
how arduino libraries work news
Posted on Friday October 25, 2019New Arduino Pro IDE: A closer look Blogdottv … Continue Reading »
Posted on Thursday October 17, 2019Bluetooth Control With Chrome Hackaday … Continue Reading »
Posted on Thursday October 10, 2019The Next Generation Arduino Nano Hackaday … Continue Reading »
how arduino libraries work Social