ESP32 MicroPython Joystick Servo Motor
by DIYables in Circuits > Microcontrollers
6 Views, 0 Favorites, 0 Comments
ESP32 MicroPython Joystick Servo Motor
This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-joystick-servo-motor
This build uses an ESP32 running MicroPython to control two servo motors, or a pan-tilt kit, with a joystick. The joystick's X-axis and Y-axis potentiometers each send a signal to the ESP32: the X-value drives servo motor #1 and the Y-value drives servo motor #2. Mounted on a pan-tilt kit, the two servos move together in three dimensions as you move the joystick thumbstick.
Supplies
Everything below is what the build uses. The links go to the parts we make and test.
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- ESP32 Uno-form board
- ESP32 S3 Uno-form board
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Joystick
- Servo Motor
- Pan-tilt kit with servo motor
- Jumper Wires
- Breadboard
- Screw Terminal Expansion Board for ESP32
- Breakout Expansion Board for ESP32
- Power Splitter for ESP32
- DIYables ESP32 Starter Kit (ESP32 included)
- DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
Connect the joystick's VRX pin to GPIO36 and VRY pin to GPIO39 on the ESP32, both ADC inputs. Connect the joystick's SW (button) pin to GPIO23. Connect servo motor #1's signal wire to GPIO13 and servo motor #2's signal wire to GPIO25. You can wire everything on a breadboard, or use a screw terminal block breakout board for a steadier connection. If you need to drive more servos, use a PCA9685 16-channel PWM servo driver module instead to save ESP32 pins and simplify the wiring.
How It Works
The two ADC pins read the joystick's X and Y voltages as 12-bit values, 0 to 4095. The code maps each value to a servo angle between 0 and 180 degrees, then sends that angle to the corresponding servo: the X-axis value drives servo motor #1 and the Y-axis value drives servo motor #2. Moving the joystick thumbstick in any direction rotates both servos to match, letting a pan-tilt kit track in three dimensions.
Code
The script sets up the two ADC pins with 12-bit resolution and 11dB attenuation so they can read the full 0-3.3V joystick range, then creates Joystick and Servo objects from the DIYables MicroPython libraries using the pins defined at the top of the script. In the main loop, it calls joystick.loop() to update readings, reads the X and Y values, maps each to a 0-180 degree angle with a helper function, moves both servos to those angles, and prints the raw joystick values alongside the resulting servo angles over serial.
esp32-micropython-joystick-servo-motor.py - the sketch from the tutorial, unchanged.
Full Tutorial
The complete tutorial, wiring diagram and code explanation: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-joystick-servo-motor