Hello! I'm new in here and don't tend to ask questions much but I'm very much stuck. (Apologies for possible mistakes around forum categorizing and such)
I have a servo that works well with Arduino Uno - I run it with batteries/external power source.
The Ardunio have simply two wires to run it - ground and input (I don't need the Analog).
When shifting the wires to the ESP, it simply won't nudge!
The exact same code. I also made sure that I'm choosing the right pin for the Servo - it's simply ground pin and input pin, which I made sure is the correct one!
What am I missing?? I thought it might be insufficient power for the input pin (Arduino is 5v, ESP is 3.3v) but the servo has its own power so I don't understand the issue
See attached video -
In this second video I just return the cables back to the Arduino and everything works again (same code, same connection pins) -
Any thoughts??
Here's the code -
#include <Servo.h>
Servo myservo;
// Control and feedback pins
byte servoPin = 4;
int feedbackPin = A0;
// Calibration values
int minDegrees;
int maxDegrees;
int minFeedback;
int maxFeedback;
int tolerance = 2; // max feedback measurement error
void calibrate(Servo servo, int analogPin, int minPos, int maxPos) {
// Move to the minimum position and record the feedback value
servo.write(minPos);
minDegrees = minPos;
delay(2000); // make sure it has time to get there and settle
minFeedback = analogRead(analogPin);
// Move to the maximum position and record the feedback value
servo.write(maxPos);
maxDegrees = maxPos;
delay(2000); // make sure it has time to get there and settle
maxFeedback = analogRead(analogPin);
}
int getPos(int analogPin) {
return abs(map(analogRead(analogPin), minFeedback, maxFeedback, minDegrees, maxDegrees));
}
void ServoSetup() {
Serial.println("Setting output");
pinMode(servoPin, OUTPUT);
Serial.println("Attaching");
myservo.attach(servoPin);
Serial.println("Calibrating");
calibrate(myservo, feedbackPin, 0, 270);
}
void setup() {
Serial.begin(9600);
ServoSetup();
}
void loop() {
Serial.println(analogRead(A0));
myservo.write(270);
Serial.println("270");
delay(2000);
Serial.println(analogRead(A0));
myservo.write(0);
Serial.println("0");
delay(2000);
}
If so then you are trying to control the servo with the ESP8266 chip pin number 4, not the ESP board pin number 4 which should be referred to as D4 in the code
But I managed to make this pin work with A PIR sensor (just to validate that it's the right pin I'm referencing).
How would you add a D4 in the code? looking at samples from others I see mostly the use of - #define servoPin D4
After more investigation, I understand now that all the pins have very unique purposes and there are very few I can/should actually use.
I also found that it's NodeMCU I'm using, so I changed the board to that and replaced the pin definition to D2, which didn't give me any errors, but the same problem persist.
byte 4 is the equivalent of D2, as it's a reference to the GPIO number.
Every couple of minutes the motor jitters, so I do know there's something happening from that pin and that it actually can send signals to the motor, I'm just not sure why it fails.
D3 didn't work either. I also tried replacing/reinstalling the Servo library for the board and also use different esp8266 boards - all the same outcome of a mild random jitter every couple of minutes.
here's my latest sketch -
#include <Servo.h>
Servo myservo;
// Control and feedback pins
#define servoPin D3
int feedbackPin = A0;
// Calibration values
int minDegrees;
int maxDegrees;
int minFeedback;
int maxFeedback;
int tolerance = 2; // max feedback measurement error
void calibrate(Servo servo, int analogPin, int minPos, int maxPos) {
// Move to the minimum position and record the feedback value
servo.write(minPos);
minDegrees = minPos;
delay(2000); // make sure it has time to get there and settle
minFeedback = analogRead(analogPin);
// Move to the maximum position and record the feedback value
servo.write(maxPos);
maxDegrees = maxPos;
delay(2000); // make sure it has time to get there and settle
maxFeedback = analogRead(analogPin);
}
int getPos(int analogPin) {
return abs(map(analogRead(analogPin), minFeedback, maxFeedback, minDegrees, maxDegrees));
}
void ServoSetup() {
Serial.println("Setting output");
pinMode(servoPin, OUTPUT);
Serial.println("Attaching");
myservo.attach(servoPin);
Serial.println("Calibrating");
calibrate(myservo, feedbackPin, 0, 270);
}
void setup() {
Serial.begin(9600);
ServoSetup();
}
void loop() {
Serial.println(analogRead(A0));
myservo.write(270);
Serial.println("270");
delay(2000);
Serial.println(analogRead(A0));
myservo.write(0);
Serial.println("0");
delay(2000);
}
I made progress but the mystery gets so much worse...
I replaced the chip with a new ESP32 I own. Feels like it's on steroids compared to 8266.
What I noticed - with the same code! (different pins and libraries) - is that the motor responds fine to the calibration function and after that no response.
I cancelled the calibration function in the code and the motor works perfectly fine!
Same commands as in the calibration function, but outside of it - weirdest thing I've ever seen.
Same code as above works fine with ESP32 but without the calibration function! I think it's due to differences in the Servo libraries for the different chips.
I'm going to stick to the ESP32 as I finally got it to work but if anyone can solve this mystery or even better can help me make this work with the ESP8266, that would be great