Motor controller by transistor and ESP8266

Thread Starter

icydash

Joined Jan 14, 2009
148
Hi all,

I'm a bit new to electronics and I'm trying to control a motor with an ESP8266-01 and a transistor. My circuit is as follows:
schematic2.png Everything seems to be working except the right-most part of the schematic, where the motor is controlled by the NPN transistor. If I disconnect the GPIO2 from the NPN transistor (i.e., leave GPIO2 floating), the ESP8266-01 boots up correctly and sends me the information I want over the TX/RX lines for debugging. As soon as I connect the GPIO2 to the NPN transistor, everything goes haywire and I just get gibberish over the TX/RX lines. For the heck of it, I tried disconnecting the GPIO2 from the NPN transistor (i.e., leave GPIO2 floating), and used a wire to just directly connect the base of the NPN transistor to the Vcc line to see if the motor would run, and the motor did -not- run and it caused the ESP8266 to go haywire again (send back gibberish over the TX/RX lines). Can anyone shed light on what I'm doing wrong? The motor is just a small DC motor for an RC car.
 

crutschow

Joined Mar 14, 2008
38,325
used a wire to just directly connect the base of the NPN transistor to the Vcc line
Never do that. :eek:
A BJT needs a resistor in series with the base to limit the current, otherwise you can zap the transistor .
and the motor did -not- run and it caused the ESP8266 to go haywire again (send back gibberish over the TX/RX lines)
Sounds like noise on the ground from the motor is causing problems.
The power and ground for the motor-transistor should have separate wires going directly back to the regulator output and supply ground, independent of the wires powering the module.
 

Alec_t

Joined Sep 17, 2013
15,105
1) Why do you have a resistor in series with the motor?
2) You must have a reverse-biased diode connected across the motor to protect the transistor from back-emf voltage spikes when the transistor switches off.
3) You may have already zapped the transistor.
 

Thread Starter

icydash

Joined Jan 14, 2009
148
Thanks everyone! So it sounds like, based on @Alec_t 's comments, I should have something more like this:schematic2.png
And based on @crutschow 's comments, my wires from the motor should separately go to the Vcc from the regulator and the supply GND. Oh and I may have to replace my probably-zapped transistor :)

Thanks everyone, I will give this a try. I'm glad to know I wasn't wildly off on my first attempt at this, though.
 

Thread Starter

icydash

