Arduino Ide

What is the Arduino IDE? How do I use it? How to program on Arduino?

Introduction

The Arduino Integrated Development Environment (IDE) is the programming software that interfaces your Arduino board with the program. The Arduino IDE software features a compiler that transforms your program into machine language that can be understood by the Arduino board.

The Arduino IDE is a derivative of Processing. It’s open source and can be used to program boards other than the Arduino.

There are two ways to create a program for your Arduino board:

Block programming, i.e. you place elements on a diagram that correspond to elements in your program, such as a for loop block or variable block.

This works with an Arduino IDE module called Ardublock.

Line-of-code programming, which is what we’re going to do here.

In other words, you’ll write the lines of code in the Arduino language yourself, without the need for graphical aids.

Where can I download it?

You can download the Arduino IDE software from the Arduino website. It’s available on Windows, Mac and Linux, and is open source.

You can also find it on your system’s store by typing Arduino on the Microsoft Store, Apple Store and Linux App store.

Discover Arduino IDE

The Arduino IDE software has a number of features that you’ll find useful when making your programs.

I. The menu

We will now look at the most useful functions of the Arduino IDE menu.

File: contains examples of programs already created. You can also change the text font in the preferences, or print your program.
Edit: we can copy our program to put it directly on the Arduino forum, or in html. You can also find a word in your program, and increase the font size.
Sketch: This menu contains the same functions as the buttons below: verification, uploading. You can also add a file to your project and manage libraries.
Tools: The tools section contains the library manager, the serial monitor and the various Arduino boards you need to select for uploading.
Help: Contains information on the software version and how to use it.

II. The buttons

Let’s take a look at the most useful buttons in the Arduino IDE.

a) Verify button

The verify button enables the software to compile your program, i.e. transform it into machine language.

This will check that all syntax is correct, i.e. that you haven’t forgotten a semicolon or parenthesis, for example. If you have a compilation problem, you can watch our course on it.

b) Transfer button

The transfer button sends your program to your Arduino board.

The software will first check your program as with the check function, so you’ll also get syntax problems displayed.

 This program will then be sent via a cable to the Arduino board. If you have problems uploading, you can take a look at our course on uploading.

c) New Project button

This button allows you to create a new program. The software will open a second tab, so you won’t lose your first project.

d) Bouton Open


This button opens an existing program. The program will open in a new page.

e) Save button

It saves your program.

Program your Arduino board

The Arduino language is derived from the simplified C and C++ languages. These two languages are widely used for programming electronic boards, so it’s logical that the Arduino language is inspired by them. When you open a new program, you’ll see two functions: void setup and void loop. Let’s see how they work.

a) void setup ()

The void setup function is launched once at the start of the program.

It can be used to assign a value to a variable, associate a component with a pin and initialize a library or serial monitor.

b) void loop ()

The void loop function will loop through your program.

This function contains all the actions of your program. This is where you turn on a LED, control a motor...

How is a program structured?

We’ll now take a look at how a program is structured, to give you an idea of what you need to control your components.

There are two important rules for starting to code in the Arduino language:

All written actions must be terminated by a semicolon so that the arduino board understands that the action has been completed.
All functions begin and end with square brackets, so that the arduino board understands when the function begins and ends.
These are two things we often forget when we write our program, and which will cause problems when we check it.

Now let’s move on to the program.

Here’s an example of the program we’re going to describe in detail:

const int Led = 2; // declare the Led
void setup() { //initialization function 
   pinMode(Led, OUTPUT); //Led is assigned as output pin
}
void loop() { //main function executes ad infinitum
   digitalWrite(Led, HIGH); //turn Led on
}

I) Declaring variables

The first part comes before the void setup. This is where you declare your program’s variables.

What are variables for?

Variables are very useful in a program because they represent the component you want to control. It’s by changing the state of the variable that your LED will turn on or off.

So if you want to turn an LED on, you can declare an LED variable.

Before the void setup, you can also call the libraries you’ll need to drive your component. To do this, you need to call your library using square brackets.

We’ll look at a few key commands that are often useful before the void setup:

#include "Arduino Factory.h" // We include the library we're going to need
const int pin_INTERRUPTEUR = 2; // The switch is attached to pin 2

II. In the void setup

The void setup is a function that is executed only once at the start of the program. It allows you to assign a value to a variable, associate a component with a pin and initialize a library or serial monitor.

Here are a few key commands often used in a void setup:

void setup(){
  Serial.begin(9600); // Serial monitor initialization at 9600 bit/s or bauds.
  Serial.println("IR Receiver Button Decode"); // A message is written to the serial monitor at the start of the program and once
  pinMode(2, OUTPUT); // Initialize pin 2 at output
}

III. In Void Loop


The void loop function loops through your program. This function contains all the actions in your program. This is where you turn on a LED, control a motor…

We’ll look at a few key commands often used in a void loop:

