In the first entry in this series (HERE), we covered the installation of the IDE and a driver for the Nano board, as well as testing the board to make sure it works. In this entry, we’re navigating he Arduino IDE – specifically, on the code editor, the Console window, the serial monitor, and an introduction to the language reference. Please note, this series is still in the “introductory” phase, to help get beginners up to speed, though there may be a bit or a byte that you’re not familiar with yet.
Traditionally, an IDE has a source code editor, a tool to build (or compile) code from source, as well as debugging tools, for at least one programming language. The Arduino IDE works with a subset of the C++ programming language, though within a narrow framework. The application of your code in this framework is called a sketch, and is actually compiled into your own custom firmware for the Arduino board, overwriting its existing firmware!
In practical terms, that means you don’t have to really learn C++ in order to program your own boards. You only need to learn a handful of functions, the way that you format your commands, how to identify and work with the “pins” (inputs and outputs) for your board, and how to rip off code from examples. The real key to coding is to understand that somebody has almost always ALREADY done what you want to do and left the code where you can find it. A good programmer makes Google a vital part of his toolbox.
The components of the Arduino IDE:
There are three main sections of the IDE. The toolbar contains a set of buttons to access primary functions, along with menus for secondary functions. The source code editor is where you write your sketches – the programs that will control your Arduino boards. The Console window displays progress messages generated by the IDE during Verify/Compile/Upload, as well as any warnings or errors.
The Toolbar:
Verify – Checks for compiling errors in your code.
Upload – Compiles your code and uploads it to your Arduino (assuming there are no errors).
New – Creates a new sketch (opens a new instance of the Arduino IDE).
Open – Loads a menu of any libraries, example sketches, and all the sketches in your sketchbook (opens a new instance of the Arduino IDE).
Save – Saves your sketch.
Serial Monitor – Opens the serial monitor.
Additionally, there are secondary commands available through the menus above the Toolbar – File, Edit, Sketch, Tools, Help. A complete list of the individual menu and sub-menu items can be found in the official Arduino reference (HERE).
The Source Code Editor:
This is where you make your magic in the IDE. A new sketch will include two functions (setup and loop) – which are all you need to start the most basic sketches.
The setup function is fired off on startup and is used to set parameters used in the loop function. The loop function repeats until either a specified end condition is met or until you disconnect the Arduino from its power source. The basic setup of a sketch is:
- Define constants and variables
- Set parameters in Setup function
- Define and execute functionality in the Loop function.
You’re not limited to two functions or even to a single tab. You can create additional headers that are used and re-used in multiple sketches, as well as multiple functions to handle tasks within your sketch. The main limitation to the size of your code is the Flash on your Arduino board. The Nano boards we use as examples come with 32k, so you’re not going to write the next version of Office. You do have a lot of room to experiment with and learn, however.
The final of the three main sections of the IDE is the console window – which is where the compiler outputs messages when the Verify or Upload functions are executed. On a successful verify, the size required by the sketch, the amount (and percentage) of your board’s dynamic memory is used by the sketch’s variables – along with the maximum amount of memory available – plus the location where the BIN and other files are written. If there are syntax problems, catastrophic errors, or a failure to connect to/write to the board, those messages will display here, as well.
There is another component of the IDE that I consider mission critical – the Serial Monitor. The Serial Monitor allows you to read output from the Arduino while your sketch is running, as well as accepting input which can be used by the sketch. Obviously, the ability to input data and assign values to variables is huge, but, for me, the most important use of the Serial Monitor is debugging. Debugging is the process that a developer follows in order to identify and correct bugs in software.
When you’re learning how to write sketches, you should take the extra time to include debugging messages in your code, which you can view in the Serial Monitor. This will help you ensure that the results that you expect are the results that you actually achieve – which can be very difficult when you simply plug code into a board and let it run!
If you don’t have Arduino, open it now, and click the “new” button.
Enter this code:
char outputString[] = “The Counter Value is: “;
int counterVariable = 1;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
while (counterVariable < 11) {
Serial.print(outputString);
Serial.println(counterVariable);
delay(50);
counterVariable++;
}
}
Line 1 declares a constant (meaning the value will not change) string or character value. This is text we’re going to display in the Serial Monitor.
Line 2 initiates a counter variable (meaning the value will change during the execution of our code). The counter will perform two duties – it will be
displayed in the Serial monitor and it will also help control the duration of our program’s operation!
Line 3 is the setup function – you initialize the serial port (which is the com port assigned to your Arduino) and set the baud rate – in this case, 115,200 baud.
Line 7 is the loop function. Within the loop function, I use a “While” loop – which is one of the types of loops that will be covered in the next blog entry – and check the value of our counter variable. As long as it is less than 11, the loop will iterate.
Next, we PRINT out the constant (outputString). This text will be identical on every iteration of the loop.
The following step, we use PRINTLN to print the value of the counter.
This is an important distinction. The PRINT command by istelf will not move the text to the next line (called carriage return or line feed or both in other languages). The PRINTLN function, however, does (LN means line). By executing two lines of code, one with the PRINT function and the other with PRINTLN, we combine two variables into one line output to the serial monitor, followed by a carriage return.
After the PRINT and PRINTLN statements, we add a delay – this isn’t needed, but it helps us follow the output more easily in the Serial Monitor.
The final line advances the value of counterVariable by 1.
Click the Verify button to find any syntax errors in your code. Once any errors are corrected, click the Serial monitor button. Change the baud rate to match that set in our code (115200). Now click upload. You should end up with a display similar to this:
I can’t stress enough – once you start working on anything with more than a couple of simple functions, you will find that adding debugging code is a life saver. You can define variables to output like I did here or you can write messages in-line, just get into the habit before you start doing anything too complex.
The next article is going to cover the basic code and syntax, as well as some of the more useful functions and values, to prepare you for plugging components into a breadboard of your own and writing sketches to illustrate how they work!
In preparation for this, please check out the official Arduino Language Reference (HERE) – but don’t worry, we’ll cover everything you need as the lessons progress.
Also, in hopes of attracting more readers, we’ve got a new coupon for the Arduino Nano clone boards – in fact, we have two separate coupons for it! The offer from last week is still valid, but, this week, we’ve got something for those of you who like to solder your own headers:
These are the same Nano boards available at the same sale price, but, if you are learning to solder or teaching someone to solder, then this is a great application to test your skills. And, YES, we do have a 20% off coupon for you – CCUOJVPY – which will make the final cost $16.79 US (less than $3.36 per board)….. if you’ve got Prime, that will be in your hands in two days, shipped free.
However, we also have something special this week – I’ll send a 90% off coupon to the first five readers who email doug at smarterhome.club ! Once those coupons are gone, I’ll update the page to indicate that they’re gone (don’t blame me if the emails all come in while I’m asleep, however)!
SmarterHome.club is the website for our Facebook community, The Smarter Home Club – which is an umbrella for all kinds of smart home technologies – home automation, security, custom electronics, weather stations, alternative energy, you name it. DIY focused.
If you’re interested in joining the Smarter Home Club’s Facebook group, please follow this link:
One Reply to “Learn To Program Arduino: The IDE”
Comments are closed.