Hacking with JavaScript: Tessel Gesture Sensor Driver

tessel gesture sensorJia Huang is a co-founder and developer at Technical Machine, the company behind the Tessel and Tessel 2 microcontrollers, small, powerful boards and modules that run JavaScript. In her recent talk at the Forward Web Technology Summit, Jia described a JavaScript Gesture Sensor Driver powered by Tessel.

Since Tessel runs JavaScript, we can push a simple script to control a servo or another module. For some more sample apps, see Tessel and PubNub Power the Internet of Things in 4 Lines of JavaScript.

The Application Concept

For this blog post, we’ll be using the Gesture Sensor module. The goal of the application is to be able to swipe my hand back and forth over the module and have Tessel tell me which direction my hand is moving.

So how do we do this? How do we get data between the module and the Tessel? The sensor block diagram provided by the manufacturer contains a pin out diagram. We’ll be focusing on SCL and SDA.

Identifying the Communication Bus

There are three main hardware communication busses that Tessel supports: I2C, SPI and UART.

I googled SCL and SDA and it came up with I2C. SCL is the clock line and SDA is the data line. What that means is that on every high line of the clock cycle, we can read the data pin.

Screen Shot 2015-06-02 at 4.10.08 PM

In binary, this gives us a series of zeros and ones. But this is a protocol, which means that not everything sent across is data. In the data sheets, you can see the protocol actually being used; in this case, a 7-bit I2C-bus addressing protocol. So the first seven bits are the data bits, the eighth bit is a ‘read’ or ‘write’ bit, and the last bit is an acknowledgement or ACK.

Now that we know what I2C is, we can use Tessel to communicate on the I2C bus. Every port that Tessel has is connected to an i2c object. I2C can be multiplexed, so that multiple devices can share the same I2C bus. That’s why our code includes an address.

Reading Data from the Sensor

To get the values from the sensor, we can check the data sheet to see what data is relevant to our purposes. In this case, we just need the up, down, left and right data. Hopefully, by doing some math, we can get the relevant gesture.

Every I2C device has an ID register. We can use Tessel, give it the ID register, and check to see if the sensor is connected on that port.

Next, we’ll check if there’s a valid gesture. Register 0xAF can give us that information. The first bit, GVALID, will tell us whether there is valid data. We can use Tessel to read the data at register 0xAF to check gesture validity.

Interpreting the Sensor Data

After that, we’ll try to figure out the gesture. We’ll get the gesture data size and keep reading data until no data remains. By setting a threshold and getting data from the four different sensors over time, we can figure out the direction of the gesture:

Screen Shot 2015-06-02 at 4.12.48 PM

PubNub and Tessel

One of the ways Tessel works with PubNub is by providing realtime sensor data that can be broadcast as a data stream across PubNub’s network. The MIT Media Lab uses Tessel and PubNub to generate and consume data in realtime. In fact, all the code you need is in this gist:

For more information, read our Tessel + PubNub Introduction blog post. Happy hardware hacking!

Try PubNub Today

Connect up to 100 devices for Free