SparkFun Linear 3D Hall-Effect Sensor - TMAG5273 (Qwiic) with Raspberry Pi python

Hello,

I am trying to create a rotary encoder using the 3D Hall-Effect sensor (TMAG5273) using my Raspberry Pi 4 model B with Python. I want to read the current angle of the magnet. I am using a diametrically polarized ring magnet.

However, I am having a hard time finding any resources on how to read data from the TMAG5273.

I wrote a sample code in Python to read the data from the sensor using I2C, however, I was not able to get anything that did make sense. (I attached my code below)

I also attached below the output from the code

I would really appreciate it if you could help me out with an example code.

Thank you,

Tozturk

from smbus2 import SMBus
import time

# I2C channel 1 is connected to the GPIO pins
channel = 1

# Device Address
address = 0x22

# Initialize I2C (SMBus)
with SMBus(channel) as bus:
    try:
            bus.pec = 1  # Enable PEC

            # Loop forever
            while(True):
                # Read 32 bits of block data
                block = bus.read_i2c_block_data(address, 0, 32)
                # Print out the block
                print(f"\nData: {block}")
                # Delay for 0.5 seconds
                time.sleep(0.5)

    except KeyboardInterrupt:
        bus.close()

It just repeats the output below, even when moving the magnet on top of it:

Data: [0, 0, 0, 0, 16, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

https://docs.sparkfun.com/SparkFun_Qwii … ple_basic/ :slight_smile:

Thank you for posting the URL to example usage for Arduino UNO. Unfortunately, I can not use this code which is written for an Arduino. I want to use it on a Raspberry Pi instead. I cannot use the Arduino Library written for this sensor because the port and bit numbers between ATMega328p on Arduino UNO versus Broadcom BCM2712 quad-core Arm Cortex A76 processor on Raspberry Pi are completely different. They are using microprocessors with completely different processors architectures.

Is there a way you guys could please provide a library for Raspberry Pi 5 that works with Python?

It doesn’t look like they have a raspberry pi library, you’re probably going to have to write your own.

^Like they said, we don’t have a python library ready-made…

but the good news is that Pi’s can run Arduino code with a bit of work How to Run Arduino Code & Programs on Raspberry Pi, or you can fairly simply make API calls to arduino libraries from within a python program Arduino With Python: How to Get Started – Real Python using i2c (must enable via the Pi’s config menu!)

For the code you posted: maybe try pySerial instead