Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Ashutosh M Bhatt
Published © GPL3+

IR remote controlled DC servo motor using Arduino

IntermediateFull instructions provided16,328
IR remote controlled DC servo motor using Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1

Hand tools and fabrication machines

Breadboard, 270 Pin
Breadboard, 270 Pin

Story

Read more

Code

Code snippet #1

Plain text
####include <IRremote.h> // add IR remote library
#include <Servo.h> // add servo motor library

Servo servo1; 

int IRpin = 12;   // pin for the IR sensor

int motor_angle=0;

IRrecv irrecv(IRpin);

decode_results results;


void setup()

{

  Serial.begin(9600); // initialize serial communication

  Serial.println("IR Remote controlled servo motor"); // display message

  irrecv.enableIRIn();  // Start the receiver  

  servo1.attach(5); // declare servo motor pin

  servo1.write(motor_angle); // move the motor to 0 deg

  Serial.println("Servo motor angle 0 deg");

  delay(2000);

}


void loop() 

{

 while(!(irrecv.decode(&results))); // wait until no button is pressed

 if (irrecv.decode(&results)) // when button is pressed and code is received

    {

      if(results.value==2210)  // check if digit 1 button is pressed

        {

          Serial.println("servo motor angle 30 deg");

    motor_angle = 30;

          servo1.write(motor_angle); // move the motor to 30 deg

        }

      else if(results.value==6308) // if digit 2 button is pressed

        {

          Serial.println("servo motor angle 60 deg");

   motor_angle = 60;          

   servo1.write(motor_angle); // move the motor to 60 deg

        }  

       else if(results.value==2215) // like wise for all  digit buttons

        {

          Serial.println("servo motor angle 90 deg");

   motor_angle = 90;          

   servo1.write(motor_angle);

        }  

       else if(results.value==6312) 

        {

          Serial.println("servo motor angle 120 deg");

   motor_angle = 120;          

   servo1.write(motor_angle);

        } 

       else if(results.value==2219) 

        {

          Serial.println("servo motor angle 150 deg");

   motor_angle = 150;          

   servo1.write(motor_angle);

        }   

       else if(results.value==6338) // if volume UP button is pressed

         {

            if(motor_angle<150) motor_angle+=5; // increase motor angle

            Serial.print("Motor angle is ");

            Serial.println(motor_angle);

            servo1.write(motor_angle);         // and move the motor to that angle

         }

      else if(results.value==6292)  // if volume down button is pressed

        {

            if(motor_angle>0) motor_angle-=5; // decrease motor angle

            Serial.print("Motor angle is ");

            Serial.println(motor_angle);

            servo1.write(motor_angle);      // and move the motor to that angle

        }

      delay(200);      // wait for 0.2 sec

      irrecv.resume();     // again be ready to receive next code

    }

 }
### 

Code snippet #2

Plain text
#include <Servo.h> // add servo motor library

Servo servo1; 

int IRpin = 12;   // pin for the IR sensor

int motor_angle=0;

IRrecv irrecv(IRpin);

decode_results results;


void setup()

{

  Serial.begin(9600); // initialize serial communication

  Serial.println("IR Remote controlled servo motor"); // display message

  irrecv.enableIRIn();  // Start the receiver  

  servo1.attach(5); // declare servo motor pin

  servo1.write(motor_angle); // move the motor to 0 deg

  Serial.println("Servo motor angle 0 deg");

  delay(2000);

}


void loop() 

{

 while(!(irrecv.decode(&results))); // wait until no button is pressed

 if (irrecv.decode(&results)) // when button is pressed and code is received

    {

      if(results.value==2210)  // check if digit 1 button is pressed

        {

          Serial.println("servo motor angle 30 deg");

    motor_angle = 30;

          servo1.write(motor_angle); // move the motor to 30 deg

        }

      else if(results.value==6308) // if digit 2 button is pressed

        {

          Serial.println("servo motor angle 60 deg");

   motor_angle = 60;          

   servo1.write(motor_angle); // move the motor to 60 deg

        }  

       else if(results.value==2215) // like wise for all  digit buttons

        {

          Serial.println("servo motor angle 90 deg");

   motor_angle = 90;          

   servo1.write(motor_angle);

        }  

       else if(results.value==6312) 

        {

          Serial.println("servo motor angle 120 deg");

   motor_angle = 120;          

   servo1.write(motor_angle);

        } 

       else if(results.value==2219) 

        {

          Serial.println("servo motor angle 150 deg");

   motor_angle = 150;          

   servo1.write(motor_angle);

        }   

       else if(results.value==6338) // if volume UP button is pressed

         {

            if(motor_angle<150) motor_angle+=5; // increase motor angle

            Serial.print("Motor angle is ");

            Serial.println(motor_angle);

            servo1.write(motor_angle);         // and move the motor to that angle

         }

      else if(results.value==6292)  // if volume down button is pressed

        {

            if(motor_angle>0) motor_angle-=5; // decrease motor angle

            Serial.print("Motor angle is ");

            Serial.println(motor_angle);

            servo1.write(motor_angle);      // and move the motor to that angle

        }

      delay(200);      // wait for 0.2 sec

      irrecv.resume();     // again be ready to receive next code

    }

 }

Credits

Ashutosh M Bhatt
5 projects • 39 followers
Contact

Comments

Please log in or sign up to comment.