Wemos D1 clone: digitalRead is unreliable

Hey there,

not sure if it's the correct category, but I believe this is an electronic issue:

I have two buttons hooked up to pin D3 and D4 of my Wemos D1 mini clone (these are the pull-up pins) that connect to GND when closed. Closing a button is supposed to change the direction of a motor through an ESC hooked to pin D0. I measured the voltage over D3 und D4 and GND and they're both nicely pulled up to 3.3V. However, the Motor is continuously changing directions (not very often but every 3-10 seconds) without any button being closed at all. I have no idea what causes this fluctuation. Maybe someone can help me here.

#include <Arduino.h>
#include <Servo.h>

Servo myservo;
int value = 90;
int upperPin = 0; //D3
int lowerPin = 2; //D4

void setup()
{
  Serial.begin(9600);
  pinMode(upperPin, INPUT_PULLUP);
  pinMode(lowerPin, INPUT_PULLUP);
  myservo.attach(16);
}

void loop()
{
  int readD3 = digitalRead(upperPin);
  delay(1);
  int readD4 = digitalRead(lowerPin);
  delay(1);

  int isFullUp = readD3 == LOW;
  int isFullDown = readD4 == LOW;

  if (isFullUp && isFullDown)
  {
    //should never happen - more of an emergency stop
    value = 90;
  }
  else if (isFullDown)
  {
    value = 180;
  }
  else if (isFullUp)
  {
    value = 0;
  }
  myservo.write(value);

}

You have not supplied any information in that “diagram” concerning how your motor and ESC is being powered.

It looks to me like you are getting interference from the motor.
Try a 1K external pull up resistor on each switch input, and look to fit more power supply decoupling.

Normally on Arduino board D3 would mean pin 3 and D4 pin 4.

Thanks I'll try that. The ESC/Motor is powered through a 5V DC power adapter. The controller is currently powered by USB.

The Wemos D1 has an interesting pinout where GPIO and pin number diverge (see here). Also the setup reacts as intended to actual button presses so that should be fine.

GPIO0, 2 can still be defined as D3, D4.

I have to take that back. Though it may be true for the NodeMCU, it's not for the WeMos.

UPDATE: I tried to run the whole system with a battery pack and it runs absolutely flawless. So what does that tell me? Do I need to do a decent external pull up wiring, because the onboard pull up is too weak to go against the interference signals of the DC transformer or do I need to (somehow, I don't know how) "dejam" the DC power supply unit?

Perhaps a hold-up cap on the WeMos VIN (5V), 100uF or so.

It tells you you haven’t got the ground connections right, and as I said before not enough supply decoupling.

It tells me that you need to finish your schematic. Where is the servo getting its power? (Asked before and not answered).

This is not an Arduino. On the Wemos D1 Mini and NodeMCU boards, D3 is GPIO0, and D4 is GPIO2. But that is taken care of in the boards.h file. I would make it a habit to avoid using D3/GPIO0 because if it is low on boot, then your ESP goes into the flash mode. But that isn't the problem here, just a tip to prevent problems in future projects.

Actually, I answered the question here:

But if visualization helps:

Could you elaborate this a little for an electronics dummy? My GND connections are currently all hooked together (common ground). Is that wrong? How do I do a decent power supply decoupling?

  • Where do I do it? DC supply, Wemos, both?
  • How do I do it? Do I simply connect VCC and GND with a capacitor?
  • How do I find the right capacity for my capacitor?

Like so?

Screenshot 2022-01-22 123122

I tried that. Does not help :confused:

Your schematic does not show this.
Your schematic also does not label the connections to the ESC.

That says that you have a power problem. Period. End of questions.
We need to see the specifications to the ESC and the 5V supply.

You do not need additional pullup resistors. The digital inputs of the ESP are high impedance and don't need a "strong" pullup.

No. You did not.
We are, mostly, engineers. You can write paragraphs about your circuit and you may as well be speaking Portuguese. Schematics and specifications are the language we understand.

Okay, for the sake of getting this solved I'll provide every bit of information I have. Unfortunatelly I cannot find decent data sheets for the components, since they are mostly no-name "made in China" parts.

Power supply unit:

  • Model: KLY-1205000
  • Input voltage: AC 100V-240V 50/60Hz (currently 230V/50Hz)
  • Output current: 5A
  • Output voltage: DC3-12V (via potentiometer, set to privide around 5.5V at the moment)
  • Link to shop

ESC:

  • Function: forward, reverse, brake
  • Forward 30A
  • Back 20A
  • Brake 40A
  • Instantaneous peak 40A
  • UBEC Output: Voltage 5.7V, Current 1A (Supply receiver, servo power)
  • Voltage support: 4 ~ 8v can be directly on the 2s lithium (7.4V)
  • Link to shop

The Motor is a DS3218MG Servo on which I removed all control electronics and connected the motor to the ESC directly.

Chip on the Wemos D1 mini clone is an ESP8266MOD:
ESP8266EX-EspressifSystems.pdf (870.6 KB)(Datasheet)

Refined schematics:

If there's something else I can provide, I'd be happy.

Perhaps you could demo your particulars, physical layout in a youtube video.

Nothing against portuguese I hope... :joy: :joy:

Only that Portuguese is one of the hardest languages to learn.

1 Like

Your schematic is still incomplete.
Where is the ESC getting 5V?
Show the grounds.
What battery did you use when it worked?

The motor can draw up to 2.5 Amps, so I am suspecting that the battery has a lot more capacity than the power supply.