Ultrasonic Arduino Presence Fan

by mill70882 in Circuits > Arduino

630 Views, 3 Favorites, 0 Comments

Ultrasonic Arduino Presence Fan

IMG_0514.jpg

An Arduino is a small computer, classified as a microcontroller. While this may not be anywhere near as powerful as the computer you may be thinking of, it is still quite capable. It works by reading electrical inputs and writing to electrical outputs. You tell the microcontroller what to read and write with code. Arduino code is quite similar to the c programming language, one of the world's most used programming languages. We will be exploring the concepts of inputs and outputs with this project.

We will be building a Presence Based Desk Fan. When there is a person close to the fan, like someone sitting at their desk, the Arduino reads this with an Ultrasonic sensor. Using the code we will write, the Arduino will check to see if the person is within the programmed distance of the sensor. If the person is within this distance, the Arduino will send an electrical output to the fan, turning it on. If the person is further than the programmed distance, the Arduino will turn the electrical output to the fan off.

Supplies

IMG_0514.jpg
IMG_0515.jpg
X2dK8-773675334.jpg

For this project, we will need an ultrasonic sensor, an Arduino, a breadboard, and breadboard wires. All of these are provided in your kit. You will also need the Arduino IDE (Integrated Development Environment), and the proper USB cable to connect the Arduino to your computer.

Ultrasonic sensors use the same method to determine distance that a sonar sensor uses, or very similar to echolocation. The sensor emits an ultrasonic frequency, then waits for the sound to reflect off of something and come back to the sensor. The Arduino then calculates the time it takes for this to happen in order to determine the distance.


Working code for this project can be downloaded HERE. Teacher reference only.

Safety

 Arduinos are a wonderful way to learn about code, but they also have some hazards. While they are safe, there are a few precautions we can take.

1.      While the Arduino is low voltage, there is potential of electrical hazards, make sure to unplug power before doing any wiring. Wiring with the power connected can lead to failures and may damage some equipment.

2.      Be sure to be careful when moving and inserting the wiring. The wiring can be pokey, and it is important to use light strength when inserting it into the breadboard.

3.      If anything goes wrong while it is connected to power, the best thing to do is step away, rather than panic and try to fix/unplug it, which can lead to injury.

4. The motor we are using is not very strong, but it is always important to take precaution with moving parts.

Programming Setup

Here is where we will learn to program the Arduino.

  1. The first step is to download and install the IDE (download page).
  2. After installing and opening the IDE, you will see a new file or "sketch" opened. It will have void setup() and void loop() typed into the sketch.
  3. Setup is where the code that only needs to be ran once will go
  4. Loop is where the code will "loop" indefinitely. This is where code that needs to be ran over and over again will go.
  5. Above these two functions is where we will initialize variables. We will set up some variables to make understanding each input and output pin easier.


Initializing Varibles

An Arduino Uno, which is the style of Arduino we are using, has 13 I/O or Input Output pins. They are numbered with 1-13 on the top side of the board. We will tell the Arduino what to do with each pin by simply using its number.

An ultrasonic sensor has 4 electrical pins.

  • Gnd (Connects to ground pin on Arduino)
  • Echo (Pin that receives the echoed ultrasonic sound)
  • Trig (Pin that tells one of the speakers on the sensor to output an ultrasonic sound)
  • Vcc (Connects to 5v pin on Arduino)

We need to initialize the Echo and Trig pins so the Arduino can talk to the sensor. We will also initialize a pin that the Arduino will output to in order to control the fan. We will call these trigPin, echoPin, and fanPin.

Since the pin numbers are integers, we will define these pins using int

You can name these variables anything that makes sense to you, just make sure to use the same variable names across all of your code. Type these above both the setup and loop functions.

int trigPin = 9;
int echoPin = 8;
int fanPin = 10;


We will also need two variables for the math required to calculate the distance. One for the time it takes for the sensor to receive the signal (in microseconds), and one to output the distance calculated in cm. We will call them duration_us and distance_cm. Since these are going to be small/precise numbers, we will declare them as a float variable. These can go right below the int variables.

float duration_us;
float distance_cm;

Setup Function

This is where we will write the code that only needs to run on the Arduino's startup. Everything we write here will go in the setup function.

We will start with defining what kind of pins each of the initialized pins from above are. This is done through setting the pin mode. The types of pin modes are INPUT and OUTPUT. We will have to do this for each pin we are using.

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(fanPin, OUTPUT);
}

Loop Function

This is where we will have the Arduino code loop, meaning the code will keep repeating. Everything we write here will go inside of the loop function.

We will start by telling the ultrasonic sensor to send out an ultrasonic frequency. This can be done with digitalWrite. We will tell the sensor to send out a signal for 10 microseconds.

We will set the trigPin to high, or send a voltage, then wait 10 microseconds, then set the pin back to low, or no voltage.

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

We now need to tell the Arduino to read the signal coming from the echoPin. This signal will be the time it takes for the ultrasonic sound to reflect back to the sensor. We will store this time in the duration_us variable. Since these are precise signals, we will have to ensure the Arduino is not "interrupted" by background activities. We will turn off interrupts using noInterrupts. We will turn these interrupts back on after reading the signal with interrupts.

noInterrupts();
duration_us = pulseIn(echoPin, HIGH);
interrupts();

We now need to calculate how far the ultrasonic signal went. We can do this using the speed of sound. The speed of sound is 343 meters per second, or 0.0343 cm per microsecond. We can get the distance the sound traveled by multiplying the time it took by the speed of the sound. Since the distance to the object is half of the distance of the sound traveled, we will multiply this time by 0.0343/2 or 0.017.