Joined Jan 14, 2009
148
So I have confirmed that my transistor is still working. I tried the abovedescribed changes (except for the diode, which I don't have at the moment) and the motor did turn on when i flipped the switch to the circuit now. But the ESP8266 doesn't appear to be behaving properly. I think it's because GPIO2 is supposed to be pulled up for the ESP8266 to start up right. And I think what is happening is that the 3.3k pull-up resistor on the GPIO2 does in fact pull up that pin when that pin is not connected to anything (floating), so the ESP8266 works properly in that scenario. But as soon as I connect it to the base of the transistor, it's probably allowing current to flow through the transistor due to the higher potential at the base than the emitter, which in turn turns on the motor but also pulls GPIO2 to ground, causing the ESP8266 to not startup right.

Any ideas on a fix? Use a PNP instead of an NPN maybe, and have the ESP8266 pull the pin low to turn on the motor after the ESP has booted up? Or would just switching the leads of the NPN work?
 
Last edited:

dendad

Joined Feb 20, 2016
4,635
Give 1K a go and see how it works, but do you need a pullup on the port pin?
I'd have a 1K resistor from the port pin to the base, a 10K from the base to the emitter to hold the transistor off, and no pullup.
Then when the ESP8266 is in an un-programmed.undefined state, the pullup will not turn the motor on.
Also, what is the max current the motor will draw? It is most likely you are pulling the 3.3V down as the regulator will drop out.
You could run a separate reg for the motor then the ESP8266 always has a clean supply. And for that matter, if the motor can handle it, just run it off the 12V supply.
And I'd add a large electro to the motor supply, maybe 1000uf or 2200uF. The 10uF is a bit small for large currents.
 

Thread Starter

icydash

Joined Jan 14, 2009
148
@dendad - thanks! I removed the pull up resistor and the ESP8266 seemed to function fine (before I attached the motor, see below), so maybe I don't need that? I thought I did because this told me so: http://www.instructables.com/id/How-to-use-the-ESP8266-01-pins/ (I was trying to implement the design in Step 4 for part of this circuit). I also tried attaching the Vcc of the transistor directly to the 9v battery (sorry, it's a 9v, i realized i'm using 12v in all my images), putting a 1k between the GPIO pin and the base, and putting a 10k from the base to the emitter, like this:
schematic2.png
I'm still having the same problems -- when I turn on the circuit, the ESP8266-01 doesn't seem to boot up correctly, and the motor makes a funny noise (it sounds like it's trying to rotate, but it's not actually rotating). I'm not sure what the max current is for this motor. If it helps at all, this is the motor: https://www.amazon.com/gp/product/B01M0XOOS5/ref=oh_aui_detailpage_o09_s00?ie=UTF8&psc=1

It seems like for the ESP8266 to boot up right, the GPIO2 pin either needs to be floating or pulled up. For some reason, when I attached it to the base of the transistor, it seems like that causes it to get pulled down (I think), which I think is what is causing the problem. I have been able to successfully recreate the problem by directly attaching GPIO2 to ground, rather than the transistor. That seems to cause the same boot-up error as when I attached GPIO2 to the base of the transistor.
 
Last edited:

dendad

Joined Feb 20, 2016
4,635
Try an LED with a series resistor in place of the motor to start with. If you can control the LED then it will probably be noise from the motor.
And the caps across the battery are still too small. For motors you need bigger caps as the current pulses are high and can introduce a lot of noise. Also make sure the motor supply and the transistor "E" connections go directly to the big cap and that is at the battery. The motor current must not flow in the lines feeding the ESP8266.

Another thing to try is a small PNP transistor driving the NPN transistor so having the port pin high will be motor off, and low = motor on.
Then you can keep the pullup.
 
Last edited:

Thread Starter

icydash

Joined Jan 14, 2009
148
So I was able to get it to work using the below schematic, recommended by the guy in the second link above (the article titled "ESP8266 stops responding when isolated relay is connected to AC tubelight"):

schematic2.png
The LED seems to prevent the GPIO2 pin (which gets pulled up at boot time) from turning on the transistor. I used the small resistor before the LED because larger resistors prevented the GPIO2 pin from being able to turn on the motor at all -- I guess enough current wasn't getting through to turn on the transistor. I started at 1k for that resistor and worked my way down, and it wasn't until I got to around 80 ohms that the motor was able to turn on, and 47 ohms worked best. As an aside, I ultimately will add a diode across the motor leads for the reasons @Alec_t mentioned earlier.
 
Last edited:

Thread Starter

icydash

Joined Jan 14, 2009
148
@dendad - sounds like you're suggesting I also add a large capacitor in parallel between the 9v source and the 100nF capacitor for the LD1117. What size would you recommend? I think you mentioned above 1000uf or 2200uF, is that right?

I thought about the PNP approach but I don't have a PNP at the moment, so this will have to do for now :)
 

dendad

Joined Feb 20, 2016
4,635
@dendad - sounds like you're suggesting I also add a large capacitor in parallel between the 9v source and the 100nF capacitor for the LD1117. What size would you recommend? I think you mentioned above 1000uf or 2200uF, is that right?

I thought about the PNP approach but I don't have a PNP at the moment, so this will have to do for now :)
Yes, put a larger cap across the battery. It lowers the impedance of the supply and an save you a lot of trouble with odd behaviors.
That circuit above is a bit odd but if it works, ok.
And the diode across the motor in the reverse direction is a very good idea. It protects from voltage spikes and will make the motor run smoother. The diode needs to be at least the max motor current rated, and more if you can.
 

Aswinth

Joined Nov 1, 2016
34
Hi Icydash,

I faced the exact same problem with one of my ESP8266 Project. I failed to find a permanent solution, even-though I tried everything that came to my mind. I am sharing my experience just in case if it helps people here. The project I mentioned can be found in the link below

https://circuitdigest.com/microcontroller-projects/smart-junction-box-for-home-automation-esp8266
https://circuitdigest.com/microcont...oice-controlled-home-automation-using-esp8266

I absorbed the following while dealing with its GPIO pins :
  1. The ESP8266-01 module has two GPIO pins which are the GPIO0 and GPIO2 pins respectively.
  2. The maximum source current of the GPIO pins are 12mA.
  3. The maximum sink current of the GPIO pins are 20mA.
  4. Due this low current we cannot drive any decent loads like a relay directly from the pins, a driver circuit is mandatory.
  5. There should not be any load connected to the GPIO pins when the ESP module is powered on. Else module will be stuck into a reset loop.
  6. Sinking more current than the recommended current will fry your ESP8266 module’s GPIO pins so be cautious.

To overcome the above shortcomings of the ESP8266 module I used a BC547 to drive the Relays and used a switch between the Emitter and ground of the BC547 Transistors. I left this connection open when the ESP module is powered ON, then closed it and left as such. Things worked fine until the next power cycle you can see more details on the link above.

I would be of great help to me (and many others) if you ppl could figure out a way to drive the loads directly without a hardware reset like I have used above.

Thanks
 

Thread Starter

icydash

Joined Jan 14, 2009
148
@Aswinth - Thanks for all that helpful information! The above circuit I posted (with the LED in series with the base of the transistor) seems to be working for now, and may be an odd but permanent solution.

@Alec_t - I agree. The only reason I said "ultimately" is because I don't have a diode right now, but I already ordered one so it should be delivered in the next few days.

@DNA Robotics - How would the opto-isolator be used in the circuit? I haven't worked with one before, so I'm not quite sure what you're envisioning here.
 

DNA Robotics

Joined Jun 13, 2014
670
The microcontroller only has to power the LED in the opto-isolator to turn on it's transistor which can switch higher voltages and amps.

In this first diagram, in the opto-isolator U1 they are showing a diode but is actually an LED.
optocoupler-interfacing-with-avr-microcontroller.jpg Opto isolator relay.jpg
 
Last edited:
Top