The iRX 2.2 ...where atoms meet bits

The need for "Digital Glue"

At the MIT Media Lab, we constantly
explore new metaphors for interconnecting the physical world and the digital
realm. We monitor heart rates for affective computing, display the latest
stock quotes for individuals, create new sensors for electronic tags, and
build LEGO toys that respond to voice commands. Each of these activities
binds some physical process or action to a corresponding digital expression.

To simplify this process of binding "atoms" to "bits," the Personal
Information Architecture group has created a simple interface device
named the "iRX 2.2." The iRX is a circuit card measuring 1.25" ×
3" with an RS-232 serial port, a visible LED, an infrared LED, and an infrared
detector. At the heart of the board is the PIC16F84A, a nifty little microcontroller
made by Microchip Technologies, Inc.
The PIC16F84A features thirteen general I/O ports, instruction cycle times
of 200 nSec. and an efficient RISC-like instruction set. The iRX 2.1 uses
five of the PIC's I/O ports; the remaining eight ports are available for
user applications. The total cost of the iRX 2.1 is under $20 US when built
in modest quantities.

About this Document

This document is written primarily for anyone who wants to create an interface
using the iRX 2.2. It is also useful for anyone who wants to understand
the process of developing applications for microcontrollers in general,
and for Microchip PIC processors in particular.

Here's what you'll find in this document:

Section 1 offers some ideas of the kinds of interfaces
you could build using the iRX 2.2 and gives an overview of the development
process.

Section 2 gives a guided tour of the iRX 2.2 board,
and includes the board layout, the schematic, and a detailed description
of each major component on the board.

Section 3 tells you everything you'll need to know
to build your own iRX 2.2 board, including a detailed Bill of Materials
for the board and fabrication hints.

Section 4 details the process of designing and programming
the iRX 2.2 board.

Acknowledgements

The author gratefully acknowledges help, support and inspiration from the
following people. Fred Martin,
Brian Silverman and members of Mitch Resnick's
Epistemology
and Learning Group introduced me to PIC chips and IR emitters and detectors.
The design of the iRX 2.1 owes much to the
Interface
Cricket, one of the many members of the
Cricket
family designed by Fred and others.

Pushpinder Singh
and the MIT Robotics and Electronic Cooperative kept things moving through
the formative stage of the iRX board. Push volunteered many of his hours
to teach our group about PICs and his design for a PIC-based "chord keyboard"
helped shape the physical design of the first iRX boards.

Encouragement and help from past and present members of the Personal
Information Architecture group--especially from professor Mike
Hawley, colleagues Maria Redin,
Steve Gray, Manish Tuteja and John
Underkoffler--has made this a fun project.

The first iRX boards were built in April of 1996. Since that time,
hundreds of iRX boards have been built and put into various forms of service.
Some iRX boards found their way to the top of Mount Everest, others have
been stuffed inside plush toys, glued under ping-pong tables, suspended
from florescent lights, and one was even concealed in an office chair as
part of a "digital whoopee cushion." The author would like to thank everyone,
both at the Media Lab and beyond, who has contributed to the success of
the iRX project.

1.0

Overview

What you get out of the iRX 2.2 depends on what you put into it. It's unlikely
that there will be any "pre-canned" configurations that exactly suits your
needs--you will generally need to add custom hardware on the board and
create a PIC program specifically for your application. But that's the
fun part of the process!

To give you a taste of what's possible with a PIC and iRX board,
here are some projects that people have created using the iRX 2.2 (or
should
have created):

A serial "TV Remote" interface. The iRX board can be programmed to convert
between serial data and the "Sony IR" remote protocol, allowing your computer
to control your stereo system, and allowing you to control your computer
with a TV Remote control.

An autonomous weather station. The PIC measures temperature (via a thermistor),
windspeed (counting pulses on a tachometer), and direction (via digital
shaft encoder) and transmits the data over a wireless link to a host system.

A MIDI <=> Serial interface converter. The PIC can be programmed to
behave as a dual UART with baud rate conversion with buffering. The addition
of a photo-isolator would make a complete interface.

Overview of the Development Process

This section briefly describes the steps you'll need to take to create
your very own iRX application.

Ready...

The first step is to make sure you have all the materials at hand. You'll
need both some hardware and some software:

Acquire one or more iRX 2.2 boards.Section 3
gives you information on how to fabricate the iRX 2.2 PC boards, where
to purchase the required components, and how to assemble your own boards.