distance_cm = 0.017 * duration_us;

We now need to set up if statements to determine when to turn on the fan, and when to turn it off.

We will compare the distance calculated by the sensor to a distance we want the fan to turn on or off at.

If we want the if loop to continue when the distance is less than 60cm, we can write if(distance_cm<=60). We also need a loop for when the distance is not less than 60cm. For this we can use else.

if(distance_cm<=60){
digitalWrite(fanPin,HIGH);
}
else if(distance_cm>=60){
  digitalWrite(fanPin,LOW);
}

Now we have completed code for the project! Now double check that you have all of the semicolons and curly brackets needed.

Uploading the Code

BoardSelect.png
PortSelect.png
DoneSC.png

Now that we have the code written, we need to upload it to the Arduino.

First we will plug the Arduino into the computer with the provided USB cable. You should see a light or two on the Arduino once it is plugged in.

  1. We will have to tell the IDE what board we are using, and what "Port" the Arduino is connected to on the computer.
  2. Click on the tools tab, hover over boards, hover over Arduino AVR Boards, then click Arduino Uno.
  3. Click on the tools tab, hover over Port, then select the COM Port that says (Arduino Uno) next to it
  4. It should look like (COM X (Arduino Uno).
  5. Now that the IDE knows how to talk to the Arduino, we can upload the code.
  6. First click on the Verify button, it should look like a check mark in the top left.
  7. If any errors show up, check your syntax. This could be a missing semicolon, a typo, or a missing () or {}.
  8. If it says Done Compiling, you are ready to upload the code.
  9. Upload the code with the Upload button next to the Verify button, it should look like an arrow.
  10. If the IDE shows Done Uploading, the code should run on the Arduino!
  11. If any errors appear, check your connection to the Arduino, and check the port and board settings.

Wiring

518c0b34ce395fea62000002-459056941.jpg
IMG_0515.jpg
IMG_0516.jpg
IMG_0522.jpg
IMG_0521.jpg
IMG_0523.jpg
IMG_0524.jpg
Screenshot 2022-12-06 105216.png

Now that we have the code uploaded to the Arduino, we need to wire up the ultrasonic sensor and fan.

Here you will need a breadboard, fan, ultrasonic sensor, and breadboard wires.

Breadboards are connected electrically through rails. You can see how they are connected in the image above.

We will first connect power pins to the breadboard.

  • Make sure the Arduino is unplugged from the computer.
  • First grab a red breadboard wire. Insert one end of the wire into the pin labeled 5v on the Arduino, and the other end into the power rail labeled + on the breadboard.
  • Grab a black wire and insert one end into a pin labeled GND or ground on the Arduino, and the other end into the rail labeled - on the breadboard.
  • Now insert the ultrasonic sensor into the breadboard.
  • We now need to connect the ultrasonic sensor to power.
  • Connect the rail with the sensor's vcc pin to the + rail on the breadboard.
  • Connect the rail with the sensor's GND pin to the - rail on the breadboard.
  • Now we can refer to our code to wire the sensor to the Arduino
int trigPin = 9;
int echoPin = 8;
int fanPin = 10;
  • As seen in the code, we have the trig pin connected to pin 9 on the Arduino.
  • Connect pin 9 to trig on the sensor.
  • We have the echo pin connected to pin 8 on the Arduino.
  • Connect pin 8 to echo on the sensor.
  • Now we need to connect the fan to the Arduino.
  • Connect the black wire on the fan to one of the GND pins on the Arduino or breadboard.
  • Connect the red wire on the fan to pin 10 on the Arduino.
  • That's it for wiring!

Testing

Now that we have a wired and programmed Arduino, we can plug it back in and test our project.

When you first plug it in, try bringing your hand close to the sensor, the fan should start spinning. When you move your hand away, the fan should stop spinning. If it isn't working, try going back to your code and check that everything is written correctly. If it is spinning in the wrong direction, you can simply switch the red and black wires of the fan.

Try inputting different distance values into your if loops in your code, the sensor should trigger the fan at those distances.

Create a Fan Holder

IMG_0518.jpg
IMG_0519.jpg
IMG_0520.jpg

The supplies we used for our fan holder were cardboard and tape. You can use any supplies that you have. Our fan holder is 3 inches tall and an inch in depth. We produced our fan cradle by measuring our motor with a strip of tape. You want your fan cradle to be snug around the motor, so it doesn’t move when the fan is turned on.

Extra Optional Steps

If you have extra time, try programming your own outputs. As an example, you can have LEDs turn on or off at certain distances. In order to use an LED with an Arduino, you will wire the longer leg to one of the I/O pins, and the shorter leg to a resistor (roughly 300 ohms), and the other end of the resistor to ground. Try writing more if loops to have an LED turn on at certain distances, along with the fan.

  • Initialize a pin number.
  • Set that pin number to an output.
  • use digitalWrite to set the pin to HIGH or LOW.


Example code with LEDs (instructor purposes)

Conclusion

After completing the project, the Arduino has taken in an input (the distance from the ultrasonic sensor) compared it to the written code (How far the distance away compares to the output potential) and given an output depending on if the input satisfied that written code. This is only one of the many uses an Arduino has. You can choose whether an Arduino can turn on a fan, buzz a buzzer, blink a light, and much more. Many of the electronics around you are based on a very similar method to this one.