void loop () {
  boolean buttonstatus = digitalRead(pin_INTERRUPTEUR); // Retrieves the status of the button connected to a digital pin
  analogWrite(led_Broche, HIGH); // Send the value to the LED connected to analog
  scanf("%d", &value); // Asks user to enter a number
  delay(30); // Wait before asking again 
}

Important code notions

We’ll now take a look at a number of functions you’ll come across frequently in programs.

a) For loop

The for instruction is used to repeat a block of instructions contained within braces. It’s an incrementing counter that stops once the counter has reached the end of its increment. It is used for repetitive operations.

for (int i = 0; i <= 255; i++) {
    //thing you want to do
  }

Here, the for loop starts at 0 and increments to 255. This means that the instruction block will execute 255 times.

b) The if statement

The if statement checks an instruction and executes the instruction block in the brace if the condition is true.

int x=150;
if (x > 120) {
 // Code you want to execute
}

Here we can see that the code executes only if x is greater than 120.

c) While loop

A while loop executes indefinitely until the value in the parenthesis becomes false.

var = 0;
while (var < 200) {
  // We do something that will be repeated 200 times.
  var++;
}

Here, the while loop will run until the var variable is greater than 200.

d) Delays

In many programs, you’ll see delays in the middle of execution, so that the user can see the value returned by the sensor, for example. This works with the delay() function, which is in milliseconds:

  delay(1000); // Wait one second

Programs already included in Arduino IDE

On the Arduino IDE, you’ll find pre-made codes. They can be useful to see how a component works and to test it quickly. You’ll find these codes in files and examples.

The example section contains a huge amount of code that will be useful whether you’re a beginner or an expert. You can use it as a code base for your projects, and then modify it to suit your needs!

 

Finally… Test your program!

Once you’ve finished your program, it’s time to check that it works. To do this, you need to click on the verification button we saw earlier.

 

If it says “Done compiling”, then your code is correct. This doesn’t mean that it will do what you want, but it does mean that there are no syntax errors.

If you have a red line, you’ve probably made a syntax error.

Problems compiling your program?

If you have problems clicking on check program, there’s a syntax error in your code. You can take a look at our course on compilation problems, which will help you resolve this error.

Bonus: How do libraries work on Arduino IDE?

To operate a particular component, a library may be required.

Libraries are a set of functions designed to simplify the use of a sensor or functionality.

As soon as an Arduino program contains a line beginning with #include, it calls a library.

Arduino IDE allows you to install and manage libraries in .zip or from the library manager. To find out more, take a look at our course on libraries!

How do I upload my program?

Once the program has been checked and completed, it’s time to upload it to the board. To do this, you need to connect your Arduino board to your computer.

I. Choosing the right type of board

The Arduino IDE software will not download the program in the same way to an Arduino Uno or Nano board, because the microprocessor inside is not the same. You must therefore specify which board you have chosen in the board manager:

If you can’t find your board in the list, you can click on Board Manager and download the module corresponding to your board.

II. Choosing the COM port

We’re now going to choose the COM port to which the Arduino board is connected. Often, the Arduino IDE software will suggest it directly, as follows:

All that’s left is to upload the program using the button we saw earlier. If you get an error, you’re having an upload problem.

Problems uploading your program?

When uploading your program, you may encounter problems, such as an unrecognized Arduino board. You can take a look at our course on uploading problems, which covers most problems.

The serial monitor

The serial monitor is an interface between the user and the Arduino board, which receives information from the Arduino board and displays it on the Arduino IDE. This information may be the temperature in degrees given by a sensor, or the angle of a servomotor.

The serial monitor can only be opened when the Arduino board is connected and the program is uploaded to it. It can be opened with the button on the left.

 

The user can also enter values from the serial monitor to control a servomotor, for example.

To have values on the serial monitor, you must set it up in your code:

void setup() {
    Serial.begin(9600); // Initialize communication with serial monitor (9600 bits/s)
}
void loop() {
    Serial.print("Distance in cm:"); // Display on serial monitor on same line
    Serial.println(distanceSensor.measureDistanceCm()); // Skip one line after the text.
}

The serial monitor must be set to a precise number of bit/s, in this case 9600. This will be useful for capturing information from the serial monitor. The serial monitor and the value you set on the Arduino board must be the same. You can leave the default 9600 bit/s if you don’t have a project that requires a precise value.

To go further...

Arduino IDE is just one of many programming environments. The Arduino is an open source world with numerous solutions for programming your board. In particular, there are block programming environments, and environments for programming your board in Python…

a) Ardublock

Ardublock is a module that can be added to the Arduino IDE.

 It lets you program in block form. You can create variables, loops and control your components. Once your code is complete, it is displayed directly as a program in the Arduino IDE.

b) Pyfirmata

Ever wanted to control your Arduino board from your computer using Python rather than the Arduino language? You’ve got Pyfirmata, a Python library based on the Firmata protocol.