ESP 8266 wont initiate my servo sg90

Hi! i wanted to ask how u got ur servo to move? even if i connected it to my esp8266 i wont function, but the serial monitor says otherwise

Which servo? How is it connected? How are you supplying power to the servo?

sg 90. i connected the orange wire to the d4 pin of esp8266, ground to ground pin of esp8266 and red wire to vin pin of esp8266. i have three 3.7v li-ion batteries which supplies 11.1v but i step down to somewhere around 4v-5v? im not sure because i havent measured the output voltage because i dont have a multimeter. but i wanted to see first if the code i running so i didnt connect it to the power source only via usb of laptop.

i have been altering and trying different code to make the servo move, but it wont move. in my project involve using blynk iot, servo, rfid sensor, buzzer, and esp8266. the logic is if it is an 'registered' rfid card the servo will move, if not the buzzer will initiate and the servo will not move. additionally, in the blynk iot it has buttons will control whether to give access to the 'registered' rfid or not, it also has live monitoring which provides history of those who accessed it.

all the components work fine but the servo is the only one not moving and idk why, ive search and some forums have same problems also. ill paste my code.

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPL64CrNZ7r0"
#define BLYNK_TEMPLATE_NAME "IoT Smart Cabinet Lock"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI

#include <Servo.h>
#include "BlynkEdgent.h"
#include <SPI.h>
#include <MFRC522.h>

#define SERVO_PIN 2// CONNECTED TO D4
#define MIN_US 1000  // Define the minimum pulse width in microseconds
#define MAX_US 2000  // Define the maximum pulse width in microseconds
#define SS_PIN 4  // sda pin D2
#define RST_PIN 0 // RST (flash) pin D3
#define BUZZER 15 //buzzer pin D8


MFRC522 mfrc522(SS_PIN, RST_PIN);        // Create MFRC522 instance.
Servo myServo;
int pos = 0;
SimpleTimer timer;
int button1 = 0;
int button2 = 0;
int button3 = 0;
WidgetTerminal terminal(V2);

void setup()
{
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();

  myServo.attach (2,1000,2000);
  pinMode(BUZZER, OUTPUT);

  SPI.begin();                // Init SPI bus
  mfrc522.PCD_Init();        // Init MFRC522 card
  timer.setInterval(3000L, iot_rfid);
}

void loop() {
  timer.run(); // Initiates SimpleTimer
  BlynkEdgent.run();
}
void iot_rfid()
{
Serial.println("RFID check started");
  // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    Serial.println("No new card present");
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    Serial.println("Failed to read card");
    return;
  }
  // Now a card is selected. The UID and SAK is in mfrc522.uid.

  // Dump UID
  Serial.print("Card UID:");
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], DEC);
  }
  Serial.println();

  // Dump PICC type
  byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  //    Serial.print("PICC type: ");
  //Serial.println(mfrc522.PICC_GetTypeName(piccType));
  if (        piccType != MFRC522::PICC_TYPE_MIFARE_MINI
              &&        piccType != MFRC522::PICC_TYPE_MIFARE_1K
              &&        piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    //Serial.println("This sample only works with MIFARE Classic cards.");
    return;
  }

  
  // defining Cards here

  if ( ((mfrc522.uid.uidByte[0] == 163) && (mfrc522.uid.uidByte[1] == 118) && (mfrc522.uid.uidByte[2] == 65) && (mfrc522.uid.uidByte[3] == 169)) && (button1 == 1) ) //jhed--> 163 118 65 169
  {
    Serial.println("Access Granted to Jhed");
    Blynk.virtualWrite(V2, "Access Granted to Jhed" );
    Serial.println ("Servo initiated");
    myServo.writeMicroseconds(1000); //servo to min position
    delay(1000);
    myServo.writeMicroseconds(1450); //neutral position
    delay(1000);
    myServo.writeMicroseconds(2400); //max position
    delay(1000);
    }

  
  else if (( (mfrc522.uid.uidByte[0] == 163) && (mfrc522.uid.uidByte[1] == 45) && (mfrc522.uid.uidByte[2] == 156) && (mfrc522.uid.uidByte[3] == 228)) && (button2 == 1) )// ava--> 163 45 156 228
  {
    Serial.println("Access Granted to Ava");
    Blynk.virtualWrite(V2, "Access Granted to Ava" );
    Serial.println ("Servo initiated");
    myServo.write(25); // Set servo position to 25 degrees
  delay(1000);
  myServo.write(90); // Set servo position to 90 degrees
  delay(5000);
  myServo.write(180); // Set servo position to 180 degrees
  delay(1000);
  myServo.write(25); // Set servo position back to 25 degrees
  delay(5000);
    Serial.println("ni gana");
    }
 
  
  else if ( ((mfrc522.uid.uidByte[0] == 147) && (mfrc522.uid.uidByte[1] == 245) && (mfrc522.uid.uidByte[2] == 153) && (mfrc522.uid.uidByte[3] == 228)) && (button3 == 1) )// nicol--> 147 245 153 228
  {
    Serial.println("Access Granted to Nicol");
    Blynk.virtualWrite(V2, "Access Granted to Nicol" );
    Serial.println ("Servo initiated");
    myServo.write(25); // Set servo position to 25 degrees
  delay(1000);
  myServo.write(90); // Set servo position to 90 degrees
  delay(5000);
  myServo.write(180); // Set servo position to 180 degrees
  delay(1000);
  myServo.write(25); // Set servo position back to 25 degrees
  delay(5000);
    Serial.println("ni gana");
    }
 
 

  else {
    Serial.println("Unregistered user or button not pressed");  // Debug print
    Blynk.virtualWrite(V2, "Unregistered user or button not pressed");
    digitalWrite(BUZZER, HIGH);
    delay(2000);
    digitalWrite(BUZZER, LOW);
  }
}

