Can a mosfet be controlled using digital only pins?

So, this might be a dumb question… I’ve only ever controlled a mosfet using the analogWrite function to create an output voltage between 0-12V (with the source being a 12V power supply) on pins that have pwm capability. My goal here is to create a relay box, and I’ve already planned to use 8 of teensy 3.5’s pwm pins, so being able to make use of the digital pins to add additional relays would be useful. If I were to write “digitalWrite(pin, high)” to a mosfet with a 12v supply, would the output just always be 12V? I would assume that analog write wouldn’t work to get a less than 12v supply on a digital pin, but am I correct about that? Are the analog/pwm pins really analog or are they just digital pins that switch on and off real fast? Couldn’t a regular digital pin do that?

Ok, any info is appreciated. I couldn’t seem to find examples of people not using analogWrite with a pwm capable pins, but I doubt I looked everywhere. I imagine most people aren’t trying to control more than a few mosfets.

Here’s a link to the relays I bought, if that helps: https://www.digikey.com/products/en?keywords=T9AS1D12-12

The maximum turn on voltage is 9v according to the information on that page (minimum is 1.2V), so I’d imagine 12v would damage it.

The mosfets I’m using: https://www.sparkfun.com/products/10213

The planned circuit design (see attachment)

I got lost with all your 12V and MOSFETs and relays but to answer one of your questions directly. Yes PWM is just digital switched on and off fast - basically analogWrite() is misnamed. And yes you can do the switching without using the built-in PWM function if you need to, using digitalWrite()s.

But relays are either on or off so I have no idea why you're talking about sending varying voltages to them. Digital pins are really good at on and off, it's what they do best.

Steve

snowskijunky:
The maximum turn on voltage is 9v according to the information on that page (minimum is 1.2V), so I’d imagine 12v would damage it.

It clearly says the coil voltage is 12V.

Limits are given in the Absolute maximum section of the datasheet.

The turn on voltage is the voltage it turns on at, the maximum turn on voltage is the maximum
over any device - ie you are guaranteed the relay will operate if you provide at least 9V, and you
can guarantee that applying 1.2V will let it turn off.

If you’d gone to the real datasheet, you’d see these termed as “operate voltage” and “release voltage”,
which is much easier to understand - remember the summary of specs in a reseller’s web page is
secondhand information, the datasheet is always the thing to check before making a decision!

Don’t PWM a relay!! It can’t keep up.

slipstick:
I got lost with all your 12V and MOSFETs and relays but to answer one of your questions directly. Yes PWM is just digital switched on and off fast - basically analogWrite() is misnamed. And yes you can do the switching without using the built-in PWM function if you need to, using digitalWrite()s.

But relays are either on or off so I have no idea why you're talking about sending varying voltages to them. Digital pins are really good at on and off, it's what they do best.

Steve

I guess I was thinking that pwm would result in a constant 9v output, but I think I misunderstood.

MarkT:
It clearly says the coil voltage is 12V.

Limits are given in the Absolute maximum section of the datasheet.

The turn on voltage is the voltage it turns on at, the maximum turn on voltage is the maximum
over any device - ie you are guaranteed the relay will operate if you provide at least 9V, and you
can guarantee that applying 1.2V will let it turn off.

If you'd gone to the real datasheet, you'd see these termed as "operate voltage" and "release voltage",
which is much easier to understand - remember the summary of specs in a reseller's web page is
secondhand information, the datasheet is always the thing to check before making a decision!

Don't PWM a relay!! It can't keep up.

Yea datasheet=good idea. I thought rated voltage was a maximum and that operate voltage was what you operate it at. my bad, but thanks for clarifying.

But anyhow, in summary, I should be fine using the digitalWrite(pin, high) to turn the mosfets on now that I know for sure I can leave the relays on at 12V.

Thanks guys!

You should consider posting your schematic for review. While it is difficult from just a board layout, it appears that you’ve omitted several rather important components which will result in both a quick and possible slow death of the Teensy. What I don’t see:

  1. Proper power supply bypass capacitors on all voltages. Physical distribution/location is important.
  2. Teensy output current limiting resistors in series with the mosfet gates.
  3. Teensy input protection. TVS diodes at a minimum, depending upon the input sources.
  4. Relay coil freewheeling diodes. Very important and necessary.
  5. Relay contact MOV’s or R/C snubbers depending upon load types.
  6. The hole patterns for the relay power contacts appears incorrect.

