0

CircuitPython i2c write value to register
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CircuitPython i2c write value to register

by rwbaugher on Mon Apr 12, 2021 10:04 am

I have a magnetometer sensor and need to send the value 0x70 to register address 0x00 at device address 0x20. boardio has a write method i2c.writeto(device address, register address), but I see no way to send a value. MicroPython has a writeto_mem method that does this. Is there a similar capability in CircuitPython?

rwbaugher
 
Posts: 7
Joined: Mon Aug 31, 2015 3:24 pm

Re: CircuitPython i2c write value to register

by adafruit_support_carter on Mon Apr 12, 2021 3:06 pm

Do you mean busio? (instead of boardio). Docs are here:
https://circuitpython.readthedocs.io/en ... 2C.writeto

The first parameter is the device address. The second parameter is a buffer which can contain more than one value and can be thought of as being general purpose. The exact way you would write/read a register can vary depending on the device. The details would be in the datasheet.

adafruit_support_carter
 
Posts: 20331
Joined: Tue Nov 29, 2016 2:45 pm

Re: CircuitPython i2c write value to register

by rwbaugher on Mon Apr 12, 2021 11:42 pm

Thanks for the help. Yes, I meant busio as you suggest.

According to the Register Map for the device, to send the value 0x70 to register address 0x00 at device address 0x20, the CircuitPython code would be i2c.writeto(0x20,bytes([0x00,0x70])), I believe.

In looking at the Arduino-compatible library for the sensor, it appears that the library had two sequential write commands, one for the address and one for the value which appears to be another way to accomplish this task.

rwbaugher
 
Posts: 7
Joined: Mon Aug 31, 2015 3:24 pm

Re: CircuitPython i2c write value to register

by adafruit_support_carter on Tue Apr 13, 2021 10:29 am

That looks correct for a common usage case. The various CircuitPython driver libraries might also help to provide code examples.

adafruit_support_carter
 
Posts: 20331
Joined: Tue Nov 29, 2016 2:45 pm

Please be positive and constructive with your questions and comments.