sparkfun ZX Distance and Gesture Sensor Technical document

ZX Distance and Gesture Sensor SMD Hookup
Guide
Introduction
The ZX Distance and Gesture Sensor is a collaboration product with XYZ
Interactive. The innovative people at XYZ Interactive have created a unique
technology that allows for simple infrared (IR) beams to be used to detect
an object’s location in two dimensions.
The ZX Sensor is a touchless sensor that is capable of looking for simple
gestures in the air above the sensor (e.g. swipe left or swipe right).
Additionally, the sensor can also recognize the distance of an object away
from the sensor at distances up to about 12 inches (30 cm), referred to as
the “Z” axis, and the location of the object from side to side across the
sensor in about a 6 inch (15 cm) span, referred to as the “X” axis.
Covered in This Tutorial
We can use I C or UART to communicate with the ZX Sensor. In this
tutorial, we will show you how to connect the sensor to an Arduino or
Arduino-compatible board as well as a computer so you can start creating
gestures to handle all our your daily tasks or add some interactive flair to
your project.
ZX Distance and Gesture Sensor
SEN-13162
2
Page 1 of 1
6

ZX Gesture and Distance Sensor Hookup Guide SparkFun Wish
List
Materials Use
d
In addition to the sensor itself, you will need a few extra components to
follow along with the Arduino examples:
ZX Distance and Gesture Sensor
SEN-13162
The ZX Distance and Gesture Sensor is a touchless sensor that is ca…
SparkFun RedBoard - Programmed with Arduino
DEV-13975
A
t SparkFun we use many Arduinos and we're always looking for the…
SparkFun USB Mini-B Cable - 6 Foot
CAB-11301
This is a USB 2.0 type A to Mini-B 5-pin cable. You know, the mini-B…
Breadboard - Mini Modular (White)
PRT-12043
This white Mini Breadboard is a great way to prototype your small proj…
Jumper Wires Premium 6" M/M Pack of 10
PRT-08431
This is a SparkFun exclusive! These are 155mm long jumpers with m…
Break Away Headers - Straight
PRT-00116
A
row of headers - break to fit. 40 pins that can be cut to any size. Us…
If you would like to try the ZX Sensor on a Windows-based PC, you will
need an FTDI Breakout:
Recommended Reading
There are a few concepts that you should be familiar with before getting
started with the ZX Sensor. Consider reading some of these tutorials before
continuing:
• What is an Arduino? – Two of the examples use an Arduino to
control the ZX Sensor
• I C – I C is the one of the protocols used by the ZX Sensor
• Serial Communication – We use serial communications to program
the Arduino, view debugging information, and transmit data from the
ZX Sensor
• How to Use a Breadboard – The breadboard ties the Arduino to the
ZX Sensor
SparkFun FTDI Basic
Breakout - 5
V
DEV-09716
22
Page 2 of 1
6

• How to Install FTDI Drivers – If you are programming an Arduino or
using the ZX Sensor demo app, chances are you will need to use an
FTDI
Board Overview
The ZX Sensor works by bouncing infrared (IR) beams of light from the two
LEDs on either side off of an object above the sensor. The bounced light
returns to the receiver in the center of the sensor, and a microcontroller on
the back of the sensor interprets the data. We can read the results using an
I C or UART connection.
Pin Descriptions
The ZX Sensor gives us two ports to connect to: I C and UART. You can
see both ports are broken out to the 0.1" thru holes. See the table below for
a list of each pin and its function.
Pin Label Description
GRN Not used
TXO UART transmit out from the ZX Sensor
RXI UART receive. Not used at this time.
VCC 3.3 - 5 V power supply
GND Connect to ground
BLK Not used, but connected to GND
DR Data Ready. High when there is data to be read via I C
CL I C clock
DA I C data
Setting the Jumpers
The ZX Sensor has a couple of jumpers on the back of the board that can
be opened or closed with a soldering iron.
I2C Pullups
The ZX Sensor, by default, comes with 4.7 kΩpull-up resistors on the SDA
and SCL I C lines. Remove the solder on this jumper using solder wick to
disconnect the pull-ups.
I2C Addr
By default, this jumper is open. Close it to change the I C address of the
sensor.
Jumper I C Address
2
2
2
2
2
2
2
2
Page 3 of 1
6

