小编
Published2025-10-15
Introduction to Arduino and IR Remote Control
If you’re someone who enjoys DIY electronics and programming, you’ll know that the Arduino platform offers countless opportunities to build fun and interactive projects. One such project is controlling a servo motor using an infrared (IR) remote control. This combination of Arduino, an IR remote, and a servo motor can be used in a variety of applications, such as robotic arms, camera panning systems, and even home automation systems.

In this tutorial, we will walk you through everything you need to know to get your servo motor working with an Arduino and an IR remote control. Whether you’re a seasoned maker or a newbie, this step-by-step guide will make it easy for you to get started.
Before diving into the code and the wiring, let’s first go over what components you will need:
Arduino Board (such as Arduino Uno)
IR Receiver Module (like the KY-022)
IR Remote Control (any standard IR remote should work)
Servo Motor (such as the SG90)
220Ω Resistor (for the IR receiver module)
Having these components ready will make the setup process smoother and faster. Once everything is ready, we can start wiring the components together and writing the code.
Let’s take a look at how to wire the components.
Arduino to IR Receiver:
The IR receiver module has three pins: VCC, GND, and OUT.
Connect the VCC pin to the 5V pin on the Arduino, the GND pin to one of the GND pins on the Arduino, and the OUT pin to a digital pin on the Arduino (for this tutorial, we will use pin 2).
Don’t forget to place the 220Ω resistor in series with the OUT pin to protect the circuit.
Arduino to Servo Motor:
Connect the VCC and GND pins of the servo motor to the 5V and GND pins on the Arduino respectively.
The signal wire (usually yellow or white) from the servo motor should be connected to a PWM-enabled digital pin on the Arduino. For this example, we will use pin 9.
Now that we’ve set up the hardware, it’s time to move on to the software part: programming the Arduino.
Installing the Required Libraries
Before you start writing the code, you need to install two essential libraries:
IRremote: This library allows the Arduino to decode and interpret signals from the IR remote.
Servo: This library is essential for controlling the servo motor.
To install these libraries:
Go to Sketch > Include Library > Manage Libraries.
Search for IRremote and Servo, and click Install for both.
Once these libraries are installed, you can begin writing the code to control your servo motor via the IR remote.
Writing the Code for IR Remote Control
Here’s the basic outline of the code for controlling the servo motor with an IR remote:
const int recv_pin = 2; // Pin connected to IR receiver
IRrecv irrecv(recv_pin); // Create an IR receiver object
decode_results results; // Variable to store IR signal results
Servo myservo; // Servo motor object
int servoPin = 9; // Pin connected to servo motor
int angle = 0; // Servo angle variable
Serial.begin(9600); // Start serial communication
irrecv.enableIRIn(); // Start the IR receiver
myservo.attach(servoPin); // Attach the servo to the correct pin
if (irrecv.decode(&results)) { // Check if an IR signal is received
long int decCode = results.value; // Store the IR code
Serial.println(decCode); // Print the IR code for debugging
// Control servo motor based on received IR code
if (decCode == 0xFF6897) { // IR code for a specific button
angle += 10; // Increase the angle by 10 degrees
if (angle > 180) angle = 0; // Reset angle if it exceeds 180 degrees
myservo.write(angle); // Move the servo to the new angle
irrecv.resume(); // Receive the next value
IRrecv and decode_results are used to receive and decode the IR signals sent by the remote control.
The servo.write(angle) function is used to control the servo motor’s position based on the received IR signal.
The if (decCode == 0xFF6897) condition checks for a specific button on the IR remote. When this button is pressed, the servo motor’s angle increases by 10 degrees.
The Serial.println(decCode) line allows you to view the IR code received by the Arduino in the Serial Monitor for debugging purposes.
Once the hardware is connected and the code is uploaded to the Arduino, open the Serial Monitor. Point your IR remote at the IR receiver and press the designated button. You should see the corresponding IR code displayed in the Serial Monitor. The servo motor should also start moving in response to the button presses.
At this point, you can fine-tune your project by adding more buttons to control the servo motor in different ways. You can map each button to different angles, or even use multiple servos for a more complex project.
Troubleshooting, Enhancements, and Real-World Applications
Now that your basic system is up and running, let’s explore some common troubleshooting tips, potential enhancements, and real-world applications that could benefit from this setup.
Ensure that the servo is correctly connected to the 5V and GND pins of the Arduino.
Check that you are using a PWM-enabled pin (like pin 9) for the servo.
Make sure that the servo library is correctly installed in the Arduino IDE.
Double-check the wiring of the IR receiver, particularly the connection to the Arduino’s digital pin 2.
Ensure the IR remote is functional, and try replacing the battery if necessary.
Check the Serial Monitor for any received IR codes. If nothing appears, there may be an issue with the receiver or the remote.
Servo Moving Erratically:
If the servo is moving unexpectedly, check if the IR remote is sending repeated or incorrect signals. Make sure the irrecv.resume() function is properly placed in your code.
You can also use the delay() function to slow down the servo's movements and prevent erratic behavior.
Once you’ve mastered the basics of controlling a servo motor with an IR remote, you can enhance your project with a few creative ideas:
Use multiple servo motors to create more complex systems, such as a robotic arm or a camera control system.
If you have an unused IR remote, you can reprogram the buttons to control different aspects of your project, such as adjusting the speed of the servo or switching between different modes.
Add a feedback system such as an LCD display to show the current angle of the servo or a series of LEDs to indicate different states of the motor.
This simple project of controlling a servo motor with an IR remote has several practical applications:
Control the movement of a robotic arm by adjusting multiple servos with different IR buttons. This could be used for tasks like object manipulation or precise positioning.
If you’re into photography or surveillance, you could use an Arduino and servo motors to remotely pan a camera to different angles.
Control curtains, blinds, or even door locks with an IR remote. The Arduino can receive signals to adjust the position of motors, allowing you to automate various home appliances.
Create an IR-controlled vehicle that uses servos for steering and throttle control. This adds a fun DIY twist to traditional remote-controlled cars.
By now, you should have a clear understanding of how to control a servo motor with an Arduino using an IR remote. With the flexibility of Arduino programming, the possibilities for enhancement are endless. Whether you're looking to create a simple robotic arm or automate aspects of your home, this project serves as an excellent foundation for more complex electronics and coding ventures.
This concludes our guide to controlling a servo motor using an IR remote with Arduino. If you encounter any issues or have ideas for improvements, feel free to experiment, troubleshoot, and continue learning. Happy making!
Kpower has delivered professional drive system solutions to over 500 enterprise clients globally with products covering various fields such as Smart Home Systems, Automatic Electronics, Robotics, Precision Agriculture, Drones, and Industrial Automation.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.