HW2: The first project with Arduino

In the previous article we have discussed about the various components on the board, now we will learn how to write the code and how to load it to the microcontroller in order to do something with Arduino.

As a first project with Arduino we will do the easiest thing possible, we will see the L LED blinking, but not that boring perfectly alternate blinking proposed by all the other other tutorials.

What we need:

  • Arduino UNO (or compatible)
  • USB cable
  • Arduino IDE

If you don’t already have Arduino IDE, you have to download it, you can find it here.

This software lets us write the code to do things with Arduino.
Once you run Arduino IDE you should see something like this

arduino ide

A couple of things to know before to start coding: be sure that in the menu Tools > Board is selected your Arduino board’s model, in our case “Arduino/Genuino Uno”, then in the menu Tools > Port you have to select the USB port where Arduino is connected, in this menu you will only see the ports’ numbers where something is connected, so it will be easy enough to choose the correct one, if you are unsure just unplug any USB device, the one remaining is the port used by Arduino and if in the future you will connect the board to that same port the number will not change.

To choose the correct port is very important or you could end up as me the first time I tried to load a sketch to the board, the code was correct, but the IDE didn’t want to load it and I couldn’t understand the reason, then I figured out that I was trying to load the code to my mouse!

I suggest you to connect Arduino to the PC only when you think that the code is ready to be uploaded, this is because during the time that the board is connected to the PC, it will be powered up and it will go on executing the last sketch uploaded and there is no reason to do that.

The IDE proposes two empty functions and they are the main functions we need:

  • setup() runs only once as the board is powered up, as the function’s name suggests, here we will write the code to setup Arduino
  • loop() runs over and over again until Arduino is powered up, here we will put the code to tell to Arduino what it has to do

On the interface you can see a few buttons, from left to right we have:

  • Verify – it checks the code for errors and asks you to save the code if you never did, because it has to know the sketche’s file name
  • Upload – uploads the code to Arduino
  • New, Load and Save are exactly what you expect: start to write a new sketch, load a saved sketch and save the current sketch
  • Serial monitor – on the far right, all alone, there is this button which opens a console usuful for debugging purpose, we will use it in the future

The programming language used by Arduino is a set of C/C++ functions, you can find a reference to the language here. All standard C/C++ constructs supported by avr-g++ should work in Arduino.

Now we are ready to write some code!

At least at the beginning, I will comment the code after the snippets because I will probably have many things to say and I don’t want to fill the code with comments for better readability. When we will be a bit more familiar with the code or when there will be more lines of code and it would be annoying to go up and down in the page I will comment within the snippets.

Line 2: the function  pinMode() says to Arduino how a pin will be used, it takes 2 parameters, the first is the pin number, the second is the mode, which can be INPUT or OUTPUT. It is necessary to declare how each pin we want to use will be used, in this case we say to Arduino that the pin 13 will be used as output, so it will send signals. We use the pin 13 because it is connected to the L LED on the board, changing the pin number we wouldn’t see anything, although a signal is being sent from that pin.

Line 6: declaration of a variable which will contain an integer number, we will use it to say to Arduino to pause and how long that pause will last.

Line 8: assigns to the variable millisecs a random number between 20 and 1000, this will say how long the pause will actually be.

Line 10: the function  digitalWrite() is used to send signals, it takes 2 parameters, the first is the pin number, the second is whether to send (HIGH) or not (LOW) a signal from the given pin. As the board is powered up, for all the pins declared as output, the signal will be “LOW” by default. In this case we say to Arduino to send a signal through the pin 13, which means that a 5V current will flow through that pin.

Line 11: delay() is used to pause the code, it takes an integer parameter which says the duration of the pause in milliseconds. We pass the variable millisecs to it, so the pause will be between 20 milliseconds and 1000 milliseconds (1 second), during this time the LED will stay on.

Line 13: as before, we assign a random number to the variable millisecs to have a different value than the previous, only to have a more chaotic blinking.

Line 15: same function as the one on line 10, but this time we pass LOW as second parameter to say to Arduino to stop giving power to the pin 13, so that the LED will be turned off.

Line 16: the same as what we did on line 11, this time the LED will stay off for the new value of the millisecs variable.

Now that we have written the code, let’s save it and click the Verify button to be sure that there are no syntax errors, if everything is ok we need to connect Arduino to our PC if we didn’t yet. Once that Arduino is connected we have to click the Upload button, after a few seconds you should see the L LED blinking randomly. If you see that, you have just uploaded your first working sketch.

In the next article I will write about a very useful component to build and test our project, the Breadboard, and we will use it for another easy test.

I’ve tested the code posted in this article on my board and it works properly, anyway I do not assume any responsibility for any damage which your components could suffer.