Normally, we write sketch
on Arduino IDE, which essentially uses libraries. Library of Arduino is Set of Instructions to Avoid Repeated Huge Coding. As we discussed in how to learn Arduino programming, C++, C are important languages are important to learn for Arduino and embedded systems. Libraries add an abstraction from reality – which is practical for using things like a temperature humidity sensor. TM1637 7 segment LED display has lot of libraries, however we can avoid them and directly write code inside Arduino IDE. Basic is the construction of a working sketch
. Here is Guide on How to Write Arduino Library of Your Own With Example. This is probably a helpful resource for knowing Arduino platform specific :
1 | https://www.arduino.cc/en/Reference/HomePage?from=Reference.Extended |
How to Write Arduino Library of Your Own
An Arduino library contains :
- A directory with name of the thing, file name is the name of the thing (C++ language)
- A
.h
header file (C++ langugage) - A
.c
or.cpp
source code file, file name is the name of the thing (C or C++ language) - A optional file named
keywords.txt
Example of name of the thing
can be like TM1637, DHT11 etc. Usually unique names used to avoid confusion. It is practical to use Abhishek_TM1637
i.e. user named library for a custom user library. You can write those file using whatever editor you want, even plain text editors will work fine OR use Arduino IDE as code editor or use something like Visual Studio Code.
---
Arduino IDE places the libraries in specific directory within the operating system, like /usr/share/arduino/libraries
for GNU/Linux systems. This is a normal sketch
of LED Blink :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | int ledPin = 13; // LED connected to digital pin 13 void setup() // run once, when the sketch starts { pinMode(ledPin, OUTPUT); // sets the digital pin as output } void loop() // run over and over again { blink(); //call the code that makes the LED blink } //make the LED blink void blink(){ digitalWrite(ledPin, HIGH); // sets the LED on delay(1000); // waits for a second digitalWrite(ledPin, LOW); // sets the LED off delay(1000); // waits for a second } |
We can make that sketch
like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | int ledPin = 13; // LED connected to digital pin 13 void setup() // run once, when the sketch starts { pinMode(ledPin, OUTPUT); // sets the digital pin as output } void loop() // run over and over again { blink(2000); //call the code that makes the LED blink } //turn the LED on void on(){ digitalWrite(ledPin,HIGH); //set the pin HIGH and thus turn LED on } //turn the LED off void off(){ digitalWrite(ledPin,LOW); //set the pin LOW and thus turn LED off } //make the LED blink void blink(int time){ on(); // sets the LED on delay(time/2); // waits for a second off(); // sets the LED off delay(time/2); // waits for a second } |
Notice void on()
, void off()
, void blink(int time)
– we made the sketch extended to implement those three functionalities.
Take that, our example library will be called LedBlinkLib
. So, our directory structure of LedBlinkLib becoming :
1 2 3 4 5 6 7 | LedBlinkLib | +--LedBlinkLib.h | +--LedBlinkLib.cpp | +--keywords.txt |
We can easily drop that custom library directory to Arduino IDE’s library. Then restarting Arduino IDE will enable the library like other library.
Header File
The header is as if a summary of what the library contains. We can define some other libraries to load it. For our LedBlinkLib
header file will go like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #ifndef LEDBLINKLIB_H #define LEDBLINKLIB_H #include <Arduino.h> class LedBlinkLib { public: LedBlinkLib(); ~LedBlinkLib(); void on(); void off(); void blink(int time); }; #endif |
First two are referred to as an include guard to prevent the code from being included into the program binary multiple times. Third line includes the Arduino code to our library to enable to use the pinMode, digitalWrite, delay etc functions. Forth line is the beginning of the class LedBlinkLib
. Next line uses public keyword.
Sixth, Seventh lines are constructor and destructor – set up library (construct), and delete it (deconstruct). Remaining lines are probably obvious.
Source File
Next, we need to write LedBlinkLib.cpp
file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include "LedBlinkLib.h" //include the declaration for this class const byte LED_PIN = 13; //use the LED @ Arduino pin 13, this should not change so make it const (constant) //<<constructor>> setup the LED, make pin 13 an OUTPUT LedBlinkLib::LedBlinkLib(){ pinMode(LED_PIN, OUTPUT); //make that pin an OUTPUT } //<<destructor>> LedBlinkLib::~LedBlinkLib(){/*nothing to destruct*/} //turn the LED on void LedBlinkLib::on(){ digitalWrite(LED_PIN,HIGH); //set the pin HIGH and thus turn LED on } //turn the LED off void LedBlinkLib::off(){ digitalWrite(LED_PIN,LOW); //set the pin LOW and thus turn LED off } //blink the LED in a period equal to paramterer -time. void LedBlinkLib::blink(int time){ on(); //turn LED on delay(time/2); //wait half of the wanted period off(); //turn LED off delay(time/2); //wait the last half of the wanted period } |
Keywords File
keywords.txt
is not mandatory, however this is example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | ####################################### # Syntax Coloring Map For LedBlinkLib ####################################### ####################################### # Datatypes (KEYWORD1) ####################################### LedBlinkLibKEYWORD1 ####################################### # Methods and Functions (KEYWORD2) ####################################### init KEYWORD2 ####################################### # Constants (LITERAL1) ####################################### |
Example of Using the Library Created
To use this library, we will write a sketch
like this, except the time we have nothing to write more :
1 2 3 4 5 6 7 8 9 | #include <LedBlinkLib.h> LedBlinkLib led;//initialize an instance of the class void setup(){/*nothing to setup*/} void loop(){ led.blink(2000);//stay one second on, then a second off } |
Conclusion
This is a very primitive example, yet explains how libraries are written. You can publish the library, like this :
1 | https://github.com/AbhishekGhosh/LedBlinkLib |
You can distribute zip as release :
1 | https://github.com/AbhishekGhosh/LedBlinkLib/releases |