• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » How to Write Arduino Library of Your Own

By Abhishek Ghosh June 16, 2018 12:39 am Updated on June 16, 2018

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 :

  1. A directory with name of the thing, file name is the name of the thing (C++ language)
  2. A .h header file (C++ langugage)
  3. A .c or .cpp source code file, file name is the name of the thing (C or C++ language)
  4. 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.

Advertisement

---

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

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

Vim
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.

How-to-Write-Arduino-Library-of-Your-Own

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

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

keywords.txt
Vim
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)
#######################################
 
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, then a second off
}

 

Conclusion

 

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

Vim
1
https://github.com/AbhishekGhosh/LedBlinkLib

You can distribute zip as release :

Vim
1
https://github.com/AbhishekGhosh/LedBlinkLib/releases

Tagged With how to write arduino library , writing arduino library , writing an arduino library , writing arduino libraries , how to write arduino code on our own , writting librayys ardunio , how to write a library for Arduino , how to setup a library from source code arduino , create library arduino windows text , arduino writing your own library

This Article Has Been Shared 561 Times!

Facebook Twitter Pinterest
Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Orthopaedic Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How to Write Arduino Library of Your Own

  • Clap Switch Using Arduino Uno And Microphone

    Here is How To Develop Clap Switch Using Arduino Uno And Microphone With Circuit Diagram, List of Components and Code. It is a basic project.

  • Arduino Spectrum Analyzer With MSGEQ7 IC

    It isTrue That Arduino itself Can Analyse Music Input. But Arduino Spectrum Analyzer With MSGEQ7 IC Makes the Total Thing Professional Grade.

  • Arduino TEMT6000 Ambient Light Sensor : Basics & Setup

    Here is Basics, Setup & Code for Arduino TEMT6000 Ambient Light Sensor. Ambient Light Sensor is used in electronic devices. SI Unit of ambient light is Luminance (lux).

  • Arduino : LED Switch On By Push Button Switch Off By IR Obstacle Sensor

    Here is Circuit Diagram and Code To Make LED Switch On By Push Button Switch Off By IR Obstacle Sensor and Arduino Board.

  • What Will Make Arduino To Read Data From Electronic Device?

    What Will Make Arduino To Read Data From Electronic Device? Quite Common Question and We Hope We Will Clarify the Basic Theory and Practical Around What to Do.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

You can subscribe to our Free Once a Day, Regular Newsletter by clicking the subscribe button below.

Click To Subscribe

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (21K Followers)
  • Twitter (5.3k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • How to Protect IP Cameras From Hackers February 25, 2021
  • 6 Sectors That Have Undergone Revamps in Digital Landscape February 24, 2021
  • What You Can Control with a Smartwatch and ESP32 February 23, 2021
  • What Does Data Cleansing Mean? February 21, 2021
  • How to Find the Right Software for Your Company February 20, 2021

 

About This Article

Cite this article as: Abhishek Ghosh, "How to Write Arduino Library of Your Own," in The Customize Windows, June 16, 2018, February 27, 2021, https://thecustomizewindows.com/2018/06/how-to-write-arduino-library-of-your-own/.

Source:The Customize Windows, JiMA.in

 

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Cookie Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2021 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy