Calibrating inline current sense for the ESP32s3

Hi all, I’m hoping for some advice about current sensing with my setup. I am using the SimpleFOCShield v3 with an ESP32s3.

I’m trying to figure out what gain to use here:

InlineCurrentSense current_sense  = InlineCurrentSense(CURRENT_SENSE_GAIN,  A0, A1, A2);

The shield uses a ACS712-05B current sense chip, which has a gain of 185 mV/A documented in its datasheet, when powered with 5V. The ESP32s3 uses 3.3v, but I’m not sure what the gain would be if I underpower the ACS712 at 3.3v.

So, my plan is power the ACS712 at 5V. I don’t expect currents higher than 0.5A, so following @Antun_Skuric’s advice here, my expected output voltage range would be:

[2.5 − 0.5×0.185, 2.5 + 0.5×0.185] = [2.4075, 2.5925]

However, one issue is that this is outside of the default measureable voltage range of the ESP32s3, which maxes out at 0.95V. The ESP has attenuation settings that can be used to increase this range, but the SimpleFOC library doesn’t appear to use these in its hardware specific ADC driver for ESP32.

Another issue is do we need to account for ADC conversion when setting the current sense gain? Or does the SimpleFOC library do this for us, automatically? I’m trying to find the code that does this, but haven’t seen it yet.

Any advice would be muchly appreciated :pray:t2::pray:t2::pray:t2::pray:t2:

1 Like

However, this doesn’t seem to be working very well… the following screenshot is with SimpleFOC in torque voltage mode, with the target voltage set to zero. Vq and Vd are both zero in the graph. However, the current is all over the place, with noise up to 100mA! It appears that I have a major current noise issue.

The ESP32s3’s ADC has 12 bits or 4095 levels of resolution. Over 3.1V that’s 0.76mV per level. Using the current sense gain mentioned above, that’s a theoretical accuracy limit of about 4mA per level from the ADC.

However it’s a very different story with the ACS712 current sense chip:


With noise of 21mV, we should expect random current variations of up to 21/185 = 112mA! Which is exactly what I’ve been experiencing.

It appears that the current sense chip in the SimpleFOCShield is just not nearly accurate enough for my motor :frowning:

Immediately after making this post, I discovered the relevant code :person_facepalming:t2:

The attenuation and resolution are both being set here, and the 11db attenuation is being used which gives a maximum input voltage of 3.1V.

Then, my understanding is that the simpleFOCLibrary automatically finds the zero current point during the initFOC routine. So I don’t need to worry that my range is not centered.

So if I’m correct, then I can just initialise the current sense exactly as shown in the example:

InlineCurrentSense current_sense = InlineCurrentSense(185.0f, A0, A2);

I have one final question: if the ACS712 was instead powered at 3.3V, would the gain be the same?

Thank you for coming to my TED Talk :man_bowing:t2:

Hi guys!
This is a very interesting topic.

I’ve never really benchmarked the ina240 vs acs712 noise. I have only qualitatively verified that they output the same things and the only real difference I’ve seen is a bit of lag on acs712 (resulting in a bit more resistance backdriving the motor at 0 torque command).

Seeing the 50mA+ noise really surprised me as I know I was able to control the motor with 0.05 target torques and less. So I just kinda assumed that the noise is much lower.

Today I checked with my stm32 nucleo and I’ve got similar results to yours @Jeremiah_Rose. Thank you for pointing this out.

This much noise is still very much usable for gimbal motors, but I would have preferred to have much less and might consider pivoting from them in the future.
I do not have a v2.0.3 shield on my hands at the moment to compare. As soon as I get one I’ll do a more in depth testing and try to get some more results.

I had no idea the ACS712 was so noisy. Thanks for pointing this out. How does it even produce useable current readings for SimpleFOC if it’s that noisy? Basically it has +/-56mA accuracy which is fine if your motor peaks at 5A where that would just be 1% of peak current but I guess your motor has too high resistance windings.

FWIW the 185mv/A works even when the ACS712 is powered at 3.3v, even though officially this is out of spec. I asked on a different thread about this.

Looking at the ACS712 datasheet, I think you can improve noise performance with the filter capacitor at the expense of rise time:


The question is: what is the sampling frequency of the ACS712 in SimpleFOC?

There is a 1nF filter capacitor used on the latest SimpleFOC Shield so increasing that would certainly reduce noise.

I think you ended up measuring 21mV noise by chance because the stated figure in the datasheet is for very specific conditions (filter capacitor = 47nF) - I think the graphs I posted above are more applicable.