Build it step by step
0 of 7 doneWire board A
Servo on D9, knob on A0.
Upload the first sketch to board A
The servo follows the knob.
Join the signal wire and the grounds
Board A D9 to board B D2, and GND to GND.
Upload the second sketch to board B
Open the serial monitor at 9600.
Turn the knob slowly
The width climbs from about 1000 to 2000 microseconds.
Compare pulse width with angle
Write down five points.
Try a second servo
Note its range.
How do you build a servo pulse width reader?
One Nano drives a servo from a knob while a second Nano listens on the same signal wire and prints the width of each pulse in microseconds. You see that an RC servo is just a one to two millisecond pulse repeated fifty times a second. You need Arduino Nano × 2, SG90 Micro Servo, Potentiometer 10 kΩ, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 70 minutes.
- Level
- Intermediate
- Time
- About 70 min
- Parts
- 5
Parts list
- Development BoardsBuyArduino Nano × 2
- Motors & ActuatorsBuySG90 Micro Servo × 1
- PassivesBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Board A: servo signal (orange) | Board A D9 | The pulse to the servo |
| Board A: servo red | Board A 5V | Servo power, a small servo only |
| Board A: servo brown | Board A GND | Common ground |
| Board A: pot outer legs | Board A 5V and GND | Knob supply |
| Board A: pot middle leg | Board A A0 | Angle setting |
| Board A D9 | Board B D2 | The same pulse goes to the reader |
| Board A GND | Board B GND | Shared ground |
Code
An RC servo does not understand angles. It receives a short pulse about fifty times a second, and the length of that pulse sets the angle: about one millisecond for one end, about one and a half for the middle and about two for the other end. The file holds two sketches for two Nanos. Board A reads a knob and turns the servo, and board B shares the signal wire and a ground, and uses pulseIn to measure how long each pulse stays high. Turn the knob and watch the width climb from about 1000 to about 2000 microseconds. Use only a small servo with light movements from the Nano's 5 volts, because a stalled or heavily loaded servo can drag the supply down and reset the board. Both boards must share a ground, or the reader sees nothing. Compare the angle guess with the real angle, and try other servos, because their ranges differ a little.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// ===== BOARD A: drives the servo from a knob =====
#include <Servo.h>
Servo servo;
void setup() {
servo.attach(9);
}
void loop() {
int angle = map(analogRead(A0), 0, 1023, 0, 180);
servo.write(angle);
delay(20);
}
/* ===== BOARD B: reads the pulse width (a separate sketch) =====
Copy everything below into a new sketch for the second board. */
/*
const uint8_t PULSE_PIN = 2;
void setup() {
pinMode(PULSE_PIN, INPUT);
Serial.begin(9600);
Serial.println("pulse (us) angle guess");
}
void loop() {
unsigned long us = pulseIn(PULSE_PIN, HIGH, 30000); // length of the high pulse
if (us > 0) {
long angle = map((long)us, 1000, 2000, 0, 180); // about 1 ms to 2 ms
Serial.print(us);
Serial.print(" ");
Serial.println(angle);
}
delay(200);
}
*/Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Knob at one end | A pulse near 1000 microseconds |
| Knob in the middle | A pulse near 1500 microseconds |
| Knob at the other end | A pulse near 2000 microseconds |
| Repeat rate | A new pulse about every 20 milliseconds |
| Grounds not joined | The reader prints nothing useful |
If it does not work
Reader prints nothing
Join the grounds and check the D9 to D2 wire.
Servo jitters or board resets
Use a smaller servo or a separate battery pack with a common ground.
Widths look half or double
Check you read HIGH pulses on the right pin.
Servo does not move
Check the three servo wires.
Angle guess is off
Your servo range may differ from 1 to 2 ms.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the servo pulse width reader?
Arduino Nano × 2, SG90 Micro Servo, Potentiometer 10 kΩ, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the servo pulse width reader take to build?
About 70 minutes for a intermediate maker, in 7 steps.
How do I know the servo pulse width reader works?
Knob at one end: A pulse near 1000 microseconds. Knob in the middle: A pulse near 1500 microseconds. Knob at the other end: A pulse near 2000 microseconds.
Reader prints nothing: what should I check?
Join the grounds and check the D9 to D2 wire.
Servo jitters or board resets: what should I check?
Use a smaller servo or a separate battery pack with a common ground.
Widths look half or double: what should I check?
Check you read HIGH pulses on the right pin.
Servo does not move: what should I check?
Check the three servo wires.
