Add to playlist !
Buy eBook " Bookmark # Code Files $ %
Controlling the camera gimbal using
ESP8266
In this section, we will make a custom gimbal with two-servo motors that can be controlled remotely. You need to
buy a Servo bracket to install the servos. You can buy a cheap one like
this: https://www.aliexpress.com/item/Servo-bracket-PT-Pan-Tilt-Camera-Platform-Anti-Vibration-Camera-
Mount-for-Aircraft-FPV-dedicated-nylon/32697306736.html (https://www.aliexpress.com/item/Servo-bracket-
PT-Pan-Tilt-Camera-Platform-Anti-Vibration-Camera-Mount-for-Aircraft-FPV-dedicated-
nylon/32697306736.html). You will need the following things:
' ESP8266 or NodeMCU
' Two-servo motors (SG90 mini gear micro servo preferred)
' 5V power supply
' Smart phone
' Router (for Wi-Fi connection)
Let's see how this is done:
1 Firstly, attach the servos to the bracket. Connect the servos to the ESP8266, as follows:
2 From the diagram, you can see that the servos are connected to the GPIO pins of the ESP8266. GPIO 0 and GPIO 2
or D3 and D4, respectively. Now you need to write code for our ESP8266 but, before that, open your Blynk app and
create a New Project :
3 Give your project a name and from Choose Device , select NodeMCU or ESP8266 , whichever you use for your
gimbal. Hit Create Project . Remember the authentication code of the project. The code will also be sent to your
email.
4 Next, swipe right to get the Widget Box and choose Slider :
5 Give your slider a name. I gave Servo 1 and Servo 2 for the two sliders we need:
6 Select a Virtual pin for each of the sliders. I chose V1 and V6 :
7 Your final look of the project will look like the following screenshot. You may change the color from the slider
properties:
Let's begin our coding to control the gimbal:
1 Fire up your Arduino IDE and add the following lines at the top of the sketch:
Copy
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
#include <ESP8266WiFi.h>
2 You need to install the ESP8266 libraries to do that.
3 Now, declare the authentication code you got for your project and the Wi-Fi name and password for the connection,
as follows:
Copy
char auth[] = "**********";
char ssid[] = "**********";
char pass[] = "**********";
4 Then, declare our servos:
Copy
Servo s1, s2;
5 Inside the void setup() , we need to start the Blynk and then attach the servo pins:
Copy
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
s1.attach(0);
s2.attach(2);
6 Then, assign the virtual pins of the Blynk, as follows:
Copy
BLYNK_WRITE(V1)
{
s1.write(param.asInt());
}
BLYNK_WRITE(V6)
{
s2.write(param.asInt());
}
7 Then in the void loop() function, run the Blynk:
Copy
Blynk.run();
The full source code can be found here: https://github.com/SOFTowaha/Selfie-Drone/blob/master/blynkCode.ino
(https://github.com/SOFTowaha/Selfie-Drone/blob/master/blynkCode.ino).
8 Now, verify and upload the code after connecting the NodeMCU to the computer.
9 From your Blynk application, run the project. You will see your sliders, as follows:
( BACK
Building Smart Drones with ESP8266 and
Arduino
+ Search this title...
Contents , Bookmarks (0)
Things to Know Before You Build a Drone )
(/BOOK/HARDWARE_AND_CREATIVE/97817884…
Assembling Your Drone )
(/BOOK/HARDWARE_AND_CREATIVE/97817884…
10 Now, if you move your sliders from left to right, you will see your servos will move 0 - 180 degrees.
You may get some resonances. So, I would suggest you use an LM1117 voltage regulator between the NodeMCU
and the battery or the power supply. Now, I have some home work for you. I want you to make a switch from the
Blynk and trigger the camera you will use in the drone using Blynk application.
( Previous Section (/book/hardware_and_creative/9781788477512/6/ch06lvl1sec45/flying-and-taking-shots)
Next Section - (/book/hardware_and_creative/9781788477512/6/ch06lvl1sec47/summary)