Micropython ADC Noisier than Arduino on ESP32 #9227
-
So I'm somewhat new to Micropython and I am not sure exactly what I am doing wrong here. The current setup is that I have a motor making clean sine waves onto an encoder and the encoder is giving me an analog signal that I need to read with my ESP32. I've noticed that the signal is noisier than I had hoped, but I thought it might have been just the hardware. I then tested the exact same hardware with Arduino code but got a significantly cleaner signal. Here is the code and results of Micropython :
Here is the Arduino code:
Since the hardware setup is exactly the same, I must have messed up the code for the Micropython. Any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 13 replies
-
I notice that with the Micropython code that the input signals are about 10 times higher. These signals come from the encoder so how do they change level? I.E. what is the encoder source impedance? |
Beta Was this translation helpful? Give feedback.
-
Not merely are your results a different scale vertically, but the ESP32 seems to be recording the red trace with a period of roughly 27 ms (if that's the horizontal time unit) while the Arduino records the same period as around 100 ms. |
Beta Was this translation helpful? Give feedback.
-
Are those waveforms actual measurements or simulations? I don't understand how the levels could be different unless the ESP32 ADC is being setup differently between Arduino and Micropython |
Beta Was this translation helpful? Give feedback.
-
Is the encoder being driven by the ESP32 as well? Break the connection between the encoder and the ESP32s ADC to verify that the noise is coming from the encoder. I was focusing on the ADC input creating the noise. |
Beta Was this translation helpful? Give feedback.
-
@stoplime Can you tell us more about how you generated the "constant" waveform. What's connected to the pin while you're measuring that? It's really interesting that the glitches are perfectly correlated on both inputs. FWIW, I tried to reproduce this here with a sine wave from the signal generator but wasn't able to see the glitches. Out of curiousity, what's the frequency of the motor sinusoids? (@scruss Frustratingly the arduino plotter's x axis is sample-number, not time). In general the ESP32 (and especially MicroPython on the ESP32) is not particularly well suited to this sort of task because you will end up with a large amount of jitter in your sampling. Behind the scenes MicroPython is running in a FreeRTOS task and getting given little slices of CPU time. Your samples are not evenly spaced in time. This doesn't explain why you see these sharp drops though...something else is going on there. |
Beta Was this translation helpful? Give feedback.
-
What are you using for an encoder? So, that I can understand why an ADC is involved. |
Beta Was this translation helpful? Give feedback.
-
These two facts seem to point to an external electrical source of interference which is being coupled into both signals. For good ADC results you need a low impedance circuit and you need to ensure that the signal is arranged to match the ADC sensitivity. In other words, the signal minimum should result in an ADC value close to 0, and the maximum should produce something close to full scale. Meeting these requirements can involve using an op-amp for gain, level shifting and impedance reduction.
Quite. A schematic would help. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Two hardware issues with the 3V3 regulator on these boards ... the AMS1117 has been a suspect part for being able to handle the current required by WiFi. If you don't see that voltage drop running Arduino then it suggests to me that Arduino handles the start-up condition of the WiFi better. Run a test where you just boot to a state before any WiFi is activated and compare Arduino with Micropython. I have been in the habit of putting 1000uF on the 3V3 pin. Also, there are lots of threads on poor USB supplies and cables, but that shouldn't be your problem. I also often even up putting 1000uF on the 5V pin as well. Does D1 and Q1 have the linearity that you require? |
Beta Was this translation helpful? Give feedback.
-
The 1000uF on 3V3 to ground "seems" to help "with the boot process", but whether or not it reduces the noise to an acceptable level is now your job :) If the 3V3 is that critical maybe you need a proper supply. The AMS1117 seems to be able to handle the surge current required for this external cap but no guarantees from me. I would start another thread with your current question. Good luck |
Beta Was this translation helpful? Give feedback.
Two hardware issues with the 3V3 regulator on these boards ... the AMS1117 has been a suspect part for being able to handle the current required by WiFi. If you don't see that voltage drop running Arduino then it suggests to me that Arduino handles the start-up condition of the WiFi better.
Run a test where you just boot to a state before any WiFi is activated and compare Arduino with Micropython.
I have been in the habit of putting 1000uF on the 3V3 pin. Also, there are lots of threads on poor USB supplies and cables, but that shouldn't be your problem. I also often even up putting 1000uF on the 5V pin as well.
Does D1 and Q1 have the linearity that you require?