# Arduino for beginners

Arduino is a microcontroller board you can tell when to light an LED and when to read a sensor. You write the program in simplified C++, upload it over USB, and it runs on its own, without a computer. Your first blinking LED takes about twenty minutes.

## Safety first — before your first circuit

The rules are short, and you should not break them even "just once, carefully".

**Mains voltage is not for beginners.** Anything that plugs into a wall socket (120 V
or 230 V, depending on your country) must not be taken apart, modified or wired to
the board. Controlling a room light, a kettle or a heater looks like a tempting first
project and is not one: a mistake here costs a life, not a burnt board. Start, and
stay for your first year, with low-voltage circuits: power from USB, batteries or a
5–12 V adapter.

**Do not short the power supply.** A wire between the `5V` and `GND` pins is a short
circuit: the board heats up, the computer's port shuts down to protect itself, and
traces can burn out. Before applying power, trace the circuit with your eyes from plus
to minus and make sure there is a load in the path.

**An LED only through a resistor.** Without one, the LED fails almost immediately and
can take a board pin with it.

**Unplug USB before rewiring.** Moving wires on a powered board is a reliable way to
burn something.

**A motor never connects straight to a pin.** An Arduino Uno pin is rated for about
20 mA, and a motor, a relay or a long LED strip draws dozens of times more. Put a
transistor or a ready-made driver module with its own power supply between them.

**Mind the polarity.** An electrolytic capacitor connected backwards swells and can
pop; on an LED, the long leg is the plus.

## What the board is and which one to buy

The Arduino Uno is a board carrying an ATmega328P microcontroller, a USB connector for
talking to the computer and rows of pins along the edges. The key numbers: 16 MHz
clock speed, 32 KB of program memory, 2 KB of RAM, 5 V operating voltage.

The pins fall into three groups:

* **digital**, numbered 0–13 — they know only two states, "5 volts" and "nothing";
  six of them (3, 5, 6, 9, 10, 11) can also output an average value;
* **analog inputs** A0–A5 — they measure voltage and turn it into a number;
* **power** — the `5V`, `3.3V`, `GND` (common ground) and `VIN` pins.

Take an Uno as your first board: it has the largest supply of ready-made examples and
headers you simply push a wire into. The Nano has the same internals in a smaller
package and is handy when a circuit moves into the case of a finished device.

Besides the board you need a handful of parts: a solderless breadboard, jumper wires,
LEDs, 220 and 330 ohm resistors and tactile push buttons. All of this comes in starter
kits and is inexpensive.

## The IDE and your first upload

Download the Arduino IDE from the official site and install it. Then three steps:

1. connect the board with a USB cable;
2. in the Tools menu choose the board type — Arduino Uno;
3. in the same menu choose the port: on Windows it is `COM3` or similar, on Linux
   `/dev/ttyUSB0` or `/dev/ttyACM0`, on macOS `/dev/cu.usbmodem…`.

