AutomateDirectUSAMotion LibraryIndustrial monitors & PCs

Comparison

AS5048A PWM vs SPI Angle Reading on a Multi-Encoder Robot

Sensors, encoders and machine vision on the machine Published 7 min read By AutomateDirectUSA engineering team

Short answer

Read the AS5048A over SPI and leave the PWM pin open. The PWM output repeats every millisecond in about 4119 slots, so each sensor needs pulse timing finer than one slot, and edge jitter of even one microsecond spans four slots. SPI returns the 14-bit angle word directly with no pulse measurement, wraps cleanly through zero, and scales to a whole robot with one chip-select per sensor.

On this page
  1. Two outputs on one sensor
  2. The timing budget the PWM output sets
  3. Why PWM capture fails to scale across joints
  4. What SPI costs instead
  5. PWM output versus SPI angle register
  6. Pitfalls that bite either interface
  7. Prove the choice on your own board
  8. The decision
  9. Questions asked

Read the AS5048A over SPI and leave its PWM output unconnected. Both pins carry the same angle, but the PWM pin makes the microcontroller measure a pulse width finer than one PWM slot on every sensor, while SPI hands over the 14-bit digital angle word with no timing measurement at all. On a robot with many of these encoders hanging off one ESP32, the PWM route spends interrupt time on every joint and still returns a coarser, noisier angle. SPI costs three shared wires plus one chip-select line per sensor.

Two outputs on one sensor #

The AS5048 is a 360-degree magnetic angle position sensor with a 14-bit resolution output; ams OSRAM documents a maximum system accuracy of 0.05 degrees when linearization is used. The die is factory-programmed into interface variants: the A part, the AS5048A, carries the SPI digital angle interface together with a PWM output pin, and the B part carries I2C. These are two independent ways to obtain the same measurement. PWM is not a protocol in the bus sense and shares nothing with SPI: it is a duty-cycle-encoded copy of the angle. The SPI path reads the angle register itself. Nothing in the sensor converts one into the other, so you commit to an interface at design time.

Whichever output you choose, magnet placement still governs absolute accuracy, and the choice between sensing at the motor's rear shaft and sensing through a belt reduction is its own decision covered in the rear-shaft magnetic encoder versus belt-driven encoder comparison.

The timing budget the PWM output sets #

Per the AS5048 datasheet, the PWM output repeats at 1 kHz and the frame is built from about 4119 time slots. Confirm the exact frame structure, including any header and trailer slots, in the datasheet of your exact part revision before hard-coding firmware numbers against it. Worked example, calculated from those two datasheet figures:

  • PWM frame period: 1 ms
  • Time slots per frame: 4119
  • Slot width: t_slot = 1 ms / 4119 = 242.8 ns
  • Angle represented by one slot: 360 degrees / 4119 = about 0.087 degrees

Two limits fall out of that arithmetic.

Resolution: resolving a single step means the capture timebase must be several times finer than the slot width, and the edge timestamps must survive interrupt latency undisturbed. The PWM route therefore delivers roughly 4119 distinguishable steps per turn. The SPI angle register is 14 bits wide, 16384 steps per turn, about 0.022 degrees per step, roughly four times finer than the PWM frame can express.

Update rate: the PWM angle refreshes once per frame, so a control loop running faster than 1 kHz sees repeated angles, and any velocity estimated by differencing comes out stepped and noisy.

Why PWM capture fails to scale across joints #

Hardware input-capture channels fast enough to timestamp edges at nanosecond-class granularity are a limited resource on an ESP32, and a many-joint robot runs out of them quickly. The fallback is edge interrupts serviced in software against a cycle counter, and every interrupt carries latency jitter from flash cache misses, higher-priority interrupts and the wireless stack. Even 1 microsecond of jitter spans 1000 ns / 242.8 ns, about 4.1 slots, which is roughly 0.36 degrees of angle uncertainty at a joint that is supposed to be standing still. With N sensors at the frame rate, the processor absorbs 2N edge interrupts per millisecond, competing directly with the control loop for CPU time.

The wraparound problem is separate and electrical rather than a timing bug. Duty-cycle encoding reserves space at the extremes of the frame, so the measured pulse near zero and full scale can sit where the code is ambiguous or clamped. A dog's joints pass through zero in normal travel, so the artifact will appear in ordinary use. For background on the conversion mechanics, the walkthrough of how servo PWM pulse widths translate into degrees covers how a measured pulse becomes an angle at all.

What SPI costs instead #

SPI delivers the angle as a digital word, so the microcontroller performs no pulse measurement. Wiring is simple: share MISO, MOSI and SCLK across all sensors and give each AS5048A its own chip-select GPIO. The SPI command frame is 16 bits wide, made up of a parity bit, a read/write bit and 14 data bits, as the datasheet defines the frame. Take the SPI mode and the maximum clock from the datasheet of this part family, never from another encoder's numbers.

Bus time is easy to budget. As an illustrative calculation only, at an assumed clock of 1 MHz one 16-bit frame takes 16 microseconds, so eight encoders cost about 128 microseconds of bus time plus chip-select setup and turnaround, comfortably inside a 1 ms control cycle. Recompute with your actual clock, encoder count and setup times. Trigger the reads from a hardware timer or DMA so they do not wait on task scheduling, and check the parity and error flags in every returned frame, discarding anything flagged.

