How to Create an Arduino Library on Windows

by handyharris17 in Circuits > Arduino

1015 Views, 7 Favorites, 0 Comments

How to Create an Arduino Library on Windows

Library.jpeg
How to Make an Arduino Library

(This image was found via Google Images at pexels.com - pexels.com)

The only items you need for today's tutorial are the following:

  • Arduino (any)
  • USB type B
  • Some LEDs and resistors (between two-hundred and two-thousand ohms (3 LEDs and 3 resistors))

Creating Library Folder

Arduino IDE.JPG
File.JPG
Preferences.jpg
Browse.JPG
Arduino.JPG
libraries.JPG
libraries2.JPG

  1. Open Arduino IDE
  2. Click "File"
  3. Click "Preferences" (toward the bottom of the list)
  4. Click "Browse"
  5. Click "Arduino"
  6. Click "Libraries"
  7. If you are new to Arduino, the "libraries" folder should be empty, if not, it's okay because here we will add a new folder anyway by right-clicking and clicking "New Folder"
  8. Name your new library folder
  9. Press "Enter" when done

Creating Arduino Library

Notepad.png
Arduino IDE testLibrary.JPG

There are two MAIN ways to creating a new library via Arduino:

  1. Creating the folder in the Arduino IDE
  2. Or using a text editor, such as Notepad ++, to create the necessary things needed: Notepad ++ download

I will go over both.

Your First Sketch

firstSketch.JPG
newTab.JPG

If you're new to Arduino and have started your first sketch, chances are you have already seen this: ... This is the first sketch you will probably have started as out. I'm not going to explain how this works because that's for other tutorials, but it does a thing called "blink" or it flashes the LED on and off, simple, right? Anyway, we will basically do this in our new library (don't copy and paste this code).

/*
Blink
  Turns an LED on for one second, then off for one second, repeatedly.
  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
   https://www.arduino.cc/en/Main/Products
  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman
  This example code is in the public domain.
   http://www.arduino.cc/en/Tutorial/Blink

*
/ the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Outcome

Eventually, it will look like this for the example sketches. Much simpler, right?

#include "testLibrary.h"
testLibrary LEDPinSetup(2, 3, 4);
void loop() {
  // put your main code here, to run repeatedly:
  LEDPinSetup.on();
  delay(1000);
  LEDPinSetup.off();
  delay(1000);
}
void setup() {
  // put your setup code here, to run once:
}

Creating Your New Library Via the Arduino IDE

newTab.JPG
newLibraryTab.JPG

If you look back at the image, you can kinda see how I created the Arduino library, I just added some new tabs, easy. Here is how to do it:

  1. Open IDE
  2. Go to the top-right and with click on the downward triangle or arrow or Ctrl+Shift+N.
  3. Type in "testLibary.h" in the name section
  4. Repeat 1. and 2.
  5. Type in "testLibary.cpp" in the name section

Now, you have created two new tabs or files, great job!

Copy and paste the following from this website:

In the .h paste this:

/* * myFirstLibrary.h - An introduction to library setup
 * Created by Christian @ Core Electronics on 1/06/18 
* Revision #5 - See readMe  */
//	The #ifndef statement checks to see if the myFirstLibrary.h
//	file isn't already defined. This is to stop double declarations
//	of any identifiers within the library. It is paired with a
//	#endif at the bottom of the header and this setup is known as 
//	an 'Include Guard'. #ifndef myFirstLibrary_h
//	The #define statement defines this file as the myFirstLibrary
//	Header File so that it can be included within the source file. 
                                          #define myFirstLibrary_h
//	The #include of Arduino.h gives this library access to the standard
//	Arduino types and constants (HIGH, digitalWrite, etc.). It's 
//	unneccesary for sketches but required for libraries as they're not
//	.ino (Arduino) files.#include "Arduino.h"

//	The class is where all the functions for the library are stored,

//	along with all the variables required to make it operateclass myFirstLibrary{	

//	'public:' and 'private:' refer to the security of the functions	

//	and variables listed in that set. Contents that are public can be 	

//	accessed from a sketch for use, however private contents can only be	

//	accessed from within the class itself.	public:			

//	The first item in the class is known as the constructor. It shares the		

//	same name as the class and is used to create an instance of the class.		

//	It has no return type and is only used once per instance.			

myFirstLibrary(int pinOne, int pinTwo, int pinThree);				

//	Below are the functions of the class. They are the functions available		

//	in the library for a user to call.				

void on();              		

void off();		

void flash(int delayTime);	

