Using the ESP8266 as a micro-controller for servomotors

Schematics

The ESP8266 is at the center of our circuit diagram (Figure 4). The voltage supply is 3.3 volts. As you may be aware from parts 1 and 2 of this project, this powers the transfer of data (USB to serial) as well as the circuitry for the operating mode (Flash or Run) [2].

Figure 4: Circuit diagram for the robot arm.

Looking at the above diagram, you'll see that the servos are now controlled via an opto-isolator. Opto-isolators provide galvanic isolation of circuit pathways. R5 – R9 are resistors connected in series for the LEDs in the opto-isolator. Resistors R10 to R13 generate a predefined signal to the servos when the output transistors of the opto-isolators are inactive. Protecting the circuits via galvanic separation is worthwhile to keep the ESP8266 safe from any interference by the servos.

Capacitor C1 is used to keep the ESP8266 operating voltage constant, even when the servos are working. Transistor T1 turns the magnet on and off. At first glance, you may think diode D1 serves no useful purpose. However, it plays an important role when the coils come into play. (See the box out Coils below).

Coils

A coil is thinly insulated copper wire wrapped around a metal core. Coils will behave differently depending on whether you use AC or DC voltage. For this project, we're using DC voltage.

As soon as the transistor is activated, the full operating voltage is applied to the coil and current begins to flow. The magnetic field increases with the current. Once the magnetic field is established, the current is determined by the ohmic resistance of the coil. Energy is stored in the coil's magnetic field.

When the transistor deactivates, current doesn't simply stop flowing through the coil. The coil instead loses current until the magnetic field collapses. In this electrical circuit however, there's a non-conducting high-resistance transistor, through which the coil is pushing current.

This means that the voltage across the transistor spikes sharply to several thousand volts. Under normal circumstances, this would result in the transistor going up in a puff of smoke. Fortunately diode D1 comes to the rescue, acting as a "flyback diode", directing energy in the same direction in which the current generated by the coil is flowing. As a result, there's no unsafe voltage spike during the switch process. Make sure to connect the flyback diode, as without it the transistor or the ESP8266 itself could be damaged.

The motors in the servos are also equipped with coils, meaning the motor itself prevents high voltages. However, the servo will start drawing a fairly large current as soon as it activates. This can cause the operating voltage to lower briefly. Capacitor C1 only partially resolves this issue.

In light of this, if the servos fail to fire up you may think there's a bug in the program, when in actual fact the operating voltage is probably not high enough. There are two ways to get around this problem. One solution is to separate the servo power supply from that of the ESP8266 so the dip in voltage doesn't cause the program to crash. Alternatively, use a voltage source that can deal with fluctuations like these, such as a laboratory power supply.

Program

The program Robot.ino (Listing 1) acts as a simple web server on the ESP8266 to control the individual servos and magnets via a URL. As in the previous part of this series, adjust the network settings to your needs. Make sure the server has a fixed IP address.

Listing 1

Robot.ino web server

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Servo.h>
const char* ssid = "mywifinetwork";
const char* pass = "E1vtbbuY!";
IPAddress ip(192.168.1.21);
IPAddress gateway(192.168.1.1);
IPAddress subnet(255.255.255.0);
Servo ser1,ser2,ser3,ser4;
String inString;
ESP8266WebServer server(80);
void handleRoot() {
  server.send(200, "text/plain", "ESP Robot");
  ser1.detach();ser2.detach();ser3.detach();ser4.detach();
}
void handleRobot() {
  int s1=atoi(server.arg("s1").c_str());
  int s2=atoi(server.arg("s2").c_str());
  int s3=atoi(server.arg("s3").c_str());
  int s4=atoi(server.arg("s4").c_str());
  if (server.arg("m")=="1") {
    digitalWrite(4, HIGH);
  } else {
    digitalWrite(4, LOW);
  }
  server.send(200, "text/plain", "ok");
  ser1.write(s1);ser2.write(s2);ser3.write(s3);ser4.write(s4);
  delay(1500);
}
void setup() {
  pinMode(4, OUTPUT);
  ser1.write(90);ser2.write(90);ser3.write(90);ser4.write(90);
  ser1.attach(13);ser2.attach(12);ser3.attach(14);ser4.attach(16);
  Serial.begin(115200);
  Serial.println("\r\n");
  WiFi.begin(ssid, pass);
  WiFi.config(ip, gateway, subnet);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  server.on("/",handleRoot);
  server.on("/robot", handleRobot);
  server.begin();
  Serial.println("HTTP server started");
}
void loop(void) {
  server.handleClient();
}

The setup() command defines the operating modes for the individual GPIOs and specifies the URLs to which the web server responds. You can see here how easy it is to steer the ESP8266 servomotors.

Use the write() command for the servo object to set the angle for the servo lever. The attach() and detach() functions connect and disconnect the servo from a specific GPIO port. The setup() command is used initially to move all servos into the middle position. From there you can control each servo with the ESP8266.

For the web server, first define the relevant object with the command ESP8266WebServer server (80). In this example, the server will now listen on port 80. You can attach this object to a URL using the on() command and its related handler. This is used to process HTTP requests.

The handleRoot() function acts as a sort of panic button, separating all servo objects from the GPIO ports. If you want to use the robot arm after this, you'll need to do a hard reset.

The handleRobot() function is used to control the arm. It will read the individual parameters for each servo and transfer values to the servo objects. The parameters are s1 to s4 for the servos and m1 for the electromagnet. The delay function gives the servos time to reach the desired position.

To control the whole robot arm, simply open your web browser and go to the URL in Listing 2. The IP address is the same as your network server. The URL below contains a command to set all servos into mid-position and deactivate the magnet.

Listing 2

Initialize Robot Arm

http://192.168.1.21/robot?s1=90&s2=90&s3=90&s4=90&m=0

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content