PWM help

I'm using a Teensy 4.1 My ultimate goal is to use PWM to control a bunch of solenoids, but first I need to get one working. I started with analogWrite and that didn't seem to work for me so I backed up to starting with the blink sketch. Just bit-banging it.

Code:
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(15);                      // wait for a second
  digitalWrite(13, LOW);   // turn the LED off by making the voltage LOW
  delay(15);                      // wait for a second
}

I'm using this Mosfet module (I can't buy the parts this cheap). The solenoid is powered by 12V. I can blink a led so it's working but I can't activate the solenoid. I even powered the VCC pin with 3.3 from the Teensy and also tried with 5V. If I use the same code on an arduino powering from 5 V, the solenoid will run all day even without VCC powered.

I'm out of ideas. Any ideas will be appreciated. Thanks
 
perhaps the device really doesn’t work from 3.3V like it says it does. You could test that with some wires,3.3v and a multimeter.

or perhaps your wiring on the teensy is wrong.. missing ground connection.. need to power off and double check with a meter what you think you know.
 
That IRF520 module will not be reliable and probably will not work at all with 3.3V. The description says "Voltage: 3.3V, 5V", but that is simply wrong info.

Quick Google search turned up the IRF520 datasheet. On page 2, the threshold voltage needed to just barely get it to turn on is anywhere between 2 to 4 volts. If you get very lucky, it might work. But generally speaking you should never design any circuit to use this transistor with less than 4 volts. It should not be used ever near 4 volts, because the threshold is the point where it just barely is able to work.

You can also see in the Amazon reviews other customers have found it does not really work at 3.3 volts. The product description that claims it can be used at 3.3V is a lie.

screenshot.png
 
The datasheet also has this chart on page 3. You can see it has 15V as the Top voltage and 4.5V at the "Bottom" voltage used to turn on the transistor. At 4.5V the horizontal part of the line is much lower, which means it will be capable of much less current than it driven with more voltage. You can infer the lack of any info below 4.5V to mean this part really is not usable with less voltage.

If fact, if you look carefully at the Y axis (which is log scale) and count the horizontal lines carefully, you can see this transistor needs at least 6.5 volts to be able to get the curve above 5 amps. The product listing claims this part can switch 5 amps, but it definitely is not capable of that much when used with only 5 volts.

screenshot.png
 
Last edited:
Also just reading the top of the datasheet or the "Drain source on-state resistance" line in the specs shows its designed for 10V gate drive (meaning 12V).

I'd simply advise to ignore the threshold voltage spec for using a MOSFET as a switch, its simply not relevant. Always look at the on-state resistance spec and the voltage given there is the _minimum_ voltage it is designed to work with - usually this will be 4.5V for a 5V MOSFET, or 10V for a 12V MOSFET.

Screenshot 2024-07-21 at 08.20.02.png


Another thing to be aware of is that graphs for MOSFETs are "typical values", not "guaranteed values", and always should be taken with a pinch of salt. The on-state resistance is shown as a maximum value of 0.27 ohms at Vgs=10V, that is a guaranteed spec.

Looking at the gate charge graph in fig 6, its clear the plateau voltage is greater than 5V in a typical device - that screams that this is not a "logic-level MOSFET" (i.e. not 5V compatible) - the plateau voltage is usually 50 to 70% of the gate on-voltage, and threshold voltage is 30% or so of the gate on voltage

Gate voltages in a MOSFET vary considerably between devices (2V spread for this device since the threshold is quoted as 2 to 4V) and drift over time as the device ages due to ion-migration in the gate oxide, only the on-state resistance spec shows what the manufacturer guarantees to work reliably as a switch.
 
Agreed, IRF520 is really meant for 12 volt gate drive. Performance with 5V gate drive is marginal, but at least within specs if not conducting nearly as well is ok.

This "HiLetgo 5pcs IRF520 MOSFET Driver Module MOSFET Button Drive for Arduino MCU ARM Raspberry PI" which has a part number 140C07 printed on its red circuit board, is a terrible product. So are numerous - others - on Amazon with IRF520 MOSFETs. Nobody should use these with Raspberry Pi or Teensy or the newer Arduino boards, which all have 3.3V signals. They're not even very good to use with old 5 volt Arduino.

Whoever is making these MOSFET modules with IRF520 and marketing them to makers claiming usable with Raspberry Pi and Arduino really ought to be ashamed. (hopefully this message with keywords is found by more people struggling to use these terrible products)
 
