In this guide, we will show you how to wire up MOSFET transistors as a digitally controllable switch, which will enable you to control higher-powered components like solenoids, motors, and Peltiers with a microcontroller.

Transcript

The digital pins of your microcontroller can supply only 3 to 5 volts at maybe a few milliamps, so if we wish to control higher powered components like solenoids, motors or even Peltier modules, things that might need a few amps of current, we're going to need to use one of the greatest innovations born of human ingenuity, the transistor.

Hey everyone, Jaryd here. Today we're going to go over how to use a transistor as a digital switch that will allow us to control these power hungry components with a microcontroller. Now there is a wide range of transistor types to choose from, but for this guide we'll be sticking with an n-channel MOSFET, as the setup we give you is extremely common and easy to use.

But first we're going to need to know a little bit about how transistors work, so let's jump on over to the e-glass real quick. Now MOSFETs can be pretty complicated, but thankfully using one as a switch is pretty simple. So we have three pins on our MOSFET. We have the gate, we got the drain and we have the source. And if we apply a voltage to the gate pin, we allow current to flow between the source and the drain pins. We apply no voltage and we don't allow any current to flow. And this is how we can use it as a digitally controllable switch.

Now this is specific to an n-channel MOSFET, and the exact MOSFET model that we're using, link in the guide below, it likes to turn on and allow current to flow at 3.3 or 5 volts, and at 0 volts it'll stop this process. We call this a logic level MOSFET, as this aligns nicely with the output pins of our microcontroller.

Now there is a specific way that we need to wire up our MOSFET to use it as a switch, so let's break it down into three simple steps. But before we dive into that, Let's just go over our four components. We've got our MOSFET, we've got our power supply, we've got our microcontroller and we have a load. What is a load? It's whatever you're trying to control. It could be a solenoid, it could be a motor, it could be an LED. Whatever you want to control, we're just going to call it a load for now.

Now these steps are in no particular order, but first we'll connect the source pin of our MOSFET to the ground or the negative terminal of our power supply. Then we'll connect an output pin of our microcontroller to the gate pin of our MOSFET. Then we just simply connect the load across the drain pin of the MOSFET to the positive terminal of the power supply.

Now there are two more things that you're going to want to check here or you might run into some issues. So first we want to ensure that our microcontroller and our power supply share a common ground, basic circuitry. Then we're going to want to add something called a pull-down resistor to our output pin here. Long story short, when we set the voltage to high and then low, the actual voltage can float around a bit. So if we add a resistor here to ground, we pull that voltage down to zero when we set it to zero.

That is our circuit all nice and wired up. We set high on this pin here, we allow current to flow and we turn our circuit and our load on. We set it to low, we don't let current flow and it turns off. And that is the beauty of our circuit. We can put anything for our load. A 24 volt motor, a 12 volt LED. As long as our power supply can power it, we just set this pin high and low and we control it like a switch.

Let's start with an easy example. Let's control an LED as our load. Now an LED isn't a very large load and we could very easily power it directly off our microcontroller pins like we're doing so here. But we're going to control it with our MOSFET because it makes for an easy to follow example that we can wire up with just breadboard and jumper wires.

Links to all the parts we're going to be using in the article below.

So first let's populate our breadboard by placing in our microcontroller. We'll be using a Raspberry Pi Pico for our examples, but any microcontroller running 3.3 or 5 volts will do. Then we're going to place our load on the board which is an LED and an accompanying 470 ohm current limiting resistor.

Now for the power supply, we're going to be using the power coming from the USB which we can source from the VBUS pin here at the very top. So we'll connect that as well as the ground pin to the power rails of our breadboard here. And so now we can pretend that these power rails here are our external power supply.

So first step we're going to connect the source pin of the MOSFET to ground. Super easy. Then we'll connect the MOSFET gate pin to GP15 of our microcontroller, just the very bottom one here. We'll also place our pull down resistor and make sure it's connected to ground like so. And then we'll connect our load from the drain pin to the positive terminal of the power supply. And with that we have our MOSFET circuit all nice and wired up.

Now we are about to plug this circuit into a computer and incorrect wiring can damage your microcontroller or computer, so always double check that your wiring is correct.

In our article below we have some test code, so we're going to plug in our Pico. We're going to copy and paste our code into Thonny, hit run, and if everything's working we should have an LED blinking on andOff. If your LED isn't blinking, check your wiring and flip it around because it only works one way.

Now you've probably seen a microcontroller blinking an LED like this before, but there's something special going on here, and that is that the power from our LED is coming from our external power supply, but it is being controlled by the microcontroller pin, and that is all thanks to our transistor.

Now let's scale this up and start controlling something a bit bigger than an LED. This time we can't use just breadboard and jumper wires as we're dealing with a few amps of current, and these are going to get very hot and melt very quickly.

And this brings up something else very important. You need to select a MOSFET capable of handling the load that you're actually trying to switch. The MOSFET we're using in this example is quite beefy and it's rated up to 60 volts and 30 amps, so we're going to have no troubles here and plenty of headroom as we're only dealing with maybe about 5 amps in these examples at 12 volts. We're also going to have more info in the article below about selecting an appropriate MOSFET for switching if you want to check that out.

Now it may be a little hard to tell, but I've gone and wired up the same circuit as before, but this time with some wiring rated for our higher current. We've got our source to ground, we've got our gate to our microcontroller, and we've got our load connected across the positive of our power supply to the drain pin of the MOSFET. We're also this time using an actual external power supply, remembering to ensure that our microcontroller and our power supply share a common ground.

We're also running the exact same code as before, and as you can see our LED load is still blinking.On and off. Now here is where it gets cool. I'm going to stop our code and then I'm going to disconnect our LED. I'm going to adjust our power supply to 12 volts and then I'm going to grab our new load and connect it across the drain pin to the positive of our power supply.

And if I run the exact same code as before, look at that. We've got our light turning on and on just as before. Same code, same everything, completely different load. As long as our power supply is rated for it and our MOSFET can handle it, we can put any load across here and control it with our microcontroller. This is a 60 Watt car headlight that we're controlling with our microcontroller. This is ridiculous.

Here's the same setup on an electromagnet, and we can do the same on a Peltier module. We can also hook up a solenoid. Now we're going to need something called a flywheel diode, and it's for circuit protection when we're using inductive loads like a solenoid. Nothing to do with the MOSFET circuit, we treat this as the load. So connect it across exactly as we did before.

Now we're going to work a motor onto our setup. It is an inductive load, so we're going to need a flywheel diode, but we're going to do something a little bit different now. A MOSFET can switch fast, like really, really fast. So we have some demo code in the guide below that we're going to use, which will set a PWM signal on our microcontroller pin. And our MOSFET can actually switch on and off fast enough that it passes this PWM signal along to the load. And as you can see, our motor is ramping up and down in speed and matching our PWM signal.

And we can also apply this signal to our light bulb, or our LED, or any other load that can be driven with a PWM signal.There you have it. You are now equipped with the ability to use an N-type MOSFET to control a load with a microcontroller, an incredibly powerful tool to have as a maker.

Now, if you make something cool with this circuit, or if you just have some questions, feel free to let us know in our forums. We're all makers, and we're happy to help.

Till next time.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.