PIR: a USB motion sensor

PIR: a USB motion sensor

From time to time, we are asked whether Yoctopuce would be interested in building a USB motion sensor based on a PIR sensor. Let's kill the suspense right away: the answer is no, not at the present time. However, we can show you how to build one for a little bit more than the price of a Yocto-Serial.




We recently discovered the ZEPIR0BAS02MODG manufactured by ZiLOG. Behind this rather barbaric name hides a module with very interesting specificities:

  • It is tiny : ~25x16mm
  • You can drive it from a TTL serial port
  • It costs a few euros only

The  ZEPIR0BAS02MODG module from ZiLOG
The ZEPIR0BAS02MODG module from ZiLOG


Obviously, we hurried to connect one to a Yocto-Serial.

Connections

You can connect the ZEPIR0BAS02MODG module directly to a Yocto-Serial. You can even power it through the Yocto-Serial. There is only one little subtlety: to activate the serial port of the ZEPIR0BAS02MODG, you must put a 100kΩ resistance between the VDD and TX pins of the ZEPIR0BAS02MODG.

5 wires and a resistance are enough to connect the module to a  Yocto-Serial
5 wires and a resistance are enough to connect the module to a Yocto-Serial


With some skill, you can even put the whole system into a Yocto-Serial enclosure and thus obtain a USB motion sensor which is particularly compact.


The complete system  You can even put everything into a Yoctopuce enclosure
A Yocto-Serial is enough to interface the ZEPIR0BAS02MODG.



Configuration

To work with the PIR sensor, the Yocto-Serial must be properly configured. The easiest way is to use the VirtualHub. The first thing you need to do is to configure the port speed, that is to 9600bauds, 8N1. Then you must enable the power supply of the module in 3.3V.

Configuring the serial port of the  Yocto-Serial
Configuring the serial port of the Yocto-Serial


You can configure and drive the ZEPIR0BAS02MODG entirely through the serial port. For this purpose, it provides about thirty functions. For the demo, we chose to use the most obvious one: if we send the ASCII character "a", the sensor answers by:

  • "Y" if a motion was detected since the previous call,
  • "N" if no motion was detected since the previous call, or
  • "U" if it's not ready.

Therefore, we configured the Yocto-Serial to work in binary mode (generic byte stream) and we created a task that, every 500 ms, sends the 0x61 byte (a) to the sensor, then reads a byte as answer and maps it on genericSensor1. Thus, depending on the ZEPIR0BAS02MODG answer, the values 78 (N), 85 (U), 89 (Y) are mapped on genericSensor1.

Configuring the task that  drives the sensor
Configuring the task that drives the sensor


Finally, we modified the sensor mapping so that 78 corresponds to -7, 85 to 0, and 89 to 4. Thus, we only need to test whether the current value of genericSensor1 is strictly positive to know if something moved in front of the sensor. We also took this opportunity to assign the "PIR" logical name to genericSensor1.

Configuring the genericSensor1 of the YoctoSerial
Configuring the genericSensor1 of the YoctoSerial



When everything is configured, it's time for testing.

Application example

We wrote a short test program in Python which sets up a callback switching on a relay during 5 seconds each time the ZEPIR0BAS02MODG detects something.

from yocto_api import *
from yocto_relay import *

relay = None
errmsg = YRefParam()

def PIRvalueChanged(sesnor,value):
    if int(value)>0 : relay.pulse(5000)

if YAPI.RegisterHub("usb", errmsg) != YAPI.SUCCESS:
    sys.exit("init error" + str(errmsg))

sensor = YSensor.FindSensor("PIR")
relay  = YRelay.FirstRelay()

if not(sensor.isOnline()):sys.exit("No PIR sensor available")
if relay==None: sys.exit("No relay available")
sensor.registerValueCallback(PIRvalueChanged)
while True:
    YAPI.Sleep(100,errmsg)



And here is a short video showing the system in action.

  


It does seem somewhat magical like this, but you must know that, for the sensor to work correctly, it cannot be put anywhere: you must avoid putting it facing a window, in a draft, and so on... You'll find more details in the ZEPIR0BAS02MODG user manual.

Conclusion

The versatility of the Yocto-Serial allowed us to build this motion sensor in a jiffy. Obviously, the application example presented here is very trivial: it's the equivalent of these automatic lamps that you can find in all the DIY stores, but the possibilities are much larger. If you connect the Yocto-Serial driving the motion sensor to a YoctoHub-Ethernet or to a YoctoHub-Wireless-g, you can build a detection system able to interact with Internet...

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.