Home Industry InsightBLDC
Looking for a suitable motor? Looking for a suitable motor?
Looking for a suitable motor?

How to Use an IR Sensor with a Servo Motor in Arduino Projects

小编

Published2025-10-15

This article guides you through the process of connecting an IR sensor and a servo motor to an Arduino, explaining the purpose of each component and offering a detailed step-by-step code tutorial. Learn how to control a servo motor using signals from an IR sensor to create interactive projects with ease.

IR sensor, servo motor, Arduino, Arduino code, infrared sensor, servo control, Arduino project, electronics tutorial, robotics, DIY electronics

Introduction to the Components and Basic Concepts

In the world of DIY electronics and robotics, Arduino has quickly become one of the most popular platforms for building innovative projects. One of the most versatile setups you can try is using an IR sensor and a servo motor together. This combination allows you to create interactive, motion-controlled systems that can react to your commands without needing physical touch—ideal for creating gesture-controlled devices, motion-activated robots, or even automatic doors. This article will walk you through the essential components you need, followed by an in-depth look at how to set up the hardware and write the code that will bring your project to life.

What is an IR Sensor?

An infrared (IR) sensor is an electronic device that detects infrared light, typically from a remote control or other sources of infrared radiation. It consists of an IR LED and a photodiode. When an object reflects the emitted infrared light back to the sensor, it registers a signal. This process can be used for various purposes, including detecting objects, measuring distance, or reading signals from a remote control.

In an Arduino project, IR sensors can be employed to receive commands from an infrared remote, allowing you to control electronic devices without direct contact. For example, you could use an IR sensor to send a command that moves a servo motor to a specific position.

What is a Servo Motor?

A servo motor is a special type of motor that can precisely control the angular position of an object. It is often used in robotics, model vehicles, and automation applications where accurate movement is needed. Unlike standard motors, servo motors can rotate to specific angles—commonly between 0 and 180 degrees—based on the signal they receive.

In Arduino projects, servo motors are typically controlled using Pulse Width Modulation (PWM). By sending a PWM signal from the Arduino, you can control the angle at which the servo rotates.

How Do These Components Work Together?

When combined, the IR sensor can act as an input device, receiving signals from an infrared remote. These signals can then be used to control the movement of a servo motor. The servo motor will react to these signals by rotating to different positions based on the data received from the IR sensor. This creates an interactive setup, where pressing buttons on the remote results in a corresponding movement of the servo motor.

For example, pressing a button on the IR remote could make the servo motor rotate to a specific angle, and releasing the button could return the servo to its initial position. This interaction allows for the creation of various automated systems or robots that respond to your commands without requiring physical contact.

Materials Needed

Before jumping into the code, let’s first check what materials you'll need for this project:

Arduino Board (e.g., Arduino Uno)

IR Sensor (e.g., KY-022 or any compatible IR receiver)

Servo Motor (e.g., SG90 or similar)

IR Remote Control

Jumper Wires

Breadboard (optional, but helpful for prototyping)

These components are easy to obtain and affordable, making it possible for hobbyists and makers to experiment with this technology at home.

Wiring the Components and Writing the Code

Wiring the IR Sensor and Servo Motor to the Arduino

Before diving into the Arduino code, let’s first connect the hardware components. Below is a step-by-step guide for wiring the IR sensor and servo motor to the Arduino board:

IR Sensor:

The IR sensor typically has three pins: VCC, GND, and OUT.

Connect the VCC pin to the 5V pin on the Arduino.

Connect the GND pin to the GND pin on the Arduino.

Connect the OUT pin to a digital input pin on the Arduino (e.g., pin 2).

Servo Motor:

A standard servo motor has three wires: VCC, GND, and Signal.

Connect the VCC pin of the servo to the 5V pin on the Arduino.

Connect the GND pin of the servo to the GND pin on the Arduino.

Connect the Signal pin to one of the PWM-enabled pins on the Arduino (e.g., pin 9).

Power Supply:

Ensure that your Arduino and servo motor are both powered correctly. The Arduino can be powered via USB, while the servo motor typically requires external power, especially if it draws more current than the Arduino can supply.

Setting Up the Code

Now that your components are wired correctly, it’s time to write the Arduino code to control the servo motor using the IR sensor. The code will involve two main parts:

Reading the signal from the IR sensor.

Controlling the servo motor based on that input.

We’ll begin by including a library to handle the IR communication. The IRremote library is perfect for this task, as it simplifies the process of reading signals from an IR sensor. To install the library:

Open the Arduino IDE.

Go to Sketch > Include Library > Manage Libraries.

In the Library Manager, search for IRremote and install it.

Once the library is installed, you can begin coding the project.

#include

#include

const int recv_pin = 2; // IR sensor pin

IRrecv irrecv(recv_pin); // Create IR receiver object

decode_results results; // Variable to store IR sensor data

Servo myServo; // Create Servo object to control the motor

void setup() {

Serial.begin(9600); // Start serial communication for debugging

irrecv.enableIRIn(); // Start the IR receiver

myServo.attach(9); // Attach servo motor to pin 9

}

void loop() {

if (irrecv.decode(&results)) { // If an IR signal is received

long int decCode = results.value;

Serial.println(decCode); // Print the code for debugging

// Example: Control servo based on IR remote button press

if (decCode == 0xFFFFFFFF) { // Replace with your own remote's code

myServo.write(90); // Move servo to 90 degrees

}

irrecv.resume(); // Receive the next value

}

}

Code Explanation

IR Library and Servo Library: The code includes the IRremote and Servo libraries to handle the IR sensor and servo motor respectively.

IR Receiver Setup: We initialize the IR sensor on pin 2 using the IRrecv class, and then enable it to start receiving signals with irrecv.enableIRIn().

Servo Setup: We initialize the servo motor on pin 9 using the Servo class and attach it to the motor.

Main Loop: Inside the loop(), we check if any IR signal is detected with irrecv.decode(). If a signal is received, we print its value to the serial monitor for debugging. Based on the IR signal’s value (which corresponds to the button pressed on the remote), we can command the servo motor to move to a specified position (e.g., 90 degrees).

Testing Your Project

Once you’ve uploaded the code to the Arduino board, open the serial monitor. Point your IR remote at the sensor and press a button. You should see the corresponding code printed on the serial monitor. If the correct button is pressed, the servo will rotate to the specified position.

By following these steps, you can easily control the movement of a servo motor using an IR sensor, adding a layer of interactivity to your Arduino projects. Experiment with different remote buttons to control the servo motor at various angles, and get creative with the applications you can build!

Established in 2005, Kpower has been dedicated to a professional compact motion unit manufacturer, headquartered in Dongguan, Guangdong Province, China.

Update:2025-10-15

Contact a motor expert for product recommendation.
Contact a motor expert for product recommendation.

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.