Stepper motor - STM32 - L298 - AS5048A - Unable to make it work in closed loop

Hello everyone,
it is some days I’m trying to move my first steps (lol) with simplefoc and a nema 17 stepper motor.

black stm32f407ve board
L298N driver
AS5600 (i2c)
AS5048A (spi)
Nema 17 50pp stepper motor

I first started with a nema 23 and an as5600 connected via 12c, I’ve able to make it work in closed loop but without much reactivity or smoothness. I’ve been told by @runger about the limitations imposed by the i2c bus and since I’m pointing to get something nice I opted for an AS5048A encoder connected via SPI. The nema23 motor was too big for my application so I ordered the nema17 I needed.

I started playing with it, working fine in open loop, but with a huge amount of noise that required the velocity LPF to be raised to 0.5 for it to work in closed loop.

Like the best noobie out there I glued the magnet directly on the stepper shaft to discover some time later that the magnet should be mounted on a non magnetic surface (RTFM)

I’ve also discovered the drivers repository containing additional libs for the AS5048A which included the diagnostic part.

Using the diagnostic output I discovered that the field was terribly low with the magnet mounted on the shaft (1700/2000 magnitude)

The magnet I’m using is a 8mm x 1mm N42 neodymium.

After mounting it on a plastic support and closer (almost touching) the sensor I’ve been able to reach 3700 of magnitude. Sensor AGC is still fired all the way up to 255 (and afaik high AGC = noise).

I had the suspect of a low magnetic field so the last week i ordered a bigger magnet which I should get tomorrow.

Actually the position noise is 0.2 electrical revolutions (50pp)


This is a list of motor.shaftAngle() outputs with the stepper disconnected from power.

Velocity noise stays into 0.03 rads/sec with the motor standing still and an LPF filter on velocity of 0.03

CW/CCW routine works.
Positive target produces positive voltage and vice versa.
Openloop works till 30 rads/sec then it starts chattering.
Tried various SPI frequencies, all of them apparently work.
Changing pwm frequency doesnt apparently produce effects, I have a scope but I still have to check if something changes.
Using pins PB0/PB1 PA0/PA1 for PWM.

I saw on the forum that there were some problems with stepper in the release version so I moved to the github version, calling the .git url from lib_deps of PIO in vscode.

There was a little but tricky problem with a struct, already reported in issues:

However I used the name which contains the assignments and not the copy and it is ok.

If i try to tune (thanks again @runger) the PID to make it work in velocity closed loop, by setting all the parameters to 0 then raising P by steps of 0.05 I see voltage and noise (sound) rising, but no movement happens.

Hints ?

Thank you!


Today i received the new magnet 4x4mm cylinder neodymium, this one is strong enough to get rid of the low field alarm and take adc in the peaceful zone.

However i spent the day trying to understand why it wasn’t working. Angle numbers and noise were looking good so I didn’t went investigating sensor mount (which was indeed quite rude).

At least I’ve learnt something about SimpleFOC!

I’ve changed the driver too, from the L298N to the Pololu board with the dual vnh5019, will try to revert to the L298N just for confirmation. However even the vnh didn’t work before fixing the alignment.

As stated in my first post pwm frequency changes weren’t effective, followed the debugger and found that it was ending into the generic mcu file instead of the platform specific stm32 one.

lib_archive = false, this platformio.ini settings did the trick (thanks @runger )

Now its time to draw and make a reliable sensor mount and start tuning the thing…but I’m already wondering, can the vnh current sense output be used someway to enhance the behaviour of the controller ?


I’m in a similar position. Nema 17 stepper, 50 pole.-pairs, L298N and an AS5048A. Only works in open loop mode.
I tried to make it work straight away and failed so I tried a different approach.
I set up a Nema17 BLDC 4 pole-pair motor with the L298N and AS5048a. It worked straight away open loop and closed loop voltage modes. I adjusted the voltages and PIDs to tune this setup.
Using the same code except, for the motor definition, I substituted the BLDC with the stepper. Hardware wise I only changed the motor drive wires from the L298N, there now being 4 wires instead of three.
The stepper also replaced the BLDC shaft connection to the sensor. The pins on the teensy and connections to the L298N were not touched.
The new setup with stepper worked almost off the bat in velocity_openloop but nothing in closed loop worked.
I tried adjusting PID gains and voltages, resulting at best in bad motor noises and high currents.

Anyone any ideas what I can try next

Please check my posts, there are some advices about the setup.
I’m now using another driver and I’m in the middle of a work, it would be messy for me to revert to the L298N, but I’m quite prone to tell you that if it was working in open loop the driver is ok.
The issue for me were:

Sensor Alignment:
centering, the AS5048A is bitchty about it, if i recall corretcly datasheet talks of 0.25mm area around the center.

Magnetic field:
For the magnetic field to be well shaped the magnet should not sit on ferromagnetic materials, try to put a non-ferromagnetic spacer between the magnet and the shaft.
AS5048 requires a pretty strong field, the first thin neodymium magnet I used wasnt able to provide a sufficient field to the sensor (reading diagnostic data you can check this)

However, the main and most tricky issue is alignment. Ok, you would say that the BLDC works fine, but it has much less pole pairs and is much less sensible to electrical angle issues and so alignment.

AS5048 manual

AS5048A Datasheet


Just wanted to say thank you. Your reply made me go back and have another try with sensor positioning. I adjusted the AS5048a and managed to get it working in closed loop modes, but low speed and very noisy.

Taking a further look the position from the AS5048a was very noisy. Replaced with AS5600 and working well now. But only up to 5 r/sec in velocity mode. After that getting noisy. Up to about 8 r/sec in torque mode.

I have more AS5048s on order I’m hoping to speed the stepper up a bit and run it without vibration and noise

1 Like

You are welcome! Happy you solved the issue!

With the as5048a, the right magnet and a powerful enough cpu even cheapish steppers can become very fast and precise (my closed loop speed is way faster than open loop)

Check this video

And this other one

At some point cpu will play a role. As5048 will provide more performance but it’ll require more cpu.
Im running on stm32 f407 at 11k loops/sec, i’m developing all the application around the mechanism.
Even with a quite fast cpu like this whenever i did something in the code without paying much attention at how smart it was in terms of cpu performance I were getting the stepper rumbling.

I suggest you to create a handy function which tells you loops/s on serial while you go ahead with development. Your pwm frequency should be at least double your loops/s.

Also, if you come back to the as5048a deploy its diagnostics functions, they are helpful to have the right magnetic setup for the sensor.

It works even with a field much lower than the minimum required, the difference is in how it’ll work. It has an agc on board and if it has to raise the gain, the noise will rise too. Like when you have to turn the volume of an audio amplifier all the way up because the input level is low: you hear a lot of noise. Working with the sensor without diagnostics you don’t know.

Have fun!

Hi Deffie,
Thanks again, I ordered a couple more as5048a, this time on a larger PCb so I don’t have to try and solder them. :roll_eyes:
I’m using a teensy 4 so controller performance should not be a problem, I do have an $11 stepper though!!!
Did you get this performance with the L298n? Did you get in excess of 200 rad/sec?



No i’m using another driver dual vnh5019, havent checked which limitations the 298 imposes (if any)

200 rad/s is about 2k rpm, I’m actually around 100 rad/s in position mode, but theres margin for enhancement, check Motion planner code - #5 by Geronimo