Control a servo motor over WiFi from your phone to turn on a kettle or other appliances in your home.
BUILD TIME: 90 MINUTES (+3D Printing time)
DIFFICULTY RATING: Intermediate
The brief for this project was to turn a device on or off remotely. The solution needed to be simple, take advantage of the WiFi network, and have an easy to use graphical user interface.
THE BROAD OVERVIEW
The main idea of this project is to control a servo motor remotely using a mobile phone. It will use the mechanical force created by a servo to control a home appliance by mounting the servo against the ON/OFF switch of a chosen device. By moving the servo motor, it will switch the state of that device. A 3D printed design will be created to ensure that the servo is in the right position.
For this project, a commonly available kettle will be used as an example, however, the steps for the kettle can be used for any other device. The only difference will be the way the motor is mounted against the device’s switch and the 3D design.
The circuit consists of the ESP8266 module connected to a servo motor and both are powered using a 4xAA battery bank. A graphical user interface will be created on the mobile phone using a free to use platform called Blynk.
HOW IT WORKS
ESP8266
Thanks to microcontroller modules becoming popular, wireless has become a viable, affordable option to be used in relatively simple projects like this. Many of these utilise Bluetooth or WiFi communication protocols, removing the constraints of wired network connections, which is why we have chosen to use the ESP8266 for this application.
The diagram shown here exemplifies the WiFi communication between devices using this popular ESP8266 microcontroller. We will take advantage of this compact microcontroller and its WiFi communication for our project.
The main board of the project is based on the ESP8266 WiFi module (Module ESP-12E Wemos). It is a self-contained system-on-chip (SOC) with an integrated TCP/IP protocol. The module comes pre-programmed with an AT command set firmware.
The board enables electrical circuits to communicate with each other and/or communicate with other devices that can access the same Wi-Fi network. It is capable of acting as an access point server or as a client that can be connected to other access points, such as home routers.
The module’s on-board processing and storage capability allows it to be integrated with 11 general-purpose input/output (GPIO), and has an analogue-to-digital converter (ADC). Also, the board has an internal voltage regulator that reduces the input voltage to 5V. It is not recommended to use a voltage higher than 7V.
The Wi-Fi antenna is highly sensitive and can be found at the side of the PCB (printed circuit board).The design of the circuit has a high degree of on-chip integration and hence exhibits minimal external circuitry, including the front-end module. The tiny board is designed to occupy a minimal PCB area. Overall, this module is extremely maker-friendly, particularly as it is able to be breadboard-compatible with the addition of a few pins.
ESP8266 Pinout
SERVO MOTOR
The servo motor that we use has just three wires. The brown wire connects to the negative of the battery bank, and the orange wire connects to the positive of the battery bank. The yellow wire is the signal wire that controls the movement of the motor and is to be connected to the ESP8266 board.
Servo specifications:
- 5V nominal power
- 3.6V - 6V operating range
- 500° per second at 6V
- 1.6kg.cm at 4.8V
Servo Pinout
Wire Colour | Description |
---|---|
Brown | Ground (-) |
Orange | Power (+) |
Yellow | Servo Signal |
The Build:
Parts Required: | Jaycar | ||
---|---|---|---|
1 x Wi-Fi Mini ESP8266 Module | XC3802 | ||
1 × SG90 Servo Motor | YM2758 | ||
1 x 4xAA Battery Holders | PH9282 | ||
1 x 2-Way Screw Terminals | HM3130 | ||
4 x AA Batteries* | SB2425 |
* Quantity shown, may be sold in packs. You’ll also need a breadboard and prototyping hardware.
KETTLE CONTROL
For our example, we will trigger a kettle that we purchased from Kmart.
Something to be considered with this application is that the kettle switch returns to the original position automatically once the water boils. This means, we have to consider in our design that the servo does not hold the switch ON to avoid the kettle boiling dry.
The way the circuit will work is that by pressing the virtual button on your mobile phone the servo will rotate to push the kettle switch ON. After 3 seconds, the motor will go back to the original position to allow the kettle switch to return back once the water boils.
We will describe the project in three parts:
ELECTRICAL SYSTEM: The wiring between the different components.
SOFTWARE SYSTEM: The code used to program the ESP8266 module to perform the required tasks.
MECHANICAL SYSTEM: The design of the frame that will hold the servo motor against the kettle’s switch.
ELECTRONICS
If not done so already, solder the header pins onto the ESP8266 modules so that they can fit into the breadboard.
Attach the battery holders to the 2-way screw terminals (before inserting the batteries in them).
Note: To avoid damage to the ESP8266 module, do not insert the batteries during the assembly stage. The modules will be powered after programming (later). This is to avoid connecting the module to the battery and laptop simultaneously.
The wiring is straightforward. In the circuit here, the ESP8266’s positive and negative terminals are connected to the positive and negative rails of the breadboard. The servo’s positive (orange wire) and negative (brown wire) are connected also to the positive and negative rails. The servo’s signal wire (yellow wire) is connected to pin D2 of the ESP8266.
Software System
Blynk is a free to use mobile phone platform that is used to create a graphical user interface to connect your device with the cloud. Follow the following steps to get the application ready on your phone:
Download Blynk application for iOS or Android from the applications store.
Create a new account in the Blynk application.
Create a new project. In our example, we will call the project “Servo Motor Control”.
Choose “ESP8266” as your device.
An Auth Token will be sent to your email. Save it somewhere for now as you will be using it later in your code. It will be used to get the mobile phone to recognise and connect to your ESP8266 board.
The project page will open to start building the interface.
From the side menu, add a button.
Press and hold the button, name it “Pos 1”, and choose the virtual pin “V0”.
Now the interface is ready on your mobile phone.
The next step is to write the code and include all the parameters that will enable the ESP8266 to communicate with your mobile phone.
PROGRAMMING
ESP8266 CONFIGURATION
One of the features of the ESP8266 board is that it can be programmed using Arduino IDE, without the need for an additional Arduino adapter board. The configuration steps are:
Start your Arduino IDE.
Add the ESP8266 to the Board Manger. File menu > Preferences, then add the link http://arduino.esp8266.com/stable/package_esp8266com_index.json to the additional boards manager URLs.
Add the ESP8266 library. Tools menu › Board: › Board Managers, then type ESP8266 and install the latest version of “esp8266 by ESP8266 Community”.
Based on the “ESP8266 Wi-Fi Mini” datasheet, we need to select the “LOLIN(WEMOS) D1 R2 & mini” model type. Go to the Tools menu › Boards › and select LOLIN(WEMOS) D1 R2 & mini.
Next, Tools menu › Upload Speed › 115200
BLYNK CONFIGURATION
We will need to add the library that will allow the ESP8266 to communicate with the Blynk application on your mobile phone.
Go to Sketch menu › Include library › Manage libraries. In the libraries, search for “Blynk”.
If you do not see the Blynk library installed, you will need to follow one of the following two methods to install it.
OPTION 1: Install Blynk library as ZIP file in Arduino IDE
In the same email that Blynk sends you for the Auth Token you will see a link to the latest Blynk library. Download that ZIP file.
Do not unzip the downloaded library. From Sketch menu › Include library › Add .ZIP Library. Then, choose the .ZIP file of the downloaded library.
You should now be able to search and find the Blynk library in the Library Manager. Sketch menu > Include library > Manage libraries
OPTION 2: Install Blynk library manually
You follow exactly the same steps as the previous way until you download the .ZIP file on your computer. After that, instead of leaving the library zipped, unzip the library by extracting the files in a certain destination on your computer. Then, move the library files manually to the Arduino library directory.
You should now be able to search and find the Blynk library in the Library Manager. Sketch menu > Include library > Manage libraries
If all else fails, for more information about setting up the Blynk library, visit: http://help.blynk.cc/en/articles/512105-how-to-install-blynk-library-for-arduino-ide
THE CODE
The code is built to define the task of a virtual button you created on the Blynk application, and to ensure that the ESP8266 board is communicating with your mobile phone through your home WiFi network.
The first part of the code is to add the libraries that are compatible with the ESP8266 board and Blynk application.
//Include Libraries
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h></div>
Create a variable that will be used to control the servo motor through the program.
Servo servo;</div>
Add the “Auth Token” that you received by email, your WiFi username and password.
char auth[] = "<<your auth token>>";
char ssid[] = "WiFi-1C55";
char pass[] = "78203143";
The set up function will include initialising the communication with your network and the Blynk application on your mobile phone.
void setup()
{
Blynk.begin(auth, ssid, pass);
servo.attach(D2);
}
Check the connection between the ESP8266 and the Blynk application.
void loop()
{
Blynk.run();
}
By pressing the pushbutton, the motor will move from 180° to 50°° for 3 seconds and then returns back to the 180° position.
BLYNK_WRITE(V0)
{
servo.write(180);
servo.write(50);
delay(3000);
servo.write(180);
}
UPLOAD THE CODE
Connect the module to the computer through the USB cable.
Tools menu > Port: > COM13.
Note: The port number will change depending on the port the board is connected to. In our case, it is port COM13.
Upload the code to the server ESP8266 board.
Disconnect the board from the computer.
Now we have the code uploaded to the ESP8266 board. The next step is to wire the circuit.
MECHANICAL SYSTEM
The idea behind the mechanical design is to create a holder for the servo motor and the rest of the circuit that can be attached to the base of the kettle.
The motor has to be positioned in a way that the shaft can push the switch of the kettle. Hence, your kettle might be different from the one we used.
Here are the steps we followed for the design:
- The motor was held by hand to check the most effective position to switch the kettle using the shaft of the motor.
- We created a sketch of the holder to have an idea about how it will be positioned and attached to the kettle.
- In the TinkerCad file of our design (available from our website), you will find an accurate model for the motor that you can use to create the motor holder.
- TinkerCad has a model for the 350 Tie-points breadboard that we used to get accurate measurements for the breadboard’s holder.
- Finally, we got the measurements for the base of the kettle to create a grip, so our holder stays attached to the kettle.
TESTING & TROUBLESHOOTING
You should now have all the components ready to go and the servo attached to the kettle (or your alternate appliance) using the 3D print design.
Press on the play button on your Blynk application project page so the application connects with the ESP8266 board. Then press on the virtual button.
The servo should move when you press the button, and return after 3 seconds.
Electrical:
- Make sure that all the wires are connected correctly.
- Make sure that the batteries are not flat.
- To check if the ESP8266 is powered up, press on the reset button on the side of the chip. A blue LED should turn ON briefly.
Programming:
Motor Adjustment
There is a chance that the angles used with the servo in the code need to be different from the one used (in our case, 50° and 180°). This angle reference depends on the way you attach the shaft to the motor. You might need to enter two random angles at the beginning and see how the servo will perform, and then change them according to the position of the servo with the switch of your kettle or the used appliance.
WHERE TO FROM HERE?
A few examples of how this project can be scaled include:
- Use multiple ESP8266 boards to control multiple devices individually.
- Get information about the state of the devices on your mobile phone.
- Increase the complexity of the 3D printed design.
- Control the devices from any place around the world using the internet.