PIC Lesson: IR Sensor


Cahaya Infrared ataupun dikenali IR, cahaya yang mempunyai panjang gelombang 0.7um hingga 300um (micro meter). Cahaya infrared tidak dapat dilihat oleh mata kasar.

Gambar di atas adalah set lapan IR Sensor. Setiap IR Sensor terdiri daripada IR Transmitter dan IR Receiver. IR Transmitter seperti LED yang berwarna putih manakala IR Receiver seperti LED yang berwarna hitam. Schematic IR Sensor adalah di bawah.

Berikut adalah videonya.

PIC Source Code

/*
Project: IR Sensor Lesson
Programmer: Shahrulnizam Mat Rejab
PIC (Crystal Frequency):
PIC16F877A (20MHz), PIC16F887 (20MHz), PIC16F1939 (Internal 32MHz) 
PIC18F4520 (10MHz), PIC18F4550 (10MHz), PIC18F46K22 (Internal 16MHz)
Compiler: HI-TECH C for PIC10/12/16 V9.83, PIC18 V9.80
Last Modified: 8 November 2013
Website: http://shahrulnizam.com
*/

#define	HD44780
#if defined (_16F877A) || (_16F887)
	#define	IR_SENSOR	PORTB
#elif defined (_16F1939) || (_18F4520) || (_18F4550) || (_18F46K22)
	#define	IR_SENSOR	LATB
#endif

#include "LibraryHardware.h"
#include "LibraryUniversalDisplay.h"

void setup()
{
initialize();
TRISA=0b00000000;
TRISB=0b11111111;
TRISC=0b00000000;
TRISD=0b00000000;
TRISE=0b00000000;
BUZZER=1;
lcd_init();	
lcd_goto(0,0);	
lcd_string(" IR SENSOR EXAMPLE  ");
}

void loop()
{
int i;
lcd_goto(2,2);
for(i=7;i>=0;i--){
	lcd_number(IR_SENSOR>>i,BIN,1);
	lcd_write(' ');}
}

36 responses to “PIC Lesson: IR Sensor”

  1. SALAM..
    ABG SHAHRUL BOLEH TNJUKKN X CM MNE NAK DISPLAY GUNA 7 SEGMENT..KITOWANG DH CUBE..X DPT LAH..HARAP2 ABG SHAHUL BLEH TLG SYE..SAYA NAK JADKAN IR 2 SEBAGAI UP AND DOWN COUNTER..P NAK PKAI 7 SEGMENT

  2. Salam..
    Terima kasih kerana sudi berkongsi ilmu di blog ini..
    Moga2 imlu yg dikongsi dapat menambahkan ilmu saudara(shahrul)

  3. Salam.
    En Shahrul, saya ada satu soalan.
    Kalau ir sensor ni digunakan pada keadaan suhu persekitaran yang tinggi spt pada “head” enjin motosikal, akan ada ape2 kesan tak pada sensor tu?
    Sensor tu akan berfungsi spt biasa ke?

  4. Hi shahrul, I am using a infrared sensor which is similar to the one you used in this site…the emitter and receiver have already attached together when I bought it…

    For the circuit connection, I connect a 330 Ohms resistor to the longer leg of the emitter and another side of the resistor I pull to 5 volts, the shorter leg of the emitter I connect to ground.

    For the receiver, I connect a 4.7k Ohms resistor to the longer leg of the receiver and another side of the resistor I pull to 5 volts, the shorter leg of the receiver I connect to ground.

    But when I try to approach a pure white paper to the sensor, why the sensor output only decrease insignificantly?
    Initially it is 4.5V, but when I put a pure white paper on top of the sensor the sensor output only decrease to 4.4V??? Is my circuit connection wrong??? Thanks a lot.

  5. salam, shahrul saya nk tanye, saya ade masalah nak combine kan circuit sensor saya dengan pic, saya gune pic18f452, saya cube untuk buat kalau sensor detect, led nyala, saya punye circuit sensor working,tp dekat pic xde keluar output seperti yang saya nak, boleh tolong bantu saya

  6. Hello picpicpikapika, (apo namo panjang naw),
    i think you should switch the IR receiver foot. The longer foot should be connected to ground and the pull up resistor 4.7 must be connect to the short leg of the receiver.By doing this, you should solve the problems.

  7. hi, shahrul, nak tanya apakah kegunaan LM324? adakah ia generate digital signal? kalau ya, boleh tak tunjur ajar bagaimana nak jadikan IR sensor sebagai suis? aku nak buat kalau REceiver tak receive IR ray maka suis tidak connected. kalau Tx receiver IR ray maka suis connected. Tolong berikan sikit petunjuk. Terima kasih

    • sookfun, LM324 digunakan sebagai comparator. Ia untuk bandingkan voltage di IR sensor dengan voltage reference, kemudian output nya digital.
      Buat apa nak gunakan IR untuk programkan suis. Programming sahaja dah boleh connect atau tidak connect dengan suis.

  8. shahrul..xda ke tunjuk scematic dari pic ke ic lm324?? saya dapat task untuk buat ir sensor mnggunakan ic lm324 control dari pic 16f877a, tapi lepas cek kat tenet,saya da test2 tp sume xjadi…da x cadangan untuk masalah saya ni..? or link yg sesuai utk buat ir sensor..

  9. resistor yang 1K R17 utk ape?? boleh x tunjuk kan gambar full PIC ke ic lm324 yg utk projek ni..? sbb tak familiar pon dengan ic lg,so tak tahu connection yg dari port B tu ke ic masuk kat mne..??

  10. hello, saya de tengok certain design connect IR LED dengan pnp transistor…pe fungsi guna LM324 dan guna pnp transistor?

Leave a Reply to Shahrul Cancel reply

Your email address will not be published. Required fields are marked *