搜索代码

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
语言: C++
/*
 * Original Servo Library sourse: https://github.com/RoboticsBrno/ESP32-Arduino-Servo-Library
 * 
 * This is Arduino code to control Multiple servo motors with ESP32 boards over bluetooth 
 * When press a button on screen, Servo will go 0° to 180° and stops and next press will send the servo from 180° to 0° 
 * this is ESP-SERV-BT-4
 * The Mininum Angle(0°) and Maximum Angle (180°) can be set to any angle you prefer.
 * 
 * Watch video instruction for this code: https://youtu.be/FOz9u_R5eG4
-code ESP-SERV-BT-1 Using ESP32 Bluetooth to Control Servo motor to move it to any angle 
-code ESP-SERV-BT-2 Using ESP32 Bluetooth to Control Servo motor to move them 0° to 180° or in reverse or any angle    
-code ESP-SERV-BT-3 PUSH-RETURN Using ESP32 Bluetooth to Control 1 or more Servo motors to move them 0° to 180° or in reverse or any angle    
-code ESP-SERV-BT-4 Using ESP32 Bluetooth to Control 1 or more Servo motors to move them 0° to 180° or in reverse or any angle one way    

 * Written by Ahmad Shamshiri for Robojax Video channel www.Robojax.com
 * Date: Jan 10, 2019, at 20:25 in Ajax, Ontario, Canada
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * this code has been downloaded from https://robojax.com
 
 * Get this code and other Arduino codes from Robojax.com


or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include <Servo_ESP32.h>

const int servoCount = 4;
static const int servosPins[servoCount] = {12, 14, 27, 26};

Servo_ESP32 servos[servoCount];

const char turnON[] ={'a', 'b', 'c', 'd'};


int servoAngle[] ={0, 0, 0, 0};
int servoAngleStep[] ={10, 10, 10, 10};

int servoAngleMin[] ={0, 0, 0, 0};
int servoAngleMax[] ={180, 180, 180, 180};

int buttonPushed[] ={0, 0, 0, 0};
char receivedChar;// received value will be stored as CHAR in this variable
boolean fullDebug = true;//set to false or true. watch video for details

#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void handleServo(int servoNum);
void setup() {
  // robojax.com code for servo https://youtu.be/FOz9u_R5eG4
  Serial.begin(115200);
  SerialBT.begin("ESP32_Robojax"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth\n with ESP32_Robojax");
  
    for(int i = 0; i < servoCount; ++i) {
        if(!servos[i].attach(servosPins[i])) {
            Serial.print("Servo ");
            Serial.print(i);
            Serial.println("attach error");
        }
    }
    //Servo control using ESP32 from Robojax.com

}

void loop() {
  //Robojax.com ESP32 Servo BT code
    receivedChar =(char)SerialBT.read();

  for(int i=0; i< servoCount; i++)
  {
       if(receivedChar == turnON[i]){
        buttonPushed[i] = 1;
       }//if 
  }//for


  for(int i=0; i< servoCount; i++)
  {
       if( buttonPushed[i] ){
        handleServo(i);//
       }//if 
  }//for

  
  for(int i=0; i< servoCount; i++)
  {
       servos[i].write(servoAngle[i]);
      
  }//for
   
  //servos[0].write(30);
   delay(20);

}//loop

/*
 * handleServo()
 * updates the servo angle
 * returns nothing
 * Written by Ahmad Shamshiri on Jan 10, 2019
 * www.Robojax.com
 * watch video for details https://youtu.be/FOz9u_R5eG4
 */
void handleServo(int servoNum)
{
  if(fullDebug)
  {
    SerialBT.print("Servo ");// write on BT app
    SerialBT.print(servoNum+1);// write on BT app 
    SerialBT.print(" at:");// write on BT app       
    SerialBT.println(servoAngle[servoNum]);// write on BT app 
    Serial.print("Servo ");//print on Serial Monitor
    Serial.print(servoNum+1);//print on Serial Monitor
    Serial.print(" at ");//print on Serial Monitor        
    Serial.println(servoAngle[servoNum]); //print on Serial Monitor     
  }
      servoAngle[servoNum] += servoAngleStep[servoNum];
       
        if (servoAngle[servoNum] >= servoAngleMax[servoNum]) {
          buttonPushed[servoNum] =0; 
          servoAngleStep[servoNum] = -servoAngleStep[servoNum];
            if(!fullDebug)
            {
            SerialBT.print("Servo ");// write on BT app
            SerialBT.print(servoNum+1);// write on BT app 
            SerialBT.print(" at:");// write on BT app       
            SerialBT.println(servoAngle[servoNum]);// write on BT app              
            }
        }
        
        if (servoAngle[servoNum] <= servoAngleMin[servoNum]) {
          buttonPushed[servoNum] =0;       
          servoAngleStep[servoNum] = -servoAngleStep[servoNum];
            if(!fullDebug)
            {
            SerialBT.print("Servo ");// write on BT app
            SerialBT.print(servoNum+1);// write on BT app 
            SerialBT.print(" at:");// write on BT app       
            SerialBT.println(servoAngle[servoNum]);// write on BT app              
            }          
        }
    
}//handleServo

资源与参考

尚无可用资源。

文件📁

没有可用的文件。