Help with INA219

Can anyone help me out with INA219?
Im trying to convert this driver to TinyCLR.

This is what i got s far:

class Program
    {
        static I2cDevice i2cDevice;
        static int slaveAddress = 0x40;

        static void Main()
        {
            var btn = GpioController.GetDefault().OpenPin(SC20100.GpioPin.PE3);
            btn.SetDriveMode(GpioPinDriveMode.Input);
            btn.ValueChanged += Btn_ValueChanged;

            I2cController i2cController = I2cController.FromName(SC20100.I2cBus.I2c1);
            I2cConnectionSettings i2cConnectionSettings = new I2cConnectionSettings(slaveAddress);
            i2cDevice = i2cController.GetDevice(i2cConnectionSettings);

            Thread.Sleep(Timeout.Infinite);
        }

        public static void InaPointTo(Registers registerAddress)
        {
            byte[] inBuffer = new Byte[2];

            inBuffer[0] = (byte)slaveAddress;
            inBuffer[1] = (byte)registerAddress;

            i2cDevice.Write(inBuffer);
            Thread.Sleep(5);

            Debug.WriteLine($"INA Write: {inBuffer[0]} - {inBuffer[1]}");
        }

        public static byte InaRead()
        {
            byte[] outBuffer = new byte[2];
            i2cDevice.Read(outBuffer);

            Debug.WriteLine($"INA Read: {outBuffer[0]} - {outBuffer[1]}");

            return outBuffer[0];
        }

        private static void Btn_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
        {
            if (e.Edge == GpioPinEdge.RisingEdge)
            {
                Debug.WriteLine("Test INA219");

                InaPointTo(Registers.Power);
                InaRead();
            }
        }

        public enum Registers
        {
            /// <summary>
            /// All-register reset, settings for bus voltage range, PGA Gain, ADC resolution/averaging.
            /// </summary>
            Configuration = 0x00,
            /// <summary>
            /// Shunt voltage measurement data.
            /// </summary>
            ShuntVoltage = 0x01,
            /// <summary>
            /// Bus voltage measurement data.
            /// </summary>
            BusVoltage = 0x02,
            /// <summary>
            /// Power measurement data.
            /// </summary>
            Power = 0x03,
            /// <summary>
            /// Contains the value of the current flowing through the shunt resistor.
            /// </summary>
            Current = 0x04,
            /// <summary>
            /// Sets full-scale range and LSB of current and power measurements. Overall system calibration.
            /// </summary>
            Calibration = 0x05
        }

    }

This is the result, no matter which register i choose.

The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Test INA219
INA Write: 64 - 3
INA Read: 57 - 159

Without digging too much in your code and in the datasheet, I would not send the slave address.

Often, what you see in datasheets is the I2C protocol, which implies the slave address has to be sent. But this is done internally by the hardware. You don’t have to bother with that.

Like @Bec_a_Fuel suggested, if you remove the address from first bye, your code will likely just work.

Please see our i2c docs

You mean this?
https://docs.ghielectronics.com/software/tinyclr/tutorials/i2c.html

almost useless…

Useless is a bit harsh!!! I am looking at it and it has everything a user needs with the assumption the user know how i2c works. If a user doesn’t, they can check wikipedia and a thousand other sources that teach i2c… Including the official specifications.

Anyway, we are here to help!

1 Like

i think documentation at least need to have an fully working example by an sensor at least
beside description,

because there’s a lot of people who start using i2c on .net
or even it used on arduino way but for them is still hard to understood way how to use it on C# (or Arduino) because they all used it by templated way

  1. define i2c(with internal address)
  2. do write
  3. do delay
  4. do read …

and that all ,and most of people do not have idea that i2c have also ask/ack response too
i’m talking by experience have of usage sensors and asked people out of GHI ,too.

until i bought Cypress FX Logic Analyser and started to learn how protocol acts/works with PulseViw…

When using I2C modules, the hardest part is almost always to understand the datasheet of the slave device. I have often frustrations because every manufacturer has its own way of handling data.
But… the protocol by itself is always the same and its usage with TinyCLR is pretty straightforward, to me.

Reading and understanding the datasheet is paramount. Again, it’s not that easy, even when used to that.

I just disagree here. We purposely make sure not to put full examples on docs. There is drivers and samples repos for full examples.

not to put sample on doc ,
you can make reference link to an driver as look how was used at …refered to example/driver link…

1 Like

Yeah, but i don’t have a lot of time, lots of other work to do…

Agree with this as there is no actual indication of which sample uses which bus or feature. A link to each sample that uses the I2C, SPI but etc, would be handy to have on the documentation page.

Not trying to sell you anything here but GHI does offer consulting services for anything that needs extra help.