private:                  				

//	When dealing with private variables, it is common convention to place		

//	an underscore before the variable name to let a user know the variable		

//	is private.				

int _pinOne, _pinTwo, _pinThree;};

//	The end wrapping of the #ifndef Include Guard

#endif

Arduino Creating a Library

In the .cpp tab copy and paste the following:

/*<br> * myFirstLibrary.cpp - An introduction to library setup
 * Created by Christian @ Core Electronics on 1/06/18
 * Revision #5 - See readMe
 *
/	The #include of Arduino.h gives this library access to the standard
//	Arduino types and constants (HIGH, digitalWrite, etc.). It's 
//	unneccesary for sketches but required for libraries as they're not
//	.ino (Arduino) files. 
#include "Arduino.h"
//	This will include the Header File so that the Source File has access
//	to the function definitions in the myFirstLibrary library.
#include "myFirstLibrary.h" 
//	This is where the constructor Source Code appears. The '::' indicates that
//	it is part of the myFirstLibrary class and should be used for all constructors
//	and functions that are part of a class.
myFirstLibrary::myFirstLibrary(int pinOne, int pinTwo, int pinThree){
	//	This is where the pinModes are defined for circuit operation.
	pinMode(pinOne, OUTPUT);
	pinMode(pinTwo, OUTPUT);
	pinMode(pinThree, OUTPUT);
	//	The arguments of the constructor are then saved into the private variables.
	_pinOne = pinOne;
	_pinTwo = pinTwo;
	_pinThree = pinThree;
}
//	For the 'on', 'off' and 'flash' functions, their function return type (void) is
//	specified before the class-function link. They also use the private variables
//	saved in the constructor code.
void myFirstLibrary::on(){
  digitalWrite(_pinOne, HIGH);
  digitalWrite(_pinTwo, HIGH);
  digitalWrite(_pinThree, HIGH);
}
void myFirstLibrary::off(){
  digitalWrite(_pinOne, LOW);
  digitalWrite(_pinTwo, LOW);
  digitalWrite(_pinThree, LOW);
}
void myFirstLibrary::flash(int delayTime){
  for(int i = 0; i < 4; i++){
  digitalWrite(_pinOne, HIGH);
  digitalWrite(_pinTwo, HIGH);
  digitalWrite(_pinThree, HIGH);
  delay(delayTime);
  digitalWrite(_pinOne, LOW);
  digitalWrite(_pinTwo, LOW);
  digitalWrite(_pinThree, LOW);
  delay(delayTime);
  }
}

Arduino Creating a Library

Now, onto the blank sketch, the sketch you were supposed to create in the beginning, copy and paste the following:

#include "testLibrary.h"
testLibrary LEDPinSetup(2, 3, 4);
void loop() {
  // put your main code here, to run repeatedly:
  LEDPinSetup.on();
  delay(1000);
  LEDPinSetup.off();
  delay(1000);
}
void setup() {
  // put your setup code here, to run once:
}

Arduino Creating Library

Notepad++ header.JPG
Notepad++ cpp.JPG
Notepad++ keywords.JPG
Notepad++readMe.JPG
My Documents.JPG
myTestLibrary.JPG
ZIP.JPG

Here is the different method of creating the library that I prefer:

  1. After completing step 1 do the following:
  2. Download Notepad ++ or use something similar, such as the regular old Notepad
  3. Copy and paste the .h file (save as C++ and .h for Notepad ++)
  4. Copy and paste the .cpp (save as C++ and .cpp for Notepad ++)
  5. Save the keywords as "keywords.txt"
  6. Ctrl+S (save) as .txt or click on .txt
  7. Save as "keywords.txt"
  8. And save the readMe.txt for library information (doesn't matter for this name).
  9. Save into a blank folder
  10. Create example code
  11. Create folder on desktop (for Windows) called "examples"
  12. Right click on desktop to create the new folder called "examples"
  13. Put your example code here
  14. Create a new zip file
  15. Put everything in the .zip file
  16. Add .zip file to Arduino by going to Sketch -> Include Library -> Add .zip library
  17. *This is how to check if the .zip file went in*
  18. Taskbar
  19. Folders
  20. Documents
  21. Arduino
  22. Libraries
  23. Open the new folder
  24. *Examples must be from .ino files or Notepad ++ .ino files*
  25. *If you have already completed the first (easiest) method, than don't do any of the following on the top*

Questions?

question.jpg

Any questions? Let me know! Email: uptownkitten453@gmail.com or the discussion board down below.

I

I

I

I

V