Getting a 3.3V digital sensor to work with 5V

Robot_Leader_Halloween_2010_084_small.jpg

I recently purchased a Parallax PIR Motion Detector from Radio Shack. These devices are only about $10 US, and do a nice job of general motion detection when your robot is staying still. (You cannot use PIR to track motion, just to detect it, and only if the PIR sensor itself is not moving.

The manual (downloadable from Parallax, but not provided by Radio Shack) indicated that the sensor could be used for 3.3V or 5V operation. However, when I powered the unit with my regulated 5V that I use for my Picaxe28 project board, I found that I could not capture the output of the PIR going HIGH on a digital input pin of the Picaxe.

Some quick measuring showed that the PIR was only putting out a voltage of just under 3V when HIGH. This wasn't enough to trigger the digital input of the Picaxe. I considered adding a transistor to bump up the voltage. Yuck, more hardware to get around a device issue.

Then I thought, "wait, an analog pin will read any voltage I need from 0V to 5V". So I connected the output of the PIR to an analog pin, and ran this code to look for a logical HIGH. Worked like a charm!

<code>

readadc pirpin,pirstate ' PIR output is digital, but only 3V when high, so I use an analog read.
if pirstate > 64 then { ' check the pirpin.
pircount = pircount +1

</code>

The readadc command looks at the analog pin I designated for the PIR, and records the analog value into the variable pirstate. I then check if this value is a logical HIGH, which I set at anything above 64 on the 8 bit analog range of 0-255. I could use this to set a variable to 0 or 1 depending on the result, and therefore translate my analog input to a digital value. In practice, I just used it to drive a counter indicating the PIR had triggered.

You could use this technique on any 3.3V sensor with a digital output, to capture the output as an analog input and then translate to digital.