If on Linux the port is visible but the upload fails with a permissions error, add
your user to the `dialout` group and log in again — this logic is explained in the
permissions section of the [Linux commands](https://skillok.org/topics/linux-commands-cheat-sheet)
cheat sheet.

There are two buttons at the top of the window: the check mark verifies the program,
the arrow compiles it and uploads it to the board. The uploaded program stays in the
microcontroller's memory and starts by itself every time power is applied — you do not
need the computer after the upload.

## Your first program: blink

A program for Arduino is called a sketch. It always has two functions.

```cpp
void setup() {
  pinMode(13, OUTPUT);   // pin 13 works as an output
}

void loop() {
  digitalWrite(13, HIGH);  // apply voltage
  delay(1000);             // wait 1000 milliseconds
  digitalWrite(13, LOW);   // remove voltage
  delay(1000);
}
```

`setup()` runs once at power-up — that is where you configure pins and
communication. `loop()` repeats forever as long as there is power. `pinMode` sets a
pin's mode: `OUTPUT` to drive it, `INPUT` to read it. `digitalWrite` puts a high or low
level on the pin. `delay` pauses the program for the given number of milliseconds, so
1000 is one second.

Upload the sketch, and the LED soldered on the board next to pin 13 starts blinking.
Instead of the number 13 people usually write `LED_BUILTIN`: on different boards the
built-in LED sits on different pins.

## Your own LED circuit

Now the same sketch, but with an LED on the breadboard.

Build the circuit: board pin 8 → resistor → the LED's long leg; the LED's short leg →
`GND`. In the sketch, replace 13 with 8 in both functions.

The resistor value comes from Ohm's law. An LED is not a resistance; it has its own
voltage drop — about 2 V for a red one. The resistor takes the rest of the voltage:

```
R = (V supply − V LED) / I
R = (5 V − 2 V) / 0.01 A = 300 Ω
```

A current of 10 mA (0.01 A) is comfortable for an LED. Your kit will not have exactly
300 ohms; take the nearest larger standard value — 330 ohms — and that is the right
direction to round: a smaller value means more current. A 220 ohm resistor is also
common and acceptable — the current comes out at about 13.6 mA.

LED polarity: the long leg is the anode and goes to the board pin through the
resistor; the short leg is the cathode and goes to ground. Put it in backwards and it
will not light, but it will not break either.

## Reading inputs and sensors

A button is connected between a pin and ground, and the internal pull-up resistor is
enabled right in the program:

```cpp
void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int state = digitalRead(2);       // pressed — LOW, released — HIGH
  digitalWrite(LED_BUILTIN, state == LOW ? HIGH : LOW);
  Serial.println(state);
  delay(50);
}
```

The inverted logic is not a mistake: with `INPUT_PULLUP` the pin is pulled up to the
supply by itself, and a pressed button connects it to ground.

`Serial.begin(9600)` opens communication with the computer, and `Serial.println` sends
a line there. The Serial Monitor window in the IDE shows these lines — it is your main
debugging tool: you cannot inspect a variable with a debugger, but you can print it.

Analog inputs measure voltage and return a number from 0 to 1023, where 0 is zero
volts and 1023 is five. This is how you read a potentiometer, a photoresistor or a
soil moisture sensor:

```cpp
int raw = analogRead(A0);              // 0…1023
float volts = raw * 5.0 / 1023.0;      // convert to volts
```

The reverse operation is `analogWrite(pin, value)` with a value from 0 to 255: the
pin switches rapidly between zero and five volts, so an LED glows at partial
brightness and a motor behind a driver turns more slowly.

Sensors to start with: DHT11 or DHT22 — temperature and humidity, HC-SR04 — distance
by ultrasound, DS18B20 — precise temperature, a photoresistor — light level. Most of
them have a library: Tools → Manage Libraries, search by name, install, then take an
example from File → Examples.

## A plan for your first projects

1. **Blink and its variations.** Two LEDs taking turns, then a "running light" of five
   — this is where you learn arrays and the `for` loop.
2. **A button.** Light on press, then a toggle that changes state with every press.
   You will run into contact bounce and fix it with a pause or a time check.
3. **Fading.** `analogWrite` in a loop — the LED brightens and dims.
4. **A sensor and a threshold.** A photoresistor turns a light on when it gets dark;
   watch the values in the Serial Monitor and choose the threshold from them.
5. **Showing readings.** An I2C display or an RGB LED as a status indicator.
6. **A complete device.** A thermometer with a display, automatic watering by soil
   moisture, a door counter. This is where your first real problem appears — moving
   away from `delay`, which blocks the whole program, to comparing `millis()`.

A separate tip about `delay`: while the sketch has one job, it is convenient. As soon
as you need to blink and listen to a button at the same time, the pause gets in the
way, because during it the board does nothing. The "remember `millis()` and compare"
technique is shown in the BlinkWithoutDelay example that ships with the IDE.

## Common mistakes

**The port or board is not selected.** The most common cause of an upload error. Check
both items in the Tools menu.

**A missing common ground.** If part of the circuit has its own power supply, its
ground must be connected to the board's `GND`, otherwise readings are chaotic and the
module stays silent.

**`pinMode` inside `loop`.** You set the mode once in `setup`; in the loop it just
wastes time.

**An LED without a resistor "just to try".** It works right up until it does not.

**Long calculations inside `loop`.** The microcontroller does not multitask: while it
computes one thing, nothing else runs.

The sketch language is C++ with a ready-made set of functions and no manual build step.
Everything you learn about types, loops and functions in ordinary
[C++](https://skillok.org/topics/learn-cpp-from-scratch) works here exactly the same. For the circuit side,
see [how to learn electronics](https://skillok.org/topics/learn-electronics-from-scratch).

## Step-by-step plan

1. **Connect and upload an example** — Install the IDE, choose the board and port, upload Blink from the built-in examples.
2. **Build your own circuit** — Move the LED onto a breadboard through a resistor, choosing the value with Ohm's law.
3. **Add an input** — Connect a button with INPUT_PULLUP and control the LED by pressing it.
4. **Read a sensor** — Read values from an analog input, print them in the Serial Monitor and pick a trigger threshold.
5. **Move away from delay** — Rewrite blink to compare millis() so the button responds instantly.
6. **Build a device** — Combine a sensor, an indicator and logic into one finished thing that works without a computer.

## Check yourself

1. Which sketch function runs once when the board is powered up?
   - loop
   - setup
   - main
   - begin
2. Which function sets a pin to output mode?
   - digitalWrite
   - analogRead
   - pinMode
   - delay
3. Supply 5 V, LED voltage drop 2 V, desired current 0.01 A. How many ohms should the resistor be by Ohm's law?
4. What number do you pass to delay() for a 2-second pause?

## Sources

- [Arduino documentation](https://docs.arduino.cc/) — The primary source: boards, language reference and tutorials
- [Arduino language reference](https://www.arduino.cc/reference/en/) — pinMode, digitalWrite, analogRead and the other functions with examples
- [Tinkercad Circuits](https://www.tinkercad.com/) — Build and run an Arduino circuit in the browser without buying a board

---

https://skillok.org/articles/en/arduino-for-beginners
