An Easy to Understand Over-engineered Fan

by 728055 in Circuits > Arduino

148 Views, 0 Favorites, 0 Comments

An Easy to Understand Over-engineered Fan

Screen Shot 2023-01-25 at 10.21.35 PM.png

This instructable explains and teaches the use of my project, how to wire it, how to code it, as well as a brief description of the purpose of both the code and materials used. To begin with, the use is motors to spin the blades which are controlled by a potentiometer, an infrared receiver (IR) informs the direction which way for it to move or to stop, and an infrared sensor module that reads the rotations per minute of the propeller. Ultrasonic distance sensor that allows the windmill to become active once someone is within a 10 inch radius. This device will completely turn on through the use of a slide switch, and to identify that the slide switch is on there will be an LED connected to the slide switch that turns on when the slide switch moves to the rights, and turns off when the slide switch moves to the left. There are three purposes for the device, the first being the device would be to act as a personal fan for an individual that only turns on when someone is near, thus saving electricity and in case an individual forgets to turn it off the device will turn off automatically. The second being that the infrared receiver module will read how fast or slow the fan is going, as a result informing the user. The third is that the potentiometer will allow the user to adjust the speed of the fan.

(The device, which is an IR receiver, that is connected to pin two is supposed to be an infrared sensor module, unfortunately Tinkercad does not have an infrared sensor module)

Supplies

image.png

Brief Explanation of Each Components Use in General and in the Build

image.jpeg
  • Ultrasonic distance sensor

What it does - The ultrasonic distance sensor may sound intimidating, however the function is to simply emit sound waves, which can be heard because the frequency is to high for humans, and once these sound waves hit an object they bounce off that object, going back into the ultrasonic distance sensor. We are able to calculate the distance through the time it takes for the sound waves to be reflected back into the ultrasonic distance sensor.

Purpose in the build - The purpose in the build is to notify the device if someone is within the 10 inches range, thus allowing it to turn on.

  • Motor 

What it does - A motor is a machine that is able to convert electricity given into to it to rotate a small cylinder known as an axel.

Purpose in the build - The purpose of the motor in the build is to rotate the blades of the fan.

  • LED 

What it does - LEDs which are also known as light-emitting diode are objects that emit light in different colours depending on the colouring of the LED. They only function when there is current flowing though it.

Purpose in the build - As mentioned in the "What it does" section it only turns on when a current is flowing through it, therefore it acts as a visual indicator for the device being on or off.

  • Infrared sensor (IR) module 

What it does - Infrared sensor (IR) module are sensors that sends the device an electrical signal when it detects an object, through the use of infrared wavelengths, which is a wavelength on the light spectrum that cannot be seen by humans.

Purpose in the build - Due to its ability to detect an object, it is also able to be used in the means of detecting how many times it sees an object's rotations per minute, therefore allowing us to know the RPM of the fan, in turn knowing its speed.

  • Infrared receiver (IR) 

What it does - An infrared receiver (IR) (going forward it will be written as IR receiver) performs the task of receiving infrared signals sent from a remote.

Purpose in the build - The purpose in the build is to receive the signals sent from the remote, as a result allowing the user to use the remote to control whether the fan goes forward, backward, or stops.

  • Remote

What it does - A remote is a device that is able to send different infrared signals to a receiver, each button has a different wavelength, in turn having a different digital signal.

Purpose in the build - The purpose in the build is for it to send the signals to the receiver, so a user is able to control the direction of the fan or to stop it from a convenient distance.

  • Slide Switch 

What it does - A slide switch is the median for an individual to turn on a device or off a device physically, through allowing or preventing a current to go through it.

Purpose in the build - The purpose in the build is to allow an individual to turn on or off the device physically.

  • Potentiometer

What it does - Potentiometer is able to add or decrease resistance, through the act of turning the pin on the top, as a result adjusting the amount of current a device gets.

Purpose in the build - The purpose in the build is to adjust the speed of the motor, meaning the intensity of the fan can be adjusted by a person.

  • Capacitor 

What it does - Capacitors store the electrical energy that passes through them, and once the energy is stored it is able to release it.

Purpose in the build - The purpose in the build is to control the surges in voltage, as a result making it more smooth for the motor to be spinning in increased speed, through the act of increasing the potentiometer.

  • Resistor 