Acquire a PIC C compiler. At $100 per license, the PCM compiler
from Custom Computing Software (CCS)
is an excellent bargain. Any code examples in this document presume the
use of the PCM compiler.

Obtain a PIC programmer. The PICSTART Plus programmer from
Microchip is available from DigiKey
and is upgraded regularly to support the newest PIC chips. Examples in
this document presume that you're using a PICSTART plus.

Obtain a copy of MPLAB. MPLAB is an integrated development environment
from Microchip. It includes a text editor, assembler, simulator, and (most
importantly) the drivers for the PICSTART Plus PIC programmer. Best of
all, you can download
it from the WEB free of charge.

Set...

It's unlikely that there's any pre-canned firmware for the PIC and iRX
2.1 that will do exactly what you want. Usually, you'll need to write a
program for the PIC (or perhaps modify an existing one). In many cases,
you'll also be adding components or wires to the iRX 2.1 board.

Don't be shy; the iRX was designed to be modified!

Understand the fundamentals of the PIC architecture. Appendix A
has pointers to the Microchip site, where you can find detailed data sheets
on the PIC16F84.

Find related firmware. Perhaps someone has already created something
similar to what you need. There are three rich sources of firmware: the
Microchip Application Notes (but those are written in assembly), the PCM
C Compiler example directories, and the iRX 2.2 firmware directory. Consult
these resources first; it can save you lots of time.

Decide on what modifications, if any, you must make to your iRX board.Section
4 details the features and differences of the various I/O pins available.

Go...

Creating and downloading code for the PIC is a five step process, described
in the picture and in the text below.

Figure
1: The development process

Write your program using your favorite text editor or the editor
included in Microchip's MPLAB. Generally, you'll write your program in
C. In exceptional cases, you may write your program in assembly code.

Compile your program. Normally, you'll use the MPLAB environment
to invoke the CCS PCM compiler. PCM will compile your C code into a .hex
file. If you've written assembly code, MPLAB will assemble your .asm file
into a .hex file.

Program a PIC16F84A by downloading the .hex file (using MPLAB) to
the PIC programmer.

Run your application. Insert the PIC into an iRX board, power it
up, and watch it run. If it runs, hooray! If it doesn't run, go to the
next step.

2.0

Guided Tour of the iRX 2.2

This section describes the layout and schematic of the iRX 2.2. It will
show you where each component is located, and describes what each component
does.

Figure 2 shows the layout of the components on the iRX 2.2 board,
Figure 3 is the schematic for the board.

2.1 Layout of the iRX 2.2

Figure 2: Layout of the iRX 2.2 board

Note that this layout is slightly out of date and needs to be corrected.
The microprocessor is a PIC16F84A, the resonator is 20 MHz, and the RC
low pass on the voltage source of the IR receiver is not shown.

2.2 Schematic of the iRX 2.2

Figure 3: Schematic of the iRX 2.2 board

2.3 Component Description

2.3.1 J1: DC Power Jack

The DC power jack is a standard 20 mm barrel connector. You can plug a
9V "wall wart" power supply into it (center pin positive), or you can wire
a plug onto a 9V battery cable and power the iRX 2.1 from a standard 9V
battery. The regulator on the board is fairly forgiving, and the circuit
will probably work if you provide anything between 7 volts and 15 volts
DC.

2.3.2 J2: Serial I/O Jack

The serial I/O jack, J4, is a telephone-style RJ-11 "modular jack". Figure
4 shows how you can use a standard telephone cable and an RJ11 to DB9F
adaptor to connect the iRX to the serial port on a computer.

Figure 4: Serial I/O port and cable

Look carefully at the order of the connections on J4 compared to the order
on the RJ11 to DB9F connector -- they're the same! Why doesn't this end
up connecting the iRX serial output directly to the computer's serial output
(rather than to the input)? This is because a standard telephone cable
has a "twist" in it, so the connections are mirrored from one end to the
other. If you follow the connection from TxD on the iRX side, you'll see
it connects to RxD on the host computer side.

The "twist" in the telephone cable also makes it possible to hook
two iRX boards together directly without creating a special "null modem"
cable.

In some cases, it's possible to steal enough power from the host
computer's DTR and RTS modem control lines to power the PIC and run the
iRX board without a battery or external power supply. To take advantage
of this, you must use a 6-pin modular cable and connect the DTR and RTS
lines as indicated by the gray lines in Figure 4. But there are several
caveats. First, not all computers have enough drive in their serial ports
to drive all PIC boards. Secondly, the controlling application on the host
computer must hold DTR and RTS high. Your mileage may vary.

