Built-in project · no microcontroller

MOSFET switch

A 2N7000 switching an LED from a slide switch, with no gate resistor anywhere: the switch a board pin reaches for. There is no microcontroller in it: the simulator solves the 4 parts as a circuit, so it runs the moment you press Run, and nothing needs compiling.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $5.80. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • A MOSFET as a voltage-controlled switch
  • Gate, drain and source
  • Why a gate must never float
  • Threshold voltage turning the channel on
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Parts: 6

How it works

There is no microcontroller here. The 5 V rail pushes current through a 220 ohm resistor and a red LED, then down through the MOSFET and to ground.

A slide switch controls the MOSFET's gate. In one position the gate sits at 5 V, the channel turns on and the LED lights. In the other position the gate is on ground, the channel is off and nothing flows.

There is no gate resistor because a MOSFET gate takes almost no current. The switch connects the gate to both rails, since a gate left unconnected would float and switch randomly.

Parts list

6 parts, plus the jumper wires. They retail for about $5.80; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω pin 15 V
Slide switch pin 35 V
MOSFET pin SGround
Slide switch pin 1Ground
LED, red pin AResistor, 220 Ω pin 2
MOSFET pin DLED, red pin C
Slide switch pin 2MOSFET pin G

The notes that come with it

There is no microcontroller in this one, so nothing is compiled: the simulator solves the circuit itself. The file in the editor is the notes that come with it.

sketch.ino
// A MOSFET switch.
//
// There is no microcontroller here. Press Run and the simulator solves the
// circuit: the 5 V rail pushes current through the 220 ohm resistor and the
// LED, down through the MOSFET's channel, and back to ground.
//
// Click the slide switch while it runs. In one position the gate sits at 5 V,
// the channel turns on and the LED lights; in the other the gate is on ground,
// the channel is off and nothing flows. Watch the readout under the part: it
// is the drain current, and it is the same current the LED is lit by.
//
// Two things worth noticing. There is no gate resistor, because the gate takes
// no current. This is the whole reason a MOSFET is the switch of choice for
// anything a board pin cannot drive. And the switch is wired across both
// rails, not just to one: a gate with nothing on it floats, and a floating
// gate turns a switch into a radio aerial.
//
// Things to try. Turn the gate supply down by swapping the 5 V symbol for a
// lower voltage and find the threshold, about 1.8 V for a 2N7000; below it
// the LED goes out completely. Or put the MOSFET in the properties panel and
// change the model to an IRF540N, whose on resistance is far lower and whose
// threshold is a good deal higher. It comes in a TO-220 with its legs in
// another order, gate, drain, source, so its legs land in other holes: move
// the three jumpers to match, the same job as on a real breadboard.

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED flickers or lights by itself. This project
    The gate is floating. The slide switch must connect the gate to either 5 V or ground in both positions.
  • The LED does not light at a lower gate voltage. This project
    A MOSFET has a threshold, about 1.8 V for a 2N7000. Below it the channel stays off.
  • A different MOSFET does not work in the same holes. This project
    Some come with legs in another order, such as gate, drain, source. Move the three jumpers to match.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
  • The load stays partly on or flickers when the pin is not driving, such as at boot or in input mode. MOSFET
    A floating gate picks up noise. Add a pull-down of about 10 k from gate to source on an N-channel part, or pull a P-channel gate up to its source voltage.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.