Doc1

9
IR Sensor Interface with PIC18F4550 In my previous project we have made a simple IR sensor Circuit . In this project, as promised before – we are going to demonstrate a PIC18F4550 microcontroller interface to IR sensor circuit. We are just going to glow few on the pic18f4550 as an example, however you can do some more intelligent operations by adding some more logics to the microcontroller coding. Interfacing infrared Proximity sensors with Microcontroller is quiet easy. This project is not only about interfacing an infrared IR sensor module but also we are going to learn - How to take digital input from a PIC18F4550 Microcontroller (Reading the Input with a Microcontroller). It means that the source code here will work same for taking input from a simple switch. You can replace the IR sensor with some simple PUSH Switch or some

description

Doc1-electronic work

Transcript of Doc1

IR Sensor Interface with PIC18F4550In my previous project we have made a simpleIR sensor Circuit. In this project, as promised before we are going to demonstrate aPIC18F4550 microcontrollerinterface to IR sensor circuit. We are just going to glow few on the pic18f4550 as an example, however you can do some more intelligent operations by adding some more logics to the microcontroller coding. Interfacing infraredProximity sensorswith Microcontroller is quiet easy.This project is not only about interfacing an infrared IR sensor module but also we are going to learn - How to take digital input from a PIC18F4550 Microcontroller (Reading the Input with a Microcontroller). It means that thesource codehere will work same for taking input from a simple switch. You can replace the IR sensor with some simple PUSH Switch or some othertypes of proximity sensors. In case of push button you would need to pull down the pin to ground with 1 k resistance, however for IR sensor you wont need to pull down the input pin with resistor.Infrared IR Sensor ModuleLets take a look at theIR Infrared Sensor Circuit Projectmodule that we made in our previous project which is an inexpensive ( Low Cost ) sensor circuit module. You can find the schematic and PCB design in my previous post. There are three pins in the Schematic - Two pins for providing the input voltage and GND to the IR Sensor Module, and the third pin from the IR module is the IR control pin. This Control Pin from the IR sensor Module will be interfaced to the PIC18F4550 microcontroller for sensor input.IR Sensor Module

ConceptThe output from the IR sensor circuit will be connected to pins of a PIC18f4550 microcontroller and the microcontroller will regard it as digital input to read either 1 or 0. According to the output from the IR sensor module, the PIC18F4550 will respond by glowing led. Since we just want to read some voltage in the microcontroller as input (either High or low) hence we are going to configure input pins as digital to read just 1 or 0 from the sensor.

PIC18F4550 Interface with IR sensor CircuitThe output from the IR sensor circuit is connected to RA0 of the pic18f4550 which is configured as input with TRISB registers, and the output will be displayed on LED connected across RD7, RD6,RD5 (PORTD) and RB0 and RB1 (PORTB) which are configured as output pins. Follow the schematic below.Schematic (IR sensor and PIC18F4550 microcontroller)

In this project we dont need to perform any Analog to Digital Conversion(ADC), hence we are going to turn the ADC off (ADCON0bits.ADON = 0) and configure all the PINS to Digital. At the default 1 MHZ oscillator frequency the output sometimes gives unstable result, hence tuning the microcontroller to 8MHZ solved the problem, Please note that pic18f4550 works by default on 1 MHZ and you can change the OSCCON bits settings to tune the oscillator frequency according to your requirement.Search in pic18f4550 datasheet for OSCCON register bits and you will find a nice description and bits settings table for available oscillator frequency and settings to configure the microcontroller oscillator frequency. Here I have configured the internal oscillator to 8MHZ to avoid switch debouncing. However it works well with 1MHZ settings as well. As a better plan the comparator is also turned off to avoid any conflict. All the resistors in the above Schematic is 1k resistance. If possible, it is also recommended to add aIC 7805liner Voltage regulator ICas a source of +5V to avoid any voltage fluctuation which could possibly damage the microcontroller. Make sure the input voltage to pic18f4550 must never exceed +5v. Please do read the excellent pic18f4550 datasheet provided by microchip.

Source Code:MPLAB IDE and C18 Compiler is used for compiling the source code, however MPLAB X IDE and XC8 Compiler can be also used with no difficulty. Download the entire project at the end of the source code below with compiled firmware.SOURCE CODE :infraredinput.c/** File: infraredinput.c* Author: ron* December 10, 2012, 1:21 PM*/#include // Include Header for PIC18F4550#define switch1 PORTAbits.RA0 // Switch on RA0#define led1 LATDbits.LATD7 // led1#define led2 LATDbits.LATD6 // led2#define led3 LATBbits.LATB0 // led3#define led4 LATBbits.LATB1 // led4#define led5 LATBbits.LATB2 // led5void main (void){/* If you want your microcontroller to work at 1MHZ then comment the three lines below */OSCCONbits.IRCF0 = 1 ; // set internal clock to 8 MHzOSCCONbits.IRCF1 = 1; // For Avoiding switch debouncing problemOSCCONbits.IRCF2= 1; ///* Input output settings*/TRISAbits.TRISA0 = 1; // RA0 Input for taking input from IR sensorTRISDbits.TRISD7 = 0; // Port D pins outputTRISDbits.TRISD6 = 0;TRISBbits.TRISB0 = 0; // Port B pins OutputTRISBbits.TRISB1 = 0;TRISBbits.TRISB2 = 0;CMCON = 0x07; // Disable ComparatorADCON1bits.PCFG0 = 1; // These 4 settings below determines the analog or digital inputADCON1bits.PCFG1 = 1; // In our case we are making all the pins digitalADCON1bits.PCFG2 = 1; // by setting them as 1111ADCON1bits.PCFG3 = 1;// Check with the datasheet for a nice desc of these bits and config.ADCON0bits.ADON = 0; // Disabled ADCwhile(1) { //Forever Loop if(switch1 == 1) // On reading IR sensor value ON { //Turn led ON led1 = 1; led2 = 1; led3 = 1; led4 = 1; led5 = 1; } else if ( switch1 == 0) // On reading IR Sensor Value OFF { //Turn led off led1 = 0; led2 = 0; led3 = 0; led4 = 0; led5 = 0; } else {} } //End While loop --forever}/* THE END */The coding above is pretty much self-explanatory, and comment lines must be also helpful for better understanding.Now you can use this project to develop your own application the way you want. It's easy to interface this project with aL293D Motor Driverto drive DC motors as an "IR sensor Motor Driver" or asimple IR infrared robot, or you could add one more IR module on some other AN pin as switch2, with which you can make it as"IR Line follower robot"very easily.In my next tutorial I have also shown how to interfacemultiple IR sensors with Arduino Uno boardand have the status of the sensor displayed on LCD.