What it does - Resistors are used to regulate or limit the flow of current a device is receiving.

Purpose in the build - The purpose in the build is to limit the flow of current to the LED, so the LED won't receive enough electricity to fry it, meaning the light source inside of the LED does not work anymore.

  • H-bridge

What it does - A H-bridge are used to make it simpler and easier to control the intensity and direction, depends on the electrical device it is controlling.

Purpose in the build - Make it easier to control the speed and direction of the motor, in turn making it easier to control the fan.

Wiring

Screen Shot 2023-01-25 at 10.21.35 PM.png

Connecting power and ground:

  1. Place the slide switch near the top of the breadboard
  2. Connect a red wire from the 5 V pin to the terminal one of the slide switch
  3. Connect the common of the slide switch to the horizontal row that has a positive symbol at the bottom using a red wire
  4. Connect terminal two to horizontal row that has a negative symbol using a black wire
  5. Connect a black wire to the GND on the arduino to the horizontal row with a negative symbol using a black wire at the top of the breadboard
  6. Connect the 220 Ω from the same row as the slide switch that has a negative charge
  7. Connect the green from the same row as the slide switch that has a positive charge
  8. Add an LED so the side that has a bent piece of metal is on the row of the green wire and the straight piece of metal is on the row with the resistor
  9. Add a red wire from the positive symbol on to right to the one on the left at the bottom of the breadboard
  10. Add a black wire from the negative symbol on to right to the one on the left at the bottom of the breadboard


Connecting potentiometer:

  1. Place a potentiometer near the top of the breadboard
  2. Connect a red wire from horizontal row that has a positive symbol to the row that the row of the first pin of potentiometer is on
  3. Connect a black wire from horizontal row that has a negative symbol to the row that the row of the third pin of potentiometer is on
  4. Connect a green wire from pin A0 on the arduino, which is located on the left side, row of the second pin of potentiometer is on


Connecting the motor, H-bridge, and capacitor:

  1. Place the H-bridge near the middle of the circuit, make sure the H-bridge has the semi-circle that should either be at the top or bottom facing the direction towards the slide switch, seen in the picture
  2. Connect a red wire from the horizontal row that has a positive symbol to the row that the first pin on the right is on
  3. Connect a red wire from the horizontal row that has a positive symbol to the row that the last pin on the left is on
  4. Connect a black wire from the horizontal row that has a negative symbol to the line of the pins that are the 4th and 5th rows from the top of the H-bridge
  5. Connect a green wire at the very top row of the H-bridge on the left side to pin 5
  6. Connect a green wire beneath the one just place and connect it to pin 4
  7. Connect a green wire on the second last row on the left side of the H-bridge to pin 3
  8. Connect the black wire that is coming from the motor to the rows of the third pin on the H-bridge on the left side
  9. Connect the red wire that is coming from the motor to the rows of the sixth pin on the H-bridge
  10. Add a capacitor, so that it is in between the black and red wires coming from the motor and the H-bridge


Connecting ultrasonic distance sensor:

  1. Place the ultrasonic distance sensor on the top of the breadboard
  2. Connect a red wire from horizontal row that has a positive symbol to the column that the first pin of ultrasonic distance sensor is on
  3. Connect a black wire from horizontal row that has a negative symbol to the column that the fourth pin of ultrasonic distance sensor is on
  4. Connect a green wire from pin 11 on the arduino to the row of the second pin of ultrasonic distance sensor is on, which states "trig"
  5. Connect a green wire from pin 12 on the arudino to the row of the third pin of ultrasonic distance sensor is on, which states "echo"


Connecting IR receiver:

  1. Place the IR receiver on the middle of the breadboard
  2. Connect a green wire from pin 13 on the arudino to the row of the first pin of IR sensor is on
  3. Connect a black wire from horizontal row that has a negative symbol to the row that the second pin of IR sensor is on
  4. Connect a red wire from horizontal row that has a positive symbol to the row that the first pin of IR sensor is on


