Seed Grove MiCS-6814 and Python

439 views
Skip to first unread message

Davide Aguiari

unread,
Mar 7, 2017, 11:15:11 AM3/7/17
to BeagleBoard
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;
}

Graham

unread,
Mar 7, 2017, 1:05:31 PM3/7/17
to BeagleBoard
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

==

asmr...@gmail.com

unread,
Mar 27, 2017, 6:09:07 PM3/27/17
to BeagleBoard
Hi any luck with this ? I'm also trying to get to work on BB

Davide Aguiari

unread,
Mar 31, 2017, 12:03:08 PM3/31/17
to BeagleBoard, asmr...@gmail.com
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?
Reply all
Reply to author
Forward
0 new messages