0

I'm trying to read the stepper motor angle using the AS5600 magnetic position encoder.

I'm using Raspberry Pi 4 and the pinout is the following:

VCC to 3.3V , GND to Ground ,SDA to GPIO2 (Pin 3) , SCL to GPIO3 (Pin 5) , DIR to Ground

The code I'm using is this:

import smbus
import time


bus = smbus.SMBus(1) # Define I2C bus number and AS5600 device address
AS5600_ADDRESS = 0x36


AS5600_REG_ANGLE = 0x0E  # Read-only register for angle value

def read_angle():
    # Read 14-bit angle value from AS5600
    data = bus.read_i2c_block_data(AS5600_ADDRESS, AS5600_REG_ANGLE, 2)
    angle = (data[0] << 8) | data[1]
    return angle

try:
    while True:
        # Read angle value from AS5600
        angle = read_angle()
        print("Angle:", angle)
        time.sleep(0.1)  # Wait for 100 ms before next reading

except KeyboardInterrupt:
    print("Program stopped by user")

I tried replacing the encoder module and wires, but no use. I don't know what to do at this point, any suggestions?

3
  • please add a link to the AS5600 datasheet ... add it into your question... do not write a comment
    – jsotola
    Commented May 25 at 13:45
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented May 26 at 9:32
  • You did not mention what specific problem you're facing. You can use this library instead if your current code is not working. github.com/Matthias-Wandel/AS5600-Raspberry-Pi-Python Once your code is fixed, you can make a breakout board like this too: pcbway.com/project/shareproject/…
    – liaifat85
    Commented Jun 26 at 16:14

1 Answer 1

0

Update your code to read from the correct angle registers.

import smbus
import time

bus = smbus.SMBus(1)  # Define I2C bus number and AS5600 device address
AS5600_ADDRESS = 0x36

AS5600_REG_ANGLE_H = 0x0C  # High byte of angle value
AS5600_REG_ANGLE_L = 0x0D  # Low byte of angle value

def read_angle():
    # Read 14-bit angle value from AS5600
    data = bus.read_i2c_block_data(AS5600_ADDRESS, AS5600_REG_ANGLE_H, 2)
    angle = ((data[0] & 0x0F) << 8) | data[1]
    return angle

try:
    while True:
        # Read angle value from AS5600
        angle = read_angle()
        print("Angle:", angle)
        time.sleep(0.1)  # Wait for 100 ms before next reading

except KeyboardInterrupt:
    print("Program stopped by user")

The high byte (0x0C) contains the 4 MSBs, and the low byte (0x0D) contains the 8 LSBs. You need to mask the high byte to get the correct 14-bit value. If you are also thinking about making an AS5600 encoder board of yourself, you can consider the following design:

https://www.pcbway.com/project/shareproject/AS5600_Encoder_Board_edbb75cc.html

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.