This autonomous 4WD robot arm combines wheeled mobility with articulated manipulation, controlled by an ESP32 microcontroller. The platform integrates four brushed DC gearmotors for drive via an L298N H-bridge, servo-driven arm joints managed by a PCA9685 controller, ultrasonic ranging for obstacle detection, and current monitoring through an INA219 sensor. The guide provides a complete wiring diagram, component parts list, and firmware source code to get the robot moving and sensing its environment.
Builders will learn how to configure the ESP32's PWM outputs for motor speed control, communicate with I2C servo and current-sensing modules, and implement interrupt-driven ultrasonic distance measurement. The assembly outline covers motor and servo connections with notes on future power distribution and remote control interfaces, making this an ideal platform for learning robotics fundamentals from the ground up.
Wiring diagram

Gather all the parts
Assemble it in 3 steps
1. Stage 1 simulation only
For each four-leg button, wire diagonally opposite legs to SIGNAL and GND. Pressed closes the contact and reads LOW. Press Rearm with E-Stop and Dead-Man released, then hold Dead-Man. Press E-Stop to latch the stop. At 115200 baud, send DIST 50 for clear range, DIST 15 for an obstacle, DIST -1 for missing echo, or DIST AUTO to return to the sensor.
2. Motor and servo connections
Each L298N output pair feeds two parallel motors on one side. Remove both enable jumpers and the onboard 5 V regulator jumper. Bridge VSS is 5 V logic; VS is a separate modeled 6 V supply. Set PCA9685 A0 closed for 0x41, and use INA219 at 0x40. CH0 through CH3 are pan, shoulder, elbow and gripper. Servo V+ is a separate modeled 5.5 V supply.
- Do not connect a LiPo or real actuators to this simulation schematic. Motor stall current, switch ratings and braking behavior have not been validated.
3. Future power and remote interfaces
The supply labels stand in for the two physical branches. Branch A stays powered for controller and diagnostics; place the INA219 breakout only on the logic branch unless its shunt and connectors are replaced by a correctly rated design. Branch B needs an independently verified normally closed stop chain, hardware watchdog/latch and rated cutoff upstream of the motor regulator and servo buck. Join logic and power returns at a six-way M3 star point, not through the controller. GPIO6 is the heartbeat; the 100 nF coupling capacitor does not implement a cutoff. The separate ESP32-C3 remote block is planned: SSD1306, two joysticks, NC stop, hold trigger, STOP/RESET and four mode keys; ESP-NOW frames at 50 Hz with receiver freshness checking. Stage 1 uses local controls; no radio receiver or transmitter is loaded.
Review all connections
1. Connections between "ina" and "ESP32"
2. Connections between "pca" and "ESP32"
3. Connections between "oe_pull" and "ESP32"
4. Connections between "pan" and "ESP32"
5. Connections between "shoulder" and "ESP32"
6. Connections between "elbow" and "ESP32"
7. Connections between "gripper" and "ESP32"
8. Connections between "sda_pull" and "ESP32"
9. Connections between "scl_pull" and "ESP32"
10. Connections between "bridge" and "ESP32"
11. Connections between "left_front_cap" and "ESP32"
12. Connections between "left_rear" and "ESP32"
13. Connections between "left_rear_cap" and "ESP32"
14. Connections between "right_front_cap" and "ESP32"
15. Connections between "right_rear" and "ESP32"
16. Connections between "right_rear_cap" and "ESP32"
17. Connections between "pwm7_pull" and "ESP32"
18. Connections between "pwm8_pull" and "ESP32"
19. Connections between "sonar" and "ESP32"
20. Connections between "echo_top" and "ESP32"
21. Connections between "echo_bottom" and "ESP32"
22. Connections between "vbat_top" and "ESP32"
23. Connections between "vbat_bottom" and "ESP32"
24. Connections between "stop" and "ESP32"
25. Connections between "stop_pull" and "ESP32"
26. Connections between "stop_cap" and "ESP32"
27. Connections between "deadman" and "ESP32"
28. Connections between "rearm" and "ESP32"
29. Connections between "blue_r" and "ESP32"
30. Connections between "blue" and "ESP32"
31. Connections between "yellow_r" and "ESP32"
32. Connections between "yellow" and "ESP32"
33. Connections between "red_r" and "ESP32"
34. Connections between "red" and "ESP32"
35. Connections between "green_r" and "ESP32"
36. Connections between "green" and "ESP32"
37. Connections between "logic_cap" and "ESP32"
38. Connections between "heartbeat_cap" and "ESP32"
Deploy the firmware
#include <Arduino.h>
#include <Wire.h>
#include <ctype.h>
#include <math.h>
#include <Adafruit_NeoPixel.h>
#include <esp_arduino_version.h>
constexpr int STOP=14, DEAD=13, RESET=2, OE=40;
Adafruit_NeoPixel rgb(1,48,NEO_GRB+NEO_KHZ800);
portMUX_TYPE mux=portMUX_INITIALIZER_UNLOCKED;
volatile uint32_t rise=0,width=0,stamp=0;
volatile bool rising=false, ready=false;
void IRAM_ATTR echoEdge(){
uint32_t t=micros(); bool high=digitalRead(16);
portENTER_CRITICAL_ISR(&mux);
if(high){rise=t;rising=true;} else if(rising){width=t-rise;stamp=t;ready=true;rising=false;}
portEXIT_CRITICAL_ISR(&mux);
}
void duty(int pin,int value){
#if ESP_ARDUINO_VERSION_MAJOR >= 3
ledcWrite(pin,value);
#else
ledcWrite(pin-7,value);
#endif
}
void speeds(int d){duty(7,d);duty(8,d);}
void direction(bool brake){for(int i=9;i<=12;i++)digitalWrite(i,brake || (i%2));}
bool reg(uint8_t address,uint8_t value){Wire.beginTransmission(0x41);Wire.write(address);Wire.write(value);return Wire.endTransmission()==0;}
bool pose(int ch,int angle){
int count=205+angle*205/180;
Wire.beginTransmission(0x41); Wire.write(6+4*ch);Wire.write(0);Wire.write(0);Wire.write(count&255);Wire.write(count>>8);return Wire.endTransmission()==0;
}
bool armed=false,latched=false,hb=false,trig=false,valid=false,driving=false,braking=false,pcaOK=false,resetReady=false;
uint32_t lastHb,lastPing,lastTick,lastLog,brakeAt,lastEcho;
int32_t dist=0,cov=4<<16,raw=0;
int angles[]={90,90,90,20}, targets[]={90,105,75,15};
enum State {IDLE, ACTIVE, HOLD, ESTOP};
State state=IDLE, shown=ESTOP;
const char* names[]={"IDLE_SAFE","ACTIVE_DRIVE","OBSTACLE_HOLD","ESTOP_CUTOFF"};
enum Mode { DRIVE, RADAR, ARM, PARK };
Mode mode=DRIVE;
bool virtualStop=false, virtualDead=false, rearm=false;
int injected=-2; // -2: hardware, -1: simulated missing echo
bool inaOK=false;
float fallbackVbat=11.80f;
bool overrideVbat=false;
void execute(char* line, Stream& reply){
while(isspace((unsigned char)*line))++line;
char* end=line+strlen(line);
while(end>line && isspace((unsigned char)end[-1]))*--end=0;
if(!*line)return;
bool ok=true;
if(!strcmp(line,"ESTOP 1")){virtualStop=true;latched=true;armed=false;}
else if(!strcmp(line,"ESTOP 0"))virtualStop=false;
else if(!strcmp(line,"DEAD 1"))virtualDead=true;
else if(!strcmp(line,"DEAD 0"))virtualDead=false;
else if(!strcmp(line,"REARM"))rearm=!virtualStop && !virtualDead && digitalRead(STOP) && digitalRead(DEAD);
else if(!strcmp(line,"MODE 4WD"))mode=DRIVE;
else if(!strcmp(line,"MODE RADAR"))mode=RADAR;
else if(!strcmp(line,"MODE ARM"))mode=ARM;
else if(!strcmp(line,"MODE IDLE")){mode=PARK;armed=false;}
else if(!strcmp(line,"DIST AUTO")){injected=-2;valid=false;}
else if(!strncmp(line,"DIST ",5)){
char* tail;long n=strtol(line+5,&tail,10);
ok=tail!=line+5 && !*tail && (n==-1 || (n>=2 && n<=400));
if(ok){injected=n;if(n==-1)valid=false;}
}else if(!strncmp(line,"VBAT ",5)){
char* tail;float v=strtof(line+5,&tail);
ok=tail!=line+5 && !*tail && isfinite(v) && v>=0 && v<=20;
if(ok){fallbackVbat=v;overrideVbat=true;}
}else ok=false;
reply.print(ok?"CMD OK: ":"CMD ERR: ");reply.println(line);
}
struct CommandInput {
char line[48]; unsigned len=0; bool overflow=false; uint32_t lastByte=0;
void finish(Stream& port){
if(overflow)port.println("CMD ERR: line too long");
else if(len){line[len]=0;execute(line,port);}
len=0;overflow=false;
}
void poll(Stream& port){
for(int budget=64;budget && port.available();--budget){
int c=port.read();if(c<0)break;
lastByte=millis();
if(c=='\r'||c=='\n')finish(port);
else if(len<sizeof(line)-1)line[len++]=char(c);
else overflow=true;
}
if((len||overflow) && !port.available() && millis()-lastByte>=100)finish(port);
}
};
void commands(){
static CommandInput primary;
primary.poll(Serial);
#if ARDUINO_USB_CDC_ON_BOOT
static CommandInput uart;
uart.poll(Serial0);
#endif
}
bool probe(uint8_t addr){Wire.beginTransmission(addr);return Wire.endTransmission()==0;}
float battery(){
if(overrideVbat)return fallbackVbat;
uint32_t mv=analogReadMilliVolts(1);
if(mv>=1000)return mv*0.0057f;
if(!inaOK)return fallbackVbat;
Wire.beginTransmission(0x40);Wire.write(0x02);
// Use a STOP between register selection and read for simulator compatibility.
if(Wire.endTransmission()!=0 || Wire.requestFrom(uint8_t(0x40),uint8_t(2))!=2){
inaOK=false;return fallbackVbat;
}
uint16_t bus=uint16_t(Wire.read())<<8;bus|=Wire.read();
return (bus>>3)*0.004f;
}
void setup(){
Serial.begin(115200);
#if ARDUINO_USB_CDC_ON_BOOT
Serial0.begin(115200);
#endif
for(int pin:{6,7,8,9,10,11,12,15,17,18,21,OE})pinMode(pin,OUTPUT);
digitalWrite(OE,HIGH);
for(int pin:{STOP,DEAD,RESET})pinMode(pin,INPUT_PULLUP);
#if ESP_ARDUINO_VERSION_MAJOR >= 3
ledcAttach(7,20000,8);ledcAttach(8,20000,8);
#else
for(int pin:{7,8}){ledcSetup(pin-7,20000,8);ledcAttachPin(pin,pin-7);}
#endif
speeds(0); pinMode(16,INPUT);attachInterrupt(digitalPinToInterrupt(16),echoEdge,CHANGE);
Wire.begin(4,5,400000);Wire.setTimeOut(2);
latched=!digitalRead(STOP);
inaOK=probe(0x40);
pcaOK=probe(0x41);
pcaOK=pcaOK&®(0,0x10)&®(0xfe,121)&®(0,0x20);
rgb.begin();rgb.setBrightness(24);
}
void loop(){
commands();
bool stop=!digitalRead(STOP)||virtualStop, dead=!digitalRead(DEAD)||virtualDead, reset=!digitalRead(RESET);
bool resetEdge=reset && resetReady;
resetReady=!reset;
if(stop){latched=true;armed=false;}
else if(!dead && (resetEdge || rearm)){latched=false;armed=true;}
rearm=false;
uint32_t now=micros(),w=0,t=0;
portENTER_CRITICAL(&mux);
if(ready){w=width;t=stamp;ready=false;}
portEXIT_CRITICAL(&mux);
if(injected==-1 || (valid && uint32_t(now-lastEcho)>150000)) valid=false;
static uint32_t lastSample=0;
bool synthetic=injected>=0 && uint32_t(now-lastSample)>=20000;
if(synthetic || (injected==-2 && w>=116 && w<=23200)){
lastSample=now;
raw=synthetic?(injected<<16):w*1124L;
if(!valid){dist=raw;cov=4<<16;}
else {cov+=1<<16;int32_t k=(int64_t(cov)<<16)/(cov+(9<<16));dist+=(int64_t(k)*(raw-dist))>>16;cov=(int64_t(65536-k)*cov)>>16;}
valid=true;lastEcho=synthetic?now:t;
}
state=latched?ESTOP:!armed||!dead||mode==PARK?IDLE:!valid||raw<(20<<16)||dist<(20<<16)?HOLD:ACTIVE;
if((state!=ACTIVE || mode!=DRIVE) && driving){
speeds(0);driving=false;direction(true);speeds(255);braking=true;brakeAt=now;
}
if(braking && uint32_t(now-brakeAt)>=15000){speeds(0);braking=false;}
if(state!=ACTIVE)digitalWrite(OE,HIGH);
if(latched){hb=false;digitalWrite(6,LOW);lastHb=now;}
else if(uint32_t(now-lastHb)>=10000){lastHb=now;hb=!hb;digitalWrite(6,hb);}
if(trig && uint32_t(now-lastPing)>=10){digitalWrite(15,LOW);trig=false;}
if(!trig && uint32_t(now-lastPing)>=60000){lastPing=now;trig=true;digitalWrite(15,HIGH);}
if(uint32_t(now-lastTick)>=20000){
lastTick=now;
if(state==ACTIVE && !braking){
if(mode==DRIVE && !driving){direction(false);speeds(170);driving=true;}
if(pcaOK){
static int panTarget=145;
int first=mode==ARM?1:0, end=mode==RADAR?1:4;
if(mode==RADAR){
if(angles[0]==panTarget)panTarget=panTarget==145?35:145;
targets[0]=panTarget;
}else targets[0]=90;
for(int ch=first;ch<end && pcaOK;++ch){
angles[ch]+=constrain(targets[ch]-angles[ch],-2,2);
pcaOK=pose(ch,angles[ch]);
}
digitalWrite(OE,!pcaOK);
}
}
}
if(shown!=state){shown=state;digitalWrite(17,state==ACTIVE);digitalWrite(18,state==HOLD);digitalWrite(21,state==ESTOP);
rgb.setPixelColor(0,state==ESTOP?rgb.Color(255,0,0):state==HOLD?rgb.Color(255,90,0):state==ACTIVE?rgb.Color(0,0,255):0);rgb.show();}
if(uint32_t(now-lastLog)>=200000 && Serial.availableForWrite()>=100){
lastLog=now;float vb=battery();
Serial.printf("%s cm=%.1f Vbat=%.2f dead=%d stop=%d servo=%d,%d,%d,%d\n",names[state],valid?dist/65536.0f:-1.0f,vb,dead,latched,angles[0],angles[1],angles[2],angles[3]);
}
}
// GPIO4/5 route Wire through the ESP32-S3 GPIO matrix.Remix this project
Make it yours in one click
Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.




