Arduino for Beginners: Your First Robot Programs
All articles

Arduino for Beginners: Your First Robot Programs

Arduino for beginners: the boards, the IDE, pins and PWM, reading a sensor, driving motors through a driver, the serial monitor, millis() and a small state machine.

Arduino for beginners: what a microcontroller is

A microcontroller is a whole small computer on one chip: a processor, flash memory for your program, a little RAM, and pins that read sensors and switch things on and off. No operating system, no screen: from power-on it runs one program, forever — the natural brain for a robot.

Arduino is an open-source platform around such chips: boards with USB, a free editor (the Arduino IDE), and thousands of examples and libraries. You write C++, but Arduino hides the hard parts.

Wondering how to learn Arduino for a robot? Programming a robot takes only the ideas below. New to voltage and current? Read Electronics basics for robotics first. GRA's 2026 SUMO rules count on a microcontroller too: the referee's start module needs a single input pin on an Arduino or a similar board.

Which board: Uno, Nano, Mega — and ESP32

  • Uno — the classic first board, in the first diagram: a 5 V ATmega328P at 16 MHz, 32 KB for programs, 14 digital pins and 6 analogue inputs.
  • Nano — the same chip on a small, breadboard-friendly board: the usual choice inside a small robot.
  • Mega 2560 — 54 digital pins and 16 analogue inputs, for robots with many sensors.
  • ESP32, in a word — far faster, with Wi-Fi and Bluetooth, but it runs on 3.3 V: a 5 V signal can damage its pins. GRA's 2026 SUMO rules allow no Bluetooth or other wireless control of a robot on the field.

