ESP32 powering a solenoid

neonrazor
Posts: 4
Joined: Tue Jun 02, 2020 7:34 pm

ESP32 powering a solenoid

Postby neonrazor » Tue Jun 02, 2020 7:41 pm

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.

ESP_Sprite
Posts: 9708
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 powering a solenoid

Postby ESP_Sprite » Wed Jun 03, 2020 8:23 am

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.)

neonrazor
Posts: 4
Joined: Tue Jun 02, 2020 7:34 pm

Re: ESP32 powering a solenoid

Postby neonrazor » Wed Jun 03, 2020 8:26 am

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).

ESP_Sprite
Posts: 9708
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 powering a solenoid

Postby ESP_Sprite » Thu Jun 04, 2020 8:08 am

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.

neonrazor
Posts: 4
Joined: Tue Jun 02, 2020 7:34 pm

Re: ESP32 powering a solenoid

Postby neonrazor » Thu Jun 04, 2020 6:35 pm

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.

bobtidey
Posts: 43
Joined: Mon Jun 18, 2018 2:24 pm

Re: ESP32 powering a solenoid

Postby bobtidey » Thu Jun 04, 2020 8:53 pm

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.

ESP_Sprite
Posts: 9708
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 powering a solenoid

Postby ESP_Sprite » Fri Jun 05, 2020 7:00 am

@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.

bobtidey
Posts: 43
Joined: Mon Jun 18, 2018 2:24 pm

Re: ESP32 powering a solenoid

Postby bobtidey » Fri Jun 05, 2020 8:27 pm

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.

bobtidey
Posts: 43
Joined: Mon Jun 18, 2018 2:24 pm

Re: ESP32 powering a solenoid

Postby bobtidey » Fri Jun 05, 2020 8:43 pm

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.

neonrazor
Posts: 4
Joined: Tue Jun 02, 2020 7:34 pm

Re: ESP32 powering a solenoid

Postby neonrazor » Fri Jun 05, 2020 9:15 pm

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.

Who is online

Users browsing this forum: No registered users and 35 guests