I appreciate the responses and the education they provide. I've been searching and I'm wondering if this won't fit the bill. The solenoid is 12v & draws 2 amps. Again needs to work on 3,3 v.

Vgs looks good 1.35 - 2.35V. The "Drain source on-state resistance" confuses me as it has:
VGS = 10V, ID = 31A ƒ = 1.0MHz
VGS = 4.5V, ID = 25A

Does this kick it out for working with 3.3V. I'd love suggestions that you know would work.
 
That's not rated for 3.3V, again the graphs show the plateau is nominally above 3.3V, I wouldn't trust it at 3.3V but its quite likely to work, but you may be unlucky. Have you tried a parametric search?
 
That's not rated for 3.3V, again the graphs show the plateau is nominally above 3.3V, I wouldn't trust it at 3.3V but its quite likely to work, but you may be unlucky. Have you tried a parametric search?

Enlighten me, what's a parametric search?
 
Whatever transistor you end up using, please also be aware of the need for a "flyback" diode wired in parallel with the solenoid. Maybe you're already know this? If not, here's a quick mention which can hopefully save you from a lot of painful problems. (I personally learned this the hard way back when I was new to electronics and tried to wire up 12 solenoids to a computer to switch compressed air blowing across glass bottled filled with different amounts water to play music ... had numerous failures and even took out my computer's printer card a few times)

The faster you switch from on to off, the worse a brief high voltage spike becomes due to the energy stored in the magnetic field of the solenoid (or motor or relay coil). Using a very large part like IRL540N (total gate charge spec is a whopping 74nC) with a fairly high resistor (1.8K in the schematic) will mitigate the problem to some degree, because a lot of electrons have to move into the gate to fill it up to 74nC, and that happens relatively slowly since the resistor limits how much current can flow. A part like IRL540N is also rated for 100V on its drain, which means it can withstand some abuse. The diode should still be used, of course, but the need is less.

With IRLML6244TRPbF, the total gate charge is 8.9nC. With the same resistor, it would switch almost 8X faster, because the gate doesn't require nearly so many electrons to fully affect the silicon underneath. Usually switching faster is a good thing, as you minimize the time the transistor dissipates power, but fast switching makes the voltage spike problem (potentially) more severe That part is only rated for 20 volts on its drain, and you've already got 12V. So only a small spike of 8 more volts puts you in risk of damage, whereas the 100V rated part gives you 88 volts of safety margin, and you'll switch almost 8X faster on the part much less able to take the abuse! But a good quality diode will clamp the spike to only about 0.7 to 0.9V, so 8V extra margin should be fine as long as you have the diode in place. Just make sure you select a "fast" diode. Do not use slow "rectifier" diodes like 1N4007.
 
Last edited:
Like paul says, and I probably should have said.. get a diode on there. I looked up my notes on that project and I measured 450V from the 12VDC solenoid valve I was using when switched instantly using some wire!!. I'm not fully convinced I tested correctly because of how hight it was but I know it went to nothing with the diode on there reverse polarity. It's possible .


1.jpg
2.jpg

Note: Probably a standard rectifier diode on there which is ok for my slow switching.
3.jpg
4.jpg



Also note the resistor value of 1.8k was to limit the charge rate as Paul says but not for switching speed reduction. (which isn't an issue for sparying stray cats). I was limiting the current from the microcontroller pin to a few milliamps. I would probably put it half that for a teensy but I can't remember what they can drive.

You could look at something like a TC427CPA (throgh hole) gate driver to make things easier.
 
Above discussion looks like the circuit will be just switching on and off. I'm curious as to why PWM was mentioned in the original post.
 
rayjorgensen‘s ultimate goal is driving a solenoid with Pwm. He started by requesting help in getting one to switch at all as this wasn’t working for him.The discussion has been around that.

Or are you asking why one would want to drive a solenoid with PWM. I’m interested in the reason now too. does it allow position control on a spring loaded solenoid?
 
Thanks for all the help!! I was going to use a fly back diode but I would have used the IN4007!! I'll look for a fast one.
Above discussion looks like the circuit will be just switching on and off. I'm curious as to why PWM was mentioned in the original post.
The end result will be a PWN based sprayer. I couldn't get pwm to work so I backed up to digitalWrite. (didn't work either) I was using the wrong mosfet and these great folks helped get the right stuff.

The big boy farmers use PWM sprayers that cost a bunch, each nozzle is in the $1,000+ range. A big block in the past was the ability to use 10 - 20 HZ range. When I stumbled on Teensy ability to use analogWrite(pin, frequency); now I can deal with the rest of the hardware. I'm looking at $40? per nozzle so I can replace a lot of part before I get to $1.000. Anyway it'll be a fun project.
 
Last edited:
Above discussion looks like the circuit will be just switching on and off. I'm curious as to why PWM was mentioned in the original post.
His original example showed equal on/off time, but guessing he will vary duty cycle to control spray. He says 10-20 Hz, so likely will use digitalWriteFast() rather than analogWrite().
 
His original example showed equal on/off time, but guessing he will vary duty cycle to control spray. He says 10-20 Hz, so likely will use digitalWriteFast() rather than analogWrite().
Nope I'm planning on analogWrite(). Each nozzle has a flow meter and based on the flow rate will feed to a PID and then analogWrite(OUTPUT);.

The equal on off in the original post was just where I left off playing with it. There was no special meaning. This project is a result of building my own RTK autosteer. It's called Ag Open GPS and you can find it here if you want to take a look. Its all opensource.. AOG I'm hoping to be able to contribute something meaningful to the project.
 
Have you tested your solenoid and whatever valve and other mechanical hardware to check if actually works in a continuously variable manner?

Probably the simplest way to test would be the power the solenoid from a lab bench power supply that gives you knobs to control the voltage and max current. Even the pretty cheap $60 type power supplies should do fine. No MOSFET or or software or microcontroller, just turn the knobs and watch the actual mechanical result.

Asking because most the solenoids I've ever used had mechanical designs that were meant to be pretty much all or nothing. Not long ago I used some valves for propane that had a solenoid to activate, but they couldn't do any sort of variable flow control because the actual mechanical action depending on a spring mechanism inside the valve which acted against the propane's pressure. In that case, testing also showed the speed the valve could actually open depended on the pressure. But that was switching vapor, not liquid. Maybe your situation will be completely different?

But if you haven't done this sort of testing, might be worthwhile to spend a little time learning the valve's actual mechanical response as you vary the voltage and current. If it really can't act in a variable manner, you can at least focus your effort on getting hardware that is capable of achieving your goals before pouring a lot more time into the electronics. You might also discover it can work, but perhaps the range of voltage / current needed depends on the water pressure (like those propane valves) which would mean you can't just program a fixed PWM duty cycle. Maybe you'll need some sort of sensor to detect the pressure or flow rate, and write code to dynamically adjust as you go. Maybe. I'm really just guessing.

My main suggestion, if you haven't already done it, is to do actual testing with the real hardware because you might have on-off style mechanical hardware that just won't ever be able to achieve software controlled variable flow no matter what you do on the electronics and software side.
 
I have done some testing and a guy in Greece has done more than me with the same valves so I feel pretty confident. After I get the new parts I'll run more tests. Heck at only $30 for the valve I'm getting that much learning and fun!!
 
If we're talking about on/off pulsing, like 1 to 10 Hz speeds, just know that's much too slow for the PWM hardware with analogWrite(). You'll need to write code using digitalWrite() and timing stuff like elapsedMillis for such very slow speeds.
 
If we're talking about on/off pulsing, like 1 to 10 Hz speeds, just know that's much too slow for the PWM hardware with analogWrite(). You'll need to write code using digitalWrite() and timing stuff like elapsedMillis for such very slow speeds.
The commercial units are running 10 - 20 Hz John Deere is running 15Hz with a dual boom which they are saying then runs at 30 Hz. All it is is the booms are staggered firing. I guess you can add 15 + 15 call it 30 and charge double for it.

Is 15 Hz to slow for PWM hardware?
 
When I was looking for a logic level driven mosfet for low fuss I chose an IRL540N https://pdf1.alldatasheet.com/datasheet-pdf/view/88237/IRF/IRL540N.html.

Ignore whatever the device name is in there as I have selected whatever I could find in the easyeda library.
I used it with 5V but 3.3v looks in spec for the device.

View attachment 35168
View attachment 35167

Thanks for the tip on the IRL540. I got them yesterday and made up a perf-board this afternoon and tested. Work like a charm!!!!
 
Back
Top