logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Arduino

How to Send IR Commands to Your Project with a Remote Control

DevicePlus Editorial Team
Published by DevicePlus Editorial Team at February 14, 2021
Categories
  • Arduino
Tags

You might already know that the remote control for your TV uses infrared light that’s invisible to the naked eye to send coded signals instructing the TV to change inputs, alter volume, or turn on and off. What you might not know is that cheap remotes can be used to send similar basic instructions to your Arduino projects. This guide will show you how.

How IR Sensors Work

Infrared sensors work by detecting light patterns emitted by remote controls. These patterns are outside the visible spectrum that humans can see—although for fun, if you point your phone’s camera at the light end of a remote control while pressing a button, you can see the emitter light up!

Infrared sensors and the remotes that can control them are relatively cheap. The remotes are labeled with some standard functions—for example, numbers, play/pause, or volume controls—but under the hood, these buttons can be programmed to do anything. Each button is actually sending a remote code that the Arduino can interpret. You can then use that input to control anything else your project is connected to. For example, in this guide, we’ll use the Play/Pause button on a remote to turn an LED on and off. To do this, we’ll need to determine what code the remote is sending, then use that code as a switch to turn the light on and off.

What You’ll Need

Most of the components you’ll need for this project come in common starter electronics kits. You might even have them already sitting around. To get started, make sure you have:

Arduino Uno
An IR sensor and remote
An LED
A 220ohm resistor
Breadboard and wiring
Arduino IDE
USB Cable

 

As with previous projects, you’ll need to use the resistor on the LED to keep it from burning out. However, some IR sensors might also require independent resistors, so check the components you’re using for details.

The Code

The sketch we’ll be using for this project has two main parts to it. First, we’ll use some code to decode the signal from the remote control that you have. Then, we’ll add that code to the sketch and turn the LED on and off. Now, here’s the full sketch that you can upload directly to your Arduino.

#include

int receiverPin = 8;
int LEDPin = 6;
bool lightOn = false;
IRrecv receiver(receiverPin);
decode_results results;

void setup() {
Serial.begin(9600);
receiver.enableIRIn();
}

void loop() {
if(receiver.decode(&results)) {
Serial.println(results.value, HEX);
receiver.resume();
}

if (receiver.decode(&results)) {
if (results.value == 0xYYYYYY) {
if (lightOn == true) {
digitalWrite(LEDPin, LOW);
lightOn == false;
} else {
digitalWrite(LEDPin, HIGH);
lightOn == true;
}
}
receiver.resume();
}
}

Now, let’s go over some of the critical bits of code. We won’t dive into every individual line, but we’ll explain the important parts.

#include<IRremote.h>

Here, this line will include the IRremote.h library that you’ll need to run this sketch. If you don’t already have the library enabled, head to Tools > Manage libraries… and search for “IRremote” to install the library.

int receiverPin = 8;
int LEDPin = 6;
bool lightOn = false;
IRrecv receiver(receiverPin);
decode_results results;

Here, we’ll define a couple of pins for the IR sensor and the LED output pin. The lightOn variable will store whether the LED is turned on or off (and it will be set to off or false to start with).

Finally, the last two lines will create an object for the IR receiver, then another object to store the results of the remote code.

void setup() {
Serial.begin(9600);
receiver.enableIRIn();
}

This section will initialize the Serial monitor, which we’ll use to detect IR codes coming from your remote. The second line here also initializes the IR receiver.

if(receiver.decode(&results)) {
Serial.println(results.value, HEX);
receiver.resume();
}

This section will read the results of the IR receiver and translate the input into hex codes that you can use in the next section of codes. This section can also be removed later once you know which codes you need for your sketch.

if (receiver.decode(&results)) {
if (results.value == 0xYYYYYY) {
if (lightOn == true) {
digitalWrite(LEDPin, LOW);
lightOn == false;
} else {
digitalWrite(LEDPin, HIGH);
lightOn == true;
}
}
receiver.resume();
}

After you figure out the hex codes that match your remote buttons, come back and edit the “YYYYYY” section and replace it with your remote’s hex code. Re-upload your sketch.

Once you’ve done that, this section will control your LED. When the IR sensor detects that you’ve pressed the corresponding button, it will turn the light on if it’s currently off, or vice versa.

The Wiring

For this project, the wiring will be fairly simple. You’ll only need three components and a bit of wiring. To add the LED, follow these steps:

● Connect the positive end of the LED to pin 6.
● Connect the negative end of the LED to a 220-ohm resistor.
● Connect the other end of the resistor to GND.

Next, for the IR sensor, here are the steps for the VS 1838b. If you have a different sensor, you may have to adapt these instructions. All instructions are with the IR sensor facing the user.

● Connect the left pin of the IR sensor to pin 8 on the Arduino.
● Connect the middle pin of the IR sensor to GND.
● Connect the right pin to 5V.

Once everything is hooked up, you can connect your Arduino to your computer via USB and open up the serial monitor. Point your remote at the IR sensor and press some buttons. Make a note of the hex codes that appear in your serial monitor, then pick one and replace the section of the sketch with the code for the button you want to use. Re-upload the sketch, and you should be able to control the light with the press of a button.

DevicePlus Editorial Team
DevicePlus Editorial Team

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • Using Arduino with Parts and Sensors – Infrared Remote Control (First Part)Using Arduino with Parts and Sensors – Infrared Remote Control (First Part)
  • Using Arduino with Parts and Sensors – Infrared Remote Control (Last Part)Using Arduino with Parts and Sensors – Infrared Remote Control (Last Part)
  • How to Build a Motion Activated LightHow to Build a Motion Activated Light
  • Using Sensors with the Arduino: Create a Simple Device to Detect Movement and ReactUsing Sensors with the Arduino: Create a Simple Device to Detect Movement and React
  • How to detect motion in your Arduino with an accelerometerHow to detect motion in your Arduino with an accelerometer
  • Arduino Hexapod PART 3: REMOTE CONTROLArduino Hexapod PART 3: REMOTE CONTROL
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort