Ultrasonic Motion Sensing Fan

by KBSAUCE in Circuits > Sensors

28 Views, 1 Favorites, 0 Comments

Ultrasonic Motion Sensing Fan

Screenshot 2025-06-12 224336.png
Screenshot 2025-06-12 211437.png

Hello everyone! This is my final project for my tech course, and I chose to do a fan that can be turned on and off with a wave of your hand! Now you can cool yourself down without needing to press a single button!

Supplies

Screenshot 2025-06-12 225312.png

Here is a list of the supplies you'll need:

  1. 1x Solderless Breadboard (Larger one is recommended)
  2. 1x Arduino UNO
  3. 1x Ultrasonic Distance Sensor (HC-SR04)
  4. 1x 3- 6V DC Motor
  5. 1x NPN Transistor
  6. 1x 220 Ohm Resistor
  7. Jumper Wires (I used 11)

Breadboard Diagram (Tinkercad Circuit)

Screenshot 2025-06-12 220422.png

The wiring is relatively simple. Let's start with the ultrasonic sensor.

We will first connect the Vcc (Power) and GND (Ground) to the positive and negative holes on the breadboard, respectively. Then we connect TRIG (What produces an ultrasonic pulse) to the digital pin 3 on the Arduino. After that, we connect the ECHO (What senses the reflected pulse from the environment) to the digital pin 2.

Now let's wire the motor.

Motors typically need transistors to help limit power and prevent the motor from drawing too much power from the power source. So let's start by placing an NPN transistor anywhere on the breadboard (Preferably away from the ultrasonic sensor to make it easier to see). Ensure the transistor's flat side faces you when you follow the next instructions. We can start by connecting the Collector pin (Leftmost pin) to a negative hole on the breadboard. Then place a 220-ohm resistor near the transistor. Connect one end of the resistor to the digital pin 5 of the Arduino and the other to the Base pin (Middle pin) on the transistor. Then connect the negative wire of the motor to the Emitter pin (Rightmost pin) on the transistor. Afterwards, you can connect the positive wire of the motor to a positive hole on the breadboard.

Finally, you can connect the 5V and GND (On the POWER section) pins on the Arduino to the positive and negative holes on the breadboard, respectively, to power the circuit. You should then add wires connecting the positive and negative holes on the top and bottom of the breadboard.

Now your circuit is wired!

Build the Circuit

Screenshot 2025-06-12 222338.png

Just build it.

Code

I couldn't attach it so here is the code for the circuit. There are comments in the code to explain how it works:

// Define pins for the ultrasonic sensor and motor

int trig = 3;

int echo = 2;

int motor = 5;


// Track whether the fan is currently on or off. Bool is just a true or false variable

bool fanState = false;

// Track if an object was just detected so it doesn't toggle repeatedly

bool objectPreviouslyDetected = false;


void setup() {

// Set the pin modes

pinMode(trig, OUTPUT);

pinMode(echo, INPUT);

pinMode(motor, OUTPUT);


// Start the serial monitor for debugging

Serial.begin(9600);

}


// Store the last distance measured and count how many times the object is close to the sensor

float previousInches = 999;

int stableCount = 0;


void loop() {

long duration;

float inches;


// Send a short pulse to trigger the ultrasonic sensor

digitalWrite(trig, LOW);

delayMicroseconds(2);

digitalWrite(trig, HIGH);

delayMicroseconds(10);

digitalWrite(trig, LOW);


// Read how long it takes for the echo to return (To measure distance)

duration = pulseIn(echo, HIGH, 20000); // timeout after 20ms

// Convert duration to inches (if no reading, set to 999 which basically just means nothing is detected)

inches = (duration == 0) ? 999 : duration / 148.0;


// If the object is less than 2 inches away for multiple loops, count it as a detection

if (inches < 2 && previousInches < 2) {

stableCount++;

} else {

stableCount = 0; // reset if object isn't close anymore

}


// If object is close for 3 loops in a row and not already detected:

if (stableCount >= 3 && !objectPreviouslyDetected) {

// Toggle the fan on/off

fanState = !fanState;

digitalWrite(motor, fanState ? HIGH : LOW);

Serial.println(fanState ? "Fan ON" : "Fan OFF");


objectPreviouslyDetected = true; // prevent repeated toggles

stableCount = 0; // reset count

}


// Reset detection flag when object is not close anymore

if (inches >= 2) {

objectPreviouslyDetected = false;

}


// Save this reading for the next loop to prevent random toggles

previousInches = inches;


// Wait a short time before looping again

delay(100);

}

Turning It Into a Fan

Screenshot 2025-06-12 224329.png
Screenshot 2025-06-12 224336.png
Screenshot 2025-06-12 224345.png

You can definitely make yours look a hell of a lot better than mine. But generally, you just want to build around your breadboard. I used a rectangular box, as that was easy and convenient. The breadboard and Arduino are facing each other and on opposite walls (If that makes sense). The motor has a small hole for it at the top where it can stick out. You can also make a bigger propeller, but it will most likely spin a little slower due to weight (However, if you have a more powerful motor then go for it)

I hot-glued everything together as it is an easy way to glue things together, and it isn't completely permanent (So if you mess up, you can redo it by just ripping it apart). I also wired the wires to the breadboard and Arduino, which isn't recommended as it can damage your components, but it does stop the wires from unplugging or becoming loose.

IMPORTANT:

One more thing to mention when making your fan, make sure to carve out holes in your box for your ultrasonic sensor and your Arduino wire that connects to your computer (As shown in the images), as those are vital for your fan to work.

Testing It Out!

Here is a clip of my fan in action. Anyways, I wish you the best of luck when creating yours! (Note: Turning the fan on will be pretty easy, but when turning it off, you will have to hold your hand really close to the sensor for a few seconds until it turns off)

https://drive.google.com/file/d/16ipQtS6VOTxWk_-Pqn8g0LkPC8ls6beb/view?usp=sharing (PDSB only, sorry)

Have fun!,

Krishna