Built-in project · Arduino Uno R3

Fog machine trigger on an Arduino Uno

A PIR fires a relay across a fog machine’s remote button for a short burst, then a cooldown with a red lamp lets the fog drift and the heater recover. The whole build, an Arduino Uno and 8 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 About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

Build it as the Halloween Prop Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $66, free on Mokxi.

Fog machine trigger · 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

  • A state machine with READY, FOGGING and COOLING
  • Timing with millis instead of waiting
  • Using a relay to press a button safely
  • Motion sensing with a PIR
  • Why cooldowns protect hardware
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 10

How it works

A PIR motion sensor on pin 2 watches for someone approaching. When it goes HIGH, the sketch closes a relay on pin 7 for 2.5 seconds. The relay stands in for pressing a fog machine's remote button. Here the relay's contacts light a white LED.

After the burst, a 20 second cooldown starts and the red LED turns on. When it ends, the green ready LED comes back and the sensor can fire again. The cooldown counts from the end of the burst.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

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
Relay module pin INArduino Uno R3 pin 7
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 4
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 3
Motion sensor pin OUTArduino Uno R3 pin 2
Motion sensor pin VCCArduino Uno R3 pin 5V
Relay module pin VCCArduino Uno R3 pin 5V
Relay module pin COMArduino Uno R3 pin 5V
LED, green pin CArduino Uno R3 pin GND
LED, red pin CArduino Uno R3 pin GND
LED, white pin CArduino Uno R3 pin GND
Motion sensor pin GNDArduino Uno R3 pin GND
Relay module pin GNDArduino Uno R3 pin GND
LED, green pin AResistor, 220 Ω (1) pin 2
LED, red pin AResistor, 220 Ω (2) pin 2
Relay module pin NOResistor, 220 Ω (3) pin 1
LED, white pin AResistor, 220 Ω (3) pin 2

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
// Fog machine trigger: a PIR sees someone coming up the path and a relay
// presses the fog machine's remote button for a short burst. Then a cooldown
// keeps it from firing again until the fog has had time to drift and the
// machine has had time to heat back up.
//
//   PIR         VCC -> 5 V   GND -> GND   OUT -> pin 2
//   relay       VCC -> 5 V   GND -> GND   IN -> pin 7   (high pulls it in)
//               COM and NO go across the fog machine's remote button
//   ready LED   green, pin 4 through 220 ohms to GND
//   cooling LED red, pin 3 through 220 ohms to GND
//
// On the canvas the relay's contacts switch the 5 V rail to a white LED. That
// LED stands in for the fog machine's remote: when it lights, the contacts are
// closed and the machine would be pushing fog.
//
// SAFETY. Do not switch the fog machine's mains power with this relay, and do
// not open the machine. Most fog machines come with a wired remote that is
// just a low-voltage push button: wire the relay's COM and NO across that
// button (or across a spare remote's button) and the mains side never comes
// near the Arduino. Use a relay module rated for the job, with the coil
// driven by its own transistor, as these blue modules are. Fog fluid on a hot
// heater is the whole trick, so keep the machine where nobody can touch the
// nozzle, never leave it running unattended, keep fog away from smoke alarms
// you cannot switch off, and keep it away from anyone who has asthma.
//
// Most machines also need to stay plugged in to keep their heater hot, and
// will not fog while it recovers. If yours has a READY light, set COOLDOWN_MS
// to at least the time that light takes to come back after a burst.
//
// Things to try. Click the PIR. Make the burst shorter for a tabletop prop or
// longer for a whole yard. Add a pushbutton on pin 8 as a manual "fog now"
// that still honors the cooldown.

const int PIR_PIN = 2;
const int RELAY_PIN = 7;
const int READY_LED = 4;
const int COOL_LED = 3;

const unsigned long BURST_MS = 2500;      // how long the button is held
const unsigned long COOLDOWN_MS = 20000;  // before it may fire again

enum State { READY, FOGGING, COOLING };
State state = READY;
unsigned long since = 0;
unsigned int bursts = 0;

void show(State s) {
  digitalWrite(READY_LED, s == READY ? HIGH : LOW);
  digitalWrite(COOL_LED, s == READY ? LOW : HIGH);
  digitalWrite(RELAY_PIN, s == FOGGING ? HIGH : LOW);
}

void setup() {
  Serial.begin(9600);
  pinMode(PIR_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(READY_LED, OUTPUT);
  pinMode(COOL_LED, OUTPUT);
  show(READY);
  Serial.println("Fog trigger ready");
}

void loop() {
  unsigned long now = millis();

  switch (state) {
    case READY:
      if (digitalRead(PIR_PIN) == HIGH) {
        state = FOGGING;
        since = now;
        bursts++;
        show(state);
        Serial.print("Fog burst ");
        Serial.println(bursts);
      }
      break;

    case FOGGING:
      if (now - since >= BURST_MS) {
        state = COOLING;
        since = now;
        show(state);
        Serial.println("Cooling down");
      }
      break;

    case COOLING:
      if (now - since >= COOLDOWN_MS) {
        state = READY;
        show(state);
        Serial.println("Ready");
      }
      break;
  }
}

Code walkthrough

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

show sketch.ino

Sets all three outputs for a state in one place. The green LED is on only in READY. The red LED is on in the other two states. The relay pin is HIGH only while fogging.

void show(State s) {
  digitalWrite(READY_LED, s == READY ? HIGH : LOW);
  digitalWrite(COOL_LED, s == READY ? LOW : HIGH);
  digitalWrite(RELAY_PIN, s == FOGGING ? HIGH : LOW);
}

loop sketch.ino

Runs the state machine. In READY it waits for the PIR, then records the time and starts a burst. In FOGGING it waits BURST_MS. In COOLING it waits COOLDOWN_MS. Each step uses millis, so nothing blocks.

void loop() {
  unsigned long now = millis();

  switch (state) {
    case READY:
      if (digitalRead(PIR_PIN) == HIGH) {
        state = FOGGING;
        since = now;
        bursts++;
        show(state);
        Serial.print("Fog burst ");
        Serial.println(bursts);
      }
      break;

    case FOGGING:
      if (now - since >= BURST_MS) {
        state = COOLING;
        since = now;
        show(state);
        Serial.println("Cooling down");
      }
      break;

    case COOLING:
      if (now - since >= COOLDOWN_MS) {
        state = READY;
        show(state);
  // ... 5 more lines in the editor

Troubleshooting

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

  • It never fires. This project
    Check the PIR's OUT goes to pin 2 and that it has 5 V and GND.
  • It fires again too soon. This project
    Raise COOLDOWN_MS. For a real machine, make it at least as long as its READY light takes to return.
  • The white LED stays off. This project
    The relay's COM goes to 5 V and NO goes through the resistor to the LED. IN goes to pin 7.
  • 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 sensor reads high for ages after power up. Motion sensor
    A real PIR needs 30 to 60 seconds to settle. Ignore its output during that time, and keep still in front of it until the delay passes.

Change it and keep it

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