The Program to Control the Servo Motor Using Blynk

Integrating Blynk with an ESP32 to control a servo motor involves writing a program that establishes communication between the ESP32 microcontroller and the Blynk IoT platform. The program typically includes configuring Wi-Fi credentials, specifying the Blynk authentication token, defining the hardware pin to which the servo motor is connected, and implementing Blynk-specific functions to receive commands from Blynk. Additionally, the program will include logic to interpret commands received from Blynk and adjust the position of the servo motor accordingly. 

#define BLYNK_TEMPLATE_ID “TMPL39DMPQeEs” 

#define BLYNK_TEMPLATE_NAME “servo” 

#define BLYNK_AUTH_TOKEN “BY19gLSIrBMDF4Q17br9DfDnHak8quqX” 

 

#include <WiFi.h> 

#include <WiFiClient.h> 

#include <BlynkSimpleEsp32.h> 

#include <ESP32Servo.h> 

 

char auth[] = “YourAuthToken”; // Enter your Blynk auth token here 

char ssid[] = “YourWiFiSSID”; // Enter your WiFi SSID here 

char pass[] = “YourWiFiPassword”; // Enter your WiFi password here 

 

Servo myservo; 

 

void setup() 

 Serial.begin(115200); 

 Blynk.begin(auth, ssid, pass); 

 

 myservo.attach(2); // Set the servo signal pin, change the pin number if needed 

 

void loop() 

 Blynk.run(); 

 

// Widget to control servo position in Blynk app 

BLYNK_WRITE(V1) 

 int angle = param.asInt(); // Get the angle value from the Blynk app (0 to 180) 

 myservo.write(angle); // Set the servo position based on the received angle 

Explanation of the code 

  1. Blynk Template Configuration:
  • BLYNK_TEMPLATE_ID: Identifies the Blynk template being used.
  • BLYNK_TEMPLATE_NAME: Specifies the name of the Blynk template.
  • BLYNK_AUTH_TOKEN: Authentication token provided by Blynk for accessing the project.

2. Library Inclusions:

  • WiFi.h: Library for connecting to Wi-Fi networks.
  • WiFiClient.h: Library for creating a Wi-Fi client.
  • BlynkSimpleEsp32.h: Library for connecting ESP32 to the Blynk server.
  • ESP32Servo.h: Library for controlling a servo motor with an ESP32.

3. Global Variables:

  • auth: Authentication token for Blynk.
  • ssid: SSID of the Wi-Fi network.
  • pass: Password for the Wi-Fi network.

4. Servo Configuration:

  • Servo myservo: Creates a Servo object named myservo.
  • myservo.attach(2): Attaches the servo to pin 2 of the ESP32. Change the pin number if the servo is connected to a different pin.

5. Setup Function:

  • Serial.begin(115200): Initializes serial communication with a baud rate of 115200.
  • Blynk.begin(auth, ssid, pass): Initializes the Blynk connection with the provided authentication token and Wi-Fi credentials.

6. Loop Function:

  • Blynk.run(): Keeps the Blynk connection alive by handling incoming commands and maintaining communication with the Blynk server.

7. Blynk Widget Function:

  • BLYNK_WRITE(V1): Defines a Blynk function that is triggered when the value of a virtual pin (V1) is updated in the Blynk app.int angle = param.asInt(): Retrieves the angle value (0 to 180) sent from the Blynk app.
  • myservo.write(angle): Sets the position of the servo motor based on the received angle value.

Leave a comment

Your email address will not be published. Required fields are marked *