Seeed
Published © MIT

Smart Home: IR Remote Control Fan - based on Seeeduino XIAO

Let’s learn how to make a remote control fan using Seeeduino XIAO expansion board

IntermediateFull instructions provided6 hours1,776

Things used in this project

Hardware components

Seeeduino XIAO
×1
Grove IR(Infrared) Receiver
×1
Grove servo
×1
Grove - Mini Fan v1.1
Seeed Studio Grove - Mini Fan v1.1
×1
Seeeduino XIAO expansion board
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Remote control fan

C/C++
This code for control the motor fan by IR receiver on the Seeedruino XIAO expansion board
#include <IRremote.h>
#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
int RECV_PIN = 7; // set pin 2 as IR control
 
IRrecv irrecv(RECV_PIN);
 
decode_results results;
 
int pos = 90;    // variable to store the servo position
int fanPin = 0;  // set D6 as control switch
int fanState = LOW;
int IO = 0;
 
void setup()
{
  Serial.begin(9600);
  Serial.println("Enabling IRin");  // remind enabling IR
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");
  myservo.attach(5);  // attaches the servo on pin 2 to the servo object
  pinMode(fanPin, OUTPUT);
 
}
 
//  power_encode 2155829415     left  2155870215  right  2155821255
 
void loop() {
  if (irrecv.decode(&results)) { //checking IR signal
    if (results.value == 2155829415) {    // Power off/on
      IO++;
      if (IO % 2 == 0) {
        fanState = HIGH;
        digitalWrite(fanPin, fanState);
        delay(100);
      }
      else {
        fanState = LOW;
        digitalWrite(fanPin, fanState);
        delay(100);
      }
    }
 
    if (results.value == 2155821255 ) {    // fan swing to left
      for (pos; pos <= 89; pos += 1) { // goes from 0 degrees to 90 degrees
        // in steps of 1 degree
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
 
        delay(40);                       // waits 15ms for the servo to reach the position
        if (irrecv.decode(&results)) {
          irrecv.resume();
          if (results.value == 2155870215)
            break;
        }
      }
    }
 
    if (results.value == 2155870215 ) {    // fan swing to right
      for (pos; pos >= 1; pos -= 1) { // goes from 90 degrees to 0 degrees
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(40);                       // waits 15ms for the servo to reach the position
 
        if (irrecv.decode(&results)) {
          irrecv.resume();
          if (results.value == 2155821255)
            break;
        }
      }
    }
    Serial.println(pos);
    Serial.println(results.value, HEX);
    Serial.println(results.value);
    irrecv.resume();                    //recive next intrustion
 
  }
  delay(100);
}

Credits

Seeed

Seeed

101 projects • 159 followers
Seeed R&D Team

Comments