I'm trying to build a simple circuit with ESP8266 , a button and a servo.
Button is on D0 (5) and servo is on D2 (3);
Button is regular 4-pin pushbutton. On leg is connected to GND with a 10K resistor. The other one is on 3.3V.
The diagonal leg to 3.3V is connected to D0.
In the default state the voltage on D0 is 3.03V and when I press the button it's 3.23V.
Please help.
Sketch:
#include <Servo.h>
Servo myservo;
#define MY_BTN 5
int pos = 95; // variable to store the defafult servo position
int buttonVal = LOW;
void setup() {
Serial.begin(115200);
myservo.write(pos);
myservo.attach(3);
pinMode(MY_BTN, INPUT);
Serial.println("START");
}
void loop() {
int val = digitalRead(MY_BTN);
if (val == LOW) {
Serial.println("MY_BTN " + (String)pos);
if (pos == 180) {
pos = 95;
} else {
pos = 180;
}
myservo.write(pos);
delay(1000);
}
}
I cannot make sense of your explanation of the circuit. Can you please post a drawing of the circuit with the contacts of the switch clearly identified
You should post a photo of the actual connection or a handwritten schematic.
Well... This is my prediction, but probably you should physically rotate the tact switch 90 degrees to correct connection.
(And I thought, the input pin is probably floating when not pressed, currently.)
Oh, I'm sorry, I misunderstood.
I was confused by overlooking that the blue wire from the switch was connected to VCC.
Now, the connection seems fine, but for that connection it should be LOW when the button is not pressed.
As he says, you need to double-check the pins that are actually connected.
(1) Check with an AVM (analog volt meter) which 2-pin get shorted when you press the knob and note down the pin numbers as 1 and 2.
(2) Connect the Button of Step-1 and the Servo with ESP8266 (NodeMCU) as per following diagram (Fig-2). Connect MY_BTN at D1; it does not work with D0 (why?).
Figure-2:
2. Upload the following Test Sketch into NodeMCU. (tested)
3. Check that Servo is not turning. 4. Gently press and release MY_BTN. Check that Serovo sweeps to-and-fro. 5. Now, correct, if necessary, your sketch of Post-1 to meet your objectives.