ESP32 PS4 Controller Robot Car: Bluepad32 Setup and Wiring
ESP32 PS4 controller robot car: set up Bluepad32, pair the controller, wire an L298N or TB6612, then add stick speed control, a deadzone, rumble and light bar.
Building an ESP32 PS4 controller robot car is the easiest way to get a robot you drive like a real RC car, using the analog sticks of a console controller. Thanks to the Classic Bluetooth radio inside the ESP32, a DualShock 4 connects straight to the board with no receiver module, phone or console in between. What makes this possible is Bluepad32, an open-source library by Ricardo Quesada: it recognises PS4, PS5, Xbox and Nintendo Switch controllers and lets you read the sticks, buttons and triggers with one-line functions.
In this guide we build the car from start to finish: installing Bluepad32 in the Arduino IDE, pairing the controller, wiring the ESP32 to an L298N or TB6612FNG motor driver, the main sketch with throttle on the left stick and steering on the right, a deadzone that kills stick drift, and rumble plus a light bar that changes colour when you shift gear. Unlike a Wi-Fi car there is no phone involved here; you pick up the controller and drive.

What is Bluepad32 and how does a PS4 controller connect to an ESP32?
A DualShock 4 works as a standard Bluetooth HID (human interface device), exactly as it does when you pair it with a PC or a phone. Normally the console or the phone manages that link; Bluepad32 puts the ESP32 in that host role instead. The library runs the BTstack Bluetooth stack, decodes the raw reports from the controller and gives you the same functions for every brand: axisY() returns the vertical position of the left stick, throttle() how far R2 is pressed and a() whether the bottom face button is down. Its biggest advantage over the older PS4Controller library is that you don't have to mess with the MAC address stored in the controller; putting the controller in pairing mode is enough.
| Controller | Bluetooth type | Original ESP32 | ESP32-S3 / C3 | Note |
|---|---|---|---|---|
| DualShock 4 (PS4) | Classic (BR/EDR) | Works | Doesn't work | Light bar and rumble supported |
| DualSense (PS5) | Classic (BR/EDR) | Works | Doesn't work | Player LEDs can be controlled too |
| Xbox Wireless (firmware 5.x or newer) | BLE | Works | Works | Update older firmware with the Xbox Accessories app |
| Nintendo Switch Pro | Classic (BR/EDR) | Works | Doesn't work | Pairs with the sync button on top |
| DualShock 3 (PS3) | Classic (special) | Works | Doesn't work | The ESP32 address must first be written to the controller over USB |
Parts list
| Part | Qty | Note |
|---|---|---|
| ESP32 DevKit V1 (ESP32-WROOM-32) | 1 | Not S2, S3, C3 or C6; you need the Classic Bluetooth version |
| PS4 DualShock 4 controller (or PS5 / Xbox) | 1 | Genuine controllers give the most reliable results |
| L298N or TB6612FNG motor driver | 1 | Drives two DC motors independently |
| 2WD robot car chassis (2 TT motors + wheels) | 1 | The caster wheel and battery switch usually come in the kit |
| 18650 Li-ion cells and a 2-cell series holder | 2 + 1 | 7.4 V nominal, 8.4 V fully charged |
| LM2596 buck converter | 1 | Only to power the ESP32 if you use the TB6612 |
| 470 µF electrolytic + 100 nF ceramic capacitors | 1 + 2 | Against supply dips and motor noise |
| Jumper wires | 10+ | Female-female and male-female |
Installing Bluepad32 in the Arduino IDE
- Install esp32 by Espressif Systems from the Boards Manager; the Bluepad32 package takes its compiler tools from it.
- In File > Preferences > Additional Boards Manager URLs, add the board package URL given in the Bluepad32 documentation (ricardoquesada/bluepad32 on GitHub, Arduino section).
- Search the Boards Manager for bluepad32 and install esp32_bluepad32 by Ricardo Quesada.
- Under Tools > Board pick esp32_bluepad32 > ESP32 Dev Module. If you pick the board with the same name from the regular esp32 menu, the library won't be found.
- Upload File > Examples > Bluepad32_ESP32 > Controller, open the Serial Monitor at 115200 baud and pair the controller; values should scroll as you press buttons.
Pairing a PS4 controller with the ESP32
| Controller | Pairing mode | What the light does |
|---|---|---|
| DualShock 4 (PS4) | Hold SHARE and PS together for 3–5 seconds | The light bar double-flashes quickly |
| DualSense (PS5) | Hold Create and PS for 3–5 seconds | The light around the touchpad flashes quickly |
| Xbox Wireless | Hold the pair button on top for 3 seconds | The Xbox logo flashes quickly |
| Switch Pro | Hold the small sync button on top | The player LEDs run back and forth |
Once the sketch is uploaded, put the controller in pairing mode; within a few seconds the light goes solid and the Serial Monitor prints the controller model. Bluepad32 stores the pairing keys in the ESP32's flash, so next time you only need to press the PS button. If a PS4 console is switched on nearby, the controller will try to connect to it first; turn the console off or unplug it while you test. If the controller was paired with a phone or PC before, switching off their Bluetooth also helps.
Wiring: ESP32, L298N and TB6612FNG
| Signal | L298N | TB6612FNG | ESP32 |
|---|---|---|---|
| Left motor speed (PWM) | ENA (remove the jumper) | PWMA | GPIO 25 |
| Left motor direction 1 | IN1 | AIN1 | GPIO 26 |
| Left motor direction 2 | IN2 | AIN2 | GPIO 27 |
| Right motor speed (PWM) | ENB (remove the jumper) | PWMB | GPIO 33 |
| Right motor direction 1 | IN3 | BIN1 | GPIO 18 |
| Right motor direction 2 | IN4 | BIN2 | GPIO 19 |
| Standby | – | STBY | 3V3 (keeps the driver enabled) |
| Motor supply | +12V | VM | Battery pack positive (7.4 V) |
| Logic supply | +5V output → ESP32 VIN | VCC ← ESP32 3V3 | – |
| Ground | GND | GND | GND (shared with battery negative) |
These are the same pins as in the ESP32 Wi-Fi car guide, so if you built that car you only need to change the code. The L298N is an old bipolar driver that drops 1.5–3 V: with a fully charged 8.4 V pack the motors only see 5–6 V. The TB6612FNG uses MOSFETs, loses less than half a volt and delivers 1.2 A continuous per channel; that's plenty for TT motors, and the car is noticeably faster on the same battery. Its only drawback is the missing 5 V regulator; to run the ESP32 from the battery, get 5 V from an LM2596 buck converter and feed it into VIN. Both drivers share the same logic table, so the code stays the same; there's a detailed comparison in the motor driver comparison article.
Main sketch: driving the car with a PS4 controller
The sketch combines two jobs: Bluepad32 reads the controller and the LEDC peripheral sets the speed of both motors with PWM. Push the left stick forward and the car drives; push the right stick sideways and it turns. Like a real RC car, this splits throttle and steering between your two thumbs and stops you turning by accident when you want to go straight. R1 shifts up and L1 shifts down; on every gear change the light bar turns green, orange or red and the controller confirms with a short rumble. Holding Cross brakes. The PWM setup is split by core version with #if ESP_ARDUINO_VERSION_MAJOR, so the code compiles with packages based on both core 2.x and 3.x.
1#include <Bluepad32.h>
2
3// L298N ya da TB6612FNG / L298N or TB6612FNG
4// Sol motor / left motor: ENA(PWMA)=25, IN1(AIN1)=26, IN2(AIN2)=27
5// Sağ motor / right motor: ENB(PWMB)=33, IN3(BIN1)=18, IN4(BIN2)=19
6const int ENA = 25, IN1 = 26, IN2 = 27;
7const int ENB = 33, IN3 = 18, IN4 = 19;
8
9const int PWM_FREQ = 1000; // L298N: 1 kHz, TB6612: 20000 Hz
10const int PWM_RES = 8; // 8 bit: 0-255
11const int MIN_DUTY = 90; // motorun kıpırdadığı en düşük değer / lowest duty that moves the motor
12const int DEADZONE = 60; // çubuk ölü bölgesi (0-512) / stick deadzone (0-512)
13const int GEAR_LIMIT[3] = {120, 190, 255}; // vites başına en yüksek hız / top speed per gear
14
15#if ESP_ARDUINO_VERSION_MAJOR >= 3
16const int PWM_L = ENA, PWM_R = ENB; // 3.x: ledcWrite pin alır / 3.x: ledcWrite takes the pin
17#else
18const int PWM_L = 0, PWM_R = 1; // 2.x: ledcWrite kanal alır / 2.x: ledcWrite takes the channel
19#endif
20
21ControllerPtr pad = nullptr; // sürüşü yapan kumanda / the controller that drives
22int gear = 1; // 0 yavaş, 1 orta, 2 hızlı / 0 slow, 1 medium, 2 fast
23bool lastL1 = false, lastR1 = false;
24
25void setupPwm() {
26#if ESP_ARDUINO_VERSION_MAJOR >= 3
27 ledcAttach(ENA, PWM_FREQ, PWM_RES);
28 ledcAttach(ENB, PWM_FREQ, PWM_RES);
29#else
30 ledcSetup(PWM_L, PWM_FREQ, PWM_RES);
31 ledcSetup(PWM_R, PWM_FREQ, PWM_RES);
32 ledcAttachPin(ENA, PWM_L);
33 ledcAttachPin(ENB, PWM_R);
34#endif
35}
36
37// speed: -255 … 255 (negatif = geri / negative = reverse)
38void setMotor(int pwm, int inA, int inB, int speed) {
39 speed = constrain(speed, -255, 255);
40 digitalWrite(inA, speed > 0);
41 digitalWrite(inB, speed < 0);
42 int duty = (speed == 0) ? 0 : map(abs(speed), 1, 255, MIN_DUTY, 255);
43 ledcWrite(pwm, duty);
44}
45
46void stopMotors() {
47 setMotor(PWM_L, IN1, IN2, 0);
48 setMotor(PWM_R, IN3, IN4, 0);
49}
50
51// Ölü bölge + yeniden ölçekleme: -512…512 → -255…255 / deadzone + rescale
52int shape(int v) {
53 if (abs(v) < DEADZONE) return 0;
54 int s = constrain(map(abs(v), DEADZONE, 512, 0, 255), 0, 255);
55 return v > 0 ? s : -s;
56}
57
58// Vitesi ışık ve titreşimle bildir / report the gear with light and rumble
59void showGear(ControllerPtr ctl) {
60 const uint8_t colors[3][3] = {{0, 255, 0}, {255, 120, 0}, {255, 0, 0}}; // yeşil, turuncu, kırmızı / green, orange, red
61 ctl->setColorLED(colors[gear][0], colors[gear][1], colors[gear][2]);
62 ctl->playDualRumble(0, 120, 0x80, 0x00); // 120 ms hafif titreşim / 120 ms light rumble
63}
64
65void onConnected(ControllerPtr ctl) {
66 if (pad != nullptr) return; // ilk kumanda sürer / the first controller drives
67 pad = ctl;
68 Serial.printf("Bagli / connected: %s\n", ctl->getModelName().c_str());
69 showGear(ctl);
70}
71
72void onDisconnected(ControllerPtr ctl) {
73 if (ctl != pad) return;
74 pad = nullptr;
75 stopMotors(); // bağlantı kopunca dur / stop when the link drops
76 Serial.println("Baglanti koptu / disconnected");
77}
78
79void drive(ControllerPtr ctl) {
80 int throttle = -shape(ctl->axisY()); // sol çubuk: yukarı negatif gelir / left stick: up reads negative
81 int steer = shape(ctl->axisRX()); // sağ çubuk yatay / right stick horizontal
82
83 int left = throttle + steer; // tank karışımı / tank mixing
84 int right = throttle - steer;
85 int biggest = max(abs(left), abs(right));
86 if (biggest > 255) { // oranı koruyarak küçült / scale down, keep the ratio
87 left = left * 255 / biggest;
88 right = right * 255 / biggest;
89 }
90 left = left * GEAR_LIMIT[gear] / 255;
91 right = right * GEAR_LIMIT[gear] / 255;
92
93 if (ctl->a()) left = right = 0; // Çarpı (Cross): fren / Cross: brake
94 setMotor(PWM_L, IN1, IN2, left);
95 setMotor(PWM_R, IN3, IN4, right);
96
97 bool l1 = ctl->l1(), r1 = ctl->r1(); // R1 vites yükselt, L1 düşür / R1 gear up, L1 gear down
98 if (r1 && !lastR1 && gear < 2) { gear++; showGear(ctl); }
99 if (l1 && !lastL1 && gear > 0) { gear--; showGear(ctl); }
100 lastL1 = l1;
101 lastR1 = r1;
102}
103
104void setup() {
105 Serial.begin(115200);
106 int pins[] = {IN1, IN2, IN3, IN4};
107 for (int p : pins) pinMode(p, OUTPUT);
108 setupPwm();
109 stopMotors();
110
111 BP32.setup(&onConnected, &onDisconnected);
112 BP32.enableVirtualDevice(false); // dokunmatik yüzeyi fare yapma / do not expose the touchpad as a mouse
113 // BP32.forgetBluetoothKeys(); // eşleşmeleri silmek için bir kez aç / uncomment once to clear pairings
114 Serial.printf("Bluepad32 %s hazir / ready\n", BP32.firmwareVersion());
115}
116
117void loop() {
118 if (BP32.update() && pad && pad->isConnected() && pad->hasData() && pad->isGamepad()) {
119 drive(pad);
120 }
121 delay(10); // görev bekçisine zaman ver / give the watchdog some time
122}| Line | What it does |
|---|---|
| BP32.setup(&onConnected, &onDisconnected) | Registers the functions called when a controller connects or disconnects |
| BP32.update() | Fetches new data from the controllers; must be called constantly in loop() |
| -shape(ctl->axisY()) | Vertical value of the left stick; pushing up reads negative, so the sign is flipped |
| left = throttle + steer | Mixes throttle and steering into the speed of the two wheels (tank drive) |
| map(abs(speed), 1, 255, MIN_DUTY, 255) | Starts even the lowest speed at the PWM value where the motor actually moves |
| onDisconnected → stopMotors() | The car stops if the controller turns off, runs flat or goes out of range |
| delay(10) | Lets the loop breathe; otherwise the task watchdog may reset the board |
Analog sticks and deadzone tuning
Bluepad32 reports the sticks as a number from -511 to 512; centre is 0 and pushing up on the vertical axis gives a negative value. In real life no stick returns exactly to 0: a new DualShock 4 usually sends ±5–20 at rest, and a worn one up to ±40. That small number looks harmless, but because the code lifts even the smallest speed to MIN_DUTY, the motor's start-up threshold, the car will creep along without a deadzone even when you're not touching the stick. The shape() function treats everything below DEADZONE as 0 and rescales the remaining range to 0–255, so just outside the deadzone the speed starts from zero instead of jumping.
| Raw stick value | Output without deadzone | With DEADZONE = 60 | What the car does |
|---|---|---|---|
| 0–20 (drift) | 0–9 | 0 | Creeps without a deadzone, stays still with one |
| 60 | 29 | 0 | Stays still |
| 100 | 49 | 22 | Pulls away very slowly |
| 256 (half) | 127 | 110 | Medium speed |
| 512 (full) | 255 | 255 | Top speed allowed by the current gear |
Rumble and light bar feedback
The light bar on the DualShock 4 and DualSense is set with setColorLED(red, green, blue), and the rumble motors with playDualRumble(delay, duration, weak, strong). The controller has two rumble motors, a light one and a heavy one; their strength runs from 0 to 255 and the duration is in milliseconds. These commands travel to the controller as separate Bluetooth packets; call them on every loop and the link clogs up and stick response lags. That's why the code only sends colour and rumble when you change gear or when the controller connects. Using the same idea, an obstacle sensor can give a short kick on the strong motor when it sees something, or the light bar can turn red when the battery voltage drops. The small player LEDs on the DualSense are lit with setPlayerLEDs().
Racing mode: R2 to accelerate, L2 to reverse
If the driver is used to racing games, putting the throttle on the triggers feels more natural. throttle() returns how far R2 is pressed and brake() does the same for L2, both from 0 to 1023. Their difference gives a throttle value from -1023 to 1023: press R2 to go forward, L2 to reverse, and press both equally to stop. Just replace the first two lines of drive() with the three lines below; steering moves to the left stick's horizontal axis, so you can drive one-handed.
1// drive() içindeki ilk iki satırın yerine / instead of the first two lines in drive()
2int gas = ctl->throttle() - ctl->brake(); // R2 - L2: -1023 … 1023
3int throttle = (abs(gas) < 40) ? 0 : gas / 4; // tetik ölü bölgesi, -255 … 255 / trigger deadzone
4int steer = shape(ctl->axisX()); // direksiyon sol çubukta / steering on the left stickButton names on Xbox and PS5 controllers
Bluepad32 names the buttons using the Xbox layout regardless of the controller brand: a() is always the bottom face button and y() always the top one. That means the same code works unchanged with a PS5 or Xbox controller. The Xbox controller has no light bar, so setColorLED does nothing there; rumble still works.
| Bluepad32 | PS4 / PS5 | Xbox |
|---|---|---|
| a() | Cross | A |
| b() | Circle | B |
| x() | Square | X |
| y() | Triangle | Y |
| l1() / r1() | L1 / R1 | LB / RB |
| brake() / throttle() | L2 / R2 (0–1023) | LT / RT (0–1023) |
| axisX() / axisY() | Left stick | Left stick |
| axisRX() / axisRY() | Right stick | Right stick |
Common problems and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| "Bluepad32.h: No such file or directory" | The board was picked from the regular esp32 menu | Choose Tools > Board > esp32_bluepad32 > ESP32 Dev Module |
| Controller never connects, light keeps flashing | The board is an ESP32-S3, C3 or S2 with no Classic Bluetooth | Use an original DevKit based on the ESP32-WROOM-32 |
| Controller connects and drops straight away | A nearby console or an old pairing gets in the way | Turn the console off, uncomment BP32.forgetBluetoothKeys() once and pair again |
| "'ledcSetup' was not declared in this scope" | The package uses core 3.x but the code calls the old PWM functions | Use the #if ESP_ARDUINO_VERSION_MAJOR version of the code from this guide |
| The car creeps when you let go of the stick | Deadzone smaller than the controller's drift | Raise DEADZONE to 80–100 |
| The ESP32 resets when the motors start | Battery voltage dips for a moment (brownout) | Use good 18650 cells and add 470 µF at the driver input |
| Pushing forward makes the car reverse or spin | One motor's wires are swapped | Swap that motor's two wires on the driver |
| Light bar and rumble don't respond | Clone controller or a model that ignores output reports | Try a genuine controller; driving still works either way |
Ideas to upgrade your car
- Collision brake: Put an HC-SR04 at the front, ignore forward commands when an obstacle is closer than 20 cm and rumble the controller. See the HC-SR04 distance sensor guide for the sensor.
- Camera turret: Map the right stick's vertical axis to two servos for a turret that looks up and down; details in the servo motor guide.
- Headlights and brake lights: Add a WS2812B strip that glows white when driving and red when braking; the WS2812B NeoPixel article is enough to get started.
- Tilt steering: Read the DualShock 4's built-in gyro and accelerometer with gyroX() and accelX(), and steer by turning the controller like a wheel.
- Wi-Fi as a second mode: If you also want to drive the same chassis from a phone, keep the ESP32 Wi-Fi car sketch as a separate version.
- Four-wheel drive: Add a second driver to run four motors independently and build a car that slides sideways on mecanum wheels.
The ESP32 PS4 controller car teaches Bluetooth, PWM, motor drivers and safe control logic in one go, and once it's done it's genuinely fun to play with. If you want to brush up on the basics of motor drivers, take a look at DC motor control with the L298N. In the BlueGrays app you'll find the Bluetooth-controlled L298N car, the obstacle-avoiding robot and the joystick servo control projects step by step, with wiring diagrams and ready-to-use code.
Frequently asked questions
Can an ESP32 connect to a PS4 controller without a PS4?
Yes. With Bluepad32 the ESP32 becomes the host the controller connects to directly; you don't need a console, phone or USB dongle. Just put the controller in pairing mode with SHARE and PS.
Does Bluepad32 work with the ESP32-S3 or ESP32-C3?
It does, but these chips only have BLE, so only BLE controllers such as the Xbox Wireless controller will connect. PS4 and PS5 controllers use Classic Bluetooth and need an original ESP32 (WROOM-32 or WROVER).
Why won't my PS4 controller connect to the ESP32?
The usual culprits are a PS4 console switched on nearby, the controller not being in pairing mode, and the wrong board selection. Turn the console off, hold SHARE and PS until the light flashes quickly, and make sure you picked the board from the esp32_bluepad32 menu in the Arduino IDE. Some cheap clone controllers aren't recognised at all.
What is the range of a PS4 controller with an ESP32?
The DualShock 4 is a Class 2 Bluetooth device, so expect about 10 metres indoors and a little more in open space. A metal chassis or wires covering the ESP32's antenna end shorten the range, so keep the antenna end of the board clear.
Can I connect two controllers to one ESP32?
Yes, Bluepad32 supports up to four controllers at once (BP32_MAX_GAMEPADS). The sketch in this guide only drives with the first controller that connects; you can store a second one in its own variable and use it for a turret or a robot arm, for example.
Should I use the PS4Controller library or Bluepad32?
PS4Controller only supports the DualShock 4 and makes you either rewrite the host MAC address stored in the controller with a separate tool or give the ESP32 that address; it can also fail to compile on newer ESP32 cores. Bluepad32 uses normal pairing, also handles PS5, Xbox and Switch controllers, and is actively maintained.
Projects to build after reading this
See all projects
Bluetooth Controlled Car with L298N
Turn your phone into a remote and drive your own robot car. The Arduino receives commands from a remote-control app on your phone through a Bluetooth module and spins the wheels with a motor driver…