2.3.3 J4: PIC I/O Ports

J4 on the circuit board isn't really a connector, it's just a row of ten
holes that provide access to power, ground, and the PIC's I/O ports. You
can solder a "SIP socket" into these ten holes if you want a simple means
to temporarily connect wires and components to your iRX board.

The layout of J4 appears as follows (and is also printed on the
circuit board). A description of the characteristics of each I/O is in
Section 4.

Figure 5: J4: I/O Ports

2.3.4 J5, J6: Ground and Power

To the left of the prototype area on the circuit board are two columns
of holes. All of the holes in the left column are connected to +5 volts,
all the holes in the right column are connected to ground.

Figure 6: J5, J6: Power and Ground

2.3.5 D1, D2, D3: Power Collecting Diodes

D1, D2, and D3 are three Schottky diodes (1N5817). D1 connects to J1 (the
DC input jack), D2 and D3 connect to the serial port, and can be driven
from modem control lines from your computer's serial port. The diodes serve
two purposes. First, they protect the circiut: if you connect the DC supply
backwards, nothing bad will happen. Second, these diodes serve as a "power
or" circuit: you can supply a voltage through the DC jack OR through the
serial connector; the iRX board will get the power wherever it can.

2.3.6 U1: 78L05 voltage regulator

The voltage regulator on the iRX board is a garden variety +5V linear regulator.
Depending on the exact model, it will accept voltages as low as 6.5 volts
and as high as 15 volts and produce a clean +5 volts for the devices on
the iRX board.

2.3.7 U2: MAX233 RS232 Level Converter

The MAXIM MAX233 chip converts between "TTL levels" (0 to +5 volts) of
the PIC and "RS232 levels" (+9 to -9 volts) of the serial line. The magic
of the MAXIM chip is that it does this conversion with a single +5 V supply;
oscillators and charge pumps internal to the chip generate the required
+ and - 9 volts.

The price of this magic is in power consumption: the MAX233 consumes
about 5mA whether or not serial data is being sent or received. If your
application doesn't require serial I/O and you're concerned about power
consumption, simply omit this chip when you're building the board.

2.3.8 U3: PIC16F84A

The PIC is the heart of the board. It's a programmable microcontroller
with 1K words of program memory and 68 bytes of general purpose RAM. It
has 13 general purpose I/O ports. On the iRX board, five of these ports
are dedicated (Serial In, Serial Out, Infrared Receive, Infrared LED, RED
LED). You can use the remaining eight ports for anything you wish.

2.3.9 U4: Sharp IS1U20 IrDA receiver

The Sharp IS1U20 is an IrDA-compliant infrared receiver. When it detects
IR, it outputs a low signal. You can write code for the PIC to decode IrDA
signals (such as sent by laptops and PDAs) or many other IR communication
protocols.

2.3.10 L1: Infrared LED

The infrared LED cited in the bill of materials has a wavelength of 940
nM and a 40 degree dispersion pattern. You can use an IR LED with a narrower
pattern (e.g. 20 degrees) for applications that need a longer range.

As an aside, note that the IrDA specifications calls for a wavelength
of 850 to 900 nM. DigiKey doesn't carry any infrared LED emitters in that
range, and the 940 nM LED we use seems to work passably well.

2.3.11 L2: RED LED

The Red LED is any garden variety LED. It's typically used simply as a
status indicator (e.g. flash upon startup), so it doesn't even need to
be red.

2.3.12 Y1: 20MHz Ceramic Resonator

A ceramic resonator is like a crystal oscillator, only it's slightly less
accurate and considerably less expensive. This particular part has the
built-in capacitors required to make it oscillate properly. Resonators
with other frequencies are available, but 20MHz was chosen since it's the
maximum specified frequency for the PIC16F84A.

3.0

Building an iRX 2.2

This section details the process of building complete iRX 2.2 boards from
scratch:

Fabricate the iRX PC boards

Purchase the components

Solder the components onto the PC board

3.1 Fabricate the iRX PC Boards

Since you can't simply go to Radio Shack and purchase an iRX 2.2 (yet),
you'll have to have the Printed Circuit (PC) Boards fabricated by a PC
Board fabrication shop. Be warned: a shop will charge almost as much to
make one printed circuit board as to make 100. When possible, pool your
orders to make as many iRX boards as possible at once.

3.1.1 Choose a PC Board Fabrication shop

