Built-in project · Arduino Uno R3

MG995 servo sweep on an Arduino Uno

An MG995 metal-gear servo on pin 9 jumps to 0, 90 and 180 degrees and sweeps back, powered from its own 6 V pack with only the ground shared. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $35. On Mokxi they're free.

MG995 servo sweep · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Motors
  • Using the Servo
  • Using the Battery: AA
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

An MG995 metal-gear servo on pin 9 jumps to 0, 90 and 180 degrees and sweeps back, powered from its own 6 V pack with only the ground shared.

Arduino Uno R3 runs the sketch below. The circuit around it: Servo, MG995 metal gear, Battery: AA, coin cell, LiPo. delay waits 1000 milliseconds. Serial.begin opens the serial port at 9600 baud. attach connects the servo library to 9.

Parts list

3 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Servo, MG995 metal gear pin GNDArduino Uno R3 pin GND
Battery: AA, coin cell, LiPo pin V-Arduino Uno R3 pin GND
Servo, MG995 metal gear pin PWMArduino Uno R3 pin 9
Battery: AA, coin cell, LiPo pin V+Servo, MG995 metal gear pin VCC

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Servo sweep: the horn swings to 0, 90 and 180 degrees, then sweeps back.
//
//   servo brown (GND)   -> GND
//   servo red (VCC)     -> 5V
//   servo orange (PWM)  -> pin 9
//
// A hobby servo turns to an angle and holds it. It reads the angle from the
// width of a pulse sent fifty times a second: about 0.5 ms is one end, about
// 2.4 ms the other. The Servo library sends that pulse for you; write(angle)
// only changes how wide it is.
//
// The horn needs time to get there. This one moves about 60 degrees in a
// tenth of a second, so each jump waits a second before the next.

#include <Servo.h>

Servo horn;

void moveTo(int angle) {
  horn.write(angle);
  Serial.print("angle ");
  Serial.println(angle);
}

void setup() {
  horn.attach(9);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: servo sweep");
}

void loop() {
  moveTo(0);
  delay(1000);
  moveTo(90);
  delay(1000);
  moveTo(180);
  delay(1000);
  // A slow sweep back, one degree at a time.
  for (int angle = 180; angle >= 0; angle--) {
    horn.write(angle);
    delay(10);
  }
  Serial.println("swept back to 0");
  delay(1000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once when the board starts. It gets the pins and libraries ready.

void setup() {
  horn.attach(9);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: servo sweep");
}

loop sketch.ino

Runs over and over for as long as the board has power. This is where the project does its work.

void loop() {
  moveTo(0);
  delay(1000);
  moveTo(90);
  delay(1000);
  moveTo(180);
  delay(1000);
  // A slow sweep back, one degree at a time.
  for (int angle = 180; angle >= 0; angle--) {
    horn.write(angle);
    delay(10);
  }
  Serial.println("swept back to 0");
  delay(1000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The battery is wired backwards, or the voltage is too low or too high for the circuit. A fresh AA is about 1.5 V, a LiPo is 3.7 V nominal and 4.2 V full. Battery: AA
    Match the plus and minus marks. Check the voltage your parts need. Two AAs make 3 V, four make 6 V. Never feed a LiPo to a 3.3 V chip directly at 4.2 V.
  • The circuit resets or browns out when a motor or servo starts. A battery has internal resistance, so the voltage sags under load. Battery: AA
    Use a fresh pack with enough current rating. Add a 100 uF or larger capacitor near the load. Power motors from their own pack and join the grounds.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.