Open 0x10
Closed 0x11
Hardware Hookup
Add Headers
Solder a row of male headers to the nine headers holes on the board.
To keep the board from tilting while soldering, place the unused break away
headers sideways under the board.
Heads up! Do not solder headers to the row of holes at the top of the
board. Those are for programming the PIC micrcontroller.
Connect the Breakout Board
For the Arduino examples, we will be using I C. Connect the breakout
board to the following RedBoard pins:
2
Page 4 of 1
6

ZX Sensor RedBoard
VCC 5V
GND GND
DR 2
CL A5
DA A4
Note that we connect the DR pin, but we will only use it in the Arduino:
Gesture Example. DR stands for “Data Ready,” which is active high
whenever data is ready to be read from the ZX Sensor. We can attach this
to an Arduino interrupt so we don’t have to continuously poll the sensor.
Arduino Library Installation
All of the hard work for the ZX Sensor is being accomplished in the
microcontroller on the sensor itself. All we need to do is read the results!
We have created an Arduino library to make that even easier for you. Click
the button to download the latest version of the ZX Sensor Arduino Library.
You can also find the latest files in the GitHub repository.
DOWNLOAD THE ZX SENSOR ARDUINO LIBRARY!
Unzip the downloaded file. Follow this guide on installing Arduino libraries
to install the files as an Arduino library.
Page 5 of 1
6
https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library/archive/master.zip

Arduino: ZX Exampl
e
Load the ZX Demo
Open up the Arduino program and select File →Examples →
SparkFun_ZX_Distance_and_Gesture_Sensor →I2C_ZX_Demo.
Attach a USB mini cable from your computer to the RedBoard. If you have
not previously done so, install the FTDI drivers.
For reference, here is the I2C_ZX_Demo.ino sketch.
Page 6 of 1
6

/*********************************************************
*******
I2C_ZX_Demo.ino
XYZInteractiveZXSensor
ShawnHymel@SparkFunElectronics
May6,2015
https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gestu
re_Sensor_Arduino_Library
TeststheZXsensor'sabilitytoreadZXdataoverI2C.Th
isdemo
configurestheZXsensorandperiodicallypollsforZaxi
sandXaxisdata.
HardwareConnections:
ArduinoPinZXSensorBoardFunction
5VVCCPower
GNDGNDGround
A4DAI2CData
A5CLI2CClock
Resources:
IncludeWire.handZX_Sensor.h
Developmentenvironmentspecifics:
WritteninArduino1.6.3
TestedwithaSparkFunRedBoard
Thiscodeisbeerware;ifyouseeme(oranyotherSparkFu
n
employee)atthelocal,andyou'vefoundourcodehelpfu
l,please
buyusaround!
Distributedasis;nowarrantyisgiven.
**********************************************************
******/
#include<Wire.h>
#include<ZX_Sensor.h>
//Constants
const int ZX_ADDR= 0x10;//ZXSensorI2Caddress
//GlobalVariables
ZX_Sensorzx_sensor= ZX_Sensor(ZX_ADDR);
uint8_tx_pos;
uint8_tz_pos;
void setup(){
uint8_tver;
//InitializeSerialport
Serial.begin(9600);
Serial.println();
Serial.println("");
Serial.println("SparkFun/GestureSense I2CZXDemo");
Serial.println("");
//InitializeZXSensor(configureI2CandreadmodelI
D)
Page 7 of 1
6

