Essential of the Arduino

Arduino, Home Automation, RoboticsNo Comments

You Are Here:, Home Automation, RoboticsEssential of the Arduino

In this tutorial, you will understand how an Arduino platform is working, how to use it and its utility in our projects.

What is an Arduino ?

The Arduino is a programming electronic platform (micro-controller)  which allow interaction in the “real world”. Thereby, the Arduino got “Pin(s)” which will be used as inputs or outputs depending of the project that we are realizing.

Kind of inputs : a temperature sensor, a motion detector, an electronic switch…

Kind of outputs : a light, a motor, a screen…

A classical Arduino board
A classical Arduino board

The double arrow shows the digital pins that we will use as inputs or outputs.

The double arrow shows POWER and ANALOG pins. We will see in another tutorial how to use them precisely.

The Arduino micro-controller can be powered with an USB cable (a printer cable), by a 9v battery or even with the 5v and the GND (=ground) pins (be careful of the voltage used).

How to get an Arduino ?

There is different types of Arduino : Genuino, Nano, Leonardo… In this website, we will mainly use the classical one : the Arduino UNO.

You can buy an Arduino UNO on this site for an average of 20$. You can even, if you want, buy an Arduino kit (with sensors, cables, LEDs…) so that you have all the stuff to start testing.

By the way, if you buy your Arduino on Aliexpress, Amazon, etc… Most of them are not directly compatible with the software that we’ll use. The activation of these ones will be in another tutorial 😉

The IDE Arduino 

Before going straightforward in code, we need to install the Arduino IDE to program.

To do so, go on the official website of Arduino : https://www.arduino.cc/

capture-decran-2016-09-15-a-08-51-01

 

Click on Download, select your operating system (Windows, macOS or Linux).

capture-decran-2016-09-15-a-08-51-07-1

 

Click on “Just download”.

capture-decran-2016-09-15-a-08-51-11

 

Your download is starting, once done, click on it and follow the installation steps.

capture-decran-2016-09-15-a-08-51-23

 

Launch the IDE, you should have this :

capture-decran-2016-09-15-a-08-55-44

 

It’s on this software that we will be writing instructions that we will transmit to the Arduino.

Arduino’s programmation logic

The goal of this part is to understand the Arduino’s programmation logic. That’s why we will be simplifying the code giving theoretical examples. We’ll study the pure Arduino code in another tutorial.

When we open an Arduino file (named sketch), we have by default this code

 void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

A sketch is always divided in three parts : the header, void setup() and void loop().

void setup() and void loop() are what we call “functions”.

  • The header

The header is a place where we initiate every necessary variables for our code. More simple : we indicate to the Arduino what we need in our project.

Easy example 1 : You want to create your robot, your robot needs two motors to go forward. You indicate the number of pin where is located your motors cables. For example, pin 8 and 9.

Theoretical code (false code just for logic) :

  motor1 = pin8
  motor2 = pin9

Easy example 2 : You want to get the temperature of a room with your temperature sensor. You’ll indicate the pin number where is located your sensor. For example pin 10.

Theoretical code :

  sensor = pin10
  • void setup()

That’s the first function of the sketch.

Setup means that we will set up all the necessary tools to execute our code.

Do you remember what we said just before ?

 

The Arduino got “Pin(s)” which will be used as inputs or outputs depending of the project that we are realizing.

 

In the set up function we will precise if we are going to use solicited pins as inputs or outputs.

In the first example, we will indicate that we want to use our pins as outputs, because the Arduino is sending informations to the motors and not the opposite.

Theoretical code :

 void setup() {
  
  pinMode(motor1, OUTPUT)
  pinMode(motor2, OUTPUT)

}

In the next example, we will indicate that we want to use pins as inputs, because the Arduino receive informations from the temperature sensor.

Code théorique :

 void setup() {
  
  pinMode(sensor, input)

}
  • void loop()

That’s the main function of the sketch.

The name of this function is loop, you can understand that what we’ll be writing in this function will be repeated like a loop. The Arduino is a micro-crontroller which is working endlessly. The loop will be executed until the Arduino is no more powered.

