Using IR Communication

Week 3/4

IR Transmitter & Receiver 

I teamed up with Max, who had a IR Transmitter to create a circuit which incorporated both of our modules. For our mini project we will be making a  DC Motor turn on when a push button is pressed using IR communication. Applications for this idea could be: Security doors, extractor fan systems or remote controlled toys.

thumb_IMG_0055_1024

We firstly found a setup online which gave us a good idea of how we can use our modules together and helped us with writing the code and setting up are Arduino circuit.

http://osoyoo.com/2015/03/25/ir-receiver-module/

http://osoyoo.com/2015/03/25/infrared-transmitter-module/

The setup on these websites used IR communication to turn on a LED which showed that the two modules were working together. We thought that this would be a good place to start as a prove of concept and would be a good basis for setup.

Fritzing Setup

 

Arduino Setup

thumb_IMG_0056_1024.jpg

We decided to put the setup on one Arduino rather than two shown in the tutorial as we didn’t yet know the distances the receiver would pick up the IR from the transmitter.

How it Works 

When the button is pressed, the IR transmitter sends a quick burst of IR light which is picked up the the Receiver. When this happens the LED is turned on which let us know that the two modules were working.

Receiver Code
#include <IRremote.h> //adds the library code to the sketch
const int irReceiverPin = 8; //receiver module S pin is connected to arduino D8
const int ledPin = 7; 
IRrecv irrecv(irReceiverPin);  
decode_results decodedSignal;  

void setup()
{
   Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
    irrecv.enableIRIn();
}
boolean lightState = false;
unsigned long last = millis();

void loop()
{
    if (irrecv.decode(&decodedSignal) == true) //this is true if a message has been received
    {
      Serial.println(decodedSignal.value, HEX);
        if (millis() - last > 250) {
            //has it been 1/4 sec since last message
            lightState = !lightState;
            //toggle the LED
            digitalWrite(ledPin, lightState);
        }
        last = millis();
        irrecv.resume();
        // watch out for another message
    }
}

Transmitter Code
IRsend irsend;
const int buttonPin = 4; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}

void loop() {

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
// turn LED on:
irsend.sendNEC(0x80BFA15E, 32);

}

}


In order for the the code to work we firstly had to download the IRremote library,  this allowed us to receive or transmit Infrared Remote Control codes so should be used in all code with IR communications.

 

Incorporating a DC Motor

From the code above, we wanted to add a motor to the setup so when the IR signal has been received it turns on a motor.

Fritzing Setup

thumb_IMG_0057_1024.jpg

Arduino Setup

thumb_59252723-30ED-4043-87C2-0155AC74722D_1024

In order for this to work we changed the code to incorporated the DC Motor and to only turn on when the IR signal had been received.

#include <IRremote.h>
#include <IRremoteInt.h>

IRsend irsend;
const int buttonPin = 4; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status


const int irReceiverPin = 8; //receiver module S pin is connected to arduino D8
const int Motor = 7; 
IRrecv irrecv(irReceiverPin); 
decode_results decodedSignal; 
boolean motorState = false; //motor off 
unsigned long last = millis();

void setup()
{
pinMode(buttonPin, INPUT);
Serial.begin(9600);
pinMode(Motor, OUTPUT);
irrecv.enableIRIn();
}



void loop() {

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {
// turn Motor on:
irsend.sendNEC(0x80BFA15E, 32);

if (irrecv.decode(&decodedSignal) == true) //this is true if a message has been received
 {
 Serial.println(decodedSignal.value, HEX);
 if (millis() - last > 250) {
 //has it been 1/4 sec since last message
 motorState = !motorState;


 //toggle the Motor
 digitalWrite(Motor, motorState);


 }
 last = millis();
 irrecv.resume();
 // watch out for another message

}
Serial.println();

}
}



After completing the circuit and testing that it worked a couple of times, we wanted test the IR Transmitter using another circuit. Unfortunately doing this test overloaded the Transmitter and it blew the module. This then left us with the dilemma of not having a working circuit.

To overcome this we decided to use a remote which had a built in IR Transmitter  and sent out a frequency of 38kHz. Although not ideal, it worked well and allowed us to show how both IR modules worked together as seen in the below video:

 

As you can see from the video, when a button on the remote and the push button are pressed simultaneously it sends a signal to the receiver telling the DC Motor to turn on. In the video i showed that pushing the separate buttons at different times didn’t turn the DC Motor on and could only be turned on when both were pressed at the same time.

Ideally we would just press the push button to send the IR signal but unfortunately this was not possible because of the broken transmitter module.

Outcome pre broken Transmitter:

Although the circuit works well and we are happy with the outcome and how we overcame some challenges, it can be very temperamental at times and often it will take a dozen pushes of the button for the transmitter to send a signal. We are unsure as to why it does this.

Reference:

http://osoyoo.com/2015/03/25/ir-receiver-module/

http://osoyoo.com/2015/03/25/infrared-transmitter-module/

 

Advertisements

IR Receiver Module

‘The KY-022is miniaturised infrared receivers for remote control and other applications requiring improved ambient light rejection.’

Infrared receiver

Specification 

Operating Voltage 2.7 to 5.5V
Operating Current 0.4 to 1.5mA
Reception Distance 18m
Reception Angle ±45º
Carrier Frequency 38KHz
Low Level Voltage 0.4V
High Level Voltage 4.5V
Ambient Light Filter up to 500LUX

 

 

My first step was to understand how the IR receiver worked. to do this i found an online tutorial which controlled LED’s using my IR receiver and a remote control.

https://maker.pro/education/controlling-leds-with-an-arduino-an-ir-sensor-and-a-remote

Fritzing Setup 

Arduino-IR-Sensor-and-LEDs (1).jpg

Circuit Setup

thumb_IMG_0052_1024.jpg

Above shows my Arduino setup and the remote control i used to send IR signals to my receiver.

Code

This is the code i copy a pasted from the website. Whenever a button on the remote is pressed, the Arduino receives a code, the Arduino will compare this code with the codes already saved, and if any of them match, the Arduino will turn on the LED connected to that button.

I realised that the the code received from my remote control, was different from the code received in the tutorial meaning that the the LED’S wouldn’t turn on because the codes didn’t match.

I changed this by opening the serial monitor and taking note of the code it received when i pressed a button. I then changed the code (shown below in bold) to match what was being received.

#include <IRremote.h>
#include <IRremoteInt.h>

#define first_key 38110
#define second_key 11168
#define third_key 46114

int receiver_pin = 8;

int first_led_pin = 7;
int second_led_pin = 6;
int third_led_pin = 5;

int led[] = {0,0,0,0};
IRrecv receiver(receiver_pin);
decode_results output;

void setup()
{
Serial.begin(9600);
receiver.enableIRIn();
pinMode(first_led_pin, OUTPUT);
pinMode(second_led_pin, OUTPUT);
pinMode(third_led_pin, OUTPUT);

}

void loop() {
if (receiver.decode(&output)) {
unsigned int value = output.value;
switch(value) {
case first_key:
if(led[1] == 1) {
digitalWrite(first_led_pin, LOW);
led[1] = 0;
} else {
digitalWrite(first_led_pin, HIGH);
led[1] = 1;
}
break;
case second_key:

if(led[2] == 1) {
digitalWrite(second_led_pin, LOW);
led[2] = 0;
} else {
digitalWrite(second_led_pin, HIGH);
led[2] = 1;
}
break;
case third_key:

if(led[3] == 1) {
digitalWrite(third_led_pin, LOW);
led[3] = 0;
} else {
digitalWrite(third_led_pin, HIGH);
led[3] = 1;
}

break;
}
Serial.println(value);
receiver.resume();
}
}

 

I was then able to test this for myself shown in the video below. As you can see, when i pressed only the 1,2,3 buttons on my remote did an LED turn on as the other buttons did not send the correct key code to match what was preset in the code above.

 

I was unsure as to why the LEDs sometimes stayed on as they were programmed to only turn on and off when a button is pressed.

This test has given me a good idea of how my IR module works and how i could potentially use it for the mini project where i have teamed up with Max who has an IR transmitter.

Reference:

https://maker.pro/education/controlling-leds-with-an-arduino-an-ir-sensor-and-a-remote

Wk 1/2

Infared – We use infrared transmitters almost every day; when you turn almost anything on remotely, for example TV remotes.

Infrared travels at the same speed as light, it will travel forever however, on earth in some cases – not very far. In others, it will happily go through all the atmosphere.

The level of attenuation (the opposite of transmittance) of infrared is well known, and there are windows where it has very high transmittance.

Picture1

Light below 1 is mostly visible light and near infrared. Visible light cannot travel through the atmosphere as far as mid-wave infrared light, between 3 and 4 microns. From about 5 to 8 nearly all infrared is absorbed through the atmosphere, so infrared viewers avoid this area of spectrum most of the time.

 

To measure it in kilometres it is important to consider the density of the air, so the figures above are for the thickness of the atmosphere. So, for air at sea level, that means the transmission of IR would be specified for about 120km of air.

 

Based on this, for a particular part of the IR spectrum, you could work out how much light was lost over a distance, and therefore work out how far it would go before you felt the signal was below the noise level.

 

 

 Pros & Cons

 

Advantages

  • IR Transmitter and Receiver pair form a simple circuit which can be easily built.
  • Can be used for simple remote controlling applications, small data transfer, etc.
  • IR Transmitter and Receiver pair as a module can be used in security applications, proximity sensors, distance measurement applications, etc.

 

