An Arduino library for magnetic rotary encoders that scales each turn by how fast you turn. A slow turn moves a parameter a little, and a fast one sweeps the whole range. It reads an AMS AS5600 or TI TMAG5273 over I2C and tracks position across multiple turns.
We wrote it for our instruments. It works the same in any Arduino project, and you can switch off the velocity scaling to use it as a plain encoder library.
We also make the Velocity Encoder, a small board with a TMAG5273B on it at I2C address 0x35. The library works just as well with AS5600 and TMAG5273 breakout boards.
| AS5600 | TMAG5273 | |
|---|---|---|
| Sensor | 12-bit on-axis magnetic encoder | 3D Hall-effect sensor with CORDIC angle |
| Resolution | 4096 counts per turn | 5760 counts per turn (1/16°) |
| Default address | 0x36 |
0x35 (A part). B 0x22, C 0x78, D 0x44 |
| Supply | 3.3 V or 5 V, depending on the breakout | 3.3 V (1.7 to 3.6 V) |
| Extra data | Angle | X, Y, Z field in mT, temperature, diagnostics, registers |
Wiring: VCC, GND, SDA and SCL to your board. On a TMAG5273, also tie TEST to GND. INT is optional. An SH1106G 128×64 OLED can share the bus at 0x3C.
In the Arduino IDE, use Sketch → Include Library → Add .ZIP Library…, or put this folder in your sketchbook's libraries directory. The core needs only Wire. The optional AlchemyOled helper also needs Adafruit SH110X and Adafruit GFX from Library Manager.
#include <MagEncoder.h>
MagEncoder encoder; // AS5600 by default
float cutoff = 0.0f;
void setup()
{
Serial.begin(115200);
if (!encoder.begin())
Serial.println("AS5600 not found. Check power, SDA, and SCL.");
}
void loop()
{
if (!encoder.isConnected())
return;
encoder.update();
cutoff = constrain(cutoff + encoder.takeParameterIncrement(0.0f, 1.0f, 4), 0.0f, 1.0f);
}The last argument is how many full turns span the range. Here, four turns cover 0.0 to 1.0.
takeParameterIncrement() applies each encoder count once, which is what you want in a fast loop. Call clearPendingTicks() when you change pages or modes so movement on one page doesn't land on the next.
For a TMAG5273:
MagEncoder encoder(MagEncoder::Sensor::TMAG5273);After update(), encoder.tmag() gives you the raw field, temperature and diagnostics without another I2C read.
These are the defaults we use. Every knob has a different travel and range, so set the curve in your sketch:
MagEncoder::Config cfg;
cfg.minVelDps = 90.0f; // speed where fine control ends
cfg.maxVelDps = 2400.0f; // speed where fast control tops out
cfg.minScale = 0.008f; // fine multiplier
cfg.maxScale = 3.2f; // fast multiplier
cfg.curveExponent = 1.8f; // shape of the middle of the curve
cfg.velocitySmoothing = 0.08f; // smaller is smoother
MagEncoder encoder(cfg);update() reads the sensor at most every 5 ms (readIntervalMs), so calling it on every loop is safe.
- VelocitySensitiveKnob: a velocity-scaled parameter with an optional OLED readout.
- TMAG5273Explorer: angle, field, temperature and diagnostics on cycling OLED screens.
- I2CBusCheck: checks SDA and SCL idle levels and scans the bus at 100 and 400 kHz.
The full API, including the TMAG5273 driver and AlchemyOled, is in docs/API.md.
MIT. See LICENSE.


