How to Write Arduino Library of Your Own

Advertisement

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 :

Vim

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 :

example.ino

Vim

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 pin13

void setup()//run once,whenthesketchstarts

{

pinMode(ledPin,OUTPUT);//sets the digital pin as output

}

void loop()//runoverandoveragain

{

blink();//callthe code that makes the LED blink

}

//make the LED blink

void blink(){

digitalWrite(ledPin,HIGH);//sets the LED on

delay(1000);//waits forasecond

digitalWrite(ledPin,LOW);//sets the LED off

delay(1000);//waits forasecond

}

We can make that sketch like this :

example.ino

Vim

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 pin13

void setup()//run once,whenthesketchstarts

{

pinMode(ledPin,OUTPUT);//sets the digital pin as output

}

void loop()//runoverandoveragain

{

blink(2000);//callthe code that makes the LED blink

}

//turn the LED on

void on(){

digitalWrite(ledPin,HIGH);//setthe pin HIGH and thus turn LED on

}

//turn the LED off

void off(){

digitalWrite(ledPin,LOW);//setthe pin LOW and thus turn LED off

}

//make the LED blink

void blink(int time){

on();//sets the LED on

delay(time/2);//waits forasecond

off();//sets the LED off

delay(time/2);//waits forasecond

}

Notice void on(), void off(), void blink(int time) – we made the sketch extended to implement those three functionalities.

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 :

LedBlinkLib.h

Vim

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#ifndef LEDBLINKLIB_H

#define LEDBLINKLIB_H

#include<Arduino.h>

classLedBlinkLib{

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 :

LedBlinkLib.cpp

Vim

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 forthis class

const byte LED_PIN=13;//use the LED@Arduino pin13,this should not change so make it const(constant)

//<<constructor>>setup the LED,make pin13an 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);//setthe pin HIGH and thus turn LED on

}

//turn the LED off

void LedBlinkLib::off(){

digitalWrite(LED_PIN,LOW);//setthe pin LOW and thus turn LED off

}

//blink the LED inaperiod 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 :

keywords.txt

Vim

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#######################################

#SyntaxColoring MapForLedBlinkLib

#######################################

#######################################

#Datatypes(KEYWORD1)

#######################################

LedBlinkLib KEYWORD1

#######################################

#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 :

Vim

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,thenasecond off

}

Conclusion

This is a very primitive example, yet explains how libraries are written. You can publish the library, like this :