⌁ Hardware & electronics

Arduino for beginners: microcontroller, IDE and first project

What Arduino is, how it differs from a computer, how setup and loop work, and how to read a button and light an LED without damaging the board.

Arduino is a platform created to make programmable electronics accessible to people who are not engineers. At its heart is a microcontroller: a chip that contains a processor, memory for the program, a little RAM and input and output peripherals. It has no operating system, no desktop, and does not connect to the Internet on its own. It runs a single program, in a continuous loop, from the moment it receives power. That very simplicity is what makes it reliable and well suited to controlling physical things.

Microcontroller or computer?

Arduino (e.g. Uno) Raspberry Pi
Operating system none Linux
Boot instant tens of seconds
Programs one at a time many at once
Logic voltage 5 V on the Uno, 3.3 V on many newer boards 3.3 V
Analog inputs yes no, external converters needed
Abrupt power-off no problem risk of corrupting the card

If the project has to read a sensor and react predictably, a microcontroller is often the best choice. If it has to handle data, networking and complex interfaces, you need a computer.

The development environment

You program it with the free Arduino IDE, or with more advanced editors through dedicated extensions. The language is C++ with a library that simplifies common operations. The workflow is always the same:

  1. connect the board via USB;
  2. select the board model and the port in the IDE;
  3. write the sketch, that is, the program;
  4. upload it with the upload button: it is compiled and transferred to the microcontroller's memory.

Once uploaded, the program stays in memory even without USB: just power the board and it starts again.

The structure of a sketch

Every program has two mandatory functions:

void setup() {
  // runs only once at startup
  pinMode(13, OUTPUT);
}

void loop() {
  // runs forever
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}

This is the classic "blink": it flashes the built-in LED connected to pin 13 on the Arduino Uno. It is trivial, but it checks in one go that the board, cable, driver and upload all work. It is worth always doing it as the first test with a new board.

Connecting an external LED

An LED is never connected directly between a pin and ground: without a limit, the current rises until it damages the LED or the pin. You need a series resistor. With a 5-volt output and a common red LED, a resistor between 220 and 330 ohms is a typical, cautious value.

Mind the orientation too: the LED has a longer leg, the anode, which goes toward the positive side, that is, toward the output pin.

Reading a button

Reading a digital input looks simple but hides a trap: a pin connected to nothing "floats" and returns random values. The solution is a pull-up resistor, which keeps the pin high until the button pulls it to ground. Arduino has an internal one, enabled like this:

const int button = 2;
const int led = 13;

void setup() {
  pinMode(button, INPUT_PULLUP);
  pinMode(led, OUTPUT);
}

void loop() {
  if (digitalRead(button) == LOW) {
    digitalWrite(led, HIGH);
  } else {
    digitalWrite(led, LOW);
  }
}

With the pull-up the logic is inverted: pressed reads LOW. The button is wired between pin 2 and GND.

Another phenomenon worth knowing is bounce: mechanical contacts oscillate for a few milliseconds as they close, and the microcontroller may read several presses. It is solved in software, by ignoring changes that come too close together, or with a small capacitor.

The serial monitor

To understand what the program is doing, the most useful tool is the serial port:

Serial.begin(9600);
Serial.println(analogRead(A0));

The values appear in the IDE's serial monitor. The speed set in the code must match the one chosen in the monitor, otherwise you see gibberish. It is the first thing to check when the output looks like garbage.

Common mistakes

  • Powering motors or relays from the pins: the outputs handle limited currents; bigger loads need transistors, drivers or relay modules with a separate supply.
  • No common ground: if you use two power supplies, their grounds must be connected, otherwise the signals have no reference.
  • Mixing 5 V and 3.3 V: a 3.3-volt sensor connected to a 5-volt output can break; level shifters are used.
  • Using delay() everywhere: it blocks the whole program. To handle several things at once, compare elapsed time with millis().

Where to go next

After LEDs and buttons, the natural next steps are a temperature sensor, a display, a small servo motor. Each component usually has a ready-made library you can install from the IDE. In my work I recommend always reading the component's datasheet, at least the part on maximum voltages and currents: that is where failures are avoided.

In short

  • Arduino is a microcontroller: it runs a single program, immediately, without an operating system.
  • Every sketch has setup(), run once, and loop(), repeated forever.
  • LEDs always need a series resistor, buttons a pull-up.
  • The serial monitor is the first diagnostic tool.
  • Large loads and different voltages require interface components.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. With degrees in computer science and psychology, he combines technical analysis with an understanding of the human factor.

Read also