NodeMCU can't work servo mototr in pinD2 & D3, but can work in D1

I'm using NodeMCU 1.0(ESP12E Module)
I had debug that if I using D2,D3 can't and work get WDT
but it can work with D1
I don't know why it can't work with D2,D3. I had try to reconnect the wire & change the servo
But still can't work.
How can I use the D1&D2&D3 without get error

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Arduino.h>
#include "HX711.h"
#include <Servo.h>

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 12;
const int LOADCELL_SCK_PIN = 13;

HX711 scale;
Servo servo1;
Servo servo2;
 //Servo servo3;


// Update these with values suitable for your network.

const char* ssid = "**********";
const char* password = "***********";
const char* mqtt_server = "broker.hivemq.com";

WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE	(50)
char msg[MSG_BUFFER_SIZE];
int value = 0;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

      //hx711
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(-478.507);
  scale.tare();

}

void callback(char* topic, byte* payload, unsigned int length) {

  
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }

  // 將 payload 轉換為字串
  String payloadString = "";
  for (int i = 0; i < length; i++) {
    payloadString += (char)payload[i];
  }

  // 分割字串並儲存數值到 w1、w2 和 w3
  String values[3];
  int index = 0;
  for (int i = 0; i < payloadString.length(); i++) {
    char c = payloadString.charAt(i);
    if (c >= '0' && c <= '9') {
      values[index] += c;
    } else if (values[index] != "") {
      index++;
    }
  }

  // 將數值轉換為整數並儲存到 w1、w2 和 w3
  int w1 = 0, w2 = 0, w3 = 0;
  if (index >= 0) {
    w1 = values[0].toInt();
  }
  if (index >= 1) {
    w2 = values[1].toInt();
  }
  if (index >= 2) {
    w3 = values[2].toInt();
  }

  // 印出儲存的數值
  Serial.println();
  Serial.print("w1: ");
  Serial.println(w1);
  Serial.print("w2: ");
  Serial.println(w2);
  Serial.print("w3: ");
  Serial.println(w3);


}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      //client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("machine/powderdispensor/in");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  servo1.attach(5); //D1
  servo2.attach(6); // D2
  //servo3.attach(7); // D3
  servo1.write(0);
  servo2.write(0);
  //servo3.write(0);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);


}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  unsigned long now = millis();
  if (now - lastMsg > 1000) {
    lastMsg = now;
    ++value;
    char buffer[10];
    dtostrf(scale.get_units(10), 5, 2, buffer);
    client.publish("machine/powderdispensor/out", buffer);
  }

  //   if (scale.get_units(10)+1 < w1) {
  //   servo1.write(90);  // 90 度表示 w1 未達到
  //   servo2.write(0);
  //   servo3.write(0);
  // } else if (scale.get_units(10) <= w1 + w2) {
  //   servo1.write(0);   // 0 度表示 w1 達到、w2 未達到
  //   servo2.write(90);
  //   servo3.write(0);
  // } else if (scale.get_units(10) <= w1 + w2 + w3) {
  //   servo1.write(0);   // 0 度表示 w1 達到
  //   servo2.write(0);   // 0 度表示 w2 達到、w3 未達到
  //   servo3.write(90);
  // }else if (scale.get_units(10) >= w1 + w2 + w3) {
  //   servo1.write(0);   // 0 度表示 w1 達到
  //   servo2.write(0);   // 0 度表示 w2 達到、w3 未達到
  //   servo3.write(0);
  // } else {
  //   servo1.write(0);   // 0 度表示 w1 達到
  //   servo2.write(0);   // 0 度表示 w2 達到
  //   servo3.write(0);   // 0 度表示 w3 達到
  // }


}

The Serial Monitor when I trying to use D2 or D3

19:31:33.933 ->  ets Jan  8 2013,rst cause:4, boot mode:(3,6)
19:31:33.933 -> 
19:31:33.933 -> wdt reset
19:31:33.933 -> load 0x4010f000, len 3424, room 16 
19:31:33.933 -> tail 0
19:31:33.933 -> chksum 0x2e
19:31:33.933 -> load 0x3fff20b8, len 40, room 8 
19:31:33.978 -> tail 0
19:31:33.978 -> chksum 0x2b
19:31:33.978 -> csum 0x2b
19:31:33.978 -> v000469f0
19:31:33.978 -> ~ld
19:31:34.024 -> �a�n�r��n|�l�l`bbrl�nb�nl`�rl�l��

Serial Monitor can work with D1

19:29:22.940 -> Connecting to CHT-1233
19:29:23.507 -> .......
19:29:27.248 -> WiFi connected
19:29:27.248 -> IP address: 
19:29:27.248 -> 192.168.1.116
19:29:28.511 -> Attempting MQTT connection...connected
19:29:45.020 -> Message arrived [machine/powderdispensor/in] ["100","400","700"]
19:29:45.020 -> w1: 100
19:29:45.020 -> w2: 400
19:29:45.020 -> w3: 700```

Moved to Project Guidance.... Please don't post in Uncategorized again.

You use GPIO 6+7. These GPIO's are used for the connection to the flash and cannot be used otherwise. Where did you get these mappings D2->GPIO6 and D3->GPIO7? It's wrong.
The mapping of all ESP8266 boards that I use is D2->GPIO4 and D3->GPIO0. If you select the correct board you should be able to use Dx pin names.

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