How to Create an Arduino Library on Windows
by handyharris17 in Circuits > Arduino
1059 Views, 7 Favorites, 0 Comments
How to Create an Arduino Library on Windows
(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
- 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
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
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
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
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?
Any questions? Let me know! Email: uptownkitten453@gmail.com or the discussion board down below.
I
I
I
I
V