Use ADC properly AVR MCU V4
In this video, I will shed some light on how to code ADC properly and correctly.
ADC is easy for me that I have done an extensive amount of prototype boards, but it can be hard for most people, especially when you are using a very badly written library to interface with it. ADC as a concept is easy, and the actual code to power it is also easy, but in reality, getting good results requires one to fully understand absolutely every aspect of how the ADC really works on many levels.
First thing is on the hardware level, you want to have a good design and good tracing to avoid inducing noise into the system. It's simple as that, but unfortunately this also gets escalated very quickly for more demanding applications with a lot of noise. For this demonstration, I am using my V4 board with 2 ADC channels connected to 1 potentiometer and 4 voltage based buttons. This board is not necessarily the ideal board to demonstrate ADC, but its draw traces for the 2 channels are adequate for minimizing the amount of noise induced to the inputs.
Second thing or the main thing is on the software level. I am not going to explain every bit on how to code the ADC. The most important thing or should I say the most common issue is that ADC has so much unstable noise due to the fact that residual charges from the previous input channel source get mixed to the new ADC channel. In other words, a spacing in time is required before switching to a new input source. This is the absolute golden key, but there are many other decision choices that could impact how smooth, how accurate, and how stable the ADC readings are. ADC can be configured many ways, but many time, for your own application, there is only one or two most optimized configuration.
So good ADC code has a lot to do with timing. You need to know how many samples to make in one second for each input. This is the most basic. The driver code for the V4 can be found in its corresponding page. For my driver, I am using a 1kHz timing loop to manage these 2 input channels, and 2 spacing, so the rate for sampling is 1000 over 4 or 250 Hz for both the 4 buttons and the potentiometer, which is actually more than enough. ADC can be extremely complex, especially when you are exploiting all its capability on a system level, as there are many interconnected paths between each part of the MCU, but for simple basic reading, it’s still relatively easy.
That is it. Enjoy the rest of the video of me playing with the ADC potentiometer every accurately and responsively. This may not seem a lot, but it can be very impressive in a subtle technical way.