Seed Grove MiCS-6814 and Python

Hi everyone.
I’m trying to read my Seed Grove Multichannel gas sensor. (http://wiki.seeed.cc/Grove-Multichannel_Gas_Sensor/)
I now, in the webpage the’re a red X on Beaglebone, but I thought It was due to lack of python library.

So I started coding from the Arduino cpp library (that works): https://github.com/Seeed-Studio/Mutichannel_Gas_Sensor/blob/master/MutichannelGasSensor.cpp

If I plug the groove, I see it with i2cdetect -r 2:

`
root@beaglebone:~# i2cdetect -r 2
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-2 using read byte commands.
I will probe address range 0x03-0x77.
Continue? [Y/n]
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: – 04 – -- – -- – -- – -- – -- –
10: – -- – -- – -- – -- – -- – -- – -- – --
20: – -- – -- – -- – -- – -- – -- – -- – --
30: – -- – -- – -- – -- – -- – -- – -- – --
40: – -- – -- – -- – -- – -- – -- – -- – --
50: – -- – -- UU UU UU UU – -- – -- – -- – --
60: – -- – -- – -- – -- – -- – -- – -- – --
70: – -- – -- – -- – --

`

At the moment I’m using Adafruit_I2C library to write/read.

This is my Class:

`
class MutichannelGasSensor:
address = None

def init(self, mode=1, address=0x04, i2c=None, **kwargs):
i2c = Adafruit_I2C(0x04, busnum=2, debug=True)

self._device = i2c
self.address=0x04
self.adcValueR0_NH3_Buf = 0;
self.adcValueR0_CO_Buf = 0;
self.adcValueR0_NO2_Buf = 0;

#POWER ON
dta_test = [11,1]
self._device.writeList(self.address, dta_test)

self.calcGas()

def calcGas(self):

how to calc ratio/123

#ledON
dta_test = [10, 1]
self._device.writeList(self.address, dta_test)
time.sleep(1)

A0_0 = self.get_addr_dta(6, 8)
time.sleep(1)
A0_1 = self.get_addr_dta(6, 10)
time.sleep(1)
A0_2 = self.get_addr_dta(6, 12)

print "A0_0: " + str(A0_0)
print "A0_1: " + str(A0_1)
print "A0_2: " + str(A0_2)

def get_addr_dta(self, addr_reg, __dta):
self._device.write8(0x04, addr_reg)
self._device.write8(0x04, __dta)

testArray = self._device.readList(self.address, 2)
dta=0
dta = testArray[0]
dta <<= 8
dta += testArray[1]

if addr_reg == 8: #CH_VALUE_NH3
self.adcValueR0_NH3_Buf = dta;
elif addr_reg == 10: #CH_VALUE_CO
self.adcValueR0_CO_Buf = dta;
elif addr_reg == 12: #CH_VALUE_NO2
self.adcValueR0_NO2_Buf = dta;

return dta
`

The problem is when I call calcGas() and get_addr_dta(6, 8) , get_addr_dta(6, 10) and get_addr_dta(6, 12):

I wrote in three different registers, but I read always the same value from it (A0_0, A0_1 and A0_2) while they’re differente on an Arduino.

Why?

The same function in Arduino is:

`
unsigned int MutichannelGasSensor::get_addr_dta(unsigned char addr_reg, unsigned char __dta){
START:
Wire.beginTransmission(i2cAddress);
Wire.write(addr_reg);
Wire.write(__dta);
Wire.endTransmission(); // stop transmitting

Wire.requestFrom(i2cAddress, 2);

unsigned int dta = 0;
unsigned char raw[10];
int cnt = 0;

while(Wire.available())
{
raw[cnt++] = Wire.read();
}

if(cnt == 0)goto START;

dta = raw[0];
dta <<= 8;
dta += raw[1];

return dta;
}
`

The sensor datasheet is useless: https://raw.githubusercontent.com/SeeedDocument/Grove-Multichannel_Gas_Sensor/master/res/MiCS-6814_Datasheet.pdf

Thank you for the support :slight_smile:

Davide:

The adafruit library does some unexpected things (at least to my thinking) with respect to byte order of multiple byte reads and writes.
I would look for some “known values” in the part that are multi-byte reads and writes, and make sure you are getting the expected results.
I would not assume that adafruit code works the same as arduino code with respect to byte order in multi-byte operations.

The address 0x04 is a dangerous address to use in I2C.
In the current standard it is reserved for sending commands to HS (High Speed) devices, and is not a valid I2C device address.
Valid I2C device addresses start at 0x08 and go up from there. Since this is the only part on the bus, this is not your problem, but if you ever added an HS capable part onto the bus, the world will stop turning.

— Graham

Hi any luck with this ? I’m also trying to get to work on BB

Not yet.
I stopped working on this sensor on Linux but I will try again in the next weeks.
I hope it’s a sw problem. Trying to change 0x04 address didn’t solve the problem, in my test.

Did you try it too?