Disadvantages

  • IR Transmitter and Receiver require line of sight communication i.e. they need to be facing each other.
  • The range of IR communication is less and is reliable for short range and small

amounts of data.

This IR sensor module consists of a PIN diode and a pre amplifier which are embedded into a single package. The output of TSOP is active low and it gives +5V in off state. When IR waves, from a source, with a centre frequency of 38 kHz incident on it, its output goes low.

 

 

https://www.engineersgarage.com/electronic-components/tsop1738-datasheet

 

 

 

 

Automatic Gate Sensor

https://circuitdigest.com/microcontroller-projects/automatic-door-opener-project-using-arduino

#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

#define PIR_sensor 14
#define m11 0
#define m12 1

void setup()
{
lcd.begin(16, 2);
pinMode(m11, OUTPUT);
pinMode(m12, OUTPUT);
pinMode(PIR_sensor, INPUT);
lcd.print(”    Automatic    “);
lcd.setCursor(0,1);
lcd.print(”   Door Opener   “);
delay(3000);
lcd.clear();
lcd.print(“CIRCUIT DEGEST “);
delay(2000);
}

void loop()
{
if(digitalRead(PIR_sensor))
{
lcd.setCursor(0,0);
lcd.print(“Movement Detected”);
lcd.setCursor(0, 1);
lcd.print(”    Gate Opened    “);
digitalWrite(m11, HIGH);         // gate opening
digitalWrite(m12, LOW);
delay(1000);
digitalWrite(m11, LOW);          // gate stop for a while
digitalWrite(m12, LOW);
delay(1000);
lcd.clear();
lcd.print(”   Gate Closed    “);
digitalWrite(m11, LOW);           // gate closing
digitalWrite(m12, HIGH);
delay(1000);
digitalWrite(m11, LOW);            // gate closed
digitalWrite(m12, LOW);
delay(1000);
}

else
{
lcd.setCursor(0,0);
lcd.print(”   No Movement   “);
lcd.setCursor(0,1);
lcd.print(”   Gate Closed   “);
digitalWrite(m11, LOW);
digitalWrite(m12, LOW);
}
}

 

KY-022 INFRARED RECEIVER MODULE

‘This module consists of a 1838 IR receiver, a 1kΩ resistor and a LED. It works together with the KY-005 IR transmitter module. Compatible with popular electronic platforms like Arduino, Raspberry Pi and ESP8266.’

https://arduinomodules.info/ky-022-infrared-receiver-module/

 

Infrared receiver

 

Infrared (IR) communication is a widely used and easy to implement wireless technology that has many useful applications. The best example in day to day life are TV/video remote controls, motion sensors, and infrared thermometers.

 

What Is Infrared? 

Infrared radiation is a form of light similar to the light we see all around us. The only difference between IR light and visible light is the frequency and wavelength, meaning that infrared light lies outside the range of visible light, so it is inviable to the human eye. A good way of seeing Infrared light is to view it through an old camera, this is because they contain a filter which makes the IR light visible, usually seen as a purple glow.

Arduino-IR-Tutorial-Electromagnetic-Spectrum-Diagram.png

 

IR Signal Modulation 

IR light is all around us, emitted by the sun, light bulbs and anything else that produces heat. So it would be easy to assume that an IR Receiver would pick up all this extra IR radiation rather than the one being sent by a transmitter. To prevent this extra radiation from interfering with the IR signal, a signal modulation technique is used.

In IR signal modulation, an encoder on the IR remote converts a binary signal into a modulated electrical signal. This electrical signal is sent to the transmitting LED. The transmitting LED converts the modulated electrical signal into a modulated IR light signal. The IR receiver then demodulates the IR light signal and converts it back to binary before passing on the information to a microcontroller as seen below in the diagram.

Arduino-IR-Remote-Receiver-Tutorial-IR-Signal-Modulation.png
IR Signal Modulation

A modulated IR signal is a series of light pulses switched on and off at a high frequency, this is known as the carrier frequency. The carrier frequency used by most transmitters is 38 kHz, because it is rare in nature and therefor can be distinguished from ambient IR noise. This way the IR receiver will know that the 38 kHz signal was sent from the transmitter and not picked up from the surrounding environment.

IR Receiver Test

I firstly wanted to test that my module was actually functional. I did this by finding a very simple circuit online which would allow me to do this.

I used a remote control form a Bose speaker as seen below to test that my module was working. When i pressed a button on the remote it sent a modulated 38kHz signal to my receiver and i could read this signal being sent using the serial monitor, this showed me that my receiver was fully functional.

thumb_IMG_0060_1024.jpg

Reference

http://www.circuitbasics.com/arduino-ir-remote-receiver-tutorial/

https://arduinomodules.info/ky-022-infrared-receiver-module/

https://learn.sparkfun.com/tutorials/ir-communication#receiving-ir-example