Button is never LOW on ESP8266

Hello.

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.)

Sure, I just had some trouble with fritzing.

Look at this illustration to understand the tact switch.
Then check your connection.
image

Which pins of your switch are connected to which switch contacts ?

Oh, I'm sorry, I misunderstood. :woozy_face:
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.

Exactly as in the picture I posted above.


Here is a detail:
Until I connect a wire from D0 to a switch contact, the voltage on it is 0. After I connect D0 it's 2.49V

The Fritzing picture tells us nothing about the actual switch contacts

Probably I don't really understand the question.

Does this help?

No. We still can't tell which pins on the button are actually connected to which switch contacts

What do you mean by switch here?


Never power a servo from the Arduino.

1 Like

Thank you, @LarryD . This is only for prototyping.

When you press the button which of its pins become connected ?

1. Assume you have a 4-pin button like below (Fig-1)


Figure-1:

(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?).
NodeMCUButtonServo
Figure-2:

2. Upload the following Test Sketch into NodeMCU. (tested)

#include <Servo.h>

Servo myservo;
int pos = 0;
#define MY_BTN D1

void setup()
{
  myservo.attach(D2);
  pinMode(MY_BTN, INPUT);
  while(digitalRead(MY_BTN) != HIGH)
  {
    ;
  }
}

void loop()
{
  for (pos = 0; pos <= 180; pos += 1)
  {
    myservo.write(pos);             
    delay(15);                      
  }
  for (pos = 180; pos >= 0; pos -= 1)
  {
    myservo.write(pos);            
    delay(15);                   
  }
}

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.

Omit the resistor, use pinMode (xy, INPUT_PULLUP);
One pb terminal goes to GND and the other goes to MY_BTN (5).

#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_PULLUP);  //   NEW ! !
  Serial.println("START");
}

void loop() 
{
  int val = digitalRead(MY_BTN);
  if (val == 1) 
  {
    Serial.println("MY_BTN " + (String)pos);
    if (pos == 180) 
    {
      pos = 95;
    } 
    else 
    {
      pos = 180;
    }
    myservo.write(pos);
    delay(1000);
  }
}

Is it allowed to issue write() command before attaching signal wire?

Yes. It overrides the default 90 degree start position that the servo moves to when attach() is called

That means, the following codes will also work, and the Hello message will appear on the Serial Monitor?

Serial.print("Hello");
Serial.begin(9600);