12 V lamp dimmer with a MOSFET module on an Arduino Uno
A knob on A0 sets the PWM on pin 9, and a D4184 MOSFET module dims a 12 V lamp on a wall adapter with it: the pin switches the gates, not the lamp. 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.
Beginner Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 12 V lamp dimmer: a potentiometer on A0 sets the PWM on pin 9, and a D4184
// MOSFET module switches a 12 V lamp from a wall adapter with it.
//
// A0 the potentiometer's wiper (its ends on 5 V and GND)
// 9 the D4184 module's TRIG, and the Uno's GND to the module's GND
// 12 V the adapter's jack into VIN+ and VIN-, the lamp on OUT+ and OUT-
//
// A pin can give 20 mA at 5 V. The lamp wants 100 mA at 12 V, so the pin
// only drives the gates of the module's two AOD4184A MOSFETs, which take
// almost no current, and the MOSFETs switch the lamp's current from the
// adapter. The AOD4184A is a logic-level part: 5 V on its gate turns it fully
// on, about 10 milliohms. An IRF520 board, sold for the same job, needs 10 V
// for that and is only half on from a 5 V pin.
//
// analogWrite() does not make a lower voltage. It switches the pin fully on
// and fully off about 490 times a second, and the share of each cycle it is on
// (the duty) is what the knob sets: 0 to 255 for 0 to 100 %. The lamp's
// filament cannot heat and cool that fast, so it glows at the average power.
// The same works for a 12 V LED strip, which flickers too fast to see.
//
// Press Run, then turn the knob.
const int KNOB_PIN = A0;
const int MOSFET_PIN = 9; // a PWM pin: 3, 5, 6, 9, 10 or 11 on an Uno
int lastDuty = -100; // so the first reading is printed
void setup() {
pinMode(MOSFET_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("12 V lamp dimmer");
}
void loop() {
int knob = analogRead(KNOB_PIN); // 0 to 1023
int duty = knob / 4; // 0 to 255
analogWrite(MOSFET_PIN, duty);
// Print only when the knob has moved a step or more.
if (abs(duty - lastDuty) >= 3) {
lastDuty = duty;
Serial.print("duty ");
Serial.print(duty);
Serial.print(" of 255, ");
Serial.print(duty * 100L / 255);
Serial.println(" %");
}
delay(20);
}
Parts list
5 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
How it is wired
9 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; D4184 dual MOSFET switch module pin GND
- Arduino Uno R3 pin 9; D4184 dual MOSFET switch module pin TRIG
- Arduino Uno R3 pin 5V; Potentiometer pin 3
- Ground: Arduino Uno R3 pin GND; Potentiometer pin 1
- Arduino Uno R3 pin A0; Potentiometer pin 2
- D4184 dual MOSFET switch module pin VIN+; DC jack and adapter pin V+
- D4184 dual MOSFET switch module pin VIN-; DC jack and adapter pin V-
- D4184 dual MOSFET switch module pin OUT+; Filament lamp pin 1
- D4184 dual MOSFET switch module pin OUT-; Filament lamp pin 2
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.