We all ever wanted to create our own library and share it so that others can use it. When we start creating projects with Arduino we reach a point where we need to install some library, either to control an LCD screen, a servo motor, or any sensor that uses a special protocol, we use them, but we don't know how they got there or even how they work.
This isn't an official definition or anything, but I like to describe libraries as a set of functions that do the hard work behind the scenes, functions that we can call from the main code without declaring them directly.
If you are a visual learner I know that a video is worth more than 1000 words, so here is a tutorial video. (I am a Spanish speaker, so please consider turning on English subtitles):
The HardwareFor this tutorial, we are going to use my energy meter module, since to use it you need a library and it is not officially published in the Arduino manager, so we will take advantage of it and do it. I invite you to see the tutorial where we created this module so that you are in context.
This module, in summary, has a chip specifically designed to measure energy and the variables associated with them such as voltage, current, power, and power factor. The chip communicates via SPI or serially, super common protocols found in almost any microcontroller. But let's get to the point.
The PCB and 3D partsI designed the circuit in EasyEDA, using the available components (which are many) to also be able to request the assembled board from there, from the surface mount components and those that are not, all that remained to do was solder the white connector and the pins. (for saving).
Note: I ordered my PCBs at JLCPCB PCBA assembly services.
If you want to know more about the circuit working principles, go and check this tutorial.
The 3D files are attached in this project and I really recommend JLCPCB 3D printing services.
How the library worksHow do we get the information from the chip? We can find this in its datasheet, we will compare it with the code in parallel.
The first thing we do to start using the module is to reset it, for this, we must send 0x5A5A5A hexadecimal to address 0x19, but in the code, we just write Reset() and that's it, but if we go to this function inside the library we will see that indeed, we are writing 0x5A5A5A to address 0x19.
We continue with the configuration of the frequency, according to the datasheet, we have 16 bits in register 0x18 and to configure the 60Hz we only have to set bit 9 to 1, which in the SetFrequency function, we can see, at address 0x18, we send the bit in 1 if it is at 60hz or in 0 if it is at 50hz.
I'm not going through every function but in essence, this is what happens in the background, with simple intuitive words, we configure the module, keeping the code understandable and aesthetic.
Uploading the library to GitHubNow we are going to share our library so that anyone can find it.
For this, first, we must upload it to GitHub, create a repository, clone it and upload our library files to it using the GitHub desktop version, at least that makes it easier for me.
Arduino has a standard that we must follow, within which the library is required to have the.cpp file, which is where we were reviewing the functions, the.h file that contains the declarations of all the functions and variables, some properties files where we define name, version, author, description, and other things. A license that we assign directly when we create the repository, the keywords or keywords where the functions and variables of the library are basically named, and a folder with the examples.
We go to the list of all the Arduino libraries, and we fork the list (this is to have a copy of ours), as I had done before it won't let me.
I go to the forked repository, click edit and add the link of ours, it can be anywhere.
We hit Commit changes, then Create pull request and then Create pull request again. Now we only have to wait for our library to be validated and accepted, this can take 1 day to appear in the Arduino IDE Library Manager.
We can go to the library manager, search for it by name and it should be there.
Let's try the example with the Raspberry Pi Pico that was left over from the module tutorial. We load the code, and there we have the energy measurements of our load.
We learned how to upload and share our own libraries with the Arduino community. This is very useful to share our electronics projects and also for having our own repository well sorted and trackable.
Libraries are free and we all have taken advantage of them, so it's time to start returning the favor back.
Comments
Please log in or sign up to comment.