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](/proxy/?url=https://content.instructables.com/F7N/MJX9/KLP5YVQQ/F7NMJX9KLP5YVQQ.jpg&filename=Library.jpeg)
![How to Make an Arduino Library](/proxy/?url=https://content.instructables.com/F6I/DDCP/KLP5ZQGL/F6IDDCPKLP5ZQGL.jpg&filename=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](/proxy/?url=https://content.instructables.com/FNH/W6ZR/KLP5YVNO/FNHW6ZRKLP5YVNO.jpg&filename=Arduino IDE.JPG)
![File.JPG](/proxy/?url=https://content.instructables.com/F9I/OUDR/KLP5YVNP/F9IOUDRKLP5YVNP.jpg&filename=File.JPG)
![Preferences.jpg](/proxy/?url=https://content.instructables.com/FYR/LBB8/KLP5YW0H/FYRLBB8KLP5YW0H.jpg&filename=Preferences.jpg)
![Browse.JPG](/proxy/?url=https://content.instructables.com/FV2/4UBZ/KLP5YVNT/FV24UBZKLP5YVNT.jpg&filename=Browse.JPG)
![Arduino.JPG](/proxy/?url=https://content.instructables.com/FPA/V73A/KLP5YVNQ/FPAV73AKLP5YVNQ.jpg&filename=Arduino.JPG)
![libraries.JPG](/proxy/?url=https://content.instructables.com/F8A/CB7U/KLP5YVNR/F8ACB7UKLP5YVNR.jpg&filename=libraries.JPG)
![libraries2.JPG](/proxy/?url=https://content.instructables.com/FJL/E9JO/KLP5YVNS/FJLE9JOKLP5YVNS.jpg&filename=libraries2.JPG)
- Open Arduino IDE
- Click "File"
- Click "Preferences" (toward the bottom of the list)
- Click "Browse"
- Click "Arduino"
- Click "Libraries"
- 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"
- Name your new library folder
- Press "Enter" when done
Creating Arduino Library
![Notepad.png](/proxy/?url=https://content.instructables.com/F3U/6HQQ/KLP5YWL0/F3U6HQQKLP5YWL0.png&filename=Notepad.png)
![Arduino IDE testLibrary.JPG](/proxy/?url=https://content.instructables.com/FHC/C0JS/KLP5Z194/FHCC0JSKLP5Z194.jpg&filename=Arduino IDE testLibrary.JPG)
There are two MAIN ways to creating a new library via Arduino:
- Creating the folder in the Arduino IDE
- 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](/proxy/?url=https://content.instructables.com/FNP/PFXC/KLP5Z4ZX/FNPPFXCKLP5Z4ZX.jpg&filename=firstSketch.JPG)
![newTab.JPG](/proxy/?url=https://content.instructables.com/FU7/2SW3/KLP5Z8M7/FU72SW3KLP5Z8M7.jpg&filename=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](/proxy/?url=https://content.instructables.com/FM2/4Z5Y/KLP5Z6MD/FM24Z5YKLP5Z6MD.jpg&filename=newTab.JPG)
![newLibraryTab.JPG](/proxy/?url=https://content.instructables.com/FFI/BZ12/KLP5Z8WE/FFIBZ12KLP5Z8WE.jpg&filename=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:
- Open IDE
- Go to the top-right and with click on the downward triangle or arrow or Ctrl+Shift+N.
- Type in "testLibary.h" in the name section
- Repeat 1. and 2.
- 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](/proxy/?url=https://content.instructables.com/FIK/F7MH/KLP5ZBU2/FIKF7MHKLP5ZBU2.jpg&filename=Notepad++ header.JPG)
![Notepad++ cpp.JPG](/proxy/?url=https://content.instructables.com/FVT/FQ7A/KLP5ZBU3/FVTFQ7AKLP5ZBU3.jpg&filename=Notepad++ cpp.JPG)
![Notepad++ keywords.JPG](/proxy/?url=https://content.instructables.com/FHG/4TC2/KLP5ZBU4/FHG4TC2KLP5ZBU4.jpg&filename=Notepad++ keywords.JPG)
![Notepad++readMe.JPG](/proxy/?url=https://content.instructables.com/FIH/VZ5C/KLP5ZBU5/FIHVZ5CKLP5ZBU5.jpg&filename=Notepad++readMe.JPG)
![My Documents.JPG](/proxy/?url=https://content.instructables.com/FFJ/2BV9/KLP5ZCAK/FFJ2BV9KLP5ZCAK.jpg&filename=My Documents.JPG)
![myTestLibrary.JPG](/proxy/?url=https://content.instructables.com/FVP/YN36/KLP5ZCAL/FVPYN36KLP5ZCAL.jpg&filename=myTestLibrary.JPG)
![ZIP.JPG](/proxy/?url=https://content.instructables.com/FHV/IK06/KPTRPXAU/FHVIK06KPTRPXAU.jpg&filename=ZIP.JPG)
Here is the different method of creating the library that I prefer:
- After completing step 1 do the following:
- Download Notepad ++ or use something similar, such as the regular old Notepad
- Copy and paste the .h file (save as C++ and .h for Notepad ++)
- Copy and paste the .cpp (save as C++ and .cpp for Notepad ++)
- Save the keywords as "keywords.txt"
- Ctrl+S (save) as .txt or click on .txt
- Save as "keywords.txt"
- And save the readMe.txt for library information (doesn't matter for this name).
- Save into a blank folder
- Create example code
- Create folder on desktop (for Windows) called "examples"
- Right click on desktop to create the new folder called "examples"
- Put your example code here
- Create a new zip file
- Put everything in the .zip file
- Add .zip file to Arduino by going to Sketch -> Include Library -> Add .zip library
- *This is how to check if the .zip file went in*
- Taskbar
- Folders
- Documents
- Arduino
- Libraries
- Open the new folder
- *Examples must be from .ino files or Notepad ++ .ino files*
- *If you have already completed the first (easiest) method, than don't do any of the following on the top*
Questions?
![question.jpg](/proxy/?url=https://content.instructables.com/FR5/AV52/KLP5ZCPJ/FR5AV52KLP5ZCPJ.jpg&filename=question.jpg)
Any questions? Let me know! Email: uptownkitten453@gmail.com or the discussion board down below.
I
I
I
I
V