There are many companies that will fabricate PC boards directly from machine-readable
design files. See Appendix A for a short list of places
that have been recommended.

3.1.2 Send the PC Board design files

The design files for the iRX 2.2 can be found in the design
files directory. For PC Board fabrication, the most important file
is irx2_2.zip, a single ZIP file that
contains all the files a PC Board Fabrication shop will need to make the
boards:

File name

Description

irx2_2.gtl

Gerber format: Top ("component") side traces

irx2_2.gbl

Gerber format, Bottom ("solder") side traces

irx2_2.gts

Gerber format, Top side solder mask

irx2_2.gbs

Gerber format, Bottom side solder mask

irx2_2.gto

Gerber format: Top side silk screen

irx2_2.gko

Gerber format, Board outline

irx2_2.txt

NC drill file

irx2_2.drr

"Drill report" (indicates drill sizes) for NC drill file

Contents of irx2_1pc.zip

For most fabrication shops, all you need to do is send them the ZIP file
and a README.TXT file. The README.TXT tells the fabrication shop the particulars
of your order - usually just the names of the files and what format/layer
they are (as above). Normally, you'll send the files via e-mail or by uploading
them on a modem line.

In a week or two, you should receive your boards. In the meantime,
you can purchase the components.

3.2 Purchasing the components

All of the components, with the exception of the infrared detector and
the printed circuit board itself, are available from Digi-Key, a mail-order
electronics part supplier. While Digi-Key is not always the cheapest, they
have a large selection, take telephone credit card orders, and will get
you your parts the next day via FedEx. If you have more time than money
on your hands, you may be able to shop around and get a better price on
some of the parts shown here.

The following lists detail the parts require to build 100 iRX
2.2 boards. The first list specifies components that go on the circuit
board. The second list shows ancillary components that are generally useful
for complete iRX projects, but are not always required.

When possible, I've shown Digi-Key's part numbers and prices (as
of February 2001).

Auxiliary Components for the iRX

3.3 Soldering the components onto the PC board

The only components that don't care which way they're inserted
are the resistors (R1, R2, R3, R4), the ceramic capacitors (C3, C4), and
the ceramic resonator (Y1). For all others, use the silk screen printed
on the board as a guide. Note that for each LED and electrolytic capacitor,
the longer wire goes in the hole marked with a '+'.

To keep components from falling out while you're soldering others
(since you have to flip the board over to solder the leads) work from the
"shortest" to the "tallest" components in the following order:

The test leads TP1 and TP2 are simply little wire loops, but they're useful
when it comes time to debug the board since they give you a handy place
to connect oscilliscope probes. To make the wire loops, take two pieces
of leftover lead wire clipped from a resistor or capacitor, bend them in
the jaws of needle nose pliers to form a .1" rectangular loops. They should
look something like this when you're done:

In the picture above, notice how the Red LED faces "up" and the infrared
LED's leads are bent so it faces "forward." This way, the infrared LED
"speaks" in the same direction that the infrared receiver "listens".

Be careful not to overheat the sensitive components during soldering,
especially the voltage regulator (U1), the IR detector (U4), and the MAX233
(U2).

4.0

Developing an Application

The steps towards creating your PIC application are straightforward and
have been outlined in Section 2. In this section, we
jump into the details.

4.1 Not all I/O Ports are created Equal

The PIC16F84 has thirteen ports: RA0 - RA4 and RB0 - RB7. Of these, the
iRX 2.2 "reserves" RB0 - RB3 and RB5 for serial and optical inputs and
outputs. (Note: you can still use RB0,RB2 and RB4 if you're willing to
give up optical inputs and outputs) Some ports have particular traits that
may make them more or less suited for your particular application. The
following table summarizes the differences; for the full scoop, consult
the Microchip data sheets.

Except as noted, all ports support TTL levels and are bi-directional
under program control.

Table 3. Summary of I/O ports on PIC16F84

Port

Reserved For

Traits

RA0

(available)

RA1

(available)

RA2

(available)

RA3

(available)

RA4

(available)

Schmitt Trigger input, Open collector output (needs 5K resistor to
Vcc to use as an output), can be programmed to be input to TMR0 clock

RB0

IR In

can be programmed for external interrupt (INTF)

RB1

Serial Out

RB2

Red LED Out

RB3

IR LED Out

RB4

(available)

can be programmed for interrupt on change (RBIF)

RB5

Serial In

can be programmed for interrupt on change (RBIF)

RB6

