Getting Started with Arduino
IDE, sketch structure, uploading code, and non-blocking timing
Skills taught: embedded.arduino-setup
The Arduino IDE and Sketch Structure
Every Arduino program is called a sketch (file extension .ino). The structure is rigid: two mandatory functions — setup() and loop(). Install Arduino IDE 2 from arduino.cc, select your board (Tools → Board → Arduino Uno), select your COM port, and click the Upload arrow.
// Classic Blink — the Hello World of embedded systems
// Works on Arduino Uno, Nano, Mega, ESP32, and most others
const int LED = 13; // built-in LED on most Arduino boards
void setup() {
// Called ONCE at power-on or after reset
pinMode(LED, OUTPUT);
Serial.begin(115200); // open serial port at 115200 baud
Serial.println(F("Blink sketch started!"));
// F() stores the string in flash instead of SRAM
}
void loop() {
// Called CONTINUOUSLY, forever
digitalWrite(LED, HIGH); // 5V on pin
Serial.println(F("LED ON"));
delay(1000); // pause 1000 ms
digitalWrite(LED, LOW); // 0V on pin
Serial.println(F("LED OFF"));
delay(1000);
}
Installing Libraries
Libraries extend Arduino with ready-made drivers for sensors, displays, and protocols. Install them via the Library Manager (Sketch → Include Library → Manage Libraries) or by placing a folder in your Arduino libraries directory. After installing, include with #include <LibraryName.h>.
// Example: using the DHT sensor library
// 1. Install: Tools → Library Manager → search "DHT sensor library" by Adafruit
#include <DHT.h>
#define DHT_PIN 4
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
Serial.begin(115200);
dht.begin();
Serial.println(F("DHT22 sensor initialised"));
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("Temp: "));
Serial.print(temperature);
Serial.print(F("°C Humidity: "));
Serial.print(humidity);
Serial.println(F("%"));
delay(2000);
}
Non-Blocking Timing with millis()
delay() blocks everything — no sensor readings, no button checks, no serial data. For anything beyond the simplest blink, use millis() (milliseconds since boot) to track elapsed time without halting the program.
const int LED_PIN = 13;
const long INTERVAL = 1000; // ms between toggles
unsigned long lastToggle = 0;
bool ledState = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
unsigned long now = millis();
// Check if 1000ms has passed since last toggle
if (now - lastToggle >= INTERVAL) {
lastToggle = now;
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
Serial.print(F("LED: ")); Serial.println(ledState ? F("ON") : F("OFF"));
}
// Other code here runs EVERY loop iteration
// — no blocking!
}
Tip: Avoid delay() in production sketches — it blocks everything for the duration. Use the millis() pattern above to run multiple tasks 'simultaneously'. The same pattern works for debouncing, sensor polling, and heartbeat LEDs.
Serial Monitor Debugging
Arduino's Serial Monitor (Ctrl+Shift+M) is your primary debugging tool. It shows messages sent via Serial.print() and lets you send commands from your PC. Always ensure the Serial Monitor baud rate matches Serial.begin() — a mismatch shows garbled characters.
void setup() {
Serial.begin(115200); // set baud rate
while (!Serial) delay(10); // wait for USB serial on Leonardo/Micro
Serial.println(F("=== Debug mode ==="));
}
void loop() {
// Print multiple values on one line
Serial.print(F("A0=")); Serial.print(analogRead(A0));
Serial.print(F(" A1=")); Serial.print(analogRead(A1));
Serial.print(F(" uptime=")); Serial.print(millis() / 1000);
Serial.println(F("s"));
// Read command from Serial Monitor
if (Serial.available()) {
char c = Serial.read();
if (c == 'r') { // type 'r' to reset
Serial.println(F("Resetting..."));
asm volatile ("jmp 0"); // soft reset AVR
}
}
delay(500);
}
Warning: On Arduino Uno, pins 0 (RX) and 1 (TX) are shared with USB serial. Using them for other hardware while also using Serial.print() will corrupt both. Reserve pins 0/1 for USB debug; use SoftwareSerial on other pins for additional serial devices.
Frequently Asked Questions
Can I write Arduino code in pure C++ without the .ino wrapper?
Yes. Arduino sketches compile as C++. You can use all C++ features, STL (carefully — SRAM is limited), templates, classes, and namespaces. The .ino extension just means the IDE auto-adds #include <Arduino.h>.
What is PlatformIO and when should I use it instead of the Arduino IDE?
PlatformIO is a professional IDE extension (VS Code) for embedded development. It supports 1000+ boards, dependency management (like npm for Arduino libraries), unit testing, and CI/CD. Use it for any project larger than a weekend prototype.
My millis() wraps around after 49 days — will that break my code?
Only if you use ==, >, or < comparisons. The pattern `if (now - lastEvent >= interval)` is rollover-safe because unsigned subtraction handles the wrap correctly. Never compare with ==; always use >=.