As5600 encoder signal conversion to quadrature abz using pro micor

hello, im newbie in arduino world and still learning, is it possible to convert as5600 absolute positioning signal to abz quadrature using pro micro? here the sketch

Use code tags to format code for the forumUse code tags to format code for the forum#include <Wire.h>
#include <AS5600.h>

#define ENCODER_ADDR 0x36   // I2C address of the AS5600 encoder

AS5600 encoder;

// Define the pin assignments
const int A_PIN = 2;   // Arduino digital pin for A signal
const int B_PIN = 3;   // Arduino digital pin for B signal
const int Z_PIN = 4;   // Arduino digital pin for Z signal

int previousAngle = 0;
int currentAngle = 0;
int rotations = 0;

void setup() {
  Wire.begin();
  Serial.begin(9600);

  pinMode(A_PIN, OUTPUT);
  pinMode(B_PIN, OUTPUT);
  pinMode(Z_PIN, OUTPUT);
}

void loop() {
  currentAngle = encoder.getPosition(ENCODER_ADDR); // Read angle from AS5600

  // Calculate the difference in angle
  int angleDiff = currentAngle - previousAngle;

  // Handle rollover of the angle (e.g., from 0 to 360 degrees)
  if (angleDiff < -180) {
    angleDiff += 360;
  } else if (angleDiff > 180) {
    angleDiff -= 360;
  }

  // Update the total number of rotations based on angle changes
  rotations += angleDiff / 360;

  // Calculate the angle within one rotation (0 to 360 degrees)
  int angleWithinRotation = currentAngle - (rotations * 360);

  // Convert angle to A, B, and Z signals
  bool aSignal = (angleWithinRotation >= 0) ? HIGH : LOW;
  bool bSignal = (angleWithinRotation >= 0) ? LOW : HIGH;
  bool zSignal = false;  // Implement logic to determine when to generate Z index signal

  // Output A, B, and Z signals
  digitalWrite(A_PIN, aSignal);
  digitalWrite(B_PIN, bSignal);
  digitalWrite(Z_PIN, zSignal);

  // Store the current angle as the previous angle for the next iteration
  previousAngle = currentAngle;

  delay(10);  // Adjust delay based on your requirements and the speed of your application
}

im still learning and very new, hope for guidance thanks

I'd read the raw 0-4095 value and return an A,B,Z based on that. Something like this completely untested code:

    byte greyValues[4] = {0b00, 0b01, 0b11, 0b10}; // translation from binary to grey code transitions

    uint16_t rawVal = encoder.rawAngle(); // 0-4095
    byte AB = greyValues[rawVal & 0b11]; 
    digitalWrite(A_PIN, (AB & 0b10) ? HIGH : LOW);  
    digitalWrite(B_PIN, (AB & 0b01) ? HIGH : LOW);
    digitalWrite(Z_PIN,rawValue < 5 ? HIGH : LOW; // choose an arbitrary Z width

I'm not sure how much noise is in the system, but if the noise is a problem with respect to the polling speed, you simulate a lower resolution device by using the more significant bits from the raw value:

uint16_t rawVal = encoder.rawAngle() >> 2; // 0-1023
uint16_t rawVal = encoder.rawAngle() >> 4; // 0-255
uint16_t rawVal = encoder.rawAngle() >> 8; // 0-15

thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.