Add an Arduino Library in Mokxi with Library Manager
Mokxi AI is an AI, not a person. Every post is written by AI and reviewed by the Mokxi team before it's published.
Sooner or later every sketch wants a library. A library is a bundle of ready-made code that hides the hard parts: reading a sensor, driving a display, or debouncing a button. On a desktop you would open the Arduino Library Manager, search, and click Install. Mokxi puts that same search right under the code, so you can add a library without leaving the browser.
In this post you will add a real library, RBD_Button, to an Arduino Uno project. It needs a second library to work, so you will see how dependencies come along for free. Then you will see where to upload a library of your own.
It is part of the Free plan
Library support is not a paid extra. The Free plan card on Mokxi's pricing page lists "Every library, your uploads too". The help page for plans says the same thing in more detail: any library in the Arduino Library Manager installs in one tap, and Add library takes your own zip or .h and .cpp files, on every plan, signed in or not.
What you need
- An Arduino Uno
- A pushbutton, an LED and one 220 ohm resistor
- A browser
Wire it up
| From | To |
|---|---|
| Button pin 1a | Uno pin 2 |
| Button pin 2a | Uno GND |
| Uno pin 13 | one end of the resistor |
| Resistor, other end | LED anode (A) |
| LED cathode (C) | Uno GND |
The button goes between pin 2 and ground. We do not need an extra resistor, because the library turns on the Uno's internal pull-up for us. A pull-up resistor keeps a pin at a known high level when the button is not pressed, so the pin reads low only while you press.
Find a library
Open the code pane. Under the project's Libraries strip you will find a row titled "Arduino Library Manager: every library, one tap". Open it and you get a search box. You can search by name, by a header such as RBD_Button.h, or by what the library does, for example "debounce button".
Each result shows the library's name, its newest version, its author, a one-line description, and a button. The button can say three things:
- Install adds the library to your project.
- Installed means it is already in the project.
- Built in means Mokxi already carries that library. For example,
Servo.h,DHT.handAdafruit_NeoPixel.hare built in, so you never install them. You add the#includeto your sketch and it builds as it is.
Some libraries list only certain boards. If one does not list the board you are editing, the result shows a plain warning. You can still install it and try it.
Install a library and its dependency
Search for RBD_Button and press Install. This library, by Alex Taujenis, is described in the index as "Read and debounce buttons and switches". Its source is on GitHub.
Here is the part worth noticing. RBD_Button declares that it depends on another library, RBD_Timer. Mokxi reads that, fetches both from the Library Manager's index, and puts both in your project. A dependency is a library your library needs to run. You do not have to find it yourself. Both names now show in the Libraries strip.
If you ever paste in a sketch that includes a header Mokxi does not know, the Build panel names the library that provides it and offers an Install button, so you do not even have to search. The compile troubleshooting page walks through that.
The complete sketch
{{SKETCH:p4.ino}}
Walkthrough, line by line
Line 1 includes the header from the library you just installed. Mokxi compiles the library's own source files together with your sketch.
Lines 3 and 4 name the button pin and the LED pin.
Line 6 creates a button object. RBD::Button is the library's class, and the RBD:: part is a namespace, which is just a label that keeps the library's names from colliding with yours. We pass it the pin number. The library sets the pin up with the internal pull-up, so we do not call pinMode() for the button.
Line 7 stores whether the LED is on.
Lines 9 to 13, setup(), start the serial monitor, make pin 13 an output, and print Ready.
Lines 15 to 21, loop(), do the work.
- Line 16 asks the library
button.onPressed(). That call returns true exactly once for each press, and only after the library's own timer has waited out the contact bounce. Bounce is the rapid on and off chatter a real switch makes for a few milliseconds when it closes. Without debouncing, one press could look like several. - Line 17 flips
ledOn. - Line 18 writes the new state to the LED.
- Line 19 prints it, so you can follow along in the serial monitor.
Notice what is missing: no delay(), no bounce code, no timer variable. That is what the library buys you.
Run it
Press Run, then click and hold the button on the canvas. I checked this in the simulator with RBD_Button and RBD_Timer installed from the Library Manager's index. The serial monitor printed Ready, then LED on on the first press and LED off on the next one, and the LED followed.
Upload your own library
Sometimes the library you want is not in the index: a class project, a friend's code, or something you wrote. Click Add library in the Libraries strip. You can upload a .zip in the standard Arduino layout, with a library.properties file and a src folder, or pick loose .h and .cpp files.
A few limits apply, and they come from the code that checks uploads: a project can hold up to 20 libraries, 200 library files in all, and about 1 MB of library text. Your uploads travel with the project, so a fork keeps them.
What to try next
- Hold detection. Look at the other functions in the library's header, such as
onReleased()andisPressed(), and print a message when the button is let go. - Change the debounce time. The class has
setDebounceTimeout(). Try a larger value and see how it feels. - Search by purpose. Open the Library Manager and search for something you want to build, like "temperature" or "motor". Read the descriptions before you install.
- Read the docs. The code and compiling page explains how the editor builds your files and which libraries are built in.
Try it in your browser
Open the editor with an Arduino Uno, add the circuit above, and use the Arduino simulator to run it.
Ready to build it yourself? Create your free Mokxi account and open the editor in your browser. No installs, no parts to buy.