In this tutorial, you will learn how to control a servo motor using the Blynk app on an IoT platform.
Hardware Requirements
Software Requirements
This tutorial is divided into four parts:
Part 1: NodeMCU Initialization with WiFi Name and Password
#include <ESP8266WiFi.h>
char ssid[] = "Your_WIFI_Name";
char pass[] = "Your_Password";
Explanation: This code includes the WiFi library and sets the WiFi name and password for internet connection.
Part 2: Blynk App Setup
#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial
char auth[] = "Your_Auth_Key";
Explanation: This code includes the Blynk library and sets the authentication key received from your email after setting up the Blynk app.
Part 3: Servo Operation
#include <Servo.h>
Servo servo1;
#define servo1Pin D2
void setup() {
servo1.attach(servo1Pin);
}
void loop() {
int PotReading = analogRead(A0);
int servo1Angle = map(PotReading, 0, 1023, 0, 180);
servo1.write(servo1Angle);
delay(15);
}
Explanation: This code includes the Servo library, creates a Servo object named "servo1", and defines the NodeMCU pin to attach the Servo. The map()
function maps the analog reading (0 to 1023) to the servo angle (0 to 180 degrees).
Part 4: Creating a Blynk App to Control the Servo
Steps:
- Open the Blynk app and create a new project.
- Select NodeMCU as your hardware model.
- Add a slider widget to control the servo.
- Set the range of the slider from 0 to 180.
- Link the slider to a virtual pin (e.g., V1).
- Use the following code to update the servo position based on the slider value:
BLYNK_WRITE(V1) {
int servoValue = param.asInt();
servo1.write(servoValue);
}
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
servo1.attach(servo1Pin);
}
void loop() {
Blynk.run();
}
Explanation: This code reads the slider value from the Blynk app and updates the servo position accordingly.
NodeMCU to Servo Connection
Schematic:
- Connect the VCC of the servo to the 3.3V pin on the NodeMCU.
- Connect the GND of the servo to the GND pin on the NodeMCU.
- Connect the signal pin of the servo to D2 on the NodeMCU.
Download Code
Click Here to Download the Code
More Projects
- Introduction to NodeMCU8266 & Steps to Import ESP Board Libraries
- NodeMCU8266 Basic Project - Blink an LED
- NodeMCU8266 Digital Read (Push Button)
- Analog Data Read Using NodeMCU8266
- NodeMCU LED Control Using Blynk App in IoT Platform
- NodeMCU to DHT Interface in Blynk App | On IoT Platform
- The Best Way to NodeMCU GPS Tracker Blynk App | in IoT Platform
- NodeMCU RGB LED | For Beginners
This tutorial provides a step-by-step guide to control a servo motor using the Blynk app on an IoT platform, making it easy to integrate hardware and software for your IoT projects.
No comments