PWM output versus SPI angle register #

PWM output pin SPI angle register
Microcontroller work Measures a pulse width on every frame None: one framed bus transaction
Steps per turn About 4119 time slots 16384 (14 bits)
Smallest step About 0.087 degrees About 0.022 degrees
Update rate Fixed at the 1 kHz frame rate Limited only by bus time
Scaling to N joints 2N edge interrupts per millisecond plus jitter One transaction per joint from a timer or DMA
Behavior through zero degrees Possible dead or clamped zone at the frame ends Clean wrap from full scale to zero
Wiring One pin per sensor Three shared lines plus one chip-select per sensor

Pitfalls that bite either interface #

  • Differencing angles across the wrap point without modular arithmetic produces a full-turn velocity spike on both interfaces.
  • A shared MISO line where a deselected sensor fails to release the line corrupts reads; verify with a scope that only the selected device drives MISO.
  • Magnet air gap and alignment limit accuracy and raise error flags regardless of interface; keep the field inside the datasheet window and read the diagnostic bits rather than trusting the angle word alone.
  • Long ribbon cables out to leg joints pick up ringing on SCLK; keep the clock conservative and route chip-select lines away from motor phase wiring.

When you probe a powered axis, clip the scope ground to a quiet chassis point before touching any signal pin, because a ground clip that lands across motor phase wiring kills the probe and the encoder in one slip.

Prove the choice on your own board #

  • Hold a joint still and log 1000 SPI samples; record the peak-to-peak spread in counts, then repeat the same log on the PWM capture of the same position for a direct comparison on your hardware.
  • Sweep the joint slowly through the zero crossing and confirm the SPI word wraps from full scale to zero with no dead band or stall.
  • Run all joints at the planned loop rate and log the worst-case time to read every encoder; it must sit well under the loop period.
  • Compare two consecutive reads at constant joint speed: a steady difference confirms the velocity estimate is usable.

The decision #

For a robot dog with many AS5048A joints on one ESP32 and no extreme joint speed, wire SPI with one chip-select per sensor and leave the PWM pin open. Within a 1 ms loop you get 16384-step angles for roughly 128 microseconds of bus time at the assumed example clock, versus 4119-step angles that cost 2N interrupts per millisecond and an angle uncertainty of several slots from ordinary interrupt jitter, with a possible dead zone at zero on top.

PWM earns its place in exactly two situations: a single sensor on a bench setup where a spare hardware capture channel exists and nothing else competes for the CPU, and a debug tap on a test pad while you commission the SPI bus. Everywhere else, the SPI angle register is the working interface.

Questions asked

Does the AS5048B also have a PWM output?

No. The A and B suffixes are factory-programmed interface variants of the same sensor: the A part carries the SPI digital angle interface together with the PWM pin, while the B part carries I2C. On a B variant you read the angle register over I2C instead, which removes the pulse-width measurement exactly the way SPI does.

What magnet strength does the AS5048A need?

The datasheet specifies a magnetic field strength of 30 to 90 mT over the sensing surface. A magnet that is too small, too far away, or oversized and too strong pushes the field outside that window and raises the sensor error flags. The diagnostic bits in the SPI read frame let you catch that condition in software instead of guessing at it.

Can the AS5048A track rotation beyond one turn?

No. It is a single-turn absolute sensor: the angle word wraps from full scale to zero once every 360 degrees and there is no internal turn counter. Track whole turns in your controller with modular arithmetic on the angle delta, or combine the sensor with a separate home or limit switch for the turn count.

How fast can I clock the SPI bus to these encoders?

Take the maximum clock rate and the required SPI mode from the electrical characteristics section of the AS5048 datasheet, not from another encoder family. As an illustration only, a 1 MHz clock moves one 16-bit frame in 16 microseconds; on ribbon cables running out to leg joints, staying conservative on the clock is far cheaper than chasing ringing on a live robot.

Keep reading

  • Comparison

    Absolute joint encoders for a stepper robot arm without homing

    Fit a single-turn magnetic absolute encoder to the output side of each joint, after the gearbox: it reads the joint angle the moment the arm powers up, so no homing move or switch is needed. A 14-bit part resolves about 0.022 degrees per count, roughly 0.12 millimetres at the tip of a 300 millimetre link.

    Sensors, encoders and machine vision on the machine

  • Explainer

    Adding an Encoder to a Mini Servo: Measure the Joint Output

    An optical disk encoder can be fitted to a mini RC-style servo when the output shaft or hub has room for the disk and the housing can carry a stationary reader. Put it at the joint output, not the motor shaft, use two phase-shifted channels plus a reference, and leave the servo's internal sensor running its own loop.

    Sensors, encoders and machine vision on the machine

  • Explainer

    How a CN1 pulse train absolute position reading works

    A CN1-style absolute transfer sends the multi-turn count and the within-turn count as two separate pulse bursts at power-up, then switches to ordinary incremental pulses. Arm hardware counters before the drive transmits, decode both fields per the drive manual, compute initial position as multi-turn turns times counts per revolution plus the within-turn count, and hand off to pulse counting at a latched boundary.

    Sensors, encoders and machine vision on the machine