Language: EN

nuestro-primer-programa-en-arduino

Our first program in Arduino

In this entry, 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 entry, and the standard Arduino IDE correctly installed, as we saw in this entry.

Preparing the connection

First, we connect our Arduino board using a USB A-B cable, the type commonly used to connect printers. For now, there is no need for a power connection or additional cable; programming is sufficient with just the USB.

arduino-usb-a-b-e1383069974142

Next, we open the Arduino IDE environment. We select the model of the board we are using.

arduino-board

We select the communication port to which it is connected.

arduino-porrt

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

We now have the connection configured and ready to upload our first program.

Basic program structure

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

//DECLARATIONS AREA

void setup()
{
  // SETUP function area
}

void loop()
{
  // LOOP function area
}

Where each part has the following function:

  • Declarations area: In this part, variables, functions, objects, and structures are declared.
  • Setup function: This function runs every time the Arduino board is turned on or the Reset button is pressed. It initializes peripherals, communications, variables, etc.
  • Loop function: This function runs continuously. It performs the bulk of the tasks of the automaton.

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

Uploading an example

To test the functionality of our setup, we will 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 LED variable 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 LED used is integrated into many Arduino boards (UNO, MEGA, etc) physically connected to PIN 13.

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).

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

Finally, we click the highlighted button to compile and send the programming 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 blinks, the board will start executing the program, turning the LED on and off (we agree that it is not very spectacular, to say the least).

But the important thing is that if you have made it this far, it means everything works and is well configured, and from here you are ready to start playing 🎉.

Don’t forget about PIN 13 when creating 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 so that Arduino works without a computer.

Arduino UNO and MEGA can be powered through two means. The power source is selected automatically.

Regulated 5V power supply via USB port

We can connect a transformer, an external 5V battery, or any other 5V source via a USB connector.

This input must be regulated, meaning it must be stable, constant, and fixed at 5V (since Arduino does not monitor the voltage input through this port).

Power supply via the Vin connector

There is a PIN called Vin, through which we can power Arduino. We must supply a voltage between 6 to 20 volts, although the ideal range is between 7 to 12 volts. For example, we can:

  • Connect a transformer
  • A 9V battery
  • A grouping of 4 or 6 1.5V batteries

Avoid voltages higher than 12 volts for an extended period. 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 programming we have created, of course without the need for a connected computer.

Download the code

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