0

Context

I keep the board in deepsleep mode and want to wake it with different buttons. I need to know which button has been pushed on boot. The board is immediately put back in deepsleep mode afterward.

I've been able to reset the board and pull a pin LOW at the same time. (see circuit below). The reset pin is momentarily pulled low so it reset the board effectively.

I'm not an electronic engineer, just a hobbyist, so any other method to do something are always welcome.

Problem

With the actual code/circuit, I need to hold the button until the device boot up so it can detect button was pushed but I would like to be able to just push the button quickly instead of having to hold it.

Question

How to add some sort of delay that will "hold" the SW-D5 button until the board boot?

Or anything that keep my D5 pin LOW for a short amount of time after the button has been pushed?

I think something in the range of 500 to 1000 milliseconds would be enough.

My actual circuit

schematic

simulate this circuit – Schematic created using CircuitLab

My code

#include <Spi.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>

int buttonPin = D5;

void setup() {
  // Read pin value as soon as possible after boot
  pinMode(buttonPin, INPUT_PULLUP);
  int buttonValueAtBoot = digitalRead(buttonPin);

  pinMode(BUILTIN_LED, OUTPUT);
  Serial.begin(115200);
  Serial.println();

  if (buttonValueAtBoot == LOW) {
    Serial.println("D5 was pressed");

    // Do something useful here like making an HTTP request.
    digitalWrite(BUILTIN_LED, LOW);
  }

  // Avoid problem when the button is being hold
  while (digitalRead(buttonPin) == LOW) {
    // wait before going to deepsleep
    // or the capacitor will be drained
    delay(1);
  }

  Serial.println("going to sleep");
  ESP.deepSleep(0);
}

void loop() {
}
  • write a pin to hold it down; pinMode() in setup() kicks in in about 200ms – dandavis Oct 29 '17 at 23:37
  • @dandavis the problem is that the pin already changed its state back to normal once in setup().Push SW-D5 -> trigger reboot -> release SW-D5 -> few hundreds of milliseconds later -> setup() – Remi Oct 30 '17 at 1:16
1

A simple RC network should do the job.

schematic

simulate this circuit – Schematic created using CircuitLab

Ok. A quick explanation of what we have here. C1 and R1 are the same as you have at the moment for your reset circuit. R1 is the existing pullup resistor in the Wemos. R3 recharges the C1 when the button is released. It's effectively the pullup for the button.

D2 is to separate the two parts of the circuit. It prevents R3 from charging the capacitor C2.

The magic happens with C2 and R2. Pressing SW1 discharges C2, and the GPIO goes low. When you release SW1 C2 charges up again through R2. When the voltage across C2 rises to the VIH of the GPIO the GPIO will go high.

D1 is to protect the GPIO from the negative pulse you can get from C2 as it discharges.

Here's a simulation. Top line is the GPIO, bottom is RESET.

enter image description here

VIH for the ESP8266 is 0.75VIO, so 75% of 3.3V, or 2.475V.

The charge time of the capacitor C2 is calculated as T=RC, where T is the time taken to charge up to 63% of the supply voltage (2.08V)

With the shown values the time taken to rise to 63% of 3.3V will be 10,000 x 0.0001 seconds, or 1 second. So to get to 75% it will take a little longer than 1 second. 75% is about 1.4T, so if T is 1 as in the above equation then it will take 1.4 seconds (1 * 1.4) to rise enough for the GPIO to register as HIGH.

Note these times are all from the moment you release the button. Reset has already happened before that time.

Also the internal pullup of the GPIO should be disabled otherwise it will also charge the capacitor and the charge time will be reduced. Since you don't know that resistor value it then becomes impossible to calculate what the charge time will be. Simplest to just disable it and only use the external resistor R2.

  • If the button is pressed twice, C1 will still be charged after the first press. Reset does not work. When and how can C1 be discharged? – AltAir Oct 29 '17 at 20:14
  • Good point. It wants a pullup - and maybe a diode to isolate the two signals. I'll tweak it. – Majenko Oct 29 '17 at 20:19
  • Not so long ago, circuits for me were hieroglyphs. Now I can look at them and appreciate their beauty, how they work, the reason of everything. The joy of learning. – user31481 Oct 29 '17 at 20:46
  • @majenko thanks for the time you took for all those very appreciated explanations. Will it work if I use diode instead of Schottky diode? I will try your suggested circuit as soon as I receive bigger capacitor this week – Remi Oct 29 '17 at 21:46
  • Yes it should work with a silicon diode. – Majenko Oct 29 '17 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.