The Uno's pin map:

  1. USB — uploads programs, gives 5 V, carries serial data;
  2. power jack — 7–12 V from a battery;
  3. digital pins 0–13;
  4. PWM pins, marked ~ (3, 5, 6, 9, 10, 11);
  5. pins 0 and 1 (RX/TX) — shared with USB: keep them free while uploading;
  6. power pins — 5V, 3.3V, GND and VIN (the battery's voltage);
  7. analogue inputs A0–A5;
  8. the ATmega328P microcontroller;
  9. the built-in LED "L", on pin 13;
  10. reset — restarts your program.

A pin safely gives about 20 mA: enough for an LED, never for a motor.

The Arduino IDE and the first sketch: setup() and loop()

  1. Install the free Arduino IDE and connect the board with a USB data cable — some cables only charge.
  2. In the IDE choose the board and the port. A clone board with a CH340 USB chip may need its driver first.
  3. Open File → Examples → 01.Basics → Blink, press Verify (✓), then Upload (→).

An Arduino program is a sketch with two functions:

  • setup() runs once, at power-on or reset — set up the pins here: pinMode(13, OUTPUT);
  • loop() runs again and again — the robot's real work: digitalWrite(13, HIGH) lights the LED, delay(500) waits half a second, digitalWrite(13, LOW) turns it off.

Change 500 to 100 and upload again: that is programming in robotics — change, upload, observe.

Libraries add ready-made code for sensors, servos and displays: install them in the IDE's Library Manager, then #include them. A real case: in GRA's 2026 SUMO rules, the Arduino code for a home-made start module uses three — IRLib to decode the referee's IR remote, EEPROM to remember the state through a reset, and FiniteStateMachine.

Digital and analogue pins, and PWM

Digital means two states: HIGH (5 V on an Uno) or LOW (0 V). digitalRead(pin) reads a button. Set pinMode(pin, INPUT_PULLUP) and wire the button between the pin and GND: the pin reads HIGH, and LOW while pressed. A pin left unconnected "floats" and reads random values.

GRA's 2026 school competition asks for exactly this: a "start" button, and the robot must start moving within 5 seconds of pressing it.

Analogue inputs measure voltage: analogRead(A0) returns 0–1023 for 0–5 V, about 4.9 mV a step — for IR line sensors, potentiometers, a battery divider.

A pin cannot output half a voltage; it fakes it with PWM (pulse-width modulation) — very fast on/off switching with a varying on-time. analogWrite(pin, 0–255) works on the PWM pins. In the second diagram:

  1. 64 — on 25 % of the time, about 1.25 V on average;
  2. 128 — 50 %, about 2.5 V;
  3. 191 — 75 %, about 3.75 V;
  4. one period — about 2 ms at the Uno's usual 490 Hz (pins 5 and 6: 980 Hz).

The red dashed lines are the averages: a dimmer LED, or a slower motor — through a driver.

Reading a sensor, and the serial monitor

Sensors are invisible until you print what they see. The serial monitor shows text the board sends over USB:

  1. in setup(): Serial.begin(115200);
  2. in loop(): Serial.println(analogRead(A0));
  3. open Tools → Serial Monitor at the same 115200 baud — a wrong speed shows rubbish.

Two typical robot sensors:

  • An IR line sensor on an analogue pin: hold it over white, then black, and note both numbers; the threshold lies between. Repeat in the competition hall — light changes them.
  • An ultrasonic sensor (HC-SR04 type): a 10 µs pulse on TRIG, then pulseIn(echoPin, HIGH, 30000) times the echo in microseconds; distance in cm ≈ time ÷ 58. Always give that timeout — without it, a lost echo freezes the robot for a full second.

Print first, then decide: "my code doesn't work" is often a sensor reading something you didn't expect.

Driving a motor through a driver

A pin gives 20 mA; a gear motor wants hundreds, and its noise can reset the board. So the Arduino only sends commands, and a motor driver (an H-bridge of the TB6612FNG, DRV8833 or L298N type) switches the battery's current. In the third diagram:

  1. sensors → the Arduino's inputs;
  2. the Arduino — reads, decides, commands;
  3. the motor driver;
  4. the left and right motors;
  5. the start button and a status LED;
  6. the battery;
  7. the power switch;
  8. a regulator (or the board's VIN) making 5 V for the logic;
  9. one common ground (GND) — without it nothing works.

Black arrows are signals, red ones power. Per motor, a TB6612FNG- or L298N-type driver has two direction pins and a PWM pin: IN1 HIGH + IN2 LOW = forward, the opposite = backward, both LOW = stop; PWM sets the speed. Write one function — say drive(left, right), each −255…255 — and the rest of the program never touches a pin.

Test with the wheels in the air first. A wheel turning the wrong way? Swap its two wires, or flip the sign in drive().

millis() instead of delay(), and a small state machine

delay(1000) freezes the whole board for a second: no sensor, no button. Use millis(), the milliseconds since power-on, and check the time instead of waiting:

  1. keep the time of the last action in an unsigned long variable, last;
  2. in loop(): if millis() − last >= 500, act and set last = millis();
  3. everything else in loop() keeps running in between.

Then split the behaviour into states: one variable, state, and a switch (state) in loop(). For a small robot with a start button:

  1. WAIT — motors off, the LED blinking; the button → GO.
  2. GO — drive or follow the line; an obstacle closer than 15 cm → AVOID.
  3. AVOID — reverse 300 ms, turn 400 ms (timed with millis()), back to GO.
  4. STOP — the button pressed again, or the finish: motors off.

Each state does one thing and knows when to leave. GRA's 2026 SUMO rules ask for the same structure in a home-made start module. A fight as states is in How to build a SUMO robot, and smooth steering (PID) in Building a line follower.

Common mistakes, and what next

Mistakes every beginner makes once:

  • the wrong board or port, or a charge-only cable;
  • no common GND between the boards;
  • a motor or servo powered from the 5V pin — the board resets;
  • wires on pins 0 and 1 during upload;
  • = instead of == in an if;
  • an int overflowing at 32 767 — keep times in unsigned long;
  • 5 V into an ESP32's 3.3 V pin.

What next:

  1. Build the two-wheel robot from My first robot and give it the states above.
  2. Pick a goal — a line follower, a labyrinth robot or a SUMO robot — and read the line follower rules.
  3. Join or start a team: a robotics club at school, and GRA's school competition 2026.
  4. Find the next event on the competitions page — each championship publishes its current rules on its own page — and more guides under the Guides tag.
Blog

More articles

All articles