// in Blynk app writes values to the Virtual Pin 3
BLYNK_WRITE(V3)
{
  button1 = param.asInt(); // assigning incoming value from pin V3 to a variable


}


// in Blynk app writes values to the Virtual Pin 4
BLYNK_WRITE(V4)
{
  button2 = param.asInt(); // assigning incoming value from pin V4 to a variable

}

BLYNK_WRITE(V5)
{
  button3 = param.asInt(); // assigning incoming value from pin V5 to a variable

}

first card i tried with pulse width but still no response from servo. the other cards also has no reponse from servo

i actually followed a youtube tutorial on this project but i change the solenoid to servo.
i hope u can help me, this is driving me crazy. (apologies if its hard to understand, im a beginner of arduino.)

If you don't know the voltage or current supply capability of Vin, DO NOT connect your servo to it. Your servo needs a supply of 5 ~ 6V and current of at least 800mA.

could i connect it to 3.3v of esp8266? will it still work? ive observe from youtube tutorials that the servo works 3.3v

Be sure to power the servo separately with 4.8 to 6V, and don't forget to connect the grounds. Example wiring for Uno:

I doubt the 3.3V pin could source near enough current for a servo. What is the servo's current rating anyway?

sorry im a noob with these things. what is that and how can i get that?

i tried connect it with my power source but still no response

Hi, @ell_5
The ESP8266 is a 3V3 output, the servos are 5V logic input.

Have you written some simple code to JUST test the servo, before using your code?

If not then please write some simple code that JUST drives the servo.

Prove your hardware control before writing your main code.

Tip; Check each input and output device individually before going into major code writing.

Tom... :smiley: :+1: :coffee: :australia:

What is what? :slightly_smiling_face:

current servo rating

For small servos like the SG-90, the power supply should be 4.8 to 6V, and capable of providing 1 Ampere. A 4xAA battery pack is fine.

A larger servo, like the MG996R, requires 4.8 to 6V at 2.5 Amperes. A 4xAA battery pack of fresh alkaline cells struggles to do that, and probably won't work for long, if at all.

my power source are three 3.7v li-ion batteries placed on battery holder, i step down the voltage to 11.1v to 5v. i dont know the ampere

What are you using for the stepdown?

First time you've mention three batteries.

i dont know the ampere

We don't either, because you haven't provided the required information.

For help, please read and follow the instructions in the "How to get the best out of this forum" post.

I have merged your cross-posts @ell_5.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.