Imagine walking up to a spooky scene at your Halloween party, and just as you get close enough, a dense layer of fog starts rolling out like magic. You can make this happen with an Arduino-based ultrasonic sensor system that triggers your fog machine! The beauty? It’s all automated and reactive, adding an extra eerie vibe when visitors get too close. Below, I’ll walk you through how to replace the stock switch of a Spirit Hallowe’en fog machine with sensors, powered by an Arduino Nano.
What You’ll Need
Hardware:
- Arduino Nano Every (x1)
- Whadda 5V Relay Module (x1)
- HC-SR04 Ultrasonic Sensor Modules (1 to 3, depending on the coverage area)
- Spirit Hallowe’en 400W Low-Lying Fog Machine (or similar with a manual switch)
- Generic USB Battery Backup (5V/2.4A, 5200mAh or higher)
- Female-to-Female Dupont Jumper Wires (minimum 7, maximum 15)
Software:
- Arduino IDE v2.3.2
- NewPing Arduino Library v1.9.7 (Can be installed via the Arduino IDE under “Manage Libraries”)
Steps to Build Your Fog Machine Trigger
Wiring Diagram and Circuit Setup
- Modify the Fog Machine Switch:
- The stock switch of the fog machine has three wires (white, black, and green). Cut these wires.
- Green wire connects to relay C (Common).
- White wire connects to relay NO (Normally Open).
- You can tape off the black wire—it’s not needed.
- Relay Module Wiring:
- Connect Arduino D5 to Relay Signal (S).
- Connect Arduino GND to Relay –.
- Connect Arduino +5V to Relay +.
- Ultrasonic Sensor Wiring:
- Use 1 to 3 ultrasonic sensors for a wide detection range.
- Power each sensor via the Arduino’s analog pins (A0, A1, A2). This is unconventional but works as each sensor draws less than 20mA.
- Trigger/Echo pin combinations for each sensor:
- Sensor 1: D6 and D7.
- Sensor 2: D8 and D9.
- Sensor 3: D10 and D11.
- Ground all sensor GND pins to the Arduino’s GND.
- Power the System:
- Power the Arduino using the USB battery backup.
Uploading the Code
Once you’ve set up the hardware, upload the following code using the Arduino IDE. Ensure your board and COM port are selected correctly in the IDE.
#include <NewPing.h>
const int sonarNum = 3; // Number of HC-SR04 sensors
const int maxDistance = 400; // Max distance to ping for (in cm)
const int sensor1PowerPin = A0;
const int sensor2PowerPin = A1;
const int sensor3PowerPin = A2;
NewPing sonar[sonarNum] = {
NewPing(6, 7, maxDistance),
NewPing(8, 9, maxDistance),
NewPing(10, 11, maxDistance)
};
const int fogPin = 5;
const int led = LED_BUILTIN;
const unsigned long pingTimer = 50;
unsigned long lastPingInterval = 0;
unsigned long lastFogCountdown = 0;
unsigned long fogOnInterval = 5000; // Fog on for 5 seconds
byte state;
int ledState;
int fogPinState;
void setup() {
pinMode(fogPin, OUTPUT);
pinMode(led, OUTPUT);
pinMode(sensor1PowerPin, OUTPUT);
pinMode(sensor2PowerPin, OUTPUT);
pinMode(sensor3PowerPin, OUTPUT);
digitalWrite(sensor1PowerPin, HIGH);
digitalWrite(sensor2PowerPin, HIGH);
digitalWrite(sensor3PowerPin, HIGH);
fogPinState = LOW;
ledState = LOW;
Serial.begin(115200);
state = 0;
}
void loop() {
unsigned long pingInterval = millis();
if (pingInterval – lastPingInterval >= pingTimer) {
lastPingInterval = pingInterval;
for (uint8_t i = 0; i < sonarNum; i++) {
if ((sonar[0].ping_cm() == 0) && (sonar[1].ping_cm() == 0) && (sonar[2].ping_cm() == 0)) {
state = 0;
} else if ((sonar[0].ping_cm() >= 10) || (sonar[1].ping_cm() >= 10) || (sonar[2].ping_cm() >= 10)) {
state = 1;
}
}
}
switch (state) {
case 0:
fogPinState = LOW;
ledState = LOW;
noFog();
break;
case 1:
fogPinState = HIGH;
ledState = HIGH;
deployFog();
break;
default:
fogPinState = LOW;
ledState = LOW;
noFog();
break;
}
}
void deployFog() {
Serial.println(“Fog ON”);
unsigned long thisFogTimer = millis();
while (thisFogTimer + fogOnInterval >= millis()) {
digitalWrite(fogPin, fogPinState);
digitalWrite(led, ledState);
}
Serial.println(“Fog OFF”);
state = 0;
}
void noFog() {
digitalWrite(fogPin, fogPinState);
digitalWrite(led, ledState);
}