Map Three Joystick Axes to Bounded Servo Pulses
The Story
A joystick can request a change without telling the ESP32 where a joint actually is. This lesson keeps the three-axis control idea while separating commanded pulse width from measured travel and deliberate enable from automatic startup motion.
Explain Like I'm 12
A pulse requests a servo position; it does not measure one. The program starts with no pulses. After a deliberate enable it requests 1500 µs, which can move the shaft immediately from an unknown starting position.
Learning Support
- Recommended ageAges 13+
- Adult supervisionAdult supervision recommended for servo power wiring and first motion tests.
- Classroom useUse it to discuss control loops and real robot safety standards without claiming certification.
- Parent promptWhy are pulse commands different from a measured joint position or a mechanical stop?
- Screen-free activityTrace separate power rails and mark where shaft feedback and independent protection are absent.
- Next challengeStudy measured position feedback and mechanical limits as a separate engineering design; do not attach a load to this bench.
- Skills practiced
- Servo pulse timing
- Joystick ADC
- Bounded commands
- Separate power domains
- Learning outcomes
- Command three documented FS90 units without a startup sweep.
- Bias ADC inputs while acknowledging undetected fault modes.
- Use explicit enable and a nonrenewable command lease.
- Distinguish pulse disable from power removal and homing.
- Mini experiments
- With external servo power off, inspect reset, enable and fault messages.
- On the restrained unloaded bench, compare small pulse commands without inferring angles.
Safety Standards
- Unloaded, restrained servo bench only; no load-bearing arm or home/travel validation.
- Separate positive rails and common signal ground; reachable independent servo-power switch.
- Button/X/timeout stop pulses only; no independent emergency stop or guaranteed torque/motion behavior.
What You Will Build
Three joystick axes increment three positional-servo pulse commands on a restrained, unloaded bench. The historical robot-arm URL remains, but no load-bearing arm assembly or mechanical travel calibration is supplied. There are no limit switches, homing sensors, encoders or verified joint positions. Keep horns and loads removed for this supported exercise. The overview is a logical illustration, not a hardware photo or test result. Use the specified parts and wiring map below; physical connector orientation must be checked separately.
Learning Objectives
- Command three documented FS90 units without a startup sweep.
- Bias ADC inputs while acknowledging undetected fault modes.
- Use explicit enable and a nonrenewable command lease.
- Distinguish pulse disable from power removal and homing.
Components List
- Classic ESP32-WROOM DevKitGPIO16/17 must be free (not a WROVER/PSRAM map); GPIO36 may be labelled VP. Core 3.3.2 review baseline.
- Three FEETECH FS90 positional servos, Pololu #2818 referenceUse the supplier-documented 3.3 V signal-compatible variant. No horns or mechanical load; FS90R is excluded.
- Two passive SparkFun COM-09032 thumb joysticksTwo approximately 10kΩ pots each; use X/Y from one and X from the second. Use a verified passive breakout; no 5 V/comparator or I2C substitute.
- Separate switched, regulated 5 V / 3 A current-capable supplyServo positive only; verify actual startup/stall response. USB/DevKit rails do not power servos.
- Bare momentary enable buttonGPIO27 to GND with INPUT_PULLUP; software gate, not an independent stop.
- Three each: 10kΩ, 1kΩ, 100kΩ resistors and 100 nF ceramics; 470 µF ≥10 V bulk capacitor10k signal pull-down per servo; 1k series, 100k pull-down and 100 nF per used ADC node; bulk cap on external rail.
- Suitable power distribution, short signal connections and USB data cableIdentify connector functions from documentation/continuity, not colors or this logical map.
Bill of Materials
| Part | Qty | Estimated Cost | Notes |
|---|---|---|---|
| Classic ESP32-WROOM DevKit | 1 set | Varies | GPIO16/17 must be free (not a WROVER/PSRAM map); GPIO36 may be labelled VP. Core 3.3.2 review baseline. |
| Three FEETECH FS90 positional servos, Pololu #2818 reference | 3 | Varies | Use the supplier-documented 3.3 V signal-compatible variant. No horns or mechanical load; FS90R is excluded. |
| Two passive SparkFun COM-09032 thumb joysticks | 2 | Varies | Two approximately 10kΩ pots each; use X/Y from one and X from the second. Use a verified passive breakout; no 5 V/comparator or I2C substitute. |
| Separate switched, regulated 5 V / 3 A current-capable supply | 1 set | Varies | Servo positive only; verify actual startup/stall response. USB/DevKit rails do not power servos. |
| Bare momentary enable button | 1 set | Varies | GPIO27 to GND with INPUT_PULLUP; software gate, not an independent stop. |
| Three each: 10kΩ, 1kΩ, 100kΩ resistors and 100 nF ceramics; 470 µF ≥10 V bulk capacitor | 1 set | Varies | 10k signal pull-down per servo; 1k series, 100k pull-down and 100 nF per used ADC node; bulk cap on external rail. |
| Suitable power distribution, short signal connections and USB data cable | 1 set | Varies | Identify connector functions from documentation/continuity, not colors or this logical map. |
Wiring
Base/shoulder/elbow signal labels mean logical channels: GPIO13/16/17 respectively, each with 10kΩ to GND. Three used passive joystick wipers feed GPIO34/35/36 through separate 1kΩ resistors; each ADC node has 100kΩ and 100 nF to GND. Enable button joins GPIO27 to GND. Servos use external switched 5 V and common ground; no external positive reaches ESP32 USB/3V3.
-
1
Disconnect both supplies and remove all horns/loads. Identify exact FS90 connectors and passive joystick pot ends/wipers from documentation plus an unpowered continuity/resistance check. This is not a physical connector-order drawing.
-
2
Connect servo signals13/16/17 and their 10kΩ pull-downs. Route the servo positive leads to the separate 5 V distribution rail, and their returns directly to the supply minus. Join that minus to ESP32 GND for signal reference.
-
3
Place the selected 470 µF ≥10 V capacitor across the external rail near distribution, with correct polarity. Do not route servo current through a solderless breadboard/DevKit or join the external positive to USB.
-
4
Wire joystick pot ends to ESP32 3V3/GND. One joystick X→1k→GPIO34; Y→1k→GPIO35. Second joystick X→1k→GPIO36. At EACH ADC node fit 100kΩ and 100 nF to GND. Leave unused wiper and joystick click switches unconnected; verify the actual breakout netlist.
-
5
Connect a bare momentary button from GPIO27 to GND. The sketch uses INPUT_PULLUP; the button affects software only.
-
6
With external servo power off, upload/reset and check disabled startup. Release the button once. Apply servo power only with the bench clear, center axes, hold enable and send A at 115200 baud. Be prepared for immediate movement toward the commanded center region.
-
7
Use only small unloaded changes. Release enable or send X before switching external power off. Power down both sources before rewiring; do not extend pulse ranges or attach an arm load to force travel.
GPIO Mapping
| Signal | ESP32 Pin | Direction | Notes |
|---|---|---|---|
| Base / shoulder / elbow signal | GPIO13 / GPIO16 / GPIO17 | 50 Hz output | 10kΩ to GND on each; pulse commands, not measured joint angles. |
| Joystick 1 X / Y; joystick 2 X | GPIO34 / GPIO35 / GPIO36 | ADC1 input | Per axis: wiper→1kΩ→ADC; 100kΩ and100nF from ADC to GND. |
| Passive pot end terminals | 3V3 / GND | Logic-domain supply | Use only ESP32 3V3; no external 5 V joystick signal. |
| Enable button | GPIO27 to GND | Digital input | Bare momentary button; INPUT_PULLUP; release required before arming. |
| Servo positive | External switched 5 V + | Power | Not the USB/DevKit positive rail. |
| Servo returns / ESP32 reference | External supply minus + GND | Ground | Current returns go directly to the servo supply. |
| Unused axis / click switches | Not connected | Unused | Identify physical terminals separately; no universal color/pin-order claim. |
Circuit Explanation
Servo power uses a separately switched, regulated 5 V rail; USB powers the ESP32 and 3.3 V passive joystick circuits only. Join grounds, never positive rails. The selected FS90 supplier supports 3.3 V control signals; another servo needs its own threshold review or a documented buffer. Fit 10kΩ from each servo signal to GND. For each used joystick axis connect its wiper through 1kΩ to the ADC node, with 100kΩ and 100 nF from that node to GND. Potentiometer ends connect only to ESP32 3V3/GND; identify ends/wipers electrically before soldering.
Engineering Explanation
Use the documented FS90 positional variant supplied as Pololu #2818, with the FS90-C V1.0 manufacturer sheet. That specification describes 900–2100 µs, not a universal 500–2500 µs travel range. This sketch deliberately commands only 1400–1600 µs at 50 Hz and never converts commands into calibrated degrees. A 1500 µs command is not homing, and even narrow pulses do not establish mechanical clearance. The sheet gives 700/800 mA stall figures at 4.8/6 V; a separate 5 V / 3 A current-capable source for three units is a planning budget, not a measured peak guarantee or permission to stall. Check actual supply transients, wiring and the exact units. Retain 470 µF bulk decoupling (at least 10 V rated) at the distribution rail; it does not replace an adequate supply. Hobby servos contain their own drive electronics: do not add a flyback diode across the servo signal or assume bare-motor wiring. Classic ESP32 at 11 dB has a documented approximate 0.15–3.1 V ADC measurement range, different from its 3.3 V supply domain. ADC endpoint clipping deliberately causes a software fault here. GPIO34/35/36 lack internal pull resistors; external node pull-downs reduce floating on an open wiper path. They cannot detect every short, ground break or plausible intermediate fault. With 100kΩ/100 nF the nominal open-node RC is 10 ms, not an independently validated stop time.
Libraries
- Arduino-ESP32 3.3.2 built-in LEDC and ADCClassic WROOM review baseline, not latest. No external servo library. Pin-based 3.x APIs with checked attach/write status.
Code
Copy into Arduino IDE. Install any libraries noted in the component guides first.
// Classic ESP32-WROOM; Arduino-ESP32 3.3.2; built-in LEDC only.
// Three documented FEETECH FS90 positional units, no horns/load.
// No homing, shaft feedback, limit switches or patient/safety function.
#include <Arduino.h>
constexpr uint8_t SERVO_PINS[3] = {13, 16, 17};
constexpr uint8_t AXIS_PINS[3] = {34, 35, 36};
constexpr uint8_t ENABLE_PIN = 27, BITS = 16;
constexpr int MIN_US = 1400, CENTER_US = 1500, MAX_US = 1600;
constexpr uint32_t LEASE_MS = 10000, UPDATE_MS = 20;
int pulseUs[3] = {CENTER_US, CENTER_US, CENTER_US};
int center[3] = {2048, 2048, 2048};
bool attached[3] = {false, false, false}, armed = false, needsRelease = true;
uint32_t armedAt = 0, lastUpdate = 0;
uint32_t ticks(int us) {
return (uint32_t(us) * (1UL << BITS) + 10000) / 20000;
}
void disableOutputs() {
armed = false; needsRelease = true;
bool ok = true;
for (int i = 0; i < 3; ++i) {
if (attached[i]) {
if (!ledcWrite(SERVO_PINS[i], 0)) ok = false;
if (!ledcDetach(SERVO_PINS[i])) ok = false;
attached[i] = false;
}
digitalWrite(SERVO_PINS[i], LOW); pinMode(SERVO_PINS[i], OUTPUT);
}
if (!ok) Serial.println("LEDC stop error: remove external servo power");
Serial.println("Pulses disabled; external power and possible torque remain");
}
void armAtCenter() {
if (needsRelease) { Serial.println("Release enable before arming again"); return; }
if (armed) { Serial.println("Release enable before a new arm attempt"); return; }
if (digitalRead(ENABLE_PIN) != LOW) {
Serial.println("Hold enable, center all axes, then send A"); return;
}
for (int i = 0; i < 3; ++i) {
long sum = 0;
for (int n = 0; n < 16; ++n) sum += analogRead(AXIS_PINS[i]);
int value = int(sum / 16);
if (value < 1200 || value > 3000) {
Serial.println("Axis not in plausible center window; remains disabled"); return;
}
center[i] = value; // Manual centered reference, not a continuity test.
}
if (digitalRead(ENABLE_PIN) != LOW) return;
for (int i = 0; i < 3; ++i) {
pulseUs[i] = CENTER_US;
attached[i] = ledcAttach(SERVO_PINS[i], 50, BITS);
if (!attached[i] || !ledcWrite(SERVO_PINS[i], ticks(CENTER_US))) {
disableOutputs(); Serial.println("Attach/write failed; inspect with power off"); return;
}
}
armedAt = millis(); lastUpdate = armedAt; armed = true; needsRelease = true;
Serial.println("1500 us commanded: movement may be immediate; no home position measured");
}
void setup() {
Serial.begin(115200);
pinMode(ENABLE_PIN, INPUT_PULLUP); // Bare momentary button to GND; software gate only.
analogReadResolution(12);
for (int i = 0; i < 3; ++i) {
pinMode(AXIS_PINS[i], INPUT);
analogSetPinAttenuation(AXIS_PINS[i], ADC_11db);
digitalWrite(SERVO_PINS[i], LOW); pinMode(SERVO_PINS[i], OUTPUT);
}
Serial.println("Disabled. External power off for wiring/upload. No automatic sweep.");
Serial.println("Center axes, hold enable and send A; X disables. Lease is 10 seconds.");
}
void loop() {
if (!armed && digitalRead(ENABLE_PIN) == HIGH) needsRelease = false;
if (armed && (digitalRead(ENABLE_PIN) != LOW || uint32_t(millis() - armedAt) >= LEASE_MS)) {
disableOutputs();
}
if (Serial.available()) {
char cmd = Serial.read();
if (cmd == 'X' || cmd == 'x') disableOutputs();
else if (cmd == 'A' || cmd == 'a') armAtCenter();
// Newlines and unrelated characters do not move servos or extend the lease.
}
if (!armed || uint32_t(millis() - lastUpdate) < UPDATE_MS) return;
lastUpdate = millis();
int raw[3];
for (int i = 0; i < 3; ++i) {
raw[i] = analogRead(AXIS_PINS[i]);
if (raw[i] <= 100 || raw[i] >= 3900) {
disableOutputs(); Serial.println("Axis range fault; inspect disconnected input/rails"); return;
}
}
for (int i = 0; i < 3; ++i) {
int delta = raw[i] - center[i];
if (delta > 250) pulseUs[i] += 2;
else if (delta < -250) pulseUs[i] -= 2;
pulseUs[i] = constrain(pulseUs[i], MIN_US, MAX_US);
if (!ledcWrite(SERVO_PINS[i], ticks(pulseUs[i]))) {
disableOutputs(); Serial.println("Pulse write failed; remove external power"); return;
}
}
// These are commands only; no shaft-angle or motion feedback exists.
}
Code Explanation
The sketch uses only core 3.3.2 LEDC: pin-based ledcAttach/ledcWrite/ledcDetach at 50 Hz and 16 bits; no external ESP32Servo library is required. Reset does not attach PWM. First release the enable button; center the axes, hold the button and send A in Serial Monitor. The program averages each centered input, rejects values outside 1200–3000, then requests 1500 µs on all three outputs. It checks attach/write return values. Every target 20 ms it rejects raw endpoints (≤100 or ≥3900), applies a ±250-count dead zone and increments commands by 2 µs within 1400–1600. These arbitrary conservative software windows need actual bench checking; they are not continuity or travel calibration. Release, X, an input-range fault, a driver error or a 10-second nonrenewable lease disables all outputs. Releasing the button is required before another arm attempt. Loop/Serial timing is best effort, not a safety function. Sending A while armed does not renew the lease. A disconnected axis can still produce a plausible value: no universal disconnection detection is claimed.
Expected Output
Serial at 115200 should report disabled after reset, with no automatic sweep. With the exact unloaded setup, deliberate A while holding enable should request 1500 µs; joystick deflection should change only its paired pulse command inside 1400–1600 µs. Release/X/expiry should remove pulses and drive signals LOW; this does not prove shaft stop, torque release or a home position. No measured pulse, position, current or successful hardware run is supplied.
Troubleshooting
- A is rejected Release enable once after reset/disable, then center all three axes, hold enable and send A. Check power-off pot end/wiper identification and ADC node networks if a center is implausible; do not widen windows to conceal miswiring.
- An unplugged joystick is not reliably detected The 100kΩ is at the ADC node, not the disconnected end of the cable. It helps pull an open wiper path LOW, but some faults remain plausible. Power off and inspect; do not claim the range check is a safety-rated continuity monitor.
- Immediate movement after A That is possible: no shaft position is measured and 1500 µs is not homing. Remove power if unexpected; keep horns/loads removed. There is no startup motion ramp from a known physical position.
- Servo buzzes, binds or supply resets ESP32 Switch servo power off immediately. Check exact unit, connector identity, supply transient capability, common ground and restrained mechanics. Do not increase endpoints, hold a stall or rely on a capacitor alone.
- Servo keeps torque after pulses disable No universal torque-release behavior is assumed. Use the independent supply switch; a loaded arm could fall when released, which is why it is not part of this bench example.
- One direction feels reversed or response is coarse Pot orientation changes sign, and servo deadband/mechanics affect movement. With power off verify the axis mapping. Commands are µs, not calibrated angles; do not infer position or smooth motion from Serial text.
Common Mistakes
- Applying 5–6 V indiscriminately to generic SG90-class parts.
- Assuming ADC range checks detect every disconnected wire.
- Calling 1500 µs a home position or narrow software limits mechanical end stops.
- Powering servos from GPIO/USB or assuming pulse disable physically isolates power.
Testing Checklist
- Planned bench checks only; no firmware compilation or hardware operation has been performed.
- Verify model, connector, external supply and unpowered resistor networks.
- Check no PWM is attached on reset and that enable held during reset does not automatically arm.
- Check deliberate A, release, X, input-range fault and 10-second expiry with appropriate instruments before any mechanism is considered.
- Treat a future loaded-arm design, mechanical limits, homing and independent protection as separate engineering work.
Engineering Tips
- Keep commands and measured positions separately labelled.
- Do not leave a gravity-loaded mechanism supported only by hobby-servo torque.
- Record the exact servo revision before considering any wider travel.
Upgrade Ideas
- Study feedback and limit switches in a separately designed mechanism; none are wired here.
Real-World Applications
- Three-axis ADC-to-pulse mapping lesson
- Manual-enable and reset-state demonstration
FAQs
Does reset return the arm home?
No. Reset disables pulse output. No home sensor or shaft position is available. Deliberate enable commands 1500 µs and can cause immediate movement.
Are the existing SG90 page and this circuit interchangeable?
No. This example deliberately selects documented FS90 units accepting 3.3 V signals. Other servos need independent supply, threshold, pulse and mechanical review.
Review, Testing, and References
Author: Abdul Mubeen and the ESP32 Engine editorial team. Last updated: 2026-11-15. Reviewed: wiring logic, Arduino code structure, beginner safety, and learning sequence.
Educational level: Intermediate. Estimated completion time: Assembly time varies. This project is for learning and prototyping; production or unattended hardware needs additional engineering review.
- Author & Editorial Team
- Editorial Policy
- Testing Methodology
- Espressif ESP32 Documentation
- Arduino ESP32 Core
- Arduino-ESP32 3.3.2 GPIO API
- Classic ESP32 datasheet (board pin availability still requires checking)
- FEETECH FS90 supplier specifications and 3.3 V signal compatibility
- FEETECH FS90-C V1.0 manufacturer specification
- SparkFun COM-09032 passive thumb joystick
- Arduino-ESP32 3.3.2 ADC API and chip-specific measurement ranges
- Arduino-ESP32 3.3.2 LEDC API
- Servo pulse commands versus physical travel and neutral
Check your result
A limited command window is not verified mechanical travel. Check the intended disabled/armed states and actual signals separately; build/browser checks do not validate electrical or mechanical safety.
- Separate servo power
- Bound pulse commands
- Explain missing position feedback
