top of page
Writer's pictureRamesh G

Pan Tilt Servo Control Using NodeMCU

Updated: Jul 2, 2021

This tutorial, we will explore how to control Pan & Tilt servos using ESP8266, myDevices Cayenne, and MQTT, Our goal will be a PAN/TILT mechanism to position a camera (ESP32 Camera).


Pan and tilt systems used in today's robotics applications for security, industrial, and military applications have to move faster, smoother, and more accurately than ever before. At the same time, these systems need to be cost effective, easy to program.

On the other hand, Cayenne is an IoT cloud platform that provides several cloud services, such as:

  • Data visualization

  • IoT cloud

  • Alerts

  • Scheduling Events

We will focus our attention on data visualization and on the IoT cloud services.


Refer my previous post for Servo control: https://www.dofbot.com/post/cayenne-iot-based-servo-control


Circuit Diagram



Components Required

ESP8266 12E

Servo Motor SG90 or MG90S

Pan tilt Servo Bracket

Servo Tetser


Pan tilt Servo Bracket Assembly

In this assembly some Cut to be done in the servo knob before assembly, see the demo video for detail.


Pan tilt Servo Bracket Test

After Assembly we need to check the servo using the servo tester.


see the demo video for Servo testing detail.


Demo Video for servo Assembly and Test




Connecting the Servo Motor to the ESP8266

Servo motors have three wires: power, ground, and signal.

Servo Wire Color

Power-Red

GND-Black, or brown

Signal-Yellow, orange, or white

When using a small servo like the SG90 as shown in the figure below, you can power it directly from the ESP8266.

If you’re using a small servo like the SG90, you need to connect:

  • GND -> ESP8266 GND pin;

  • Power -> ESP8266 Vin pin;

  • Signal -> D1 and D2 (or any PWM pin).

Installing the ESP8266_Arduino_Library

The ESP32 Arduino Servo Library makes it easier to control a servo motor with your ESP8266, using the Arduino IDE. Follow the next steps to install the library in your Arduino IDE:


After Download the ESP32_Arduino_Servo_Library. You should have a .zip folder in your Downloads folder, Unzip the .zip folder and you should get ESP32-Arduino-Servo-Library.


Download the Cayenne-MQTT-ESP-master library from this link.

Click on Add ZIP Library and add Cayenne-MQTT-ESP-master zip file, or directly copy the folder (Cayenne-MQTT-ESP-master) and paste it in Libraries folder of Arduino IDE.


After installing the library, go to your Arduino IDE. Make sure you have the Nodemcu 1.0 ESP-12E board selected, and then, Copy and Paste code in Arduino IDE.

Subscribe and Download code.

Subscribe and Download code.

Arduino code

#include <Servo.h>


#include <CayenneMQTTESP8266.h>

#include <ESP8266WiFi.h>


char ssid[] = "TP-Link_3200";


char password[]="9500112137";


char username[] = "2031dd30-5414-11eb-b767-3f1a8f1211ba";

char mqtt_password[] = "f2ce829d98df3a768328ac6936eae9fd47d28289";

char client_id[] = "fbaaa920-6565-11eb-8779-7d56e82df461";


Servo myservo1;

Servo myservo2;



void setup()

{

myservo1.attach(D1);

myservo2.attach(D2);


Cayenne.begin(username,mqtt_password,client_id,ssid,password);

}


void loop()

{

Cayenne.loop();

}


CAYENNE_IN(1)

{

int position = getValue.asDouble() * 180;

myservo1.write(getValue.asInt());

}

CAYENNE_IN(2)

{

int position = getValue.asDouble() * 180;

myservo2.write(getValue.asInt());

}


Hardware interfacing with Cayenne IoT platform


Click on Add new and then Device/Widget in Settings, Add New Device here and select Slider for Controlling Servo 0 to 180 angle by step 1 angle to desired step value.


Configure device Generic ESP8266, MQTT username, password and client ID from Create App

Paste these respective details under username, password and client ID in Arduino source code , along with your Wi-Fi name and password.

After successfully compiling and uploading the code to NodeMCU, You will see ESP8266 connected to Wi-Fi. After the connection is established, the previous page is automatically updated on Cayenne. A new dashboard opens in the browser. Cayenne generates an ID and a device icon for your device.

Click on Custom Widgets and then value, and populate all fields .


912 views0 comments

Comments


bottom of page