Connecting IR sensor module:

  1. Place the IR sensor module on the bottom of the breadboard
  2. Connect a green wire from pin 2 on the arudino to the row of the first pin of IR sensor module is on
  3. Connect a black wire from horizontal row that has a negative symbol to the row that the second pin of IR sensor module is on
  4. Connect a red wire from horizontal row that has a positive symbol to the row that the first pin of IR sensor module is on

Coding

Screen Shot 2023-01-26 at 2.56.16 AM.png
Screen Shot 2023-01-26 at 2.56.38 AM.png
Screen Shot 2023-01-26 at 2.56.54 AM.png

(Do not add the comma or and at the end, commas are meant for the sake of grammar)

Initializing the variables:

To begin with we will be initializing our variables means to give a name to a variable so the computer knows to what you are referring to. Firstly, the variables const int controlPin1 = 3;, const int controlPin2 = 4;, const int enablePin = 5;, and const int potPin = A0;. These are the variables use the initialize the functions for the H-bridge and controlling the logic of the motor, meaning if it goes forward, backwards, or stops. Next, int motorSpeed = 0; is initializing the speed of the motor. Followingly, int recvPin = 13; is the initialization of the function of receiving the signals sent by the remote. Subsequently, float value = 0;, float rev = 0:, int rpm;, int oldtime = 0;, and time int time; initializes the variables for calculating RPM and displaying them to the user. Lastly, int trig = 12; and int echo = 11; initialize the variables for the ultrasonic sensor to work, and long duration, inches; is used to calculate the distance in inches. Make sure #include <IRremote.h> is located at the top of the initialization, as well as downloading the file needed to use the IR remote (the file is located at the bottom.


void setup:

Now that we have initialized are variables, we need to perform our void setup, which runs the code placed in here once at the beginning of our program, as well as stating the pin modes, this means if they are either a input when we give something to the computer and output means giving something to us. Firstly, pinMode(controlPin1,OUTPUT);, pinMode(controlPin2,OUTPUT);, pinMode(enablePin,OUTPUT);, digitalWrite(enablePin,LOW);, and pinMode(13,INPUT); is used to provide the pin modes to the code related to the H-bridge and motor. Followingly, Serial.begin(9600); and irrecv.enableIRIn(); is used to receive the infrared signals sent by the remote and display them using the serial port. Next, attachInterrupt(0,isr,RISING); relates to the function of measuring the RPM of the fan. Subsequently, pinMode(trig, OUTPUT); and pinMode(echo, INPUT); is the code that informs the ultrasonic distance sensor to function, and tells the computer the time it took for the sound waves to be received.


void forward, void backwards, and void stopMotors:

These sets of void allow the computer to know what to do when these phrases are said in the void loop. As one may guess by the name they function to either make the motors spin forward, backwards, or stop them from moving. The code for forwar is digitalWrite(controlPin1,HIGH); and digitalWrite(controlPin2,LOW);. The code for backward is digitalWrite(controlPin1,LOW); and digitalWrite(controlPin2,HIGH);. The code for the motors to stop is digitalWrite(controlPin1,LOW); and digitalWrite(controlPin2,LOW);. Furthermore, when the term HIGH is used it means that the device is on while LOW means that the device is off.


void isr:

This void allows the computer to know what to do when these phrases are said in the void loop. It functions to interrupt the service routine. The code is rev++;.


void motorEnable()

This void allows the computer to know what to do when these phrases are said in the void loop. It functions to turn the adjust the speed of the motor using the potentiometer, and send the information to the H-bridge. The code is motorSpeed = analogRead(potPin)/4; and analogWrite(enablePin,motorSpeed);.


void checkDistance:

This void allows the computer to know what to do when these phrases are said in the void loop. It functions to turn the ability of sending the sound waves on, receiving them, performing the calculations for converting the time into inches, and displaying them on serial port. The code is digitalWrite(trig,LOW);, delayMicroseconds (2);, digitalWrite(trig, HIGH);, delayMicroseconds(10);, digitalWrite(trig,LOW);, duration = pulseIn (echo, HIGH);, inches = duration /74/2;, and Serial.println(inches);.


void loop:

