An analogue-to-digital converter (ADC) is an electronic device whose function is to translate an analogue quantity into a digital value encoded on several bits. The converted signal is usually an electrical voltage.
As we have explained before, the GPIO pins of any microcontroller board such as Arduino, ESP32 etc can handle digital input or output only. The analogue pins of Arduino are an implementation of an Analog-to-Digital Converter (ADC).
When we are using Arduino UNO and want to read analog pin input in volts (which can be 0 to 5 V) then we can give digital output (0 to 1023) depending upon microcontroller resolution. Arduino has a 10-bit ADC which means it can detect (2^10 = 1024) discrete analog levels.
---
The conversion process is based on the quantization of a signal, i.e. by matching it to the nearest quantized level. The quantification of a signal degrades its richness (there is a loss of information), so it is a destructive transformation that takes place. The greater the number n of bits of the converter on which the conversion is performed, the more the quantization has a reduced effect on the degradation of the signal (without ever cancelling it).
An ADC converts a continuous input signal (analog signal) into a discrete-time and discrete-value sequence of digitally represented values. Due to a finite number of possible output values, quantization always takes place. The result of an AD conversion can be imagined in a signal-time diagram in a sequence of dots with stepped horizontal and vertical distances. The main parameters of an ADC are the bit rate (explained above) and its maximum sampling rate. The conversion time is usually much smaller than the reciprocal of the sampling rate.
Even the bit depth of an AD converter limits the maximum possible accuracy with which the input signal can be converted. The usable accuracy is lower due to further sources of error of the ADU. In addition to methods that are as fast as possible, there are also slow (integrating) methods for suppressing interference coupling.
In case of Arduino, the simplified equation ADC uses is:
ADC reading = (1023/5) * Analog voltage measured
So, the (1023/5)
part becomes a constant. The topic ADC is highly complex. The things to remember for building projects are resolution (in bits), maximum sampling rate and noise.
Previously we talked about Kalman filter to “smoothen” the output. You’ll get the required library for Arduino to use the Kalman Filter.
The problem of ADC and PWM with simple code like this one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | int sensorValue = 0; int outputValue = 0; void setup() { pinMode(A0, INPUT); pinMode(9, OUTPUT); Serial.begin(9600); } void loop() { // read the analog in value: sensorValue = analogRead(A0); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(9, outputValue); // print the results to the serial monitor: Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue); // wait 2 milliseconds before the next loop for the // analog-to-digital converter to settle after the // last reading: delay(2); // Wait for 2 millisecond(s) } |
… is the “jitteriness” out of noise and other factors. You’ll never a get steady output out of the box such as 3.3v, it will dance from something 2.8 to 3.7v :
To solve this issue, you can use any algorithm like Kalman Filter. There are simple Kalman filer libraries with easy examples:
1 | https://github.com/denyssene/SimpleKalmanFilter/blob/master/examples/BasicKalmanFilterExample/BasicKalmanFilterExample.ino |