Robojax

Lesson 101: Control AC Bulb, TV, DC Motor and Servo Motor using Arduino

Lesson 101: Control AC Bulb, TV, DC Motor and Servo Motor using Arduino

Please select other codes for this lecture from the links below.

Related or required files and link to this lesson

Part 14: Infrared

This is lesson 3 of 5 lessons. In this lesson we learn how to capture and decode infrared code from Infrared Remote Controller. Either Black Remote , white remote or your TV remote.
Using this code we can control a TV using Arduion as remote
We need the IRremote library to be installed first. Full details is shown in the video. We will use VS1838B IR or infrared receiver. The receiver is sold just a bare part of installed on PCB. This tutorial will show you to use both.

This IR remote series has 4 codes

    <
  1. How to decode IR remote code and Arduino
  2. How to turn ON/OFF an AC bulb using any remote control and Arduino
  3. How to control a TV using Arduino as remote control (this code)
  4. How to control a DC motor using Infrared remote and Arduino
  5. How to control a servo motor using Infrared remote control and Arduino

  /*
 * Lesson 101 code 3
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 * 
 * modefied by Ahmad Shamshiri for Robojax.com
 * on July 28, 2018 at 15:01 in Ajax, Ontario, Canada
 
  * Watch video instruction for this code:https://youtu.be/LVd9_i-BQcg
 * Get other Arduino codes from Robojax.com
This video is part of Arduino Step by Step Course which starts here: https://youtu.be/-6qSrDUA5a8

 

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can make donation using PayPal https://bit.ly/donate-robojax

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. 
 */

#include <IRremote.h>

int RECV_PIN = 11;
int RELAY_PIN = 4;

IRrecv irrecv(RECV_PIN);
IRsend irsend;

decode_results results;

// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
//  decode_results *results = (decode_results *)v
void dump(decode_results *results) {
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN) {
    Serial.println("Could not decode message");
  } 
  else {
    if (results->decode_type == NEC) {
      Serial.print("Decoded NEC: ");
    } 
    else if (results->decode_type == SONY) {
      Serial.print("Decoded SONY: ");
    } 
    else if (results->decode_type == RC5) {
      Serial.print("Decoded RC5: ");
    } 
    else if (results->decode_type == RC6) {
      Serial.print("Decoded RC6: ");
    }
    Serial.print(results->value, HEX);
    Serial.print(" (");
    Serial.print(results->bits, DEC);
    Serial.println(" bits)");
  }
  Serial.print("Raw (");
  Serial.print(count, DEC);
  Serial.print("): ");

  for (int i = 0; i < count; i++) {
    if ((i % 2) == 1) {
      Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
    } 
    else {
      Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
    }
    Serial.print(" ");
  }
  Serial.println("");
}

void setup()
{
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(13, OUTPUT);
    Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

int on = 0;
unsigned long last = millis();

void loop() {
 

  if (irrecv.decode(&results)) {
    // If it's been at least 1/4 second since the last
    // IR received, toggle the relay
    if (millis() - last > 250) {
      on = !on;
      digitalWrite(RELAY_PIN, on ? HIGH : LOW);
      digitalWrite(13, on ? HIGH : LOW);
      dump(&results);
    }
    last = millis();      
    irrecv.resume(); // Receive the next value
  }

    irsend.sendSony(0x68B92, 20);   
    Serial.println("Sent 0x68B92 using Sony 20bits");  
}