(available)

can be programmed for interrupt on change (RBIF)

RB7

(available)

can be programmed for interrupt on change (RBIF)

The iRX 2.2 board leaves Port A entirely untouched. This was a conscious
decision, making it possible to substitute other 18-pin PIC chips, such
as the PIC16C71. The PIC16C71 isn't an EEPROM device, but it does have
a four-channel A/D converter.

4.2 C versus Assembly code

Whenever possible, program in C instead of in assembly language. You'll
find the development and maintenance of firmware programs to be much faster
and easier. However, there are some times when you may still need to program
in assembly:

Time critical applications, where you must be able to account for every
cycle. In general, you can't rely on what code the C compiler will generate
nor what run-time support code will be run in the course of execution.

Code critical applications. The C compiler can't assume much about the
state of ports and registers, so it generates some extra code in the name
of saving and restoring state.

In all of my PIC programming experience, I've never had the need to write
the entire program in assembly. I've always found it better to write the
bulk of my code in C and to use the #asm construct where required.

The moral: think three times before you decide you need to write
in assembly code!

4.3 PIC C Compilers

We use PCM, a C compiler by Custom Computer Services. For $99 for a single
license or $50/user for a site license, it's a good deal. Contact and purchasing
info can be found in Appendix A.

4.4 PIC Assemblers

We use MPASM, the free PIC assembler from Microchip. It's a DOS-only application,
but has proven to be robust and reliable. Randy Sargent also has a free
Unix-based PIC assembler named picasm. See
Appendix
A for where to find them.

4.5 PIC Simulators

A PIC simulator lets you step through your program, set breakpoints, examine
registers; all those things you can't do with a real chip. I've used the
simulator built into MPLAB with good results.

Randy Sargent has written picsim, a PIC simulator that
runs under Unix. Consult Appendix A for where to find
them.

4.6 Burning your program into the PIC

Though there are many PIC programmers available, we've settled on the PICSTART
Plus programmer from Microchip. You can purchase it via Digi-Key (Universities
should ask for the educational discount), and Microchip is good about providing
upgrades for the programmer as they introduce new chips.

The easiest way to download your program into the PIC is through
the PICSTART Plus menus found in MPLAB.

5.0

Debugging Tips & Techniques

Following is a random collection of tips, techniques, art and lore. Your
mileage may vary. Press down while turning. Void where prohibited.

5.1 First principles

When you write your program, you should always make the Red LED
flash briefly upon startup, just to indicate that the chip is alive and
running.

So assume you've written your program, compiled and loaded the
code, and powered up your iRX. And the LED doesn't flash. Now what?

Are you getting power? Measure with an oscilliscope between the V+ test
point and GND test point. You should see a clean +5 volts. If not, make
sure that the PIC is inserted correctly: the "dimple" (indicating pin 1)
should be pointing towards 10K resistor at the "top" of the board.

Is the PIC's oscillator running? Look between ground and pin 15 on the
PIC using a 10x oscilliscope probe. (The impedance of a 1x probe is sufficiently
small to kill the oscillations.) If you don't see a 20MHz waveform, did
you remember to configure the programming fuses for an "HS" oscillator
type"?

5.2 Debugging

Program all unused I/O pins to be outputs. This way, you can set the state
of a particular I/O pin to be TRUE when you enter a routine and FALSE when
you leave it. At the very least, this practice, along with an oscilloscope,
can give you confirmation that a particular piece of code is being executed.

Make the board do something visible or audible at startup. In the case
of the iRX 2.0, consider turning on the Red LED for a good 500 mSec or
longer after completing the initialization sequence and before starting
the main program. This can save you lots of time in the heat of battle.

If you're using the RTCC to generate regular interrupts, set an unused
output pin to TRUE when entering the interrupts routine and FALSE when
leaving it. Monitor the pin with an oscilliscope: the distance between
pulses tells you the rate at which you're getting interrupts and the width
of the pulses tells you the percentage time you're spending in the interrupts.
Are you getting interrupts at the rate you expect? Does the duration of
the interrupt exceed the desired interrupt period?

5.3 Programming Tips

Putting the PIC into SLEEP mode reduces its current draw from a nominal
5 mA down to about 5 uA. You can wake from a sleep either from a Watch
Dog Timer interrupt or from an external interrupt (ports RB0 or RB4-7).
This savings is especially significant if you're running the iRX 2.2 off
a battery. Note, however, that the MAXIM chip draws a significant amount
of current, regardless of load.