IoT Projects

Speed control for brushless motors with an ESP8266

Christopher Méndez
· 5 min read
Send by email

Learn how the brushless motors works, how to control them with PWM using a speed controller, an ESP8266, and Ubidots.

What are Brushless motors?

Also known as synchronous DC motors, they are an special type of motor that consist on a stator and a rotor as most of the motors out there, but with a difference, "brushless motors" ,as it name tell us, do not have brushes electrically connected between the stator and the rotor to reduce friction, vibrations, inefficiency.

How do they work?

These motors are associated with an inverter. The inverter makes the supply voltage to the coils sequentially, thus the poles of the rotor move according to the magnetic field generated by the coils sequentially. The speed of the rotor and its axis, will depend on the speed of sequencing of the variator.

We can find them normally on RC airplanes or cars, also drones.

In the follow tutorial we are going to learn about Brushless motors, how to control them with an ESP8266 (NodeMCU) and Ubidots.

Application requirements:

Then, use 2 wires to connect Gnd and signal to the respective input of the ESC.

ESC NodeMCU
Back wire GND - Black wire
White wire D5 - Yellow wire

In this case we do not use the red wire of the ESC because it supplies 5v and our NodeMCU works at 3.3v, so we can damage it.

2. Ubidots Device and variable creation.

Go to the Device section of your Ubidots account create your device called "motor".

Inside your "motor" device, create a variable called "speed".

3. Ubidots Dashboard and Widget creation.

Once our device and variable are created, we can create a dashboard and widget to control the motor from a web or mobile dashboard.

Now, create a control widget to set the speed of the motor associated with the variable "speed".

Then you are ready to program and test your project.

4. Programing with the Arduino IDE.

1. If not done yet, download the Arduino IDE.

1a. Open the IDE and select Files -> Preferences

1b. Add the url below into the Additional Board Manager URLsfield. You can add multiple URLs by separating them with commas.

http://arduino.esp8266.com/stable/package_esp8266com_index.json

2. Open and Install the ESP8266 board in the Boards Manager: Tools -> Board -> Boards Manager
2a. You can easily find the board by typing “ESP8266” in the search bar.

3. Now select the NodeMCU 1.0 board from Tools -> Board menu

4. Define or double check the Port of your PC which the device is communicating with. Go to Tools -> Port: -> Select the port

4b. Ensure your IDE Upload Speed is 115200 by going to Tools -> Upload Speed -> 115200

5. Download the UbidotsESPMQTT library if you haven’t already. Now, click on Sketch –> Include Library –> Add .ZIP Library and choose the Ubidots ESP8266 MQTT library

If properly uploaded, you get the response: "Library added to you libraries."

8. Close and open again the Arduino IDE.

Programming the NodeMCU ESP8266:

Once your ESP8266 is set up, we can post and get data from Ubidots in order to control your Brushless Motor.

1. Copy and paste the following code in the Arduino IDE. Don’t forget to customize the Wi-Fi SSID and password and your Ubidots Token.

/****************************************
   Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"
#include <Servo.h>
/****************************************
   Define Constants
 ****************************************/
#define TOKEN "............" // Your Ubidots TOKEN
#define WIFINAME "............" //Your SSID
#define WIFIPASS "............" // Your Wifi Pass
#define DEVICE_LABEL  "motor"  // Put here your Ubidots device label
#define VARIABLE  "speed"  // Put here your Ubidots variable label
#define MotorPin D5 //NodeMCU pin where the signal for the ESC comes out
Servo ESC; //Servo variable
float value=0; // To store incoming value.
float MotorSpeed=0;
Ubidots client(TOKEN);
/****************************************
   Auxiliar Functions
 ****************************************/
// cast from an array of chars to float value.
float btof(byte * payload, unsigned int length) {
  char * demo = (char *) malloc(sizeof(char) * 10);
  for (int i = 0; i < length; i++) {
    demo[i] = payload[i];
  }
  float value = atof(demo);
  free(demo);
  return value;
}
// Callback to handle subscription
void callback(char* topic, byte* payload, unsigned int length) {
    value = btof(payload, length);
      value = map(value, 0, 100, 0, 180); //Map the 0-100 values from the slider to the 0-180 to use the servo lib.
      ESC.write(value); //Send the value (PWM) to the ESC
}
/****************************************
   Main Functions
 ****************************************/
void setup() {
  // put your setup code here, to run once:
  client.ubidotsSetBroker("industrial.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(true); // Pass a true or false bool value to activate debug messages
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE); //Insert the dataSource and Variable's Labels
  ESC.attach(MotorPin,1000,2000);
}
void loop() {
  // put your main code here, to run repeatedly:
  if (!client.connected()) {
    client.reconnect();
    client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE); //Insert the dataSource and Variable's Labels 
  }
  client.loop();
}


Now, verify your code is correct by clicking the check button in the Arduino IDE above the editor.

Once the code is verified, you will receive a response similar to the one below, indicating that it is properly set up.

Next, your have to upload the code into your NodeMCU. To do this, choose the right-arrow icon besides the check icon.

Once the code is uploaded, you will receive the message below in the Arduino IDE:

Testing Session:

Connect the battery or power supply to the ESC.

Then, everything is ready, just swipe the slider on your web or mobile dashboard.

Summary:

In this guide we just learned how do Brushless motors works and how to control them with the ESP8266 NodeMCU and Ubidots. This project could help us to design electronics control systems for mechanics devices. Ex: Automatic Curtains, a thermo-controlled fan for summer, and everything in wich your imagination can get into with motors.