Language: EN

nuestro-primer-programa-en-arduino

Our first program in Arduino

In this post we are going to make our first program in Arduino. We assume that we have an Arduino UNO board or similar, as we saw in this post, and the standard Arduino IDE correctly installed, as we saw in this post.

Prepare the connection

First, we connect our Arduino board using a USB A-B cable, of the type commonly used to connect printers. For now, the power connection or additional cable is not necessary, for programming it is sufficient only with the USB.

arduino-usb-a-b-e1383069974142

Next, we open the Arduino IDE environment.

arduino-ide

We select the model of the board we are using.

arduino-board

We select the communication port to which it is connected.

arduino-porrt

  • In Windows it will be something like COM1, COM3…
  • In Linux it will be like /dev/ttyACM0

We already have the connection configured and ready to load our first program.

Basic program structure

In the standard Arduino IDE, programs always have the following structure

//Declaration zone

void setup()
{
  // Setup function zone
}

void loop()
{
  // Loop function zone
}

Where each part has the following function:

  • Declaration zone: In this part variables, functions, objects, and structures are declared.
  • Setup function: This function is executed every time the Arduino board is turned on, or the Reset key is pressed. It performs initialization functions for peripherals, communications, variables, etc.
  • Loop function: This function is executed continuously. It performs the bulk of automaton tasks.

This configuration (a setup function and a loop that runs continuously) is common in automation programming, being the only one allowed by the standard Arduino IDE. Other configurations are possible by using other IDEs.

Loading an example

To test the operation of our assembly, we are going to use one of the examples included in the Arduino IDE.

arduino-ejemplos

We select the example Basics/Blink, and a code similar to the following will appear.

const int pinLED= 13;    //assign variable led as 13

void setup()
{
  pinMode(pinLED, OUTPUT);     //define pin 13 as output  
}

void loop() {
  digitalWrite(pinLED, HIGH);   // turn on LED
  delay(1000);                  // wait one second
  digitalWrite(pinLED, LOW);    // turn off LED
  delay(1000);                  // wait one second

This example turns a LED on and off every second. The function of each line is commented on the right, but for now we won’t worry about the meaning, we will see these aspects later. The LED used is integrated in many Arduino boards (UNO, MEGA, etc) physically connected to PIN 13.

You can learn a lot by reading the examples, it is highly recommended to take a look at all of them.

Finally, we click on the highlighted button to compile and send the program to our Arduino board. After a few seconds, the IDE will compile the program and the screen should look similar to the following.

arduino-compilado

After a few flashes, the board will start running the program, turning the LED on and off. We agree that it’s not very spectacular, but the important thing is that if you have made it this far, it means that everything works and is well configured, and from here you are ready to start playing.

Don’t forget about PIN 13 when making your programs. It is very helpful for debugging your programs.

External power supply

Once we have programmed our board, it’s time to disconnect the USB cable and provide an external power supply. Arduino UNO and MEGA can be powered in two ways. The power source is selected automatically.

  • Regulated 5V power supply via the USB port. We can connect a transformer, an external 5V battery, or any other 5V source using a USB connector. This input must be regulated in power, that is, it must be stable, constant, and fixed at 5V, since Arduino does not monitor the voltage input through this input.
  • Power supply through the Vin connector, with a voltage between 6 and 20 volts, although ideally it is between 7 and 12 volts. For example, we can connect a transformer, one or several batteries, a 9V battery, or a group of 4 or 6 1.5V batteries, to power our projects.

Avoid voltages higher than 12 volts for extended periods. They can overheat the voltage regulators and damage the board.

If we connect our external power supply to our Arduino board, we will see that it executes the program we have created, of course without the need for a connected computer.

In the next tutorials, we will see the Arduino programming reference and the Arduino pinout diagram, delving into its use.

Download the code

All the code from this post is available for download on Github. github-full