Step 2: Hardware Description
What is a Breadboard?
Its a prototyping platform, where you can plug components and remove them easily. Please refer to the photo to see how its made inside. There is usually 2 bands on each sides indicating the power rails. Its made to easily connect all of the (-) and (+) together.
What is a Servo?
Servo motors are great devices that can turn to a specified angle or called position.
Usually, they have a servo arm that can turn 180 degrees. Using the NodeMCU, we can control a servo to go to a specified position. As simple as that! Here we will see how to connect a servo motor and then how to turn it to different positions.
Connection to Servo
The next job is to connect your servo motor. There are two common types of servo:
- White – Red – Black wired servo
- Orange – Red – Brown wired servo
If your servo has White – Red – Black wires, then connect it as follows
- White wire connects to Digital pin D4
- Black wire connects to GND pin
- Red wire connects to 3V3 pin
If your servo has Orange – Red – Brown wires, then connect it as follows
- Orange wire connects to Digital pin D4.
- Brown wire connects to GND pin
- Red wire connects to 3V3 pin
Step 4: Preparing the Arduino IDE
After downloading the Arduino IDE navigate to
- File tab and then click on Preferences.
- In the additional Boards Manager URLs add the following link (http://arduino.esp8266.com/stable/package_esp8266com_index.json)
- Click OK and then navigate to
- Tools – Boards – Boards Manager
In the search field type esp8266 > click the esp8266 by ESP8266 Community – Click Install
Now you have setup the Arduino IDE to work along with the NodeMCU.
Bài viết liên quan
- Hướng dẫn cài đặt ESP8266 và kết nối với Blynk
- LED RGB sử dụng NodeMCU ESP8266 thay đổi màu sắc trên App Blynk
- Giám sát nhiệt độ, độ ẩm bằng App Blynk sử dụng NodeMCU ESP8266
- Cảnh báo trộm bằng cảm biến PIR (HC-SR501) thông qua App Blynk sử dụng ESP8266
- Xác định khoảng cách khi đỗ xe bằng cảm biến HC-SRF04 sử dụng ESP8266 và App Blynk
- Điều khiển động cơ Servo SG90 bằng App Blynk sử dụng NodeMCU ESP8266
Step 3: Download Arduino IDE
To get started we need to download the Arduino IDE (Integrated Development Environment) and some necessary drivers.
- To download the software head over to the Arduino site: https://www.arduino.cc
- Click on Software Click on either Windows, Mac or Linux based on your Operating System.
- You can donate if you want or just download.
- When this is done, you will simply need to continue the steps to download it to your computer.
- You are done!
Code
#define SW_VERSION “ServoCtrlBlynk_V.1” /*NodeMCU */ #include
char ssid [] = “Phamson”; char pass [] = “phamtheson”; /* Blynk */ #include
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space char auth [] = “6zCB6XaSV9PflOIBv7h1uOF7pgUV99t9”; // Servo Control Project // Servo Control Project /* OLED */ #include “SSD1306Wire.h” #include “Wire.h” const int I2C_DISPLAY_ADDRESS = 0x3c; const int SDA_PIN = 0; const int SCL_PIN = 2; SSD1306Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SCL_PIN); /* Servo */ #define servo1Pin D2 #include
Servo servo1; /* Initial pot reading and servo position set the position to neutral */ #define potPin A0 int potReading = 1023/2; int servo1Angle = 90; /* Reads slider in the Blynk app and writes the value to “potReading” variable */ BLYNK_WRITE(V0) { potReading = param.asInt(); } /* Display servo position on Blynk app */ BLYNK_READ(V1) { Blynk.virtualWrite(V1, servo1Angle); } void setup () { Serial.begin(115200); servo1.attach(servo1Pin); displaySetup(); Blynk.begin(auth, ssid, pass); } void loop() { Blynk.run(); //PotReading = analogRead(potPin); // Read Analog data from potenciometer not used here servo1Angle = map(potReading, 0, 1023, 0, 180); // Map the pot reading to an angle from 0 to 180 servo1.write(servo1Angle); // Move the servo to a position displayAngle(); delay (200); } /* Initiate and display setup data on OLED */ void displaySetup() { display.init(); // initialize display display.clear(); // Clear display display.display(); // Put data on display } /* Display Servo position */ void displayAngle() { display.clear(); display.setFont(ArialMT_Plain_16); display.drawString(10, 0, “Servo Control”); display.drawString(0, 45, “POSITION:” ); display.setFont(ArialMT_Plain_24); display.drawString(80, 40, String(servo1Angle)); display.display(); }
Giải thích code
Khai báo Tài khoản và Mật khẩu Wifi.
char ssid [] = “Phamson”; char pass [] = “phamtheson”;
Mã Token được cấp khi tạo mới Project trên Blynk.
char auth [] = “6zCB6XaSV9PflOIBv7h1uOF7pgUV99t9”;
Đọc giá trị thanh trượt trên App Blynk và ghi giá trị vào biến “potReading”.
BLYNK_WRITE(V0) { potReading = param.asInt(); }
Hiển thị giá trị Servo trên App Blynk.
BLYNK_READ(V1) { Blynk.virtualWrite(V1, servo1Angle); }
Thư viện
Introduction
Hey geeks, hope you are doing fine. Are you familiar with the working of a servo motor?
If not then this article is for you in which we are going to teach you how to Control servo motor with esp8266 using a nodemcu IoT development board.
Servo motor is different from an ordinary DC motor as it does not rotate continuously. You can also read our article on servo motor in which we explain the features and specifications of a servo motor.
It has an extra wire for the signal which is used to rotate the shaft in a specific direction. Also, do check out more such amazing projects on IoT and Arduino.
The detailed circuit diagram and code for the project are given below. The servo motor can also work well with the other microcontroller boards like Arduino and Raspberry pi, we have our articles on them also.
in this ESP8266 with servo motor tutorial we are going to explain how to work.
Step 6: Upload Your Program
- Goto Tools
- Board > NodeMCU 1.0 (ESP – 12E Module)
- Port ( Choose the right Port )
**Make sure you have your NodeMCU model selected and the correct serial port ticked (see pics).
Then just click the Upload button**
Ready To GO
Participated in the Makerspace Contest 2017
In this tutorial, we will learn how to control a small servo-motor. If you want to control the position of a mechanism, just have to use this tutorial. Good for beginners who want to make stuff move without building a motor controller with feedback & gearbox, especially since it will fit in small places
Parts Required
To build the global setup you must have the following parts:
- ESP8266 NodeMCU v1.0;
- Micro-USB to USB cable;
- Servo sg90;
- Male to male jumper wires;
- 1 Breadboard;
Note: You do not need to have a NodeMCU board nor an ESP8266 to follow this tutorial. I am using this board because it is very famous and also because, I will post future tutorials using wifi communications. In order to use other boards, you just have to change the pin number.
Servo SG90
This SG90 is a very good quality motor widely used in RC aeromodelling projects, Arduino, Raspberry etc. Is tiny and lightweight, but with high output power. A Servo can rotate approximately 180 degrees (90 in each direction) and works just like another Servo.
Where we have the servo especifications:
- Vcc voltage: 3,0 – 7,2V;
- Degree of freedom/rotation: 180 graus;
- Speed: 0,12 seg/60º without load;
- Torque: 1,2 kg.cm (4,8V) and 1,6 kg.cm (6,0V);
- Temperature: -30C ~ +60C;
- Dimentions: 32 x 30 x 12mm;
- Weight: 9g.
The servo is very simple to operate, but it is not just plugged and play. In figure 2, we have the servo-motor pinout. The red one is for Vcc, the black/brown is for Gnd and the yellow/orange is for the signal that controls the position.
The signal that we send to the servo is called a PPM signal is one of the radio receiver protocols used in RC Devices to transfer data. It is a signal similar to PWM, but with some differences. In figure 3 we can see an example of a PPM signal used by our servo motor and the times required to build the signal.
To generate this signal, we need to choose a pin in our NodeMcu, that support PWM. Despite the fact, that this signal is not a PWM, the PPM signal uses the same timers that are present in PWM pins, like in figure 4.
Circuit
Connect the signal cable (Yellow) to GPIO pin 2 (D4) because it is a PWM pin, Vcc (Red) to 3.3V and GND (Black), as figure 5. Everything will be powered from your PC using micro-USB to USB cable. In this configuration, the servo will be powered directly from the GPIO 2 pin with 3.3V. It is low voltage level but it works. All the current for the will be drawn from the board, and since each GPIO delivers only no more than a few mA, the torque can be low. Let’s try it.
Coding
Copy the main sketch below to your Arduino IDE project and save it. The Servo.h library is already included in the Arduino IDE. Have fun!!
#include < Servo.h> Servo servo; int val; void setup() { Serial.begin(115200); servo.attach(2); // attaches the servo on pin D4 to the servo object servo.write(0); } void loop() { // main program loop servo.write(90); delay(1000); for(val = 0; val <= 180; val++){ servo.write(180); // sets the servo position according to the scaled value Serial.print("Angle:"); Serial.println(val); delay(1000); // waits for the servo to get there } for(val = 180; val >= 0; val--){ servo.write(0); // sets the servo position according to the scaled value Serial.print("Angle:"); Serial.println(val); delay(1000); // waits for the servo to get there } }
In this tutorial ESP8266 we will see How to control Servo motor using NodeMCU web server? where the position of the Servo Motor can be controlled through a Web Page (within the same WiFi Network).
Introduction
Servo Motors are one of the most commonly used motors in both industries and DIY Projects.
I’ll be creating a simple Web Page (HTML) with a circular knob. When this web page is accessed from a laptop or mobile phone that is connected to the same WiFi Network as the ESP8266, you can control the position of the Servo Motor by adjusting the knob.
Web Controlled Servo using ESP8266
Normally, in order to control a Servo Motor with Arduino (for example), all you need is a Servo Motor, Arduino and a Potentiometer. Depending on the position of the POT, the PWM value of the Servo Motor input changes and consequently the position of the Servo Motor’s shaft changes.
In case of a Web Controlled Servo Motor using ESP8266, ESP8266 drives the Servo Motor but the input isn’t from a POT but a Web Page we created with AJAX.
The slider in the web page sends the angle values and is received by the NodeMCU, which acts as a Web Server. The NodeMCU, upon receiving the value, transmits corresponding PWM to servo, which then changes the position of the Servo Motor according to the value.
One important thing to remember here is that both the Server and client should be on the same network i.e. the ESP8266, which acts as the Server, and the Laptop (or a Mobile Phone), which is the client, must be connected to the same WiFi network.
Circuit Diagram
The circuit diagram for Web Controlled Servo using ESP8266 WiFi Module is given in the image below.
Components Required
- ESP8266 based Board (NodeMCU)
- Servo Motor
- Jumper Wires
- Laptop
Working of the Circuit
The main program responsible for everything i.e. connecting ESP8266 to WiFi, getting information from Web and finally controlling the Servo.
Pin D5 of NodeMCU will provide the necessary PWM signal to the Servo (Orange). Its other pins are power supply pins and are connected to 5V and GND (Red and Brown).
ESP8266 Servo Arduino Code
The Arduino IDE Code for the Web Controlled Servo using ESP8266 tutorial is given below.
Program is divided in tow parts Web Page (index.h) and Main Hardware Code ESPservo.ino
ESPservo.ino
before uploading code make changes in SSID and Password as per your wifi
/* * ESP8266 Servo Motor Control With Web Server * https://circuits4you.com */ #include
#include
#include
#include “index.h”; #define LED 2 #define ServoPin 14 //D5 is GPIO14 //WiFi Connection configuration const char *ssid = “your-ssid”; const char *password = “your-password”; Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards ESP8266WebServer server(80); //================================================ void handleServo(){ String POS = server.arg(“servoPOS”); int pos = POS.toInt(); myservo.write(pos); // tell servo to go to position delay(15); Serial.print(“Servo Angle:”); Serial.println(pos); digitalWrite(LED,!(digitalRead(LED))); //Toggle LED server.send(200, “text/plane”,””); } void handleRoot() { String s = MAIN_page; //Read HTML contents server.send(200, “text/html”, s); //Send web page } //================================================ // Setup //================================================ void setup() { delay(1000); Serial.begin(115200); Serial.println(); pinMode(LED,OUTPUT); myservo.attach(ServoPin); // attaches the servo on GIO2 to the servo object //Connect to wifi Network WiFi.begin(ssid, password); //Connect to your WiFi router Serial.println(“”); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(“.”); } //If connection successful show IP address in serial monitor Serial.println(“”); Serial.print(“Connected to “); Serial.println(ssid); Serial.print(“IP address: “); Serial.println(WiFi.localIP()); //IP address assigned to your ESP //Initialize Webserver server.on(“/”,handleRoot); server.on(“/setPOS”,handleServo); //Sets servo position from Web request server.begin(); } //================================================ // LOOP //================================================ void loop() { server.handleClient(); } //================================================
index.h
Press Ctrl+Shift+N give name index.h and copy paste below code.
const char MAIN_page[] PROGMEM = R”=====(
ESP8266 Servo | Circuits4you.com
Circuits4you.com
ESP8266 Servo Motor Control Demo
)=====";
Working of Web Controlled Servo using ESP8266
Let us now see the working of the Web Controlled Servo using ESP8266. After making all the connections as per the circuit diagram, upload the NodeMCU code.
After the code is uploaded, if you open the serial monitor, you can see the status of the ESP8266 WiFi Module. After all the initialization steps are done (set mode to Station mode, connect ESP8266 to WiFi and start web server), you can proceed for Web Control.
Now Get IP address of ESP8266 from serial monitor and open it using any web browser. If everything is right, when you change the position of the slider, the position of the servo Motor will be changed.
Applications
The idea behind Web Controlled Servo using ESP8266 is to implement a web-controlled device (in this case, a Servo Motor) i.e. to control a motor through internet.
This application can be further extended to more advanced and complex projects like controlling a Robot from the Internet.
|
|
In this project we use ESP8266 in its NodeMCU form factor as a webserver and to create a webform to control the current position of the servo motor.
All the data of the webserver are saved on the SPIFFS memory of ESP8266.
What is SPIFFS?
SPIFFS is the original filesystem and is ideal for space and RAM constrained applications that utilize many small files and care about static and dynamic wear levelling and don’t need true directory support. Filesystem overhead on the flash is minimal as well. On the NodeMCU we have 4MB of flash out of which we can the size of the SPIFFS filesystem. In this project all the webserver files including html, css, and javascript are saved on a folder named data which is the folder name required for uploading to ESP8266 with SPIFFS uploader.
I have a detailed tutorial about it in another project. Check it out.
Hardware Setup
- Start by placing the NodeMCU on a breadboard.
- Place the servo motor and make the connections as shown.
- Connections.
3V3V ➡ VCC
GND ➡ GND
SIGNAL ➡ D2
Software Setup
- Visit my GitHub page and download the necessary files.
- Download ESPAsyncWebServer library
- Download ESPAsyncTCP library
- Install libraries.
- Download ESP8266 Filesystem Uploader
- Install the libraries and Filesystem uploader as shown below.
- Compile the code and upload.
- Go to tools and use ESP8266 Sketch Data Upload tool to upload sketch data.
Changetheselinesofcodeasperyours:
GitHub Repository link
const char *ssid = "REPLACE_WITH_YOUR_SSID"; const char *password = "REPLACE_WITH_YOUR_PASSWORD"; const uint8_t servoPin = D4;
https://github.com/rahuladitya303/ESP8266_SERVO_CONTROLLER.git
For more details about how to install libraries and filesystem uploader checkout my other project
https://create.arduino.cc/projecthub/rahuladitya303/esp8266-weather-station-with-spiffs-c01037
In this tutorial, we will learn how to use a Servo Motor.
A servomotor is a rotary actuator or linear actuator that allows for precise control of angular or linear position, velocity and acceleration.[1] It consists of a suitable motor coupled to a sensor for position feedback. It also requires a relatively sophisticated controller, often a dedicated module designed specifically for use with servomotors.
Servomotors are not a specific class of motor although the term servomotor is often used to refer to a motor suitable for use in a closed-loop control system.
Introduction: Interfacing Servo Motor With NodeMCU
HelloEverybody,
This here is my first Instructable project.
So you want to get started with NodeMCU? Well, I'm here to share with you.Today, I'm going to show you how to get started with NodeMCU. Let's GO !
NodeMCU has on board ESP8266-12E makes the board suitable for IoT (Internet Of Things). In this Instructable I will show you how to get started with Servo using NodeMCU.
Connection to Servo
The next job is to connect your servo motor. There are two common types of servo:
- White – Red – Black wired servo
- Orange – Red – Brown wired servo
If your servo has White – Red – Black wires, then connect it as follows
- White wire connects to Digital pin D4
- Black wire connects to GND pin
- Red wire connects to 3V3 pin
If your servo has Orange – Red – Brown wires, then connect it as follows
- Orange wire connects to Digital pin D4.
- Brown wire connects to GND pin
- Red wire connects to 3V3 pin
nodemcu with servo motor Circuit Diagram
Connection Diagram
Nodemcu esp8266 | Servo Motor |
VV, Vin | VCC |
G, GND | GND |
D3 Pin | Signal |
- The servo motor has three wires,
- two for the power and one for the control signal.
- You have to connect these three wires with the pins of the Nodemcu.
- Connect the red wire with the 3.3 volts pin of the nodemcu.
- Join the brown wire of the servo motor with the GND pin of the nodemcu.
- Connect the signal wire (orange wire) of the servo motor with the digital-3 pin of the nodemcu.
- After completing the connections use a USB cable to power the nodemcu so that the servo motor starts moving.
PCBWay PCB Prototyping Services
I have assembled the whole circuit on a breadboard. As you know breadboard assembly is not effective for this type of projects. So, PCBWay offers Rapid PCB Prototyping for Your Research Work. I personally, recommend PCBWay because you can get your first-try boards right in 24 hours!
The prototyping stage is the most critical period of time for engineers, students, and hobbyists. PCBWay not only makes your boards quick but also makes your job right as well as cost-effective. This greatly reduces your cost and shortens the time for developing your electronic products.
PCBWay can provide 2 Layer PCBs to highly advanced HDI and flex boards. Even though the PCBs they produce differ a lot regarding functionality and areas of use. I am impressed with the quality of the boards, the delivery time, and the cost-effectiveness.
Mechanism
A servomotor is a closed-loop servomechanism that uses position feedback to control its motion and final position. The input to its control is a signal (either analogue or digital) representing the position commanded for the output shaft.
The motor is paired with some type of encoder to provide position and speed feedback. In the simplest case, only the position is measured. The measured position of the output is compared to the command position, the external input to the controller. If the output position differs from that required, an error signal is generated which then causes the motor to rotate in either direction, as needed to bring the output shaft to the appropriate position. As the positions approach, the error signal reduces to zero and the motor stops.
Programming
This is the time to setup Arduino with NodeMCU and Servo Motor.Download the code from this Github link. Once downloaded, open it up in the Arduino IDE. Then Create a new sketch and hit Upload.
Program working#include
// Include the Servo library
Servo servo;
void setup() {
servo.attach(2); // We need to attach the servo to the used pin number
servo.write(0);
delay(2000);
void loop() {
servo.write(0); // Make servo go to 0 degrees
delay(1000);
servo.write(90); // Make servo go to 90 degrees
delay(1000);
servo.write(180); // Make servo go to 180 degrees
delay(1000);
Once the code is uploaded, the motor starts to function as shown in below diagram.
ESP8266 with servo motor Code
NOTE: Please upload the code which is given below to the nodemcu. You may need to install
library to the IDE software. Check here how to add a zip library to the Arduino IDE.
//TECHATRONIC.COM #include
// servo library Servo s1; void setup() { s1.attach(0); // servo attach D3 pin of arduino } void loop() { s1.write(0); delay(1000); s1.write(90); delay(1000); s1.write(180); delay(1000); }
We hope that you understand this project and please try to make it on your own. If you have any doubts related to this project then ask them in the comments section below. Also, check out our tutorials on Arduino and Raspberry Pi.
Video sample
Điều khiển Servo thông qua NodeMCU ESP8266 và Blynk hiển thị lên OLED 0.96
Trong nội dung bài viết hôm nay mình sẽ hướng dẫn các bạn cách làm thể nào để điều khiển Servo thông qua NodeMCU ESP8266 và App Blynk hiển thị lên OLED 0.96. Để có thể hiểu rõ hơn về bài viết hôm nay các bạn xem lại bài viết bên dưới nhé.
Step 5: Coding Time
The next step is to write some code to control the Servo.
Download the "Servo.ino" file and open it up in the Arduino IDE. Then Create a new sketch and paste the code below in the arduino IDE and hit Upload.
#include
Servo servo;
void setup() {
servo.attach(2); //D4
servo.write(0);
delay(2000);
void loop() {
servo.write(90);
delay(1000);
servo.write(0);
delay(1000);
The code will take a few minutes to upload and then you should see the Servo changing angle from 0° to 90° at the interval set in the code.
You can tinker with it if you like, or just use it as it is.
Components Required
- ESP8266 nodemcu board
- Servo motor
- Jumper wires
- USB cable for uploading the code
- Breadboard
Components Table/buy link
You can buy the whole components from the given link we have to share the amazon link. from where you can get a good discount on the components for this esp8266 tutorial.
Sơ đồ nguyên lý
Các linh kiện cần thiết cho dự án
Tên linh kiện | Số lượng | Shopee |
NodeMCU ESP8266 | Mua ngay | |
Dây cắm (Đực – Đực) | Mua ngay | |
Dây cắm (Đực – Cái) | Mua ngay | |
OLED 0.96 | Mua ngay | |
Servo SG90 | Mua ngay | |
Biến trở 100k | Mua ngay | |
Breadboard | Mua ngay | |
Board nguồn (3.3V – 5V) | Mua ngay |
ESP8266 with servo motor Working
The main advantage of using a servo motor is that we can rotate it in any desired direction as per our needs.
A servo motor can rotate in very small as well as very large angles that are specified by us in the code.
- when we make this nodemcu with servo motor We use to attach a function to specify the pin of the nodemcu on which the signal pin of the servo motor is connected.
- The servo motor first comes to the zero position of the shaft and then after a delay of a second,
- it will move the shaft to the middle position that is ninety degrees. A
- gain after a delay of a second, the shaft will move to the position of the one-eighty degree.
- We use the write function to move the shaft of the servo motor.
- You can modify the values of the position according to you in the code.
You can also check the Robotic arm using servo motors made by us.
Keywords searched by users: esp8266 servo motor control
Categories: Chi tiết 41 Esp8266 Servo Motor Control
See more here: kientrucannam.vn
See more: https://kientrucannam.vn/vn/