if (zx_sensor.init()){
Serial.println("ZXSensorinitializationcomplete");
}else {
Serial.println("SomethingwentwrongduringZXSensor
init!");
}
//Readthemodelversionnumberandensurethelibrary
willwork
ver= zx_sensor.getModelVersion();
if (ver== ZX_ERROR){
Serial.println("Errorreadingmodelversionnumber");
}else {
Serial.print("Modelversion:");
Serial.println(ver);
}
if (ver!= ZX_MODEL_VER){
Serial.print("Modelversionneedstobe");
Serial.print(ZX_MODEL_VER);
Serial.print("toworkwiththislibrary.Stopping.");
while(1);
}
//Readtheregistermapversionandensurethelibrary
willwork
ver= zx_sensor.getRegMapVersion();
if (ver== ZX_ERROR){
Serial.println("Errorreadingregistermapversionnum
ber");
}else {
Serial.print("RegisterMapVersion:");
Serial.println(ver);
}
if (ver!= ZX_REG_MAP_VER){
Serial.print("Registermapversionneedstobe");
Serial.print(ZX_REG_MAP_VER);
Serial.print("toworkwiththislibrary.Stopping.");
while(1);
}
}
void loop(){
//Ifthereispositiondataavailable,readandprinti
t
if (zx_sensor.positionAvailable()){
x_pos= zx_sensor.readX();
if (x_pos!= ZX_ERROR){
Serial.print("X:");
Serial.print(x_pos);
}
z_pos= zx_sensor.readZ();
if (z_pos!= ZX_ERROR){
Serial.print("Z:");
Serial.println(z_pos);
}
}
}
Run
Make sure you have the correct serial port selected under Tools →Serial
Port and “Arduino Uno” selected under Tools →Board. If you have never
used the Arduino IDE before, this turoial should get you started.
Page 8 of 1
6

Click the Upload button and wait for the program to finish uploading to the
Arduino. Select Tools →Serial Monitor to open up the serial terminal. More
info on the Serial Terminal can be found here. Note that the Serial Monitor
settings are the default settings (9600, 8, n, 1). You should see a couple of
messages noting that “ZX Sensor initialization complete.”
Hover your hand 4 to 10 inches (10 to 25 cm) above the sensor.
Move your hand around above the sensor, and you should see Z (height
above the sensor) and X (position side to side) appear in the serial terminal.
NOTE: Z- and X- data is given as an unsigned integer between 0 and
240 (inclusive).
Arduino: Gesture Example
Load the Gesture Interrupt Demo
In addition to providing Z- and X- axis data about an object, the ZX Sensor
is also capable of detecting simple gestures. To see an example of this,
open File →Examples →SparkFun_ZX_Distance_and_Gesture_Sensor
→I2C_Gesture_Interrupt.
Page 9 of 1
6

Here is the I2C_Gesture_Interrupt.ino sketch for reference.
Page 10 of 1
6

/*************************************************************
***
I2C_Gesture_Interrupt.ino
XYZInteractiveZXSensor
ShawnHymel@SparkFunElectronics
May6,2015
https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_S
ensor_Arduino_Library
TeststheZXsensor'sabilitytoreadgesturedataoverI2Cus
ing
aninterruptpin.ThisprogramconfiguresI2Candsetsupan
interrupttooccurwhenevertheZXSensorthrowsitsDRpinhi
gh.
Thegestureisdisplayedalongwithits"speed"(howlongitt
akes
tocompletethegesture).Notethathighernumbersof"speed"
indicateaslowerspeed.
HardwareConnections:
ArduinoPinZXSensorBoardFunction
5VVCCPower
GNDGNDGround
A4DAI2CData
A5CLI2CClock
2DRDataReady
Resources:
IncludeWire.handZX_Sensor.h
Developmentenvironmentspecifics:
WritteninArduino1.6.3
TestedwithaSparkFunRedBoard
Thiscodeisbeerware;ifyouseeme(oranyotherSparkFun
employee)atthelocal,andyou'vefoundourcodehelpful,ple
ase
buyusaround!
Distributedasis;nowarrantyisgiven.
**************************************************************
**/
#include<Wire.h>
#include<ZX_Sensor.h>
//Constants
const int ZX_ADDR= 0x10;//ZXSensorI2Caddress
const int INTERRUPT_NUM=0;//Pin2ontheUNO
//GlobalVariables
ZX_Sensorzx_sensor= ZX_Sensor(ZX_ADDR);
volatileGestureTypegesture;
volatilebool interrupt_flag;
uint8_tgesture_speed;
void setup(){
uint8_tver;
//Initializegesturetonogesture
gesture= NO_GESTURE;
Page 11 of 1
6

//InitializeSerialport
Serial.begin(9600);
Serial.println();
Serial.println("
");
Serial.println("SparkFun/GestureSense I2CGestureInterrup
t");
Serial.println("Note:higher'speed'numbersmeanslower");
Serial.println("
");
//InitializeZXSensor(configureI2CandreadmodelID)
if (zx_sensor.init(GESTURE_INTERRUPTS)){
Serial.println("ZXSensorinitializationcomplete");
}else {
Serial.println("SomethingwentwrongduringZXSensorini
t!");
}
//Readthemodelversionnumberandensurethelibrarywil
lwork
ver= zx_sensor.getModelVersion();
if (ver== ZX_ERROR){
Serial.println("Errorreadingmodelversionnumber");
}else {
Serial.print("Modelversion:");
Serial.println(ver);
}
if (ver!= ZX_MODEL_VER){
Serial.print("Modelversionneedstobe");
Serial.print(ZX_MODEL_VER);
Serial.print("toworkwiththislibrary.Stopping.");
while(1);
}
//Readtheregistermapversionandensurethelibrarywil
lwork
ver= zx_sensor.getRegMapVersion();
if (ver== ZX_ERROR){
Serial.println("Errorreadingregistermapversionnumbe
r");
}else {
Serial.print("RegisterMapVersion:");
Serial.println(ver);
}
if (ver!= ZX_REG_MAP_VER){
Serial.print("Registermapversionneedstobe");
Serial.print(ZX_REG_MAP_VER);
Serial.print("toworkwiththislibrary.Stopping.");
while(1);
}
//Initializeinterruptserviceroutine
interrupt_flag= false;
zx_sensor.clearInterrupt();
attachInterrupt(INTERRUPT_NUM,interruptRoutine,RISING);
Serial.println("Interruptsnowconfigured.Gestureaway!");
}
void loop(){
//Ifwehaveaninterrupt,readandprintthegesture
if (interrupt_flag){
Page 1
2
of 1
6

//Cleartheinterruptflag
interrupt_flag= false;
//YouMUSTreadtheSTATUSregistertoclearinterrupt!
zx_sensor.clearInterrupt();
//Readlastgesture
gesture= zx_sensor.readGesture();
gesture_speed= zx_sensor.readGestureSpeed();
switch (gesture){
case NO_GESTURE:
Serial.println("NoGesture");
break;
case RIGHT_SWIPE:
Serial.print("RightSwipe.Speed:");
Serial.println(gesture_speed,DEC);
break;
case LEFT_SWIPE:
Serial.print("LeftSwipe.Speed:");
Serial.println(gesture_speed,DEC);
break;
case UP_SWIPE:
Serial.print("UpSwipe.Speed:");
Serial.println(gesture_speed,DEC);
break;
default:
break;
}
}
}
void interruptRoutine(){
interrupt_flag= true;
}
Run
Upload the sketch, and open the Serial Monitor. You should see a message
stating that initialization is complete.
Start with your hand off to one side (a “side” being the one of the infrared
LEDs with the brass covers) about 4 to 10 inches (10 to 25 cm) above the
sensor. Swipe your hand horizontally across the sensor so that your hand
passes over the one infrared LED and then the next infrared LED.
Page 1
3
of 1
6

If you performed the gesture correctly, you should see a message appear in
the Serial Monitor.
NOTE: The "Speed" of the gesture is a measure of how fast the
gesture occurred. Note that the lower the number, the faster the
gesture occurred (e.g. 3 being very fast and 25 being very slow).
Supported Gestures
Here is a list of the currently supported gestures. Make sure each gesture
begins outside of the range of the sensor, moves into the range of the
sensor, and ends outside the range of the sensor.
Gesture Description
Right
Swipe
A swipe from the left side of the board to the right and out of
range of the sensor. Make sure that your wrist/arm is not in the
sensor's range at the end of the swipe!
Left
Swipe
A swipe from the right side of the board to the left and out of
range of the sensor.
Up Swipe Object starts near the sensor, hovers for at least 1 second,
and then moves up above and out of range of the sensor.
No
Gesture
The sensor could not correctly determine the gesture being
performed.
PC: ZX Example
The ZX Sensor, in addition to responding to I C commands, continually
transmits ZX data over its UART port. We can connect an FTDI Breakout
directly to the ZX Sensor and read the output. You can use serial
applications or the screen command (Linux or Mac) to view the output.
NOTE: You can use either 3.3 V or 5 V FTDI. 5 V gives you a bit
better range with the sensor.
If you are on a Windows computer, you can use the demo application
(linked below) provided by XYZ Interactive to test the ZX Sensor.
2
Page 1
4
of 1
6

Setu
p
Connect the FTDI Breakout board to the ZX Sensor. Ensure the pins on the
FTDI Brekaout line up with the pins on the ZX Sensor (e.g. GRN connects
to GRN and BLK connects to BLK). Connect the FTDI Breakout to your
computer with a USB cable.
Download the ZX Demo application, and unzip it.
DOWNLOAD THE ZX DEMO APPLICATION
Run
Double-click to run the ZX Demo application. Under “Input:” on the right
side, drop down the list and select the COM port that corresponds to your
FTDI Breakout (if you need a refresher on find the right COM port, check
out this section of the Terminal Basics tutorial). You do not need to choose
an “Output:” port.
Click Open to connect to the FTDI Breakout.
Move your hand around above the sensor, and you should see the red ball
move.
Try out the other tabs in the application! The Z-Control tab lets your try
moving your hand toward and away from the sensor, and the Gestures tab
computes a few different gestures based on the Z- and X- data.
Resources and Going Further
After trying the basic ZX and gesture demos, you can try the other
examples in the Arduino library. A description of each of the examples is
given below:
Page 1
5
of 1
6

•I2C_Gesture_Demo – Poll the sensor over I C to see if any gestures
have occurred.
•I2C_Gesture_Interrupt – The DR pin will go from low to high when a
gesture is detected. This example reads the gesture over I2C and
tells the sensor to clear DR.
•I2C_ZX_Demo – Poll the sensor periodically over I C for Z- and X-
axis data.
•I2C_ZX_Interrupt – The ZX Sensor will throw DR high whenever
valid ZX data is ready.
•UART_Gesture_Demo –NOTE: Gestures over UART are not
supported at this time. This demo is a placeholder for the time being.
•UART_ZX_Demo – Read Z- and X- axis data from a software serial
port and display them on the Serial Monitor.
Resources
Here are some additional resources to help you with the ZX Sensor:
• ZX Sensor Datasheet
• Using the ZX Sensor with Arduino
• ZX Sensor Schematic
• ZX Sensor GitHub Repository
• ZX Sensor I2C Register Map
Other Tutorials
What will you make with the ZX Sensor? If you need some inspiration,
check out these related tutorials:
2
2
Connecting Arduino to
Processin
g
Send serial data from Arduino to
Processing and back - even at the
same time!
Serial Graphic LCD Hookup
Learn how to use the Serial Graphic
LCD.
RGB Panel Hookup Guide
Make bright, colorful displays using
the 32x32 and 32x16 RGB LED
panels. This hookup guide shows
how to hook up these panels and
control them with an Arduino.
APDS-9960 RGB and
Gesture Sensor Hookup
Guide
Getting started guide for the Avago
APDS-9960 color, proximity, and
gesture sensor.
Page 1
6
of 1
6
6
/
6
/
201
7
https://learn.sparkfun.com/tutorials/zx-distanc
e
-an
d
-gestur
e
-senso
r
-sm
d
-hooku
p
-guide?_g
a
...
This manual suits for next models
1
Table of contents
Other sparkfun Security Sensor manuals