5
  • I developed a simple push button project
  • I used a wemos D1 mini
  • I connected D3 pin to a pull down 1K resistor.
  • I used a multimeter to measure D3 pin voltage.

When I push the button, D3 pin voltage changes from 0.x volt to 4.x volt. But digital read on D3 always return HIGH.

Please help me find the problem.

This in my sketch :

    int ledPin = D2; // choose the pin for the LED
    int inPin = D3;   // choose the input pin (for a pushbutton)
    int val = 0;     // variable for reading the pin status
void setup() {

  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}

void loop(){

  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {    
    digitalWrite(ledPin, LOW);  
  } else {
    digitalWrite(ledPin, HIGH);  
  }
}

Rohit Gupta
  • 618
  • 2
  • 5
  • 18
Toni startup
  • 61
  • 1
  • 1
  • 3

2 Answers2

6

The problem is that when the button isn't pressed, the voltage at pin D3 is just floating. Since there is nothing attached, the voltage at the pin can be anything. To prevent this, you use a pull-down resistor, so that when the button isn't pressed, the voltage at D3 is pulled to ground, and you will get a LOW using digitalRead.

schematic

simulate this circuit – Schematic created using CircuitLab

Gerben
  • 11,332
  • 3
  • 22
  • 34
2

First of all - read your post before you actually post it. This code is no readable. And if you have problems with electronics add picture of schematics how you connected your board. There can be problem too.

D3 IO, 10k Pull-up GPIO0

Source: https://www.wemos.cc/product/d1-mini-pro.html

Your D3 pin have internally pull-up so no doubt that it reads HIGH all the time. Connect your board like on picture below or change your reading pin to D0 for example (It has no other function than simple IO).

schematic

simulate this circuit – Schematic created using CircuitLab

For this case you can trust internal pull-up but in more complicated projects you should add your own one.

For more information about pull-ups and pull-downs check this article: http://www.electronics-tutorials.ws/logic/pull-up-resistor.html

Adam Rosiek
  • 108
  • 5