Obstacle Avoidance Robot
How about building a smart vehicle that finds its own way? The Obstacle Avoidance Robot constantly scans its surroundings thanks to the ultrasonic sensor in front of it.

Control an LED from Your Phone via Bluetooth
A small rehearsal for switching the lights in your room on and off from your phone.

Led Control with Joystick
Push the little stick on the joystick all the way in any direction and the LED for that direction lights up: there are four different colours for right, left, up and down.

Servo Control with Joystick
Steer two motors with a joystick, just like turning a security camera. Push the stick left or right and the bottom servo motor slowly turns, push it up or down and the top servo tilts up and down…

ESP32 LED Fade with PWM
Smoothly changing the brightness of the LED from 0 to max, from max to 0. We control PWM with analogWrite() as in Arduino - the same syntax as ESP32 Arduino Core 3.0+ works without problems. Pin definition + pinMode + analogWrite is enough.

ESP32 LED Brightness Control with a Potentiometer (Analog → PWM)
In this project, we control the brightness of an LED analogically using a potentiometer. The ESP32's ADC pin reads the 0-3.3V voltage from the potentiometer, and this value is then converted into a PWM (Pulse Width Modulation) signal and sent to the LED. As a result, as you turn the potentiometer, the LED gradually brightens and dims. With Arduino Core 3.0+, analogWrite() can be used directly; on the ESP32-S2/S3, the ADC resolution is fixed at 12-bit.

ESP32 Servo Motor (Potentiometer Control)
In this project, we control an SG90 servo motor using a potentiometer. We adjust the servo angle by mapping the analog value read from the potentiometer to the 0–180-degree range. The ESP32Servo library generates servo signals on suitable output-capable GPIO pins of the ESP32; this project uses GPIO 18. This is a fundamental control method for robotic arms, camera gimbals, and automatic door mechanisms.
More articles
BlueGrays app
Build this circuit step by step on your phone
Wiring diagram, code and parts list on one screen. 500+ Arduino, ESP32, ESP8266, Pico, mBot and micro:bit projects ready to build.
Scan with your phone
Point your camera at the code and your phone's app store opens.
or open the store

