Microsoft MakeCode

I2C Read/Write Sample

Hi,

First of all, I apologize if I’m making a silly mistake here. I purchased the a micro bit, a 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 find a sample project using the i2c read/write functions in makecode and my code (below) isn’t working. Sample code I’ve found is all written in Arduino IDE with arduino functions. I’ve been able to get the sensor to work by programming the microbit with arduino IDE, so I know the sensor is functioning.

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

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

pins.i2c_write_number(72, 0, NumberFormat.UINT8_LE,True) # Writetoaddress72 (48h) register0

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

pins.i2c_write_number(72, 19, NumberFormat.UINT8_LE,False) # Write sensor settings (MSB) (00010011b)

def on_forever():

pins.i2c_write_number(72,4,NumberFormat.INT8_LE) # point to address 72 (48h) register 04h

pause(100)

data = pins.i2c_read_buffer(72,2) # read two bytes

data1 = helpers.buffer_to_array(data, NumberFormat.INT8_LE)

print(data1) 

pause(1000)

basic.forever(on_forever)

Hi HawaiiJim,

Looking at the Datasheet for the veml6030:

I see that bit 0 is the ALS_SD bit. Notice that it says 1 = ALS shutdown.

You are writing 00010011b to this and therefore shutting down the ALS.

Also, with regards to the address of 0x48, can you just confirm that you are indeed pulling the ADDR line high to set that? Otherwise the address is 0x10:

Your Write and Read sequences look good.

Let me know if that works for you.

Thanks,
Josh

Hi Josh,

Thanks for taking the time to reply!

I double checked the address and confirmed that the ADDR pin is pulled high by default. When I’ve used the scan function in python it reads the sensor’s address as 72 so I’m fairly confident about that.

Regarding the bits, unless I’m misinterpreting the datasheet, the Least Significant byte needs to be written first so the shutdown setting is written to as 0 (last bit in 00000000b). I tried flipping the two bytes around in case but it didn’t work either.

James

Hi James,

You are correct with regards to the LSB, my bad. I noticed that when you read your data from the buffer, you specify an 8 bit int. Shouldn’t you be asking for a 16 bit?

data1 = helpers.buffer_to_array(data, NumberFormat.INT16_LE)

I’d also use a unsigned int if there is one as I believe the data is unsigned?

Thanks
Josh

Hi Josh,

If I use an 8 bit it splits the data into two numbers, I switched it to 16-bit and it prints one zero instead of two zeros.

I switched the formats over to UINT from INT but we’re still at zeros :confused:

Thanks for helping with this,
James