How to use the Arduino IDE: A Beginners guide - Find out why Arduino is so popular. Quickly learn the
most important parts of the IDE; Edit and Program your Arduino Uno in
Under 10 Minutes.
The Arduino IDE (Integrated Development Environment) is one of the most, if not the most
popular, way to program microcontrollers (on Arduino boards such as the
Arduino Uno). The reason is that all that command line complexity is
hidden away under the hood, so you get all the benefits of a modern (and
always kept up-to-date) compiler and none of the
complexity.
Note: The Arduino IDE had a major update to 2.x.x around May 2022.
It means you can write code in the latest flavor of C or C++ but
programming the Arduino board is actually push-button simple - all you
need to do is download the (free) Arduino IDE, buy a board (e.g. Arduino
Uno), get or use the supplied USB cable and you're ready to go.
This, is why the Arduino system is so popular. All you have to do is
Learn C/C++ programming and you can program any Arduino board!
This tutorial shows you, in detail, screen shots of each part of the process so you can
follow along. All you need is an Arduino Uno (or other type e.g. Nano, Micro etc.)
and a USB cable.
The Arduino IDE:
Is a text editor for your C/C++ programs (Arduino code).
Is a compiler for the chip you are using (depends on the board you use).
Is an uploader (and verifier) of the target board code.
Lets you program many different types of target boards.
Gives you simple access to example code to run immediately.
Simplifies the C compilation process (so much so that you push a button and it uploads to the target board).
Support for standard board such as the Arduino Uno, Arduino Nano,
Arduino Mega and many others is built into the
software.
Note: The new Arduino IDE 2.x.x. is different but functionally the same.
The 2.x.x Arduino IDE is a total rewrite using a different language
under the hood.
The Arduino IDE is the software side of the Arduino system that
allows you to program any Arduino board using a C/C++ program. It
provides a generic interface to many different types of Arduino chip so
you don't need to setup a compiler for each chip.
The following guide shows you exactly how to use the Arduino IDE with an Arduino Uno board. You can use this guide for any supported board.
The Arduino IDE allows you to choose a board and use it instantly. There is no
going the the manufacturer website and getting their compiler
specifically for their chip, then buying a programming interface from
them etc. All that is done under the hood and for free!
All you need is:
A PC with a spare USB port,
A USB cable,
An Arduino board,
the Arduino IDE software.
You use the Arduino IDE to upload programs to an Arduino board (Here
Arduino Uno is used). These are known as sketches in Arduino (in normal
programming you call them 'c/c++ source files'). These files have the
type extension of .cpp meaing c plus plus - Note you can use C or C++ in
these files.
The IDE gives you a very simple interface which is one of the
reasons the Arduino is so popular. It gives you a simple editor and
virtually one button programming of the Arduino.
Here's the IDE when you
first start it.
The white area is where you enter your programs to
control the Arduino (currently has the default functions setup() and
loop() ready for you. Bottom right shows you the board currently
selected- and its status - (the current board is also shown in the top
board selection box).
The setup code and loop functions are always required in an Arduino
program. The function setup() is executed once, while the function loop()
is executed repeatedly.
Ardino IDE: Boards Type Manager
This section is included for completeness - you can skip it when
using the Arduino Uno - as support for this board is included as
default.
The boards manager lets you select a type of board from a specific
manufacturer and downloads all the relevant files to allow that board to
be programmed.
It means that the Arduino IDE does not have to be re-programmed every
time a new board type is created (for example a set of boards that use a
different microcontroller). If a manufacturer creates a new board type
they can create all the
required files to allow the automatic programming of their hardware
using the Arduino IDE.
The convenience of the Boards Manager lies in its ability to
facilitate quick transitions between different hardware platforms. So
you can easily change between an Arduino Nano or an ESP32 for
instance.
To set up the IDE for your board, use
the Menu:
Menu > Tools > Board > Boards Manager:
Or in the new IDE 2.x.x onwards click the 2nd icon down (it looks like an Arduino Uno board) on the left panel.
A new panel appears with a search box at the top - In this case it is
used to search for the "Arduino AVR".
Note: AVR is the manufacturer of the chip that Arduino Uno uses.
The "Arduino AVR" text shown below "BOARDS MANAGER"
was entered into the search box:
You can see that the board group (Arduino AVR) is already installed
(because the text "REMOVE" is present) so there is nothing to
do here. This step is to show you how to install a new board into the
Arduino IDE.
If you type ESP32 into the search box you'll get results for ESP32
support - the ESP32 is not installed with the Arduino IDE, so you would click "Install" to download the necessary files.
TIP: Leave the search box blank and scroll down to see more supported devices.
Note: To get rid of the boards manager panel, click the icon again.
Ardino IDE: Board Selection
The Arduino IDE can program different types of boards such as the
Uno, Nano, Mega, Leonardo and many more.
Arduino IDE: Board Selection old way
To set up the IDE for your board there are two ways - the old way using the Menu:
Menu > Tools > Board >
Hovering over the available board types (Here Arduino AVR boards)
shows the long list of available boards (as above). This is fine for a
small number of boards but means endless scrolling to find a specific
board - actually the list here was longer than shown - the image got cut
off!
Arduino IDE: Board selection new way
For the new 2.x.x interface you can use the easier board selection box at the
top of the IDE. Clicking the existing text in the search box (here
Arduino Nano - the last one I used) shows a drop down box:
Now click the entry "Select other board and port" - above to show the following popup box:
Here the central search box was filled with text "arduino uno" and below
that the options were scrolled down until "Arduino Uno" was found. It was clicked and then shows a selection
(marked with a tick) - Click OK to finish.
Now you'll see the main interface
(board selection box at the top) has changed from "Arduino Nano" to "Arduino Uno".
Arduino Uno to PC link setup
Serial Port Selection old way
The Arduino Uno communicates to your PC via a serial USB link using an
RS232 port. You now need to select the port in use by the Arduino.
Plug in the USB cable from your PC to the Arduino.
To set up the serial port connection there are two ways - the old way using the Menu:
Menu > Tools > Port
You will see the new port (after plugging in your Uno) - Select it.
Note: Your port number may be different e.g. COM5. Select whichever
has the "Arduino/Genuion Uno" text (or is a new port entry - sometimes
text is not shown).
Note: The old interface had text showing which port the Arduino is
attached to but the new one does not. This information is however shown
in the new port connection method (See the next section).
Serial Port selection new way
As before click the entry "Select other board and port" at the top of the main interface - to show the following popup box.
Setup Check
Having selected the port you can check by using the Menu:
Menu > Tools > Get Board
Info
If you selected the board correctly (and the Arduino was plugged
in) then you will see the Board and Port in use, similar to the above
image.
Now check that the board is the one expected by clicking 'Get Board
Info' (in the above menu). You will see a box popup with some serial
number information similar to the following:
BN: Arduino Uno
VID: 2341
PID: 0043
SN: 9533130303435130C2B1
Run your First Program
Using the serial Port
As well as loading programs into the Arduino you can use the USB
serial link to communicate with the board to show debug or status information, or even to control the operation of your program.
Typically you will
output information to the PC screen (from the Arduino) and quite often use this output to
debug what is going on in the program. But for now lets just get some
text output from the Arduino.
This program is going to output "Hi there" using the serial monitor built into the Arduino IDE.
First of all : Copy and paste the following program into the white
text area of the IDE replacing what is already there.
Once you have done
that hit the save button in the IDE and save the program as hi-there.
The tab in the IDE will change to the text "hi-there" as below.
Warning: There's an intentional error in this code - correct it below.
Compiling and Uploading
To change the Ardino C/C++ code (in the editor above) into code that
the specific processor chip that is on the Arduino board the program
must be compiled. For the Arduino IDE this is all done in the background
with progress indicated in the lower black box.
The tick mark on the left is the verify button - this verifies that
your code is correct i.e. compilable (without performing an upload to the board). The right arrow is the upload button. You only
need to hit this button as compilation always takes place before an upload.
So hit the upload button.
Observe what happens.
it didn't work!
There was an error in the code!
Click to show error screen You
can see that the line with the error is highlighted in pink and the
error message is in a popup window above the black panel. If there
are many errors you can expand the black panel to see them all.
The black panel normally shows status information on the compilation state and then the uploading state.
In C/C++ programming you have to be very accurate in the words used -
even putting a capital letter instead of lower case one (as here) means that
the program will not compile.
The problem is that the word 'Begin' should be all-lowercase.
You will get used to these "syntax errors" and as you get used to programming will have fewer and fewer of these types of error.
If you don't know why an error occurred then search for 'Serial' in the
Arduino reference - this gives you the details of functions you can
use.
The main serial reference is here
and the specific information is here (found by clicking begin() from within the link above).
Lets correct the error and contine
To correct the error simply change the capital 'B' to lowercase 'b'.
Then save the file again. Now click the right arrow upload button again.
The right arrow will change to an orange background during the compilation and upload process:
Arduino IDE upload
Observe that the two green LEDs on the Arduino Uno are flashing
during the upload process (this is a small program so it will be for a short
time). Uploading begins immediately compilation is complete. The LEDs indicate that communication is working in both directions
between the PC and the Uno.
You may need to re-upload to see this as it is quite a short upload time for this small program.
When compilation is complete you'll get a status report showing how much memory has been used for Flash and RAM.
Flash is the program storage area (fixed data - contents kept on power down) and
for the Arduino Uno is 32kBytes, while RAM is the variable storage area
(changing data - contents lost on power down) and for the Arduino Uno is 2kBytes.
The following image shows that program summary:
You can scroll the bottom message but the contents is:
Sketch uses 1488 bytes (4%) of program storage space.
Maximum is 32256 bytes.
Global variables use 198 bytes (9%) of dynamic memory, leaving 1850 bytes for local variables. Maximum is 2048 bytes.
When you create larger programs you may need to keep an eye on these
numbers so you don't run out of memory (of course you could use an Arduino
Mega2560 that has 256k Flash and 8k RAM!).
Open the Arduino IDE Serial monitor
To see the output data from the Arduino Uno start the serial monitor
either by clicking the magnifying glass at top right of the Arduino IDE or using the menu:
Menu > Tools > Serial Monitor
Serial output from The Arduino Uno
The output you should see is shown below:
You can see the output from the Arduino Uno in the main window. If
you don't see it make sure you set the Baud rate in the above window
(bottom right) to 9600 baud.
To make the Arduino output the data again press the reset button on the Arduino Uno (top left of the board).
So that completes installing, and setting up the Arduino IDE. You
have also programmed the Arduino Uno with your first program and should
now be familiar with programming the Arudino Uno with code.
Tips for using the IDE
Easy Arduino IDE Block Comments
Quite often you will want to get rid of a block of troublesome code
(temporarily) and there's an easy way of doing that - you add two
forward slashes at the beginning of each line you want to turn into a
comment (non action code).
You don't have to
use the cursor keys and forwards slash multiple times. just select some
lines of code and use the following keystroke:
Use Control + '/'
It also undoes commenting. Just select a set of lines and hit Control+'/' to toggle commented lines.
Arduino IDE Code Help
To get help right click on code in the text editor window, then select find in reference.
You can find Arduino code help by selecting an element
of
code, for example and selecting Serial in the code above, then right click to
open the popup menu Then choose "Find in reference". This will open a
page in your browser showing you how Serial operates.
Arduino IDE Setup Tab Spaces
This one is a bit of a pain to do (not too much though). The default tab
spacing indent is 2 spaces. Most coders use three or four spaces so I
like to change it to 3 spaces.
You can't set this unless you manually edit the Arduino IDE preferences
file. So open the preference panel to find out where the file is
located:
Menu > File > Preferences
And click the link below the text:
"More preferences can be edited directly in the file"
Now an explorer window will open. Double click the preferences.txt file to edit it.
Shut down the Arduino IDE.
Search down the file for:
editor.tabs.size=2
... and change it to:
editor.tabs.size=3
... or whatever you need. Save it and restart the Arduino IDE.
Using an Arduino IDE External Editor
When you develop code the Arduino IDE is Ok for starting out, but as
your file gets longer you will want to easily jump between
functions. So you will want to use an editor in which that can view all
the functions easily.
A free one is PSPAD. Install that.
To use the external editor save all Arduino IDE files then goto :
Menu > File > Preferences > Use external editor
Click the check box and Hit OK.
The Arduino IDE will be greyed out - so you can't use it for file editing.
Load the file into PSPAD and edit it as you need.
An easy way to do this is open a File from within the Arduino IDE.
Then open the file - the IDE will still be pointing to the current file
location. Don't click Open, just drag and drop the (.ino) file from the
"file-open-box" onto PSPAD.
At the moment this does not look to good as there is no syntax
highlighting and that is because PSPAD does not know what .ino means. In
fact .ino is just a C/C++ file so lets got and set that up in PSPAD.
(You can close the Arduno IDE file open box).
In PSPAD Select
Menu > View > Change Syntax
... and select the top one C/C++
Now you will see a nicely syntax coloured page. That's OK but we need the function list so select:
Menu > Tools > Code Explorer Window.
Now on the right hand side you can see a list of all your functions - just click on them to jump directly to them.
This makes it so much easier to navigate complex code.
You need to keep the Arduino IDE running for compilation. So make
edits in PSPAD and the Arduino IDE is ready to compile as above - it is
monitoring the current file in the window - you can see this if you
change the file in PSPAD and click into the Arduino IDE - the same
changes appear in the Arduino IDE.
After changes are made just compile and test your program on the Arduino Uno as usual.
Unlock the secrets of Arduino scrolling displays! This beginner-friendly guide shows you how to create real-time, dynamic graphics using an SSD1306 OLED, perfect for tracking sensor data and building…
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.