1

I am attempting to use a sensor in conjunction with a raspberry pi smbus. When I try and read a specific byte of data, it returns 181. No matter what byte you tell it to read it returns 181, regardless of whether that byte actually exists or not.

Equipment: raspberry PI zero w connecting to a MikroeElictronika air quality 2 click via SMBus and I2C. Connected via a 2 click board. Other slot has a Adafruit ultimate GPS sensor that works fine.

https://download.mikroe.com/documents/datasheets/iaq-core-datasheet-en-v1.pdf

Code:

import smbus 
from time import sleep


bus = smbus.SMBus(1)

def read_byte(byte):

    reading = bus.read_byte_data(0x5a,byte)

    return reading


co2 = read_byte(0x01)

print(co2)

sleep(5)

status = read_byte(0x2)

print(status)

bus.close()

this does not return any error but even if the sensor is stimulated it returns 181. none of the error messages output by the device are 181.

If you do not specify a byte, it does return a value that corresponds to air quality, but not one that corresponds with any of the outputs the sensor is meant to have. This is not the main problem but I have included it in case it helps.

Code:

reading = bus.read_byte(add)

I have no idea why this is happening. Please help as I have little experience in this area. This is a school project.

UPDATE: I have got I2C to block read the data

pigs reading returns:

9 1 199 0 0 1 249 51 0 127

I2C block read returns:

[181, 127, 255, 255, 255, 255, 255, 255, 255]

my pigpio python returns (-25L, ' ' )

1 Answer 1

1

You need to think a little about how the I2C bus works.

There may be many devices on the bus. They know if they are being addressed by checking to see if their address is placed on the bus. To distinguish their address from random bus data the address is only checked at the start of an I2C transaction.

The start of an I2C transaction is signaled by a start condition, the selected device address is then placed on the bus (with the read/write bit). The (selected) device stays selected until a stop condition is signaled.

Each SMBus command is bracketed by a start and stop condition. Your device, like many I2C devices, seems to reset its internal state when a start condition is seen.

The end result means you are always reading the same byte from the device. I.e. byte 0 (the first byte) of the 16 bit pred value.

Use a command which reads all the 9 bytes in one transaction, perhaps the SMBus I2C block read instruction.


In pigpio Python

#!/usr/bin/env python

import pigpio

pi = pigpio.pi()
if not pi.connected:
   exit()

h = pi.i2c_open(1, 0x5a)

c, d = pi.i2c_read_device(h, 9)

print(d)

pi.i2c_close(h)

pi.stop()

From the command line with pigs

sudo pigpiod # if not already running
...
pigs i2co 1 0x5A 0 # returns a handle, 0 for first open
...
pigs i2crd 0 9 # assuming 0 is handle returned by open
...
pigs i2cc 0 # close when finished, assuming 0 is handle returned by open
20
  • if you mean physical devices there is only one other device and it does not use I2C Feb 12, 2018 at 19:30
  • so you mean reading = bus.read_block_data(0x00, 0x09,byte) Feb 12, 2018 at 19:39
  • ok that does return data that changes when I blow on the sensor but how do I make the code work? Feb 13, 2018 at 16:23
  • Although the data does not really make sense. Resistance changes but the Co2 reading doesn't. Byte 2 is 194, witch makes no sense as that isn't one of the possible values. Feb 13, 2018 at 16:27
  • ok I did not know that so I was reading the data wrong Feb 13, 2018 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.