top of page
Search

Arduino Servo Project for Kids: Build a Robot Arm That Waves Hello (Full Code)

Nov 6
5 min read

A great first Arduino servo project for kids is a robot arm that waves hello when someone holds a hand in front of it. It uses just three parts (an Arduino Uno, a small SG90 servo motor and an HC-SR04 ultrasonic sensor), takes about an hour, and teaches the two big ideas behind almost every robot: reading a sensor and moving a motor to an exact position. Below you'll find the parts list, wiring, the full tested code and ideas to take it further.

What a servo motor does (and why kids love it)

A regular DC motor just spins. A servo motor turns to an exact angle you choose, from 0 to 180 degrees, and holds it there. Tell it 90 and it points straight up. Tell it 60 and it leans left. That makes servos perfect for things that need to move to a position: robot arms, grippers, steering, door locks and, today, a waving hand.

Kids like servos because the result is instant and visible. Change one number, upload, and the arm moves somewhere new. That fast feedback loop is exactly how we want young coders to learn.

What you need

  • Arduino Uno (or a compatible board) and a USB cable

  • SG90 micro servo (the small blue one that comes in most starter kits)

  • HC-SR04 ultrasonic distance sensor

  • A breadboard and about 7 jumper wires

  • Cardboard, a craft stick and tape to make the arm and hand

  • A computer with the free Arduino IDE installed

Most Arduino starter kits include all of these. If you're still choosing a kit, see our coaches' robotics kit guide.

Wiring it up

Unplug the Arduino from USB while you wire. Then connect:

  • Servo brown wire to GND, red wire to 5V, orange wire to pin 9

  • Ultrasonic VCC to 5V and GND to GND

  • Ultrasonic Trig to pin 7 and Echo to pin 6

Use the breadboard's power rails so both parts can share 5V and GND. One small SG90 servo can run from the Arduino's 5V pin. If you later add bigger or multiple servos, power them from a separate battery pack and connect that pack's ground to the Arduino's GND, or the board may reset when the motors pull too much current.

Tape a craft stick to the servo horn (the small plastic arm on top) and add a cardboard hand. Mount the servo so the stick points straight up when the horn is in the middle of its range.

The full code

Open the Arduino IDE, paste this sketch, select your board and port, and click Upload. The Servo library comes built into the Arduino IDE, so there's nothing extra to install.

#include <Servo.h>

const int SERVO_PIN = 9;
const int TRIG_PIN  = 7;
const int ECHO_PIN  = 6;

const int REST_ANGLE   = 90;   // arm pointing straight up
const int WAVE_LEFT    = 60;
const int WAVE_RIGHT   = 120;
const int HELLO_CM     = 20;   // wave when a hand is closer than this

Servo arm;

long readDistanceCm() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 0 if no echo
  if (duration == 0) return 999;                  // nothing in range
  return duration / 58;                           // microseconds -> cm
}

void moveSmoothly(int fromAngle, int toAngle) {
  int step = (toAngle > fromAngle) ? 1 : -1;
  for (int a = fromAngle; a != toAngle; a += step) {
    arm.write(a);
    delay(5);                                     // smaller = faster wave
  }
  arm.write(toAngle);
}

void waveHello(int times) {
  moveSmoothly(REST_ANGLE, WAVE_LEFT);
  for (int i = 0; i < times; i++) {
    moveSmoothly(WAVE_LEFT, WAVE_RIGHT);
    moveSmoothly(WAVE_RIGHT, WAVE_LEFT);
  }
  moveSmoothly(WAVE_LEFT, REST_ANGLE);
}

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  arm.attach(SERVO_PIN);
  arm.write(REST_ANGLE);
  Serial.begin(9600);
}

void loop() {
  long cm = readDistanceCm();
  Serial.println(cm);
  if (cm < HELLO_CM) {
    waveHello(3);
    delay(1000);                                  // pause so it doesn't wave nonstop
  }
  delay(100);
}

We compiled and ran this sketch against simulated hardware before publishing: with nothing in front of the sensor the arm rests at 90 degrees, and when a reading drops below 20 cm it waves three times between 60 and 120 degrees, then returns to rest.

How the code works

  1. readDistanceCm() sends a 10-microsecond ping out of the Trig pin and times how long the echo takes to come back. Dividing by 58 turns that time into centimeters. If no echo returns, it reports 999 so the robot treats it as "nobody there."

  2. moveSmoothly() moves the servo one degree at a time with a tiny pause between steps. Without it, the servo snaps instantly to each angle, which looks jerky and can knock a light cardboard arm loose.

  3. waveHello() swings the arm left and right a set number of times, then brings it back to rest.

  4. loop() checks the distance ten times a second. When a hand comes closer than 20 cm, the arm waves, then waits a second so it doesn't wave nonstop.

This sense-decide-act pattern is the same one our obstacle-avoiding Arduino robot uses, just with an arm instead of wheels.

Make it your own

  • Change the personality: set the delay in moveSmoothly() to 2 for an excited wave or 15 for a sleepy one.

  • Change the trigger distance: raise HELLO_CM to 40 so it greets people from farther away.

  • Count visitors: add a variable that goes up by one each wave and print it to the Serial Monitor.

  • Add a second servo: make a head that nods while the arm waves, powered from a separate battery pack.

  • Organize bigger programs: once your robot has several behaviors, try the approach in our post on state machine robot programming.

Common questions

What age is this project for? Most kids ages 10 and up can wire and upload it with a little help. Younger kids can do the building and decorating while an adult or older sibling handles the wiring, then experiment with the numbers in the code.

My servo jitters or the board keeps resetting. What's wrong? That's almost always power. Check that every ground wire is connected, keep to one small servo on the Arduino's 5V pin, and move larger servos to their own battery pack with a shared ground.

Does my child need to know how to code first? No. This sketch is a good first look at Arduino code, and reading the comments line by line is a lesson in itself. If they've never coded at all, starting with our free Scratch course builds the logic skills first.

Keep building with Fusion Robotics Education

If your child enjoyed this, our Arduino Basics for Beginners course walks through sensors, motors and servos step by step, at their own pace. You can explore free self-paced courses and the full catalog on our online courses page, start the All-Access plan with a 7-day free trial, or text (619) 786-5731 to ask about Remote 1-on-1 live lessons.

Live in San Diego? Our weekend group sessions in Scripps Ranch keep classes to 10 kids, ages 6 to 18, and every child works on their own project at their own pace. The first session is free: text (619) 786-5731 to book a spot.

 
 
 

Comments


bottom of page