MCU learning, Entry-Level, Part 4, ADC, Analog to Digital converter

From the last section, we learned about how to use the GPIO. One of function actually we did not talk about detail a lot which is ADC.

In nature world, most of the signal tha we can pick up is generally an “Analog” signal, such as sound, temperature and pressure. Digital signal is created by human for computer to process datas. Imagine how many rational number between 1 to 10? Infinite right? How many number can a computer system present? Depends on how many bits it have. But it is an fininite number for a signle data.


1. How to present an infinite state with a finite state?

=> Using a technique called “Quantization”

The behaviour will be very similar to the following picture:


In the computer world, we using the blue line(3-bits Digital signal) to present the red line which is a sine or cosine wave. As you can see, the blue line should as close as possible to the red line. But most of the time, this 2 line still have some error.

2. Why digital signal in the computer world is a finite combination?

This question also related to how the computer world presents a simple integer number. Since computer using “bit” as the smallest unit which can only be 0/1, a 16-bits system will have a 2¹⁶ different combination.

For example, you can find the ADC spec which mentioned it is a 12-bits ADC with 0-3.3V voltage range. It means that 0–3.3V is present by a 2¹² combination. Or 3.3V is “chopped” into 2¹² -1 = 4095 pieces.

3. ADC function in Particle Photon

ADC conversion also takes a period to complete we called it sample rate. So Particle also mentioned that the default value is around 16us = 0.0000016s. Generally, it is enough for basic measurement.

Let’s code!

Using the same code in Part 3: LINK

Change the current function to “ANALOG_READ”


Compile -> Flash -> Run

Open the serial monitor: Particle serial monitor

You should see the following message:

By turning the potential-meter or variable resistor, you should see the values going up and down.

Notice that the raw value will change slightly even you did not turn it. It is caused by noise in the system. The noise could come from fluctuating voltage reference, unstable potential meter and background noise.


There are several ways to reduce or eliminate noise.

A. Using hardware filter(Normally it is a circuit.)

B. Using software(Digital) filter.

C. Using a more stable/precise voltage reference.

D. Using a more stable/precise variable resistor.

For this exercise, let’s use a software filter as a demonstration.

In the original source code, I had implemented a “Moving average filter”

Change the following code


Compile -> Flash -> Run -> Serial monitor.

You should see the following message. At the same time, you should see the value after filtered is much more stable than RAW value. For this “Moving average” filter, before the array is fulfilled with values, it should be ignored.

In this case, we are using 5 data and averaging it. So the first 5 data’s filtered result should be ignored.


ADC is a useful feature for MCU to read some analog signal. But it has some limitation, such as noise, resolution and sample rate.

Advance reading

  1. There are lots of filtering algorithm you can implement to it. Even moving average algorithm has some other advance algorithm.
  2. There are also lots of technique to reduce noise and enhance accuracy. Using an LDO(Low drop output) to have a stable output voltage within the load.
  3. There is some other HIGH accuracy but low sample rate ADC called delta-sigma ADC.
  4. When you are selecting the ADC module for your application, considering S/N ratio(Signal Noise Ratio) in your system.

Software engineer, Movie lover, Karate beginner.