ESP8266 Control Servo Motor via Bluetooth

This tutorial instructs you how to program an ESP8266 to manage a Servo Motor by using either Bluetooth (HC-05 module) or BLE (HM-10 module). Instructions for both modules are given.

We will use the Bluetooth Serial Monitor App on a smartphone to transmit the angle value to ESP8266. ESP8266 will then adjust the servo motor in accordance with the received value.

ESP8266 NodeMCU Servo Motor Bluetooth

Hardware Preparation

1×ESP8266 NodeMCU
1×USB Cable Type-C
1×HC-05 Bluetooth Module
1×(Alternative) HM-10 BLE Module
1×Servo Motor
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for ESP8266
1×(Recommended) Power Splitter For ESP8266 Type-C

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Servo Motor and Bluetooth Module

If you are not familiar with Servo Motors, Bluetooth Modules, their pinouts, how they work, and how to program them, please check out the following tutorials for more information:

Wiring Diagram

  • To manage a Servo Motor with Classic Bluetooth, the HC-05 Bluetooth module should be used. A wiring diagram is supplied for reference.
The wiring diagram between ESP8266 NodeMCU and Servo Motor Bluetooth

This image is created using Fritzing. Click to enlarge image

  • To manage a Servo Motor with BLE, the HM-10 BLE module should be used. A wiring diagram is available for reference.
The wiring diagram between ESP8266 NodeMCU and Servo Motor BLE

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

ESP8266 Code - controls Servo Motor via Bluetooth/BLE

The following code is suitable for use with both the HC-10 Bluetooth module and the HM-10 BLE module. It is compatible with both.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-control-servo-motor-via-bluetooth */ #include <SoftwareSerial.h> #include <Servo.h> #define SOFT_RX D7 // The ESP8266 pin connected to the TX of the bluetooth module #define SOFT_TX D6 // The ESP8266 pin connected to the RX of the bluetooth module SoftwareSerial bluetooth(SOFT_RX, SOFT_TX); Servo servo; // create servo object to control a servo int pos = 0; // variable to store the servo position void setup() { Serial.begin(9600); bluetooth.begin(9600); servo.attach(11); // attaches the servo on pin 11 to the servo object } void loop() { if (bluetooth.available()) { // if there is data comming int angle = bluetooth.parseInt(); if (angle >= 0 && angle <= 180) { servo.write(angle); // rotate servo bluetooth.print("Rotated servo to angle: ");// reports action to smartphone app bluetooth.println(angle); } else { bluetooth.print("Invalid angle: ");// reports invalid value to smartphone app bluetooth.println(angle); } } }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Download the Bluetooth Serial Monitor App to your smartphone.
  • Take the code provided and open it in the Arduino IDE, then upload it to your ESP8266 board.
  • If you experience difficulty uploading the code, try detaching the TX and RX pins from the Bluetooth module, upload the code, and then reattach the RX/TX pins.
  • Open the Bluetooth Serial Monitor App on your smartphone and select either the Classic Bluetooth or BLE option, depending on the module being used.
Bluetooth Serial Monitor App
  • Connect the app to the HC-05 Bluetooth module or HM-10 BLE module.
Bluetooth Serial Monitor pairing
  • Enter an angle such as 45 or 90 and press the Send button.
Bluetooth Serial Monitor App
  • Witness the Servo Motor's angle alteration.
  • Examine the consequences on the Android App.
Bluetooth Serial Monitor App

If you find the Bluetooth Serial Monitor app helpful, please rate it 5 stars on Play Store. Thank you for your support!

Video Tutorial

Function References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!