I2c problem reading data from one sensor

I have to connect a sensor to and EMX module.
Producer of module say that I have to read data from his sensor like that

Producer of sensor suggest to do something like that but I don’t know how to do!!

byte byte_msb, byte_lsb; // 8bit values
int16 pressure; // 16bit value
// Set I2C unit to I2C master mode, clock speed 100 kHz and 7 bit addressing
configureI2C (I2C_MASTER | CLK_SPEED_100KHZ | ADDRESSING_7BIT);
// Set the target address of the sensor (0x78 = 120dec)
I2C_set_target(0x78);
// Send start condition for reading from sensor (slave)
I2C_send_start_read();
// Read first (MSB) data byte and answer with ACK (continue communication)
I2C_read (&byte_msb, SEND_ACK);
// Read second (LSB) data byte and answer with NACK (end communication)
I2C_read (&byte_lsb, SEND_NACK);
// Send Stop condition
I2C_send_stop();
// Put both values together

I don’t know how to do with i2c library. Actually code that I’m using replay evry time 0 for evry pressure applied to this sensor.
Sensor is working fine.

The second sensor is a temperatur sensor on i2c bus and it works fine, problem is on the first one…

Ideas???

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.IO;
 
 
namespace change_this_to_your_namespace
{
    public class Program
    {
        public static void Main()
        {
            //create I2C Device object representing both devices on our bus
            I2CDevice.Configuration conDeviceA =
               new I2CDevice.Configuration(0x78, 100);
            I2CDevice.Configuration conDeviceB =
               new I2CDevice.Configuration(0x48, 100);
 
            //create I2C Bus object using one of the devices on the bus
            I2CDevice MyI2C = new I2CDevice(conDeviceA);
            // Note you could have chosen to create the bus using the conDeviceB parameter, which ever you choose it will be the "selected" device on the bus to start with. Here's how you would do that:
            // I2CDevice MyI2C = new I2CDevice(conDeviceB);
 
            //create transactions (we need 2 in this example)
            I2CDevice.I2CTransaction[] xActions =
               new I2CDevice.I2CTransaction[2];
 
            // create write buffer (we need one byte)
            byte[] RegisterNum = new byte[1] { 0 };
            xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum);
            // create read buffer to read the register
            byte[] RegisterValue = new byte[2];
            xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue);
 
 
 
            // Explicitly set the I2C bus to access device A by setting the I2C Config to the Device A's config.
            MyI2C.Config = conDeviceA;
            if (MyI2C.Execute(xActions, 1000) == 0)
 
            {
                Debug.Print("Failed to perform I2C transaction");
            }
            else
            {
                Debug.Print("Register value: " + RegisterValue[0].ToString() + RegisterValue[1].ToString());
            }
 
            // Explicitly set the I2C bus to access device B by setting the I2C Config to the Device B's config.
            MyI2C.Config = conDeviceB;
            if (MyI2C.Execute(xActions, 1000) == 0)
            {
                Debug.Print("Failed to perform I2C transaction");
            }
            else
            {
                Debug.Print("Register value: " + RegisterValue[0].ToString());
            }
        }
    }
}

Welcome to the community! Please update your profile and give yourself a real name so we know who we’re talking to.

Have you seen the I2C tutorial?
http://www.ghielectronics.com/docs/12/i2c

Considering that you’ve got the second sensor working, it sounds like you better give more details on the exact part you are using for the first sensor. Maybe someone here has used it before and knows what the trick is.

@ ianlee74 -

This ismy sensor, I follow the i2c guide and I use that code but it doesn’t work with that sensor.

Ideas???

Regards

You need to point out a data sheet for the sensor or I2C chip in question. That way we can see if there’s anything specific discussed in there that may need to be dealt with that your code does not.

This is datasheet of my sensor

Regards

OK, great; now you need to tell us how your program executes, what your actual debug output is.

First thing I note is that you print out the returned value as two bytes where it’s meant to be a 15-bit number (agreed though, if you’re just getting zeros as both bytes, doing anything with zero will still give zero !), so once we get things working remember to deal with that…

Have you tried using a different register address in RegisterNum ? There’s nothing in the datasheet that I could see that talks about that at all.

Your xActions object contains a write and a read. That might be what is required for the temp sensor, but the pressure sensor probably cancels the communications when you try to write to it.

Create another xActions with only one read. Then it whould work ok.

Good idea, I’ve tried to make a new xaction with only read, and sensor starts to replay.
The only thing is that sensor replay all the time with the same value even if I see on analogical pin that values changes, it’s quite strange…
Ideas???

Regards

@ GMod(Errol) -

Good idea, I’ve tried to make a new xaction with only read, and sensor starts to replay.
The only thing is that sensor replay all the time with the same value even if I see on analogical pin that values changes, it’s quite strange…
Ideas???

Regards

Manual of pressure trasmitter said to send a bit after address to define is trasmitter has to send or receive how can I do that ???

Regards

you don’t, the framework knows that when you’re doing a WRITE it sets that bit to 0 and READ is set to 1. Do you have a logic analyser? You could verify it is sending correctly with that.

Sorry. I didn’t see that there was a reply, as I don’t get emailed. And I was sleeping. :slight_smile:

The sensor’s datasheet says that it updates the reading every 250us.

Make sure that you read 2 bytes. Maybe try to read 10 bytes in one transaction and see if you get 5 readings.

If you read only one byte then it might not update the registers in the sensor to prevent data corruption…

1 Like

Ok you was right.
Now it works!!

Tks very much