It’s a fairly simple project, too: just hook an Arduino Uno up to a motion sensor and an electronic switch called a relay, then plug in your fan, and you’re good to go. If that sounds complicated, don’t worry, we’ll walk you through it. And once you’ve got the hang of it, you’ll be able to apply the concept to lighting or anything else that plugs into the wall. There are, of course, commercial smart home options available, but if you want total control over your system and something you can build upon, this is a great way to implement basic home automation.
What you’ll need:
Arduino UnoArduino Uno power supplyInternet of things power relayPassive infrared (PIR) sensorPre-wired male-to-male and male-to-female breadboard jumper wires (length according to your setup)Portable fanFlathead screwdriverUSB Type-A to USB Type-B cableEnclosure (optional)
Wire the electronics
Set up the Arduino program
At this point, your Arduino has the ability to sense movement and respond, but it has no program to tell it what to do. Let’s fix that. How it works: The first few lines of the code define program variables and data storage locations. You’ll see PIRPin defined as “2” and OutPin defined as “3”—these correspond to where you plugged the PIR sensor and power relay into the Arduino. The void setup() section defines the PIRPin as an input and the OutPin as—you guessed it—the output line connected to the relay switch. While this may be obvious, computers need to be explicitly told what to do. Perhaps the term “smart device” is a bit of a misnomer, as they need an intelligent human to set everything up. The void loop() section, though, is where the real work occurs, as the Arduino loops this code repeatedly. First, it checks in with the motion sensor via PIRState=digitalRead(PIRpin) to see if has sensed any movement. Then, it records the number of milliseconds that have elapsed since the Arduino program began, with currentTime=millis(). How it works: When something triggers the sensor, the Arduino records that moment as triggerTime in Line 23, and compares it with the current time based on conditions in lines 26, 29, and 34. If the difference is less than the delayValue (10 minutes for the purposes of this story), the fan turns on or stays on. If the sensor detects movement during that 10 minutes, it tells the Arduino, and the timer resets. If the difference is higher than the delayValue, the program knows there hasn’t been any movement and turns the fan off.
Connect your fan and stay cool
Optional: Build an enclosure
The setup will work as it is, but you probably don’t want a bunch of loose wires hanging around. There are a plethora of ways to mount your electronics, from Tupperware to a custom wooden box, but I happened to have a plastic electrical enclosure on hand. It has a plastic top that would seem ideal for motion sensing, but I’ve found clear plastic can interfere with infrared light.
What you’ll need:
Drill press (or a handheld drill)1-inch spade bitHot glue gun1/2-inch drill bit (optional)1/4-inch drill bit (optional)
You can just leave the power relay on top of the enclosure, ready to use with your fan or whatever appliance you’re looking to trigger. Make sure the adjustment knobs for sensitivity and on-time for the motion sensor are facing up so you can modify how sensitive your device is and how long it stays on to signal your Arduino. While great with a fan, this programmable switch you’ve created will work with other devices, too. So whether you’re looking to activate some lights, scare burglars who enter your tool-cave with a siren, or simply keep yourself cool, it’s a great gadget to have at your disposal.