I connected the D1,D2 pins of the the Node MCU to the positive and negative of a 5V submersible motor. I wrote the following code, its getting uploaded without any errors but the motor is not working. I tested the condition of the motor by connecting to a battery,it does work.the code:

void setup() {
  // put your setup code here, to run once:

void loop() {
  // put your main code here, to run repeatedly:
  • 1
    A diagram of how things are physically connected would be very helpful here. – stevieb Jun 20 '17 at 16:29
  • You might have to drive you motor using a transistor. If you motor does happen to operate using 5V (though, I'm pretty doubtful it will because a lot of motors require more voltage to operate), you will have to hook it up to the 5V rail and one of PWM pins (D1 for an example). Otherwise, if it requires more voltage, you will have to hook it up to a 9V battery and the PWM pin. – KingDuken Jun 20 '17 at 17:10
  • 1
    all pins are PWM on ESPs... You need to switch the load with the GPIO; DON'T drive the motor from the outputs themselves. a relay or nchan mosfet would work well. – dandavis Jun 20 '17 at 18:36
  • 1
    You need an appropriate motor drive circuit, and an appopriate power source. Motor power should never pass through the MCU board, and a "9v battery" is only suited to unusually low current motors, and even then not at all economical. – Chris Stratton Jun 20 '17 at 19:01

I think you have got two issues here.

Firstly, as mentioned in the comments, you are driving the motor off the data pins so it will be getting less than 20mA which is almost certainly not enough to break the stall. You need to use the digital pins to switch a MOSFET, transistor or relay (Electrical switch) and then that will switch on a larger supply directly from a "battery pack". Have a Google for 'transistors' or 'relays' and see how they work, then you should get what I mean.

Secondly you seem to be using the pin names D1 and D2. There are a number of Node MCU boards out there but they all seem to have one thing in common and that is the pins labelled D1 and D2 on the board are generally not D1 and D2 in the code.


If this is you board then unless you have a header file that does this for you, you should be using GPIO_5 and GPIO_4 for D1 and D2 (respectively).

Oh and the last thing, make sure you sub is leak proof :)

| improve this answer | |

First of all if you don't know Then the positive pin of the submersible motor has to be connected to a power source to either switch the valve and secondly the negative pin has to be connected to the GND!
Next thing is that you should never drive a motor or anything like that directly with a microcontroller. You should use either a MOSFET or something similar.
Now for that solution would be is first of all make a circuit similar to this. With the control pin referring to the microcontroller's output and then you could exchange the 12V input with the desired voltage your motor is compatible with ! Next connect the negative pin of the motor to the common ground of the battery pack and the microcontroller and output pin to the motors positive pin.

enter image description here

| improve this answer | |
  • 1
    This is proposal is essentially "upside down" - if motor reversing is not needed, then a single switch can be used, but it should be an NFET placed in the negative supply line and driven by a positive signal from the MCU. With your proposal, the MCU would have to share its supply line with the motor and use a PFET - possibly but introducing a lot of needless complication. And device physics means that NFETs work better than comparable PFETs anyway. – Chris Stratton Jun 21 '17 at 15:00
  • 1
    -1 this will not work. That was pointed out a month ago, but the answer remains uncorrected, with the potential to mislead future visitors. It needs to be repaired or deleted. – Chris Stratton Jul 21 '17 at 15:49

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.