Built-in project · Arduino Uno R3

Solenoid tap on an Arduino Uno

An Uno taps a 12 V solenoid once a second through a MOSFET, with a diode across the coil to catch the kick when it lets go. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

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

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

Solenoid tap · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Switching a load with a MOSFET
  • Why a coil needs a flyback diode
  • Sharing a ground between supplies
  • Pulses and periods with delay
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

The Uno taps a 12 V solenoid once a second. The coil draws 300 mA, fifteen times what a pin can give. So pin 9 only drives the gate of a MOSFET, and the MOSFET carries the current from a separate 12 V supply.

A coil cannot stop its current at once. When the MOSFET turns off, the coil keeps pushing and the drain voltage climbs. A diode across the coil gives that current a path, so the drain stays about one diode drop above the supply.

Each tap is a 120 ms pulse in a 1000 ms period, and the sketch prints a count.

Parts list

5 parts, plus the jumper wires and a bench instrument. The parts retail for about $36; on Mokxi they're free, and so are the instruments.

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
MOSFET pin SArduino Uno R3 pin GND
Bench power supply pin V-Arduino Uno R3 pin GND
MOSFET pin GArduino Uno R3 pin 9
Diode pin AMOSFET pin D
Solenoid pin 2MOSFET pin D
Solenoid pin 1Diode pin C
Bench power supply pin V+Diode pin C

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Solenoid tap: an Uno taps a 12 V push-pull solenoid once a second through
// a MOSFET, with a diode across the coil to catch the kick.
//
//   solenoid  one lead to the 12 V supply, the other to the MOSFET's drain
//   MOSFET    IRLZ44N: gate -> pin 9, source -> GND
//   diode     1N4007 across the coil: cathode to 12 V, anode to the drain
//   supply    12 V, its - to GND, shared with the Uno
//
// The coil draws 300 mA, fifteen times what a pin can give, so the pin only
// drives the MOSFET's gate and the MOSFET carries the current.
//
// The diode is the part people leave out. A coil's current cannot stop at
// once: when the MOSFET turns off, the coil keeps pushing current and the
// drain voltage climbs until something conducts. Without the diode that is
// the MOSFET itself, at well over its 55 V rating. With it, the current goes
// around the diode and the coil instead, and the drain sits one diode drop
// above the supply while the current dies away.
//
// Things to try. Watch the plunger: it moves a few milliseconds after the pin
// goes high, while the current builds, and takes its travel time to seat.
// Put a multimeter on the drain and delete the diode: run it again and see
// how high the drain goes.

const int GATE_PIN = 9;
const unsigned long PULSE_MS = 120;
const unsigned long PERIOD_MS = 1000;

unsigned long taps = 0;

void setup() {
  Serial.begin(9600);
  pinMode(GATE_PIN, OUTPUT);
  digitalWrite(GATE_PIN, LOW);
  Serial.println("Solenoid ready");
}

void loop() {
  digitalWrite(GATE_PIN, HIGH);
  taps++;
  Serial.print("Tap ");
  Serial.println(taps);
  delay(PULSE_MS);
  digitalWrite(GATE_PIN, LOW);
  delay(PERIOD_MS - PULSE_MS);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts serial at 9600 baud, makes GATE_PIN an output and holds it LOW so the solenoid starts off. It prints that it is ready.

void setup() {
  Serial.begin(9600);
  pinMode(GATE_PIN, OUTPUT);
  digitalWrite(GATE_PIN, LOW);
  Serial.println("Solenoid ready");
}

loop sketch.ino

Sets GATE_PIN HIGH, adds one to taps and prints the count. After PULSE_MS of 120 ms it sets the pin LOW. It then waits the rest of PERIOD_MS, 880 ms, so the whole cycle is one second.

void loop() {
  digitalWrite(GATE_PIN, HIGH);
  taps++;
  Serial.print("Tap ");
  Serial.println(taps);
  delay(PULSE_MS);
  digitalWrite(GATE_PIN, LOW);
  delay(PERIOD_MS - PULSE_MS);
}

Troubleshooting

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

  • The solenoid never moves This project
    The Uno's GND and the supply's V- must be joined. The MOSFET's source must go to this ground.
  • The MOSFET gets hot or fails This project
    The diode across the coil is missing or backward. Its cathode goes to 12 V.
  • 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.
  • The N-channel load is always on or never turns on. Source, gate, and drain legs were mixed up. MOSFET
    Use the N-channel as a low-side switch: load to the supply, drain to the load, source to ground. Check your datasheet pinout, since 2N7000 and BS250 differ. Share the grounds.
  • The diode is in backwards, so nothing flows, or a flyback diode conducts all the time. Current flows from the anode to the cathode, the end with the stripe. Diode
    Put the striped end toward the more positive side for a series diode. For flyback across a coil, the stripe goes to the positive supply side of the coil.
  • A diode in series drops about 0.7 V (silicon) and 0.3 V (Schottky), so the output is lower than expected. A 1N4148 is also too small for big currents. Diode
    Count the drop in your voltage budget. Use a 1N4007 or similar for amps, and a Schottky when you need a small drop.
  • The board resets, or the pin burns out, when the solenoid is wired straight to a pin. Solenoid
    A solenoid draws far too much for a pin. Switch it with a MOSFET or transistor from its own supply (such as 12 V), with the grounds joined.
  • The transistor or the controller fails after some switching. Solenoid
    A coil produces a voltage spike when current stops. Put a diode across the coil, cathode on the supply side, such as a 1N4007 or 1N5819, to absorb the kick.

Change it and keep it

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