• 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 an arduino library , writting librayys ardunio , how to setup a library from source code arduino , create library arduino windows text , arduino zip libraries , arduino writing

This Article Has Been Shared 488 Times!

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, 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

Get new posts by email:

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 (24.3K Followers)
  • Twitter (5.8k 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

  • Cyberpunk Aesthetics: What’s in it Special January 27, 2023
  • How to Do Electrical Layout Plan for Adding Smart Switches January 26, 2023
  • What is a Data Mesh? January 25, 2023
  • What is Vehicular Ad-Hoc Network? January 24, 2023
  • Difference Between Panel Light, COB Light, Track Light January 21, 2023

About This Article

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

Source:The Customize Windows, JiMA.in

PC users can consult Corrine Chorney for Security.

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

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

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

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT