Control one or more servo motors using an ESP32 and Bluetooth mobile device: ESP32-SERV-BT-4

Control one or more servo motors using an ESP32 and Bluetooth mobile device: ESP32-SERV-BT-4

This project demonstrates how to control one or more servo motors wirelessly using an ESP32 microcontroller and a Bluetooth connection to a mobile device. By leveraging the ESP32's built-in Bluetooth capabilities, you can create a remote control system for various applications without needing additional hardware. This is ideal for projects requiring precise motor control from a distance.

This setup is perfect for a wide range of DIY electronics and robotics projects. For example, you could use it to:

  • Control the joints of a robotic arm.
  • Operate the steering mechanism of a remote-controlled car or boat.
  • Automate a camera pan-and-tilt mount.
  • Create a smart pet feeder with a rotating dispenser.
  • Build a remote-controlled window blind or lock mechanism.

Hardware Required

  • ESP32 development board (any variant)
  • SG90 or similar servo motor(s) (up to 4 in this example)
  • External 5V power supply (for multiple servos)
  • Breadboard and jumper wires (male-to-male and male-to-female)
  • A mobile device with Bluetooth and a serial terminal app

Wiring Guide

The wiring involves connecting power, ground, and signal for each servo (in video at 05:39). For multiple servos, it's efficient to create power and ground bus lines on a breadboard. The signal wire for each servo connects directly to a dedicated GPIO pin on the ESP32. The pins used in this example are 12, 14, 27, and 26, which are conveniently located next to each other on many ESP32 boards (in video at 09:10). When using more than one or two servos, an external 5V power supply is essential to avoid overloading the ESP32's onboard regulator. Crucially, the ground of the external power supply must be connected to a GND pin on the ESP32 to establish a common ground reference (in video at 09:41).

Software Setup

To program the ESP32, you need to set up the Arduino IDE. First, add the ESP32 board support by installing the "ESP32 by Espressif Systems" package via the Boards Manager (in video at 10:06). Next, you need to install the required library. The code uses a modified version of the ESP32-Arduino-Servo-Library. Download the provided ZIP file and add it to your IDE via Sketch > Include Library > Add .ZIP Library... (in video at 12:07).

Code Explanation

The code is designed to be highly configurable. The key settings are defined at the top of the sketch, allowing you to customize the project for your specific needs without modifying the core logic.

const int servoCount = 4;
static const int servosPins[servoCount] = {12, 14, 27, 26};
const char turnON[] ={'a', 'b', 'c', 'd'};
int servoAngleStep[] ={10, 10, 10, 10};
int servoAngleMin[] ={0, 0, 0, 0};
int servoAngleMax[] ={180, 180, 180, 180};
boolean fullDebug = true;
  • servoCount: Change this value to match the number of servos you are using.
  • servosPins[]: This array defines the GPIO pins to which each servo's signal wire is connected. Update the pin numbers to match your wiring.
  • turnON[]: These are the characters that, when sent via Bluetooth, will activate each corresponding servo. You can change these to any single character (e.g., '1', '2', 'x', 'y').
  • servoAngleStep[]: This array sets the increment (in degrees) for each servo's movement. A smaller value results in smoother but slower movement; a larger value makes it faster but more jerky.
  • servoAngleMin[] and servoAngleMax[]: These arrays define the minimum and maximum rotation angles for each servo. You can limit the range of motion for each servo individually here.
  • fullDebug: Setting this to true provides detailed angle information on the Bluetooth app. Set it to false for a cleaner display that only shows the final angle.

The handleServo() function manages the movement logic. Each time a character is received, it increments the servo's angle by the defined step until it reaches the maximum angle. The next press reverses the direction and moves it back to the minimum angle (in video at 19:55).

Live Demonstration and Usage

After uploading the code, you need a Bluetooth serial terminal app on your mobile device. A popular choice is the "Serial Bluetooth Terminal" app available on the Play Store (in video at 21:45). Enable Bluetooth on your phone and look for a device named "ESP32_Robojax" to pair with it. Open the app, connect to the ESP32, and then create buttons within the app that send the characters you defined in the turnON[] array (e.g., 'a', 'b', 'c', 'd') (in video at 23:11). When you press a button, the corresponding servo will move to its maximum angle and stop. Pressing the same button again will move it back to its minimum angle. You can also type the characters directly into the app's terminal to control the servos (in video at 24:29).

Chapters

  • [00:00] Introduction and project overview
  • [03:41] Explanation of servo motors
  • [05:39] Wiring the servos to the ESP32
  • [10:00] Setting up the Arduino IDE for ESP32
  • [12:04] Installing the required library
  • [12:47] Code explanation and configuration
  • [21:45] Connecting the mobile app and demonstration
  • [25:33] Conclusion
285-Arduino code to control Multiple servo motors with ESP32 boards over bluetooth
Language: C++
Copied!

Resources & references

No resources yet.

Files📁

No files available.