I2C write/read function

Hi,

First of all, I apologize if I’m making a silly mistake here. I purchased the a micro bit, the weatherbit kit, and a few additional Qwiic I2C Sensors to connect to the weatherbit.

I’ve come to understand that I can program the microbit from MakeCode or another coding environment like Arduino IDE. I’d like to stay in MakeCode to avoid having to translate existing MakeCode functions for the weather sensors. However, I can’t seem to figure out my i2c communications in MakeCode.

If I go to individual sensor datasheets, they describe how to communicate with a sensor. However, I can’t find documentation to see how much of it the “i2c read number” or “i2c write number” functions do internally. Sample code I’ve found is all written in Arduino IDE with arduino functions.

Long story short, my MakeCode is below in python, and here’s a link to the sensor datasheet that explains its I2C communication.

https://cdn.sparkfun.com/assets/d/7/4/2 … asheet.pdf

As far as I can tell, to configure the sensor I need to write to 7-bit address 72 (48h), register 0 (00h) with the it’s settings data separated into two 8bit numbers: 19 and 0 (00010011 00000000b). I then need to read two 8-bit numbers back. However, the read function just returns 0 at the moment.

Thank you in advance for any pointers,

James

Configure I2C sensor

pins.i2c_write_number(72, 0, NumberFormat.INT8_LE, True)# Writetoaddress72/register0

pins.i2c_write_number(72, 0, NumberFormat.INT8_LE, True) # Write sensor settings (Least Significant Byte)

pins.i2c_write_number(72, 19, NumberFormat.INT8_LE, False) # Write sensor settings (MSB)

Read I2C sensor

Lux = pins.i2c_read_number(72, NumberFormat.INT8_LE, True) # Read LSB

Lux1 = pins.i2c_read_number(72, NumberFormat.INT8_LE, False) # Read MSB

Display data

def on_forever():

basic.show_string(“I2C”)

basic.show_number(Math.idiv(Lux, 1))

basic.show_string(“and”)

basic.show_number(Math.idiv(Lux1, 1))

basic.forever(on_forever)