We have already talked about Arduino, introducing the main features to understand what it is and how it works. Now we want to describe how Arduino can be programmed by analyzing what is necessary on both the hardware and software sides.
We quickly recall that Arduino is actually a family of cards but for simplicity we are talking today about the most widespread board of the family, the Arduino UNO.
What is Arduino UNO
Arduino UNO is, trivially, a board with digital or analog communication ports (the so-called pins) to which sensors, but also small LEDs or mini displays can be connected.
The reasons for its diffusion in the field of basic electronics are mainly due to the low cost of the board, about 25 euros, in combination with a vast support for compatible accessories and online forums and guides that allow you to carry out many step-by-step projects.
How can Arduino be useful
The use of the Arduino board is currently mainly linked to the field of robotics. In fact, there are numerous projects and online tutorials that allow the construction of mini robots. The sale of third-party kits containing everything needed for the development and construction of various types of robots is also noteworthy.
Considering the widespread development of this card, it is possible that in the future it can also be used in the field of machine vision as well as in the educational field and for professional reasons.
What you need to get started with Arduino
Now let's see in detail how to start programming with Arduino and what we need, both in the software and hardware fields. In order to program it, first of all, on the hardware side, you need a PC, and of course the Arduino UNO board and a type B male - type A male USB cable, which will be used to physically connect the board to the computer. This cable is usually sold together with the card being purchased but if it is broken or lost it can be easily found online or in the best electronics stores.
For the software, instead, it is possible to simply download Arduino IDE from the official website or, if you have a Microsoft computer, from the store of the latest operating systems. The program, in addition to having the programming environment ready, contains a list of examples that cover every field of application, at least for the initial exercises.
How to program Arduino
First, open the program and immediately you will find yourself in front of the programming screen. It is important, if you have never programmed before, to go to Tools and select the board with which you want to run your first programs.
Once this is done, you are ready to go! Now you can connect your new Arduino board and install all the related drivers necessary for the correct recognition! Now try to load the first example sketch, which can be downloaded from the File -> Examples-> Basics -> 3. Blink, in order to analyze the whole structure.
The setup () and loop () functions
There are two main functions in every Arduino program: the setup () function and the loop () function. The first is essentially used to initialize variables, to set the necessary ports for the program and to include the various libraries that will be used. This function is performed only and exclusively once the board has been reset or when it is switched on for the first time.
The second, instead, contains the whole body of the program that is executed in loop (hence the name of the function).
Taking the Blink software as an example, the simplest one that simply 'blinks' (i.e. cyclically turns on and off) a LED, you can see how the loop () function contains two calls: the first digital/write is used to set the logical value of the led to high in order to turn it on, while the second digatal/write sets it to low, turning it off.
Each statement is terminated by a semicolon. Since LED 13 is digital, it only needs two voltage values to switch on and off.
Compiling and uploading
At this point you can compile the program by clicking on the Sketch menu -> Verify / Compile and then press the Load button to write the program on the board. Now trying to run it, you will notice that everything is done correctly!
In case you notice that the switching on and off of the LED are too close to each other, you can add the delay () function among the instructions, which simply makes you wait for the execution of the next instruction. The unit of measure of the instruction is the millisecond, so writing for example (1000)delay, the program will be paused for 1 second at each execution.
Trivial but not so obvious observation: remember to insert the delay even after the last function call, otherwise at each loop there will be no delay between the instructions!
Good work with Arduino!
Obviously you are only at the beginning of this journey with Arduino, and this is only the first, simple, program you can run. I recommend that you also look at the other examples contained in the IDE to better understand the structure and use of functions in the Arduino programming language ... and to experiment as much as possible, which is the real basis of electronics!
Here are some useful links to learn more about the topic:
The Arduino website;
Built-in examples, directly from the Arduino website (in English).
To conclude, you just have to start looking around and buy the first sensors to connect to your Arduino board, try to expand its features, test the sketches in the examples and start trying to write your own. Good fun!