Background

Since programming microcontrollers with the Arduino IDE is different from writing a normal C or C++ program, this guide serves as a reference for the Arduino IDE. This guide is broken up in sections, so you can reference it whenever you are unsure about anything related to Arduino.

New Sketch

When you open a new sketch on the IDE there will be two functions; a void setup() function and a void loop() function. You can delete the comments in these sections and add your code to each.

In order to run any code you write in this sketch, you must first name the sketch by clicking File, then Save As… You can then choose where to save it, and what to name the file.

Another thing you must do before uploading a sketch to a board is selecting the type of board you use. To do this, click Tools, then Board:, then the type of board you are using. In the Wifi Clock guides, we use the NodeMCU 1.0 (ESP-12E Module).

The final thing you must do before you can upload a sketch is make sure the correct port is selected. To select a port, click Tools, then Port, then the correct port. If you are programming on a Windows computer, the port will look like COMX, where X is a number. If you are programming on a Linux or Mac computer, the port will look like /dev/ttyX where X could be a number, or the word USB followed by a number.

Setup Function

This function will only be run once when the microcontroller boots up. As the name implies, this is for setup. Usually you declare the pins you’re using, initialize any chips you’re using, setup interrupts, and set any initial conditions in this function.

Loop Function

This function will run repeatedly after the setup function is called. This function is for anything you want to do over and over again, such as counting, checking conditions, or updating variables. This function can also be left blank if you have setup interrupts to handle everything.

Adding Libraries

To add libraries to your IDE, click Sketch, then Include Library, then Manage Libraries… From there, you can install any libraries you want. For the Wifi Clock project, we downloaded the LedControl library.

There are two ways to include libraries with an Arduino sketch. The first is the normal way you would do it in C by typing an include statement followed by the library’s name. You can also have the IDE do this for you by clicking Sketch, then Include Library, then clicking on the desired library.

Before the Setup Function

Like in normal C programs, the place to include libraries and define constants is the first few lines. This should go before the setup function.

It is often necessary when programming microcontrollers to use global variables, whether they are normal variables such as counters, or they are custom objects. Before the setup function is the place to initialize this.

Board Manager

If you need to add a board to the IDE, such as the NodeMCU board we use with the Wifi Clock, you can do this in the board manager. To get the the board manager, click on Tools, then Board:, then Boards Manager… From this menu, you can install firmware for new boards, or update firmware for existing boards.

Note: You can only download new third party boards by first including the link to them in preferences. To do this, go to File, then Preferences, then add the URL to the Additional Boards Manager URLs. If you need to add more than one, you can separate them with a comma.