Page 1 of 2
ESP32 powering a solenoid
Posted: Tue Jun 02, 2020 7:41 pm
by neonrazor
Hi,
I have an esp32 linked up to a 3.7 1200 mAh lipo, I then have a buck boost converter attached to gnd and pin 5, to the other end of the buck boost converter i have a 12v solenoid 300 mA.
The buck is configured to convert the input voltage to 12 volts, which on measuring works fine.
In software I have pin 5 high when I need it to be and I get the corresponding voltage before and after the buck converter but the solenoid still isnt triggering. It triggers fine when using 3 volt desktop power supply.
What am I doing wrong, is the battery not powerful enough to run the solenoid and if that is the case what suggestions would you have to replace it. I have looked at some other high power solenoids which I could buck convert down but not sure if thats what the problem is.
I did try to use a transistor as a switch attached to pin 5 but that seemed to lose too much power when measured at the buck converter.
Any help would be appreciated.
Re: ESP32 powering a solenoid
Posted: Wed Jun 03, 2020 8:23 am
by ESP_Sprite
That is not going to work. Power the boost converter directly off the LiIon, use a transistor to then use the 12V and switch the relay. A GPIO does not have enough power to power the boost-converter. (If you hook it up correctly, your idea to use a transistor to switch power to the boost-converter should also work fine, by the way.)
Re: ESP32 powering a solenoid
Posted: Wed Jun 03, 2020 8:26 am
by neonrazor
Thanks for the reply, I will go back to trying to get the transistor switch working, need to do a bit of reading on what resistor to use in line with the transistor (222A i think I have).
Re: ESP32 powering a solenoid
Posted: Thu Jun 04, 2020 8:08 am
by ESP_Sprite
Make sure you put the transistor in the ground path of the DCDC then, emittor to ground, collector to the negative of the DCDC, positive of the DCDC to 3.7V. If you put an 1K resistor into the base path to the GPIO, it should work.
Re: ESP32 powering a solenoid
Posted: Thu Jun 04, 2020 6:35 pm
by neonrazor
Yep I have done that but 12volts seems to be too much for the boost convertor on 3.7v input, so I have ordered some stronger lipos and serial them to see if high voltage helps or parallel them to see if its current. I am going to buck convert down if needed for the esp32. At least its working now though with the transistor.
Re: ESP32 powering a solenoid
Posted: Thu Jun 04, 2020 8:53 pm
by bobtidey
Using a bipolar transistor like a 2n2222 is not a great idea for switching the current to the solenoid.
For a 300mA solenoid you would need to feed about 30mA into the base of the transistor to saturate it (switch it hard on). That is to much for a GPIO pin.
You could use a darlington transistor like the TIP120 but you would be better off using a MOSFET device which behave much more like an ideal switch. The main characteristic you need in such a device is that it is turned hard on with 3.3V applied to its gate.
If you can handle small devices then the A03400 devices can handle decent amounts of current and switch on hard with 3.3V. If you want to use a larger package device then the IRF3708 is a good choice as it again it will turn hard on with 3.3V. Some people use the irlz44 but although that will typically work it is only specified at 4.0V not 3.3V.
For both bipolar devices or mosfets you should include a diode across the solenoid to absorb the back emf spike when the solenoid turns off. This is just connected in parallel with the solenoid with the cathode connected to the 12v rail.
Re: ESP32 powering a solenoid
Posted: Fri Jun 05, 2020 7:00 am
by ESP_Sprite
@bobtidey Where do you get that? A 2n2222 has a minimum DC current gain of around 40 for 300mA, meaning the base current only needs to be 7 mA, which is no issue at all for any GPIO pin. Even if it were 30mA, an ESP32 GPIO is specced to go up to 40mA, so it would work just fine. While I personally also would have plunked a logic-level mosfet in there, there's nothing wrong with using an old-fashioned 2n2222.
Re: ESP32 powering a solenoid
Posted: Fri Jun 05, 2020 8:27 pm
by bobtidey
When using bipolar transistors for switching you do not use the current gain (beta) to determine appropriate base current. That parameter is specified when the transistor is in its linear region with a significant Vce. Gain drops off dramatically as Vce falls.
The normal 'rule' for switching is that base current should be around 1/10 of the collector current to keep the transistor saturated and the Vce to a low value.
If you look at the data sheet for the 2n2222 you will see that for Ic 150mA and Ib 15mA the Vce is specified to be 0.4V. For Ic 500mA and Ib 50mA the Vce is specified at 1.6V
The 2n2222A is a bit better in having a slightly lower Vce under the same conditions.
So for a 300mA solenoid one should be providing about 30mA Ib which I think is excessive for a GPIO
As I said a MOSFET is a much better device for precisely these reasons.
Re: ESP32 powering a solenoid
Posted: Fri Jun 05, 2020 8:43 pm
by bobtidey
Just to amplify further why I think trying to exploit potential 40mA capability of a GPIO would be a bad idea.
1 Vbe sat of a 2n2222A could be 2.0V
2 Vo of a GPIO at 40mA could drop to 2.6V
3 So to guarantee 30mA Ib what base resistor would you use?
4. Then under typical conditions where GPIO might not drop so much or Vbe was lower then you would get excessive base current.
I don't think you can do a reasonable worst case design here that gives sufficient base drive but stays in GPIO specification.
A MOSFET avoids all these nasty calculations and considerations.
Re: ESP32 powering a solenoid
Posted: Fri Jun 05, 2020 9:15 pm
by neonrazor
I ordered some MOSFETs today and I also ordered some relays, will try both and see what sticks.
Thank you all for your help, love the community.