The void loop functions to repeat the code that is within it, turn the motors on and in what direction based on the distance and remote signals being sent, calculating RPM, displaying RPM, and displays the signals received from the remote. The part the turns the motors on and in what direction based on the distance and remote signals being sent is if((results.value == 0xC1AA0DF2)&&(inches<10)){ foward(); }, if(inches>10) stopMotors(); if((results.value == 0xC1AA4DB2)&&(inches<10)) { backward(); }, if(inches>10) stopMotors();, if((results.value == 0xC1AAA15E)&&(inches<10)){ stopMotors(); } if(inches>10) stopMotors();. The part that calculate RPM and displays RPM is detachInterrupt(0);, time=millis()-oldtime;,  rpm=(rev/time)*60000/3;, oldtime=millis();, rev=0;, attachInterrupt(0,isr,RISING);, and Serial.println(rpm);. Next, to code to display the signals received is if (irrecv.decode(&results)), { Serial.print("irCode: ");, Serial.println(results.value, HEX);, irrecv.resume(); }. Furthermore, the make sure to add checkDistance(); and motorEnable(); so they are performed during the loop.


In a easier way to see and copy the code is:

#include <IRremote.h>

const int controlPin1 = 3;

const int controlPin2 = 4;

const int enablePin = 5;

const int potPin = A0;


int motorSpeed = 0;

int recvPin = 13;


IRrecv irrecv(recvPin);

decode_results results; 


float value = 0;

float rev = 0;

int rpm;

int oldtime = 0;

int time; 


int trig = 12;

int echo = 11;

  

long duration, inches;


 void setup()

{

 pinMode(controlPin1,OUTPUT);

 pinMode(controlPin2,OUTPUT);

 pinMode(enablePin,OUTPUT); 

  

 digitalWrite(enablePin,LOW); 

  

 pinMode(13,INPUT); 

  

 Serial.begin(9600); 

 irrecv.enableIRIn(); 

  

 attachInterrupt(0,isr,RISING);

  

 pinMode(trig, OUTPUT);

 pinMode(echo, INPUT);

}


 void foward()

{

 digitalWrite(controlPin1,HIGH);

 digitalWrite(controlPin2,LOW);

}


 void backward()

{

 digitalWrite(controlPin1,LOW);

 digitalWrite(controlPin2,HIGH);

}


 void stopMotors()

{

 digitalWrite(controlPin1,LOW);

 digitalWrite(controlPin2,LOW);

}

  

 void motorEnable()

{

 motorSpeed = analogRead(potPin)/4;

 analogWrite(enablePin,motorSpeed);

}


 void isr()

{

 rev++;

}


 void checkDistance()

 digitalWrite(trig,LOW); 

 delayMicroseconds (2);

 digitalWrite(trig, HIGH);

 delayMicroseconds(10);

 digitalWrite(trig,LOW);


 duration = pulseIn (echo, HIGH);

 inches = duration /74/2;

 Serial.println(inches);

}


void loop()

{

 checkDistance();

 motorEnable();

if (irrecv.decode(&results))

{

  Serial.print("irCode: ");

  Serial.println(results.value, HEX);

  irrecv.resume();

  }

if((results.value == 0xC1AA0DF2)&&(inches<10)){

 foward();

}

if(inches>10)

  stopMotors();

if((results.value == 0xC1AA4DB2)&&(inches<10)){

 backward();

}

if(inches>10)

  stopMotors();

if((results.value == 0xC1AAA15E)&&(inches<10)){

 stopMotors();

}

if(inches>10)

  stopMotors();


  detachInterrupt(0);

  time=millis()-oldtime;

  rpm=(rev/time)*60000/3;

  oldtime=millis();

  rev=0;

  attachInterrupt(0,isr,RISING);

  Serial.println(rpm);

}

Errors an Individual May Make

image.png
  • Not turning on the device using a slide switch
  • Faulty equipment, like a burnt out LED
  • An object that is within 10 inches of the device, that keeps it on
  • Putting wires in the wrong pins
  • Having the electrical signal sent from the remote being different from the one I used (Which is an easy fix, merely press the desired buttons for the functions of going forward, backwards, and stopping, while the device is on, and based on what is seen in the serial port)
  • Did not download the file for the IR receiver to work (The file is IRrecvDemo.inoBinary File and IRremote.zipCompressed Archive)
  • Forgot to add a { or } at the start of end of a void function
  • Added a comma in the code