WattsThat:
You should consider posting your schematic for review. While it is difficult from just a board layout, it appears that you’ve omitted several rather important components which will result in both a quick and possible slow death of the Teensy. What I don’t see:

  1. Proper power supply bypass capacitors on all voltages. Physical distribution/location is important.
  2. Teensy output current limiting resistors in series with the mosfet gates.
  3. Teensy input protection. TVS diodes at a minimum, depending upon the input sources.
  4. Relay coil freewheeling diodes. Very important and necessary.
  5. Relay contact MOV’s or R/C snubbers depending upon load types.
  6. The hole patterns for the relay power contacts appears incorrect.

Wow, thanks for the info. Definitely making me consider some stuff that I hadn't before. Unfortunately, I don't have a schematic, I just started with a pcb layout on easy EDA. I'll see if theres a way I can back generate one, or I'll just have to learn how to make one.

  1. Here is the 5V power supply being used for the teensy and bluetooth: Pololu 5V, 1A Step-Down Voltage Regulator D24V10F5
    And here is the 3.3v one for the humidity sensor, timer, and wifi power (since the teensy 3.3v pin can only output 200ma)
    Pololu 3.3V, 1A Step-Down Voltage Regulator D24V10F3

Both of those are hooked up to a single 12V in, which also goes to the mosfets. Pololu suggests a capacitor if you use above 20v. However, with the potential of voltage spikes from a 220v relay, I suppose we could have a much larger spike. Would a single capacitor over the 12V in be sufficient? What voltage capacitor would be ideal? if I'm using 220v on the relays, should I be looking at a 220v capacitor just in case?

  1. Not sure what the reasoning for this is, wouldn't this drop the 3.3v signal too low to even turn the mosfet on? I do have a 100kohm resistor that will be going between ground and gate though (pull down), so that the signal doesn't float (have had issues running them before where an led would flash on and off randomly without this resistor). Also, if the pwm frequency is 0 (just using digitalWrite(high)), wouldn't the on current be extremely low since the gate capacitors aren't being recharged/discharged much?

  2. Would i still need to worry about this given the info I provided in #1? (assuming I add the capacitor over the 12V input to the regulator) These regulators do have short circuit protection/automatic shutdown as well.

  3. / 5. I haven't heard about these things so I'll have to do some more research on how they would be wired up.

  4. I think they are correct. The two smaller ones on the right are the 12V on/off relay coil and the ones on the left are the mechanical contacts (vise versa for the right side of the board). I added an extra set of power contacts above the trace on the board so that I may attach a 14 guage wire to increase current capacity, as I calculated (using some online calculator) that a 200 mil trace with 1 oz copper could only handle 5 amps or so (and the relays are rated for 30).

Sorry to drag you deeper into the rabbit hole :slight_smile:

Found this guy's example: https://maker.pro/arduino/projects/driving-a-relay-with-an-arduino

It seems to show what you are talking about with the flyback diode in parallel with the relay coil, as well as the resistor in series with the mosfet gate.

Here's a public version of the project if you want a better look in the meantime.
https://easyeda.com/axj2466/atmospheric-controller-public

The origin of any design is a schematic, without it, you have no basis for a pc board layout. Without a schematic, how do you verify your design?

Without a schematic, it's really rather pointless to attempt to discuss proper supply bypassing or most of the other topics that depend upon how things are connected together. As I said in my post, the bulleted points are what I can see missing from the layout. There are probably other issues that are dependent upon the design itself which cannot be gleaned from just a layout.

Other comments in no particular order.

The output current capability of the Teensy 3.5 is 10 ma absolute maximum. Those mosfet gates are very low impedance at turn on. Yes, PWM makes it worse but a 100 ohm resistor in series to limit the inrush current is a few cents at most. Use a resistor today, save a $30 Teensy tomorrow. Your choice.

Switch mode power supplies are a really poor choice for powering analog sensors. Not sure if you're using something like the DHT series but if you are, the noise will can couple to your input signal and it will need to be filtered. It is far easier (and cheaper) to use an analog regulator.