Search This Blog

Monday, January 16, 2012

IR Distance Sensing

  Finally got back into distance sensing today.  Had the day off for MLK, jr.  Did a lot more research and RTFMing.  Hooked up a test rig with the following notes:

Distance Detection with IR Led and IR Phototransistor
================================

2012.01.16 -
-----------------

   So, it's back to distance detection with IR Leds and IR phototransistors.
I used the IR pair from Radio Shack.  A few comments on these, so I don't
forget yet again,  I'm sick of re-learning this.  The dark one is the emitter.
The clear one is the detector.  The emitter has the following key properties:

Forward Current: 150 mA
Forward Voltage: 1.3 V typical.  max is 1.7 V
Reverse Voltage: 5 V
Wavelength:     950 nm

Resistance is calculated as always for a diode:

Vs = voltage source
Vf = forward voltage
I  = forward current

So,
    Vs - Vf
    _______  = resistance for max current.

       I

For the Radio Shack emitter at 5V source:
    5 - 1.3 / .150 = 24.66666

I use a 33 Ohm resistor to get ~112mA.

The big reminder:
1. The long lead does not always mean hook to Positive!!!!
   On the emitter, the long lead corresponds to the flat part of the casing.
   This corresponds to the Anode ( positive ) lead of the Led.
   On the phototransistor, the long lead hooks to the flat part of the part.
   The flat part, short lead is the Collector and goes to positive!!!
   The round part, long lead goes to the emitter ( ground ).

o Control the IR Led from the microcontroller by triggering an NPN transistor.
  I got good results with a 2N3904.  I used a 1k resistor to the base.

o The emitter uses a 33 Ohm resistor as calculated above.  If you can find
  something closer to 25 Ohms, use that.

o The detector gets unresisted 5V to the collector.  The emitter goes to ground
  via a 100k resistor.  The detection line to the microcontroller is hooked
  to the emitter.

Results:
----------
  Using a hand or white piece of paper, good results went almost 30cm. 

Non-precise measurements:
           value     |   value
cm   |  hand     |   white paper  
--------------------------------
 1     |  553       |    1023
 2     |  391       |      820
 3     |  279       |      620
 4     |  199       |      438
 5     |  162       |      351
 6     |  126       |      272
 7     |  113       |      231
 8     |   98        |      194
 9     |   89        |      166
10    |   79        |      144
11    |   75        |      132
12    |   70        |      118
13    |   67        |      109
14    |   63        |        99
15    |   61        |        88
16    |   57        |        83
18    |   52        |        74
20    |   50        |        66
22    |   48        |        61
24    |   46        |        56
26    |   45        |        54
28    |   44        |        51
30    |   44        |        49

   I hooked up a test rig to get these results.  Here is the code:
sense source code

As usual, this is assembly source code for a PIC microcontroller from Microchip.  In this case, I'm using my little work horse, the 16F88, running at 8 MHz.

Here are some photos of the rig on breadboard:

The Full Rig With Ruler

Closer View of the Board
Very Close View of Connections

   I'll whip up a schematic soon.

EDIT:
    And here it is in somewhat rough shape:

Saturday, January 14, 2012

GPSIM - Working Again

Awhile back, I upgraded my OpenSuse Linux distribution to the latest ( 12.1 ).  I won't go into the utter disappointment that is Gnome 3.  I got most of what I needed working in short order, and I reverted to the Gnome "throwback" mode.  What I couldn't get working was gpsim.  It depends on gtk-extra, and it seemed to need the ancient 2.1.12 version.  Here's what I did to get gpsim working.  I ended up using gtk-extra-3.0.1.

Linux kernel: 3.1.0-1.2-default

get readline devel package.  I used version 6.
get and install gtk-extra-3.x.  I used gtk-extra-3.0.1
get latest gpsim from svn.  I'm using gpsim 0.26.99

alter the configure file:
Mine was around line 4880.
Any lines that reference gtkextra-2.0, change to gtkextra-3.0

Mine look like this when done:

if test "$use_gui" = "no"; then

  X_LDFLAGS=`$PKGCONFIG --libs glib-2.0 gthread-2.0 gmodule-2.0`
  X_CFLAGS=`$PKGCONFIG --cflags glib-2.0`
  Y_LDFLAGS=
  Y_CFLAGS=
else

  $PKGCONFIG --exists gtkextra-3.0
  if test $? != 0; then
    as_fn_error $? "Cannot find gtkextra-3.0 package" "$LINENO" 5
  fi

  X_LDFLAGS=`$PKGCONFIG --libs gtk+-2.0 gthread-2.0`
  X_CFLAGS=`$PKGCONFIG --cflags gtk+-2.0`
  Y_LDFLAGS=`$PKGCONFIG --libs gtkextra-3.0`
  Y_CFLAGS=`$PKGCONFIG --cflags gtkextra-3.0`
  GTK_VERSION_T=`$PKGCONFIG --modversion gtk+-2.0`
  echo linking with gtk-$GTK_VERSION_T
.
N.B. - Don't change the lines that reference gtk+.

execute:
$autoconf
$./configure
$make
$su
Password:
#make install

Now, for OpenSuse ( and other distros, I'm sure ), the default search path
does not look in /usr/local/lib.  So, I edited /etc/profile as root, adding
this line:
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/local/lib"

Done!
Wasn't that easy?  It wasn't for me!!!