Hi everyone!! Since we have received requests from users on how to integrate the Gesture sensor with PIC, we came out with this tutorial. In this tutorial, we are using the SK40C with PIC16F877A to read the hands gesture and lastly display on LCD.
Covered in this tutorial:
- Material required (Hardware and software)
- Additional information about the gesture sensor
- Circuit diagram of Gesture sensor interface with SK40C
- Code overview
- Demonstration video
If you have further inquiry about this tutorial, please post it in our technical forum as we seldom check the comment section in tutorial site.
|Gesture sensor|| SK40C|
|Voltage Regulator 3.3V||Jumper|
- MPLABX IDE
- XC8 Compiler
The APDS-9960 is a hardware built in with UV and IR blocking filters, four separate diodes sensitive to different directions, and an I2C compatible interface. It have broken out the following pins: VL (optional power to IR LED), GND (Ground), VCC (power to APDS-9960 sensor), SDA (I2C data), SCL (I2C clock), and INT (interrupt).
Interface and control are accomplished through an I2C-bus serial compatible interface (standard or fast mode) to a set of registers that provide access to device control functions and output data. The devices support the 7-bit I2C-bus addressing protocol. The device supports a single slave address of 0×39 Hex using 7-bit addressing protocol.
DESCRIPTION GESTURE DETECTION
Gesture detection utilizes four directional photodiodes to sense reflected IR energy (sourced by the integrated LED) to convert physical motion information (i.e. velocity, direction and distance) to a digital information. The architecture of the gesture engine features automatic activation (based on Proximity engine results), ambient light subtraction, cross-talk cancellation, dual 8-bit data converters, power saving inter-conversion delay, 32-dataset FIFO, and interrupt driven I2C communication. The gesture engine accommodates a wide range of mobile device gesturing requirements: simple UP-DOWN-RIGHT-LEFT gestures or more complex gestures can be accurately sensed. Power consumption and noise are minimized with adjustable IR LED timing.(APDS9960 Datasheet,p1)
CAUTION!!!You must use 3.3V! If you try to use a 5V power supply or 5V I2C communications, you risk damaging the APDS-9960. If you are using a 5V signal, then you need to have some kind of level shifting/logic converter.
So we need 3.3V voltage regulator to get 3.3V output. You can refer to the datasheet of this voltage regulator for connection.
And for I2C communications pins we used logic converter to convert 5V signal to 3.3V signal.
I2C pin for PIC16F877A:
This sensor also interrupt driven I2C communication. An interrupt is generated based on the number of gesture “datasets” results placed in the FIFO.For PIC16F877A interrupt pin is at RB0.
You also can refer to datasheet here.
Here we will only discuss the important part. You can download the full sample code here.
This sample code is for Gesture Test to read hand gesture / swiping and display on LCD. To understand more about the operation of this gesture sensor you can refer to this datasheet from page 14 to 17.
1) Setup for interrupt and I2C in Main function
TRISB = 0b00000001; //RB0 as input(interrupt)
TRISC = 0b00011000; //RC3 and RC4 as input (I2C communication)
// Initialize interrupt service routine
INTF = 0; // Reset the external interrupt flag
INTE = 1; // Enable external interrupts from
PEIE = 1;// Enable peripheral interrupt
GIE = 1; // Global interrupt enable
2) Interrupt function.
If INTF = 1, means that there is interrupt occur at RB0. Disable the interrupt pin. INTE=0 so that it will not interfere if there is interrupt when main function is running . Set the handleGestureFlag=1; clear the interrupt flag, INTF = 0; enable the interrupt pin again to allow next interrupt to occur.
3) While loop in main() function.
This function will check when the handleGestureFlag = 1 or interrupt flag from interrupt function. if there is interrupt, it will run handleGesture() function.
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.
|UP||A swipe from the bottom of the board to the top 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!|
|DOWN||A swipe from the top of the board to the bottom and out of range of the sensor.|
|LEFT||A swipe from the right side of the board to the left and out of range of the sensor.|
|RIGHT||A swipe from the left side of the board to the right and out of range of the sensor.|
|NEAR||Object (e.g. hand) starts far above the sensor, moves close to the sensor, hovers for at least 1 second, and moves out of range of the sensor.|
|FAR||Object starts near the sensor, hovers for at least 1 second, and then moves up above and out of range of the sensor.|
|NONE||The sensor could not correctly guess the gesture being performed.|