I'm trying to read data from the MMC34160PJ magnetometer using the Raspberry Pi 4. However, I'm pretty stuck as to how I can do it as there's seemingly no example code or library for it. I have been able to use the magnetometer with an Arduino, but I'm struggling when trying to port the code over to the RPi using SMBus2 for Python.

I have tried reading the quick start guide on 2.3 from this manual: https://download.kamami.pl/p566664-pmod_cmps2_rm.pdf but I don't understand what commands to use to follow these instructions. I also tried following the Arduino library/Arduino example code https://www.arduinolibraries.info/libraries/mmc34160-pj with little to no luck as that seems to be using a completely different structure when working with I2C.

I'd appreciate it if someone was able to shed some light on how I can achieve the function described in the I2C pseudo code in the datasheet. I have tried writing some code, but it's really just been a shot in the dark and it seems a bit complex judging by the Arduino code.

To be a bit more precise, I'm wondering how I can do the following (These are steps from the datasheet for the sensor):

  1. Provide a START condition and call the device ID with a write bit. Ref datasheet pseudo code:

I2CBegin(0xA0); //device ID 0x30 with a write (0) bit)

  1. Wait to receive an ACK from the Pmod CMPS2.
  2. Send the Internal Control Register 0 (address 0x07) as the register to communicate with. Ref datasheet pseudo code:

I2CWrite(0x07); //address 0x07 corresponds to Control Register 0

. 4. Cycle the SCL line to receive the data bits from the X, Y, and Z registers in the SDA line, providing an ACK between each data byte. The Pmod CMPS2 address pointer automatically moves to each consecutive byte. End the communication by sending a NACK followed by a STOP command. Ref datasheet pseudo code:

I2CReadMultiple(6); //read six bytes, sending an ACK to the slave device betwe en each byte received and a NACK after the last byte

If i understand how I can do these operations, I think I'm much closer to being able to get data from the sensor.

Thank you for any guidance you may provide.

1 Answer 1


I suggest you experiment with pigs to find the commands to use.

Start the pigpio daemon.

sudo pigpiod

then play with pigs.

pigs i2co 1 0x30 0 # should return handle 0

pigs i2cwd 0 0x07

pigs i2crd 0 6

will probably do what you want.

This assumes the device appears at address 0x30 on the I2C bus.

i2cdetect -y 1

  • Thank you for suggesting pigpio. I was able to query the sensor and subsequently write a program in NodeJS (this is ideal for my use) to do this every few seconds. I was really hitting my head against the wall, but after experimenting with pigs and understanding how to actually talk to the sensor, it became much clearer. I appreciate your help, I was able to convert the raw data from the sensor to actual usable data today!
    – Frostbite
    Feb 16, 2022 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.