If you thought that this endless loop was an issue, you’ll see that the more you will write code, the more you will perceive it as an useful and easy to use function.

With the first example, we want that our robot going forward for 5 seconds, then stops for 2 seconds.

Theoretical code :

void loop() {
  goForward(motor1) //Motor 1 is going forward
  goForward(motor2) //Motor 1 is going forward
  wait(5) //We wait for 5 secs (while we wait, the motors are still going forward)

  stop(motor1) //Stop the Motor 1
  stop(motor2) //Stop the Motor 2
  wait(2) //We wait for 5 secs (while we wait, the motors are not moving)

//The loop is looping again :)

}

With the second example, we want to print to the screen the average temperature of a room thanks to our temperature sensor. This repeated every 5 minutes.

Theoretical code :

void loop() {
  printTemperature(sensor) //Shows the temperature
  wait(300) //We wait for 5 minutes (300 seconds)

}

Here is the full theoretical codes :

Example 1 :

motor1 = pin8
  motor2 = pin9

void setup() {
  
  pinMode(motor1, OUTPUT)
  pinMode(motor2, OUTPUT)

}

void loop() {
  goForward(motor1) //Motor 1 is going forward
  goForward(motor2) //Motor 1 is going forward
  wait(5) //We wait for 5 secs (while we wait, the motors are still going forward)

  stop(motor1) //Stop the Motor 1
  stop(motor2) //Stop the Motor 2
  wait(2) //We wait for 5 secs (while we wait, the motors are not moving)

//The loop is looping again :)

}

Example 2 :

  sensor = pin10

void setup() {
  
  pinMode(sensor, INPUT)

}

void loop() {
  printTemperature(sensor) //Shows temperature
  wait(300) //We wait for 5 minutes (300 seconds)

}

Maybe you’re not noticing but you just created two mini projects with the Arduino. Of course, like said earlier, these examples of code are not working but are very close of what we will produce. If you understand how an Arduino is generally working, the rest will be easier.

Saving and uploading

Vous can save your files like any other software, go to the File menu and click Save.

Arduino sketches got an “.ino” extension. Also, when you save your sketch, the IDE automatically creates a folder named like your sketch’s name. When you want to move your files, I advice you to move the entire folder instead of just the sketch. Other way, there will be compilation issues.

When your sketch is ready to be uploaded to the Arduino, plug it to your computer with the USB cable.

Next, go to the Tools menu, than “Board” and selected your type of board (we will mainly use the Arduino UNO).

 

typeboard

Always under the Tools menu, hoover the Port menu, select the one who corresponds to your Arduino. If you don’t know what type of port to select, simply memorize those which are listed, unplug your Arduino, take another look in the port menu, the one who disappeared is your Arduino. Re-plug it and select the good port.

port

 

Now that we have indicated to the software where was our Arduino, we can upload the sketch.

Take a look at the blue bar, the first logos will be those that you will be using :

barre-bleue

 

The first one at the top left is used to verify your code, the software will show the errors that your code could contain.

The second one (arrow going to the right), sends the code to the Arduino but not only : it checks if your sketch is correct (if you thought you would escape verification, too bad hahaha :D)

If the upload worked, you’ll see in the blue bar at the bottom the message : “Upload complete”. Your Arduino will be executing your program.

What’s next ?

Now, you know how an Arduino controller is working. The Arduino universe is a real gold mine for creatives, hobbyists or anyone who is interested about the idea of creating interacted objects.

Also, the Arduino is Open Source, this means that it is royalty free, you can even create yours. A lot of people are working together on it thanks to forums. On this website you will find what to begin with the Arduino, but I encourage you to go and search on the web a project/prototype that you are interested to do.

Nowadays, the Arduino platform is the brain of a lot of projects like the 3D printer.

Here is some projects that will give you the desire to learn more, all of them are Arduino based :

An automated house prototype with a security system based on sensors and cameras :

A M&M’s sorter, the Youtuber shows you every steps, from conception to production, through the code. NB : He is using a reduced version of the Arduino : The Arduino Nano, very interesting because like it’s name suggests it, it is even smaller than the Arduino UNO :

About the author:

Leave a Reply

Your email address will not be published. Required fields are marked *

Top