guild icon
Toit
#INA219 Driver
Thread channel in help
wildlife-nature-tech
wildlife-nature-tech 03/19/2023 05:55 AM
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.
bitphlipphar
bitphlipphar 03/19/2023 05:58 AM
Hi @wildlife-nature-tech! Have you tried scanning the i2c bus to make sure you have the right address?
bitphlipphar
bitphlipphar 03/19/2023 05:59 AM
import gpio import i2c main: bus := i2c.Bus --sda=gpio.Pin 21 --scl=gpio.Pin 22 print bus.scan
bitphlipphar
bitphlipphar 03/19/2023 05:59 AM
(something along those lines)
wildlife-nature-tech
wildlife-nature-tech 03/19/2023 05:59 AM
yes I'm using the correct address from the scan result.
bitphlipphar
bitphlipphar 03/19/2023 05:59 AM
Cool.
bitphlipphar
bitphlipphar 03/19/2023 06:00 AM
Are you comfortable sharing some of the code you wrote for reading the chip id?
wildlife-nature-tech
wildlife-nature-tech 03/19/2023 06:02 AM
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"(edited)
bitphlipphar
bitphlipphar 03/19/2023 07:00 AM
Are you following a datasheet or some sample?
floitsch
floitsch 03/19/2023 10:26 AM
INA219 ZerΓΈ-Drift, Bidirectional Current/Power Monitor With I2C Interface (Rev. G)
floitsch
floitsch 03/19/2023 10:26 AM
The I2C address looks right.
floitsch
floitsch 03/19/2023 10:28 AM
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)(edited)
floitsch
floitsch 03/19/2023 10:28 AM
Also, every register in the sensor is 16 bits wide.
floitsch
floitsch 03/19/2023 10:35 AM
So you would use device.write #[register_address] to set the address and then device.read 2 to read the 16 bit register.(edited)
wildlife-nature-tech
wildlife-nature-tech 03/19/2023 06:30 PM
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.
floitsch
floitsch 03/19/2023 06:31 PM
Glad it worked out. :πŸ™‚:
16 messages in total