18F PIC Tutorial - IR Modulated UART

Want to be able to transmit
data effectively, reliably, and most importantly wirelessly? Well
Infrared modulated UART could
be the cheapest and most effective solution. This guide provides a
great solution for remote control and wireless data communication,
and while it only covers the fundamentals, the possibilities are up
to you. With the circuits/products and programs listed below, I have
been able to achieve over 20 meters
of IR data communication.
This little example all came
about when I wanted to make my own IR controlled applications, but
the idea of a simple IR switch didn't really phase me, I wanted
something that could transmit data back and forth. Taking a step
sideways, I instantly thought about UART (a one wire serial
protocol), and how it could be used with a concept like this.
Before I go much further, a
quick burst on IR. Infrared has its flaws, the primary issue is
line of sight, although the
LED's I used proved to be extremely effective at dispersing IR (I
could even transmit around corners). The other issue to consider -
is the amount of Infrared found in
ambient light from the sun. It is after all just another
spectrum of light, one that can not be detected by the human eye. So
how does one go about transferring data in a reliable manner that
accommodates for ambient IR being present? Answer;
modulation.
In this case, the modulated
signal has a carrier of 38Khz, and turns on/off to encode the data.
The result is bursts of 38Khz signals being transmitted. Building a
reliable receiver would be a tough task, but why re-invent the wheel
when there are
Logic Output Infrared Sensors available to do the job for you.
This device will remain a logic high until it detects a 38Khz
signal, and then it will go low (0V) if a signal is present;


Note the PIC's
power supply/oscillator are not shown
The next task is to somehow
turn UART data signals into 38Khz modulated IR... Why don't we
utilise the onboard
Pulse Width
Modulation built into almost every PIC? If you setup the PWM
output for 38Khz and 50% Duty Cycle, then the next objective is to
somehow turn it on/off at the same time a UART output is normally
on/off. I can think of two methods, one is to use a Logic AND gate,
placing the PWM signal on one input, and the UART TX on the other -
the result would be Modulated UART with a 38Khz carrier. But you can
do this without the requirement of
external components by controlling the Tristate status
(input/output) of the PWM Pin. By making the PWM pin an input, you
are turning off the 38Khz signal, and of course enabling it again
when returned to an output. Here's what a snippet of a modulated
signal would look like;

Creating infrared light is
simple, there are many types of LED's available to do the job. Make
sure though, for best performance, that the
Infrared LED's wavelength matches the
receiver for optimal performance. To get the best range, also
use a high quality and powered IR LED. I am using the
100mA Infrared LED from
Core Electronics.

The Infrared LED can utilise
much more current then the PIC can output, and there are a variety
of methods to drive such devices. I prefer to use the
ULN2003 or
Logic Level MOSFET's for jobs like this. Note that the output of
the PWM is tied down to earth with a 100K resistor. This is used to
instantly shunt any residual voltage to earth when the pin is made
an input - not so much required with the ULN2003, but is vital with
MOSFET's. The 25ohm resistor in series with the LED was calculated
by R = V/I where V = 5
(supply) - 1.6 (Vf of the LED) - 0.9 (Vf of the ULN2003) and I = 0.1
(If of the LED).
Therefore R = (5 - 1.6 - 0.9)
/ 0.1 = 25 ohm or greater

Note the PIC's
power supply/oscillator are not shown
The library I modified can be
found
here. For more information on how to use
Swordfish libraries,
have a look here. Now you can send/receive data just as if it
were a normal UART connection, here's an example program;
Transmit Program:
Device = 18F2550
Clock = 8
Config FOSC = INTOSCIO_EC
Include "INTOSC8.bas"
Include "pwm.bas"
Include "IR_UART.bas"
Dim Variable As Byte
// start of main program
PWM.SetFreq(38000, 50)
IR_UART.SetTX(PORTC.2)
IR_UART.SetMode(umTrue)
IR_UART.SetBaudrate(sbr300)
Variable = 0
Low(PORTC.0)
While True
Inc(Variable)
IR_UART.Write(Variable)
DelayMS(500)
Wend
Receiver Program:
Device = 18F2550
Clock = 8
Config FOSC = INTOSCIO_EC
#option LCD_DATA = PORTB.4
#option LCD_RS = PORTB.0
#option LCD_EN = PORTB.1
Include "INTOSC8.bas"
Include "IR_UART.bas"
Include "convert.bas"
Include "lcd.bas"
Dim Variable As Byte
IR_UART.SetRX(PORTC.2)
IR_UART.SetMode(umTrue)
IR_UART.SetBaudrate(sbr300)
DelayMS(150)
LCD.Cls
LCD.WriteAt(1,1,"IR UART")
While True
IR_UART.Read(Variable)
LCD.WriteAt(2,1,Convert.DecToStr(Variable,3))
Wend
Video Tutorials:
-
Coming Soon...
- Coming Soon...
Download INTOSC8
Library as seen in the program
Download IR_UART
Library as seen in the program
Download PWM
Library as seen in the program

 | Site Tutorial Index |
|  | 16F PIC Examples |
|  | 18F PIC Examples |
| |  | 7 Segment Displays |
| |  | 7 Segment Displays |
| |  | RS232 and UART |
| |  | Code Snippets |
|  | Handy Tips |