MyRoboPathOpen Robotics Lab
robotics basics15 min readUpdated 2026-03-14Beginner

First Steps in Code: Arduino IDE & Wokwi Setup, Blink, Serial, I/O & Sensors

Master the fundamental building blocks of robotics programming: Arduino IDE setup, Wokwi simulation, Digital vs Analog I/O, reading buttons, measuring sensors, and driving buzzers and servos.

MyRoboPath Engineering Lab
Peer-Reviewed Open-Source Hardware & Firmware Guide

Key Engineering Takeaways

  • Arduino programs are composed of two mandatory functions: setup() (runs once on boot) and loop() (repeats infinitely).
  • pinMode(pin, mode) configures a GPIO pin as INPUT, OUTPUT, or INPUT_PULLUP (which enables an internal resistor for buttons).
  • digitalWrite() outputs either 0V or 5V, while analogRead() samples continuous voltages from 0 to 1023 (10-bit ADC).
  • PWM (Pulse Width Modulation) simulated analog output using analogWrite(pin, 0-255) to adjust motor speed or LED brightness.
  • The Serial Monitor at 115200 or 9600 baud is your #1 debugging tool to print real-time sensor numbers and error logs.
Prerequisites
  • Computer with USB port or modern web browser
Required Hardware / Tools
  • Arduino Uno or Nano
  • USB Cable
  • Breadboard + Jumper Wires
  • LED + 220Ω Resistor
  • Pushbutton
  • Potentiometer (10kΩ)
  • SG90 Micro Servo

Setting Up Arduino IDE & Wokwi Simulator

Before writing code, set up your development environment:

Option A: Install Arduino IDE 2.x

  1. 1
    Download Arduino IDE 2.x from the official Arduino website.
  2. 2
    Connect your Arduino board via USB.
  3. 3
    Select your board model under Tools → Board (e.g. Arduino Uno).
  4. 4
    Select the detected serial port under Tools → Port (e.g. COM3 on Windows or /dev/ttyUSB0 on Linux/Mac).

Option B: Zero-Install Instant Online Simulator (Wokwi)

If you don't have hardware in front of you, open [Wokwi.com](https://wokwi.com) in any modern browser. You can drag-and-drop Arduinos, ESP32s, sensors, LEDs, and servos and run real firmware with live interactive simulation.

Arduino IDE and Wokwi simulation workflow
Figure 3.1: Arduino IDE compile-and-upload workflow alongside cloud-based Wokwi circuit simulation.Visual Guide

Anatomy of an Arduino Sketch: setup() & loop()

Every robotics firmware file contains the fundamental skeleton:

firmware.ino
cpp
// 1. GLOBAL SCOPE: Pin definitions, variables, and libraries
const int LED_PIN = 13;

void setup() {
  // 2. SETUP: Runs ONCE when power turns on or reset is pressed
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(115200);
  Serial.println("Robot Controller Initialized!");
}

void loop() {
  // 3. MAIN LOOP: Repeats continuously as fast as possible
  digitalWrite(LED_PIN, HIGH); // Turn LED ON (5V)
  delay(500);                  // Wait 500 milliseconds
  digitalWrite(LED_PIN, LOW);  // Turn LED OFF (0V)
  delay(500);                  // Wait 500 milliseconds
}

Digital I/O: Controlling LEDs & Reading Buttons

Robots use digital inputs for limit switches, bumper switches, and pushbuttons. Using INPUT_PULLUP saves you an external resistor by enabling the microcontroller's internal 20kΩ pull-up resistor:

firmware.ino
cpp
const int BUTTON_PIN = 2; // Button wired between Pin 2 and GND
const int LED_PIN = 13;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP); // Button unpressed = HIGH, pressed = LOW
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN);
  
  if (buttonState == LOW) { // Button is pressed!
    digitalWrite(LED_PIN, HIGH);
    Serial.println("Bumper Switch TRIGGERED: Obstacle Hit!");
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

Analog I/O: ADC Inputs & PWM Output

Reading Continuous Analog Voltages:

Analog sensors (like light-dependent resistors or potentiometers) output continuous voltages from 0V to 5V. The built-in Analog-to-Digital Converter (ADC) converts this into a number from 0 to 1023:

firmware.ino
cpp
const int POT_PIN = A0;  // Center pin of 10k potentiometer
const int MOTOR_PWM = 9; // PWM pin on Arduino Uno (~9)

void setup() {
  pinMode(MOTOR_PWM, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  int sensorValue = analogRead(POT_PIN); // Reads 0 to 1023
  
  // Map 10-bit input (0-1023) to 8-bit PWM speed (0-255)
  int speed = map(sensorValue, 0, 1023, 0, 255);
  
  analogWrite(MOTOR_PWM, speed); // Generates PWM signal for motor driver
  
  Serial.print("Raw Sensor: ");
  Serial.print(sensorValue);
  Serial.print(" -> Calculated Speed: ");
  Serial.println(speed);
  delay(50);
}

Serial Debugging: The Robot Diagnostic Window

When your robot doesn't behave as expected, never guess—print variables to the Serial Monitor!

  1. 1
    Open Tools → Serial Monitor in Arduino IDE.
  2. 2
    Set the baud rate dropdown to match your code (115200 or 9600).
  3. 3
    Use formatted debugging prints:
firmware.ino
cpp
Serial.print("Distance: ");
Serial.print(distanceCm);
Serial.print(" cm | State: ");
Serial.println(robotState);

Installing Libraries & Driving an RC Servo

Arduino libraries provide pre-written drivers for complex hardware. The standard <Servo.h> library generates precise 50Hz PWM timing (1.0ms to 2.0ms pulses) to hold angles between 0° and 180°:

firmware.ino
cpp
#include <Servo.h>

Servo scanRadar; // Create servo object to control ultrasonic sensor turret

void setup() {
  scanRadar.attach(9); // Connect SG90 signal wire (orange/yellow) to Pin 9
}

void loop() {
  // Sweep from 0 degrees to 180 degrees
  for (int angle = 0; angle <= 180; angle += 15) {
    scanRadar.write(angle);
    delay(100);
  }
  
  // Sweep back from 180 degrees to 0 degrees
  for (int angle = 180; angle >= 0; angle -= 15) {
    scanRadar.write(angle);
    delay(100);
  }
}

Frequently Asked Questions

Why do I get gibberish characters in the Serial Monitor?

This happens when the baud rate selected in the bottom-right corner of the Serial Monitor window does not match the baud rate specified in your Serial.begin(baud) command. If your code says Serial.begin(115200);, ensure the Serial Monitor is set to 115200 baud.

Why does my servo jitter and cause the Arduino to disconnect?

Even small SG90 servos can draw 500mA+ when moving or under mechanical load. If powered directly from the Arduino 5V pin, this voltage drops sharply, resetting the USB communication. Power the servo VCC wire directly from an external battery pack or 5V regulator, keeping all GND wires tied together.

Tags:#Arduino IDE#Wokwi#Blink#Serial Monitor#Digital I/O#Analog I/O#C++ Robotics