Arduino Tutorial 13 - Motor Fan

In this project, we will use a relay and a motor to make a small fan. A relay is an electrically operated switch that allows you to turn on or off a circuit using voltage and/or current much higher than the Arduino can handle.

 

COMPONENT LIST

 

HARDWARE


Connect the button with a 220Ω pull-down resistor in order to hold the logic signal near zero volts when the button is disconnected. The relay has 6 pins. Pin 1 and 2 on the relay are input signals, connected to digital pin 3 and GND on the Arduino, respectively. Pins 3, 4, 5, and 6 on the relay are the output signals but we will only use pin 4 and pin 6 at this time. A relay is similar to a push button, which has 2 connections as well.

fig 1 Motor Fan Circuit

 

CODE

CODE
// Project – Motor Fan  
int buttonPin = 2;             // Button connected to digital pin 2  
int relayPin = 3;             // Relay connected to digital pin 3  
int relayState = HIGH;         // Initial state of the relay is HIGH  
int buttonState;              // Current state of the button  
int lastButtonState = LOW;    // Previous state of the button  
long lastDebounceTime = 0;                    
long debounceDelay = 50;     // Debounce delay in milliseconds  
  
void setup() {  
  pinMode(buttonPin, INPUT);  
  pinMode(relayPin, OUTPUT);  
  
  digitalWrite(relayPin, relayState); 
// Set the initial state of the relay  
}  
void loop() {  
  int reading = digitalRead(buttonPin);
// Reading from the button pin  
    
  // If a change is detected, record the current time  
  if (reading != lastButtonState) {     
    lastDebounceTime = millis();  
  }   
    
  // Wait for 50ms and check if the button state is still the same  
  // If not, update the button state  
  // If the button is pressed (HIGH), toggle the relay state  
  if ((millis() - lastDebounceTime) > debounceDelay) {  
    if (reading != buttonState) {  
      buttonState = reading;  
        
      if (buttonState == HIGH) {  
        relayState = !relayState;  
      }  
    }  
  }  
  digitalWrite(relayPin, relayState);  
  
  // Update the previous button state  
  lastButtonState = reading;  
}

After uploading the sketch, you can control the relay and LED with the button.

 

 

CODE REVIEW
 

The debounce of push button is the:

CODE
if (reading != lastButtonState) {   
    lastDebounceTime = millis();
  } 
if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
     ...
    }
  }

When signals are received by the Arduino, the program does not operate on them immediately - it tests if the signal is correct and waits for a certain time to confirm. If the signal is correct, the program will be operating accordingly.
The reason the program tests for a correct signal is that there is a bouncing process for the button when pressed. It might generate the wrong signal, so we test it to solve the problem in hardware.

 

 

HARDWARE REVIEW
 

Relay
A relay is an electrically operated switch that allows you to turn on or off a circuit using voltage and/or current much higher than the Arduino can normally handle. There is no connection between the low voltage circuit operated by Arduino and the high power circuit - the relay isolates the circuits from each other.
Let’s take a look at the inner structure of relay:

Relays have 6 pins. Pins 1 and 2 are connected to the digital pin and GND. We use these 2 pins to power the relay. There is a coil between Pin 1 and Pin 2. When the circuit is HIGH, current flows in the coil, generates a magnetic field, closes the switch contacts and connects the NO (Normally Open) to COM(common)pin. When the circuit is LOW, no current runs in the coil, therefore the NC (Normally Closed) connects to the common pin. We connect Pin 4 and Pin 6 to control the switching on and off of the relay and the LED.

 

The difference between the DC motor, Stepper Motors and Servos
DC (Direct Current) motors are devices that change electrical energy to kinetic energy. When you supply power to a DC motor it will start spinning continuously until that power is removed. When you switch the polarities, it will spin in the opposite direction. A motor runs continuously at a high RPM (revolutions per minute). These revolutions of the motor shaft can not be controlled to a specific angle, but you can control the speed. Because the rotations are so fast, it is impractical to use it for vehicles.
A stepper motor has a gearing set on the DC motor to step down the speed and increase torque. This makes it more practical to use for vehicle applications. Its speed can be controlled by PWM. A servo is also a motor. It controls the position of the motor by a feedback system, as we saw in the servo projects covered. Servos are practical to use for robotics arms.

icon Moter_Fan_English.zip 1KB Download(0)
License
All Rights
Reserved
licensBg
0