agrobot_base/Firmware/Sensoriamento/as5600/as5600.ino

59 lines
1.1 KiB
C++

#include <Wire.h>
// Endereço I2C padrão do AS5600
#define AS5600_ADDR 0x36
// Registrador de ângulo bruto (RAW_ANGLE, 12 bits)
#define AS5600_RAW_ANGLE 0x0C
// Ajusta aqui pros pinos que você usar no ESP32-S3
const int SDA_PIN = 1;
const int SCL_PIN = 2;
uint16_t lerRawAngle()
{
Wire.beginTransmission(AS5600_ADDR);
Wire.write(AS5600_RAW_ANGLE);
Wire.endTransmission();
Wire.requestFrom(AS5600_ADDR, 2);
if (Wire.available() < 2)
return 0;
uint8_t highByte = Wire.read();
uint8_t lowByte = Wire.read();
uint16_t raw = ((uint16_t)highByte << 8) | lowByte;
raw &= 0x0FFF; // só 12 bits válidos
return raw;
}
float rawToDegrees(uint16_t raw)
{
// 0..4095 -> 0..360°
return (raw * 360.0f) / 4096.0f;
}
void setup()
{
Serial.begin(115200);
delay(500);
Wire.begin(SDA_PIN, SCL_PIN);
Serial.println("Teste AS5600 - I2C");
}
void loop()
{
uint16_t raw = lerRawAngle();
float deg = rawToDegrees(raw);
Serial.print("Raw: ");
Serial.print(raw);
Serial.print(" | Angulo: ");
Serial.print(deg, 2);
Serial.println(" graus");
delay(200);
}