I2C (Inter-Integrated Circuit) is a widely used protocol among peripherals. There are many actuators and sensors based on I2C ,such as Humidity&Temperature sensor, 3-Axis accelerometer , OLED Display ,etc. The purpose of LinkIt smart 7688 basic tutorial #2 is to introduced how can we access these I2C devices on LinkIt smart 7688 with Mraa library. The following example shows how to read data from a Seeed Studio Grove - Digital Light Sensor attached to LinkIt Smart 7688.
Things you need:
Linkit smart 7688 *1
Breakout for LinkIt smart 7688 *1
Grove - Digital Light Sensor *1
USB Cable *1
The connection is very simple! We just need to attach the LinkIt smart 7688 to the Breakout board and connect the Grove - Digital Light to the I2C Grove interface on the Breakout board.That is fine. And if you don’t have a Breakout board , you could connect the sensor’s SDA pin to P20 of 7688 and the SCL pin to P21, of course connect the VCC and GND to the right position too.
I2C (Inter-Integrated Circuit) is a widely used protocol among peripherals. It consists of 2
signal pins - usually named SDA and SCL. LinkIt Smart 7688 comes with 1 set of I2C on GPIO4(P21) and GPIO5(P20) as SCL and SDA respectively. And I2C is capable of connecting multiple slave devices to a single I2C master. Each slave device is identified by a 7-bit address.
So how can we know about the address of our sensors? From the Seeed Studio WIKI , http://www.seeedstudio.com/wiki/Grove_-_Digital_Light_Sensor , we know that this sensor is based on the I2C light-to-digital converter TSL2561 . Then it is google time, search “tsl2561 i2c address” on google ,click the first link, you will see the following information. In this tutorial, we choose 0x29 as the sensor address.
And the datasheet of TSL2561 is also provided in the WIKI page, which is useful for us to access the sensor.
In Mraa library, we use address() to identify the sensor , writeReg() to control the register and writeByte()/readByte() to get sensor’s data. The code of this tutorial is as follow:
import mraaimport time def getlux(): i2c.writeByte(0x8c) d = i2c.readByte() i2c.writeByte(0x8d) c = i2c.readByte() i2c.writeByte(0x8e) b = i2c.readByte() i2c.writeByte(0x8f) a = i2c.readByte() print "ch0low:",d," ch0high:",c," ch1low:",b," ch1high:",a data = 3.15*(c*256+d)-5.932*(((a*256+b)/(c*256+d+1))**1.4)*(c*256+d) return data i2c = mraa.I2c(0)#device address is 0x29i2c.address(0x29)#power upi2c.writeReg(0x80,0x03)#power on setting timetime.sleep(1)#gain &intergration timei2c.writeReg(0x81,0x00)#set interrupt threshold to defaulti2c.writeReg(0x86,0x00) while True: print getlux()," lux" time.sleep(1)
Paste the code above into a new file and save it as i2clight.py. Then run it :
Ch0low,ch0high,ch1low and ch1high are 4 registers to store the intensity data of the light according to the datasheet of TSL2561. We can calculate Lux with these four data. The formula is as follow:
To drive an I2C device, you need to operate multiple I2C sequences, read/write registers and calculating. LinkIt Smart 7688 also comes with UPM pre-installed, which is a set of driver repository built upon Mraa. You can see “LinkIt smart 7688 basic tutorial #5:Use Grove - Digital Light Sensor--Describe how to use UPM” for a driver example of Seeed Studio Grove - Digital Light Sensor,which is easier to use.