INA219 Driver
# help
w
hi toit is really great! I'm having fun with it on multiple devices. It was no problem to get into it all. But I have problem to create a new driver for the Ina219 i2c sensor. I use other i2c drivers as blueprints. I get an I2C_READ_FAILED when I try to read the chip id.
k
Hi @wildlifenaturetech! Have you tried scanning the i2c bus to make sure you have the right address?
Copy code
import gpio
import i2c

main:
  bus := i2c.Bus
    --sda=gpio.Pin 21
    --scl=gpio.Pin 22
  print bus.scan
(something along those lines)
w
yes I'm using the correct address from the scan result.
k
Cool.
Are you comfortable sharing some of the code you wrote for reading the chip id?
w
Copy code
import binary
import serial



class Ina219:
  static I2C_ADDRESS ::= 0x64

  static REG_CHIPID_           ::= 0x02
  
  d := null

  registers_/serial.Registers

  constructor dev/serial.Device:
    d = dev
    registers_ = dev.registers



    r := registers_.read_u8 REG_CHIPID_
    print "r $r"
k
Are you following a datasheet or some sample?
f
The I2C address looks right.
However the ina219 has an unusual way of reading data: one has to write to a certain address first which latches the address. When reading one doesn't write the address anymore. (Section 8.5.6)
Also, every register in the sensor is 16 bits wide.
So you would use
device.write #[register_address]
to set the address and then
device.read 2
to read the 16 bit register.
w
It was a stupid mistake. I have looked into multiple samples, documentations, data sheets and other driver implementations. I learned a lot and my love for Toit is getting bigger. The
I2C_ADDRESS
was wrong in my driver 😦 I saw the correct in the i2c scan but typed in wrong in the drive. Tanks for your help and kind contact. Hope to publish the INA219 and INA3221 pkg soon.
f
Glad it worked out. 🙂
2 Views