Controlling servo motor sg 90 with Blynk

// I want to control sg90 servo motor using esp8266 with blynk

#define BLYNK_TEMPLATE_ID ""

#define BLYNK_TEMPLATE_NAME ""

#define BLYNK_AUTH_TOKEN ""

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

#include <Servo.h>

char auth[] = "";

char ssid[] = "";

char pass[] = "";

Servo servo;

void setup() {

Serial.begin(9600);

Serial.println("Connecting to WiFi...");

Blynk.begin(auth, ssid, pass);

servo.attach(D4); // Attach servo to pin D4

}

void loop() {

Blynk.run();

}

BLYNK_WRITE(V1) { // Virtual pin to control servo

int angle = param.asInt(); // Get the angle from Blynk app

Serial.print("Received angle: ");

Serial.println(angle);

servo.write(angle); // Move servo to the specified angle

}

// I uploaded above code and it connected to wifi, also on serial monitor correct angle is displaying but servo motor is not working ,it is not even making slight move.
Guys i will appreciate if you help.

@sk111 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Thanks for helping to post question properly. But can you help me with this problem with my project??

How is your servo connected to your board, and how is it powered?

Pete.

I connected pwm signal pin to d4 and power and ground Pins to 3v3 and Gnd pins of esp8266. And I have not provided external power supply to servo motor it drawing power from esp8266 itself .Esp8266 is powered using usb cabel connected to pc.

Have you tried using a non-Blynk sketch to prove the operation of the servo with this wiring setup? Maybe an example sketch from the servo library?

Pete.

yes servo is working ,I tested with example from servo library. I have written code in such way that from blynk app i have used slider as input and angle varied will be visible on serial monitor of arduino ide . Angle which i varied using slider is exactly same as shown in serial monitor.But still servo is not working ,sometimes is suddenly works but only for very short period not even to observe, which means input is working but not servo.

Sounds like a power supply issue to me, the ESP8266 consumes much more power when it’s using WiFi.

Pete.

Should i use 12 v power supply to power esp8266 or use voltage regulator??

You need to u8nderstand more about how a typical ESP8266 based dev board - such as a NodeMCU or Wemos D1 Mini - works from a powerpoint of view.

The ESP8266 chip itself required 3.3v to operate.

The dev board can be powered by 3.3v (via the 3v3 pin) or 5v (via the USB or 5v/VIN pin).
When you power it with 5v the onboard voltage regulator drops the supply voltage down to 3.3v so the chip isn’t fried. As well as powering the ESP8266 chip, the onboard voltage regulator also makes 3.3v available on the 3v3 pin. The supply voltage of 5v is also made available on the 5v or VIN pin, to power peripherals.
Although the 3v3 pin is primarily there to allow you to power the board with a 3.3v input, the power that’s made available on the 3v3 pin can also be used to power peripherals.
However, if your peripheral device (your servo in this case) tries to draw too much current the inboard voltage regulator won’t be able to cope with this power demand. This might cause the ESP8266 chip to work incorrectly, or reboot, or it might burn-out the onboard voltage regulator.

So, when you’re powering the dev board with 5v then the power that’s available on the 5v/VIN pin is connected directly to your power source, the power that’s available on the 3v3 pin has gone via the onboard voltage regulator and is limited in the amount of available current that can be supplied before you cause the chip to brownout or you destroy the onboard voltage regulator.

If you power your dev board with more than 5v you will most likely

destroy it, so the answer to this question…

is a definite NO!

You’ve said…

The specification sheet for the SG90 says that it requires a supply voltage of 4.8 to 6v, so you’re extremely lucky that it worked at all with the 3.3v you were supplying it with.

The simple solution is to stop powering the servo from the 3v3 pin on your dev board and power it from the 5v/VIN pin instead.

If you were using a servo that required 3.3v then you would need a 3.3v external regulated power supply to power the servo, and you’d need to ensure that the GND connection from that external PSU was tied across to the GND pin on the dev board to establish a common zero volts reference voltage - but that’s not the solution in this case.

Pete.