Bukit Mertajam, Penang

How does SN-IRS-01 work?

IR sensnor set(SN-IRS-01)-280x373

Hello everyone! Today, I am going to discuss about a very special light sensor (drumrolls), the INFRARED SENSOR! Many people have been asking about the operation of the infrared sensor,  the most common questions being “How do I use it?” and “Do I need to use any comparator for this sensor?” I hope with this tutorial, you can get a clearer picture on how this sensor works.

The figure shows the Infrared sensor set, SN-IRS-01. These sensors are actually diodes. The black one is the infrared receiver, which is the sensor while the white one is infrared LED which act as the transmitter. Infrared light is not visible under naked eye. However, if you are very curious about what the light looks like, you can see the light through a camera. Even a simple smartphone camera can do the trick! You will see a pink light. That is the infrared light.

When the infrared light is shined on a surface of an object, it will be reflected. The angle of reflection depends on the colour of the surface of the object. The receiver will receive the reflected signal. By using an LED (i.e. the IR transmitter) which produces light at the same wavelength as what the sensor (i.e. the IR Receiver) is looking for, you can look at the intensity of the received light. When an object is close to the sensor, the light from the LED bounces off the object and into the light sensor. This results in a large jump in the intensity, which can be detected using a threshold.

A Comparator is a circuit built to compare the voltages between the 2 inputs at the op-amp and outputs either a HIGH or LOW. In other words, it acts as an ANALOG-TO-DIGITAL converter. The figure shows a simple comparator circuit with the IR Sensor. The voltage value of the IR sensor at the ‘+’ pin is compared with the voltage reference at the ‘-‘ pin. If the voltage of the IR sensor is lower than the threshold voltage, which means the positive value is less than the negative value, the output of the OP AMP is LOW. If the voltage of the IR sensor is higher than the threshold voltage, which means the positive value is higher than the negative value, the output of the OP AMP is HIGH.

hello

For setting up this IR sensor, there is already a tutorial  here. In this tutorial, I’m going to show how to display the analog value of the IR receiver by using a microcontroller CT-UNO. Since we want to display the analog value of the sensor, so we are not going to use comparator in this circuit. Of course, if you really want a clean results, the output from the sensor should be filtered to eliminate external noise. If you don’t want to read the analog value of the sensor, you can build a simpler circuit by using the comparator without using a microcontroller. 😀

Hardware needed:

DSC00183

Part 1:

Let’s do something simple first. Lets read the analog value of this sensor. The connection are as shown below:

Coding:

[code lang=”c” highlight=””]
#define reading A0
unsigned int value;

void setup() {
// put your setup code here, to run once:
pinMode(reading,INPUT);
Serial.begin(9600); //set the baud rate of the serial monitor
Serial.print(“Value”);
Serial.println (“\tVoltage”); //”\t” create spacing of a tab
}

void loop() {
// put your main code here, to run repeatedly:
value = analogRead(reading); //take reading from sensor
float raw_voltage = value*0.00488; //convert analog value to voltage
Serial.print(value); //display analog value in serial monitor
Serial.print(“\t”);
Serial.println(raw_voltage); //display value of voltage in serial monitor
delay(1000); //create a delay of 1s
}[/code]

value
Overview:

  • Based on the results printed on the serial monitor, the left column shows the analog value from the analogRead(pin) command, which is the 10-bit ADC value, display in decimal format.
  • I further convert the value of the analog value to the voltage by multiplying the analog value with 0.00488. How did I get this value? Since it is a 10-bit value, the total number of steps is 2^10 = 1024 steps. So to calculate the voltage drop of one step: 1/1024 * 5 = 0.00488V/step.
  • We can see that the voltage decreases as the object gets nearer to the sensor; and the voltage increases as the object gets farther. With this deduction, we can start to customize the code and set some conditions.

Part 2: Detecting an object distance

In this program, I will be using a buzzer to detect the distance of an object. The maximum distance an object can be detected is around 30-32 cm (based on observation. It is not a specific value).

Should the object be 8cm from the sensor, the buzzer will beep once.

Should the object be 7cm from the sensor, the buzzer will beep at a shorter period of times.

Should the object be 4cm from the sensor, the buzzer will beep in a long pulse.

Set up

Coding:

[code lang=”c” highlight=””]
#define reading A0
#define buzzer 12
unsigned int value;

void setup() {
// put your setup code here, to run once:
pinMode(reading,INPUT); //declare pin A0 as input
pinMode(buzzer,OUTPUT); //declare pin 12 as output
Serial.begin(9600); //set the baud rate of the serial monitor
Serial.print(“Value”);
Serial.println (“\tVoltage”); //”t” create spacing of a tab
}

void loop() {
// put your main code here, to run repeatedly:
value = analogRead(reading); //take reading from sensor
float raw_voltage = value*0.00488; //convert analog value to voltage
Serial.print(value); //display analog value in serial monitor
Serial.print(“\t”);
Serial.println(raw_voltage); //display value of voltage in serial monitor
delay(100); //create a delay of 0.1s
if(raw_voltage>4 && raw_voltage<4.2){ //object is less than 8cm form sensor digitalWrite(buzzer,HIGH); delay(1000); digitalWrite(buzzer,LOW); delay(500); }else if(raw_voltage>3 && raw_voltage<4){ //object is less than 7cm from sensor
digitalWrite(buzzer,HIGH);
delay(100);
digitalWrite(buzzer,LOW);
delay(100);
}else if (raw_voltage<3) //object is less than 4cm from sensor
digitalWrite(buzzer,HIGH);
}[/code]

Code overview:

  • I have set some conditions for the sensor. Note that this is just some rough estimation. It may vary for you.
  • The object that I used to reflect the sensor is a white paper. p/s: White colour has better reflection.

This sensor is a 4 pin sensor, unlike the previous one but the components are the same. In the previous module, we connect both emitter of the receiver and the cathode pin of the transmitter together as GND. In this 4 pin socket sensor, you need to manually connect the emitter and the cathode pin.

Enjoy! Thank you so much for reading this tutorial! If you have any comments or question please feel free to post them in our technical forum! 😀

Attachment code: IR SENSOR

Inside the zip file, there are 2 file names:

1. To read analog value of the sensor –> open Arduino_IR_Serial

2. To use buzzer along with the sensor –> open Arduino_IR_Buzzer

Buy

Related Posts

Comments (6)

Hello!

Very instructive. Thank you.

if i use arduino uno
in that case any changes must be required

what if i have to increase the distance the ir sensor is measuring

Daniel Fernandes

Hi friend! Where is the ZIP file?
Thank you

أردوينو 1.8.3 Hourly Build 2017/05/31 06:33 (Windows 10), لوحة:”Arduino/Genuino Uno”

sketch_jan08:1: error: stray ‘\342’ in program

[code lang=â€‌câ€‌ highlight=â€‌â€‌]

^

sketch_jan08:1: error: stray ‘\200’ in program

sketch_jan08:1: error: stray ‘\235’ in program

sketch_jan08:1: error: stray ‘\342’ in program

sketch_jan08:1: error: stray ‘\200’ in program

sketch_jan08:1: error: stray ‘\235’ in program

sketch_jan08:1: error: stray ‘\342’ in program

sketch_jan08:1: error: stray ‘\200’ in program

sketch_jan08:1: error: stray ‘\235’ in program

sketch_jan08:1: error: stray ‘\342’ in program

sketch_jan08:1: error: stray ‘\200’ in program

sketch_jan08:1: error: stray ‘\235’ in program

sketch_jan08:11: error: stray ‘\342’ in program

Serial.print(“Valueâ€‌);

^

sketch_jan08:11: error: stray ‘\200’ in program

sketch_jan08:11: error: stray ‘\234’ in program

sketch_jan08:11: error: stray ‘\342’ in program

sketch_jan08:11: error: stray ‘\200’ in program

sketch_jan08:11: error: stray ‘\235’ in program

sketch_jan08:12: error: stray ‘\342’ in program

Serial.println (“\tVoltageâ€‌); //â€‌tâ€‌ create spacing of a tab

^

sketch_jan08:12: error: stray ‘\200’ in program

sketch_jan08:12: error: stray ‘\234’ in program

sketch_jan08:12: error: stray ‘\’ in program

sketch_jan08:12: error: stray ‘\342’ in program

sketch_jan08:12: error: stray ‘\200’ in program

sketch_jan08:12: error: stray ‘\235’ in program

sketch_jan08:20: error: stray ‘\342’ in program

Serial.print(“\tâ€‌);

^

sketch_jan08:20: error: stray ‘\200’ in program

sketch_jan08:20: error: stray ‘\234’ in program

sketch_jan08:20: error: stray ‘\’ in program

sketch_jan08:20: error: stray ‘\342’ in program

sketch_jan08:20: error: stray ‘\200’ in program

sketch_jan08:20: error: stray ‘\235’ in program

sketch_jan08:1: error: expected unqualified-id before ‘[‘ token

[code lang=â€‌câ€‌ highlight=â€‌â€‌]

^

C:\Users\admin\Documents\Arduino\sketch_jan08\sketch_jan08.ino: In function ‘void setup()’:

sketch_jan08:11: error: ‘Value’ was not declared in this scope

Serial.print(“Valueâ€‌);

^

sketch_jan08:12: error: ‘tVoltage’ was not declared in this scope

Serial.println (“\tVoltageâ€‌); //â€‌tâ€‌ create spacing of a tab

^

C:\Users\admin\Documents\Arduino\sketch_jan08\sketch_jan08.ino: In function ‘void loop()’:

sketch_jan08:17: error: ‘value’ was not declared in this scope

value = analogRead(reading); //take reading from sensor

^

sketch_jan08:20: error: ‘t’ was not declared in this scope

Serial.print(“\tâ€‌);

^

C:\Users\admin\Documents\Arduino\sketch_jan08\sketch_jan08.ino: At global scope:

sketch_jan08:30: error: expected unqualified-id before ‘[‘ token

}[/code]

^

exit status 1
stray ‘\342’ in program

هذا التقرير قد يحتوي على معلومات أكثر مع تفعيل الخيار
“Show verbose output during compilation”
في ملف -> تفضيلات

Thx I was linked this by my uncle who works with and teaches mentaly and phsicaly disabled kids and he needs my help with the kids that are blind he would supply me with materials i request to make a device to help the blind kids we are makeing two of them so this could be useful for both.

Leave a comment

Loading...