Skip to main content

Analog Inputs

Analog inputs are a special type of microcontroller (MCU) pins. They allow conversion of an analog voltage into a digital value. On most MCUs, not all pins can be used as analog inputs. When selecting the board model you want to use (top left corner of Node Blue window), all the specific information concerning analog pins, ADC resolution, analog reference, sampling rate, etc. are automatically updated in the interface.

An ADC (Analog to Digital Converter) converts a voltage into a digital number. The resolution of the ADC determines how many distinct values it can produce. For example, a 10-bit ADC produces values between 0 and 1023, while a 12-bit ADC produces values between 0 and 4095. Most MCUs have 8 to 16 bits ADCs. To simplify design, all analog modules have signed 16 bits outputs. Analog pins accept volages between 0v (Gnd) and a reference voltage. Sometimes you can choose between several analog references. However you must never feed the ADC with a voltage higher than the MCU supply voltage (usually 3.3v or 5v). On some MCUs, you'll find hardware averaging features, to reduce the noise that will always be more or less there in the signal. Noise can come from the device you're measuring, the electromagnetic fields, ny the internal MCU switching, etc. You can select how many sample to avergage, at the cost of reducing the effective max sampling rate. If you need more averages, you can use an additional average module.

Now let's discover a few examples:

Select the board type you want to use, then choose the analog pin in the Analog In Cal module.

This example reads the voltage on an analog pin and sends the value to the serial monitor. Connect a potentiometer (or any analog sensor) to the selected analog pin, with the other two pins connected to Gnd and 3.3V (or 5V depending on your board). Open the serial monitor (# icon at the top of the window) and observe the values changing as you turn the potentiometer.

The Analog In Cal module provides a calibration feature so that inputs that don't use the whole voltage range are mapped to the whole output range. Dead zones can also be set so you'll be reaching a robust min or max output even when there's noise on the input (which will always be the case in real world).

If you need the raw ADC value instead, you can use the basic Analog In module, which outputs the raw integer value from the ADC.

Sampling rate and the Nyquist–Shannon theorem​

An ADC doesn't measure continuously: it takes a snapshot of the voltage at regular intervals. The number of snapshots per second is the sampling rate fs (in samples/s, often written Hz). NodeBlue reads each analog module once per MainLoop pass, so your effective sampling rate is roughly the loop frequency, possibly lowered by hardware averaging.

The Nyquist–Shannon sampling theorem tells you how fast that has to be: to faithfully capture a signal, you must sample at more than twice the highest frequency it contains.

fs > 2 · fmax (equivalently: fmax < fs / 2)

The half-sampling-rate limit fs / 2 is called the Nyquist frequency. Any frequency above it doesn't just get lost — it folds back into the measured band and shows up as a slower, completely fake signal. This is called aliasing, and once it has happened in the ADC, no amount of software processing can remove it: the false signal is now indistinguishable from a real one.

Aliasing is irreversible

A 60 Hz mains hum sampled at 100 S/s will appear as a phantom 40 Hz wave riding on your measurement. The only cure is to stop those frequencies from reaching the ADC in the first place — with an analog filter, before sampling.

A practical rule of thumb: sample at 5 to 10 times the fastest variation you care about, not just the bare 2×. The theorem's 2× is a mathematical floor that assumes a perfect filter and infinite precision; real signals need headroom.

Anti-aliasing: the external RC filter​

The classic, cheap way to keep out-of-band frequencies away from the ADC is a first-order RC low-pass filter: one resistor in series with the signal, one capacitor to ground at the pin.

signal ──[ R ]──┬── ADC pin
│
[ C ]
│
GND

Its cutoff (−3 dB) frequency is:

fc = 1 / (2 · π · R · C)

Pick fc comfortably below the Nyquist frequency (fs / 2) but above the bandwidth of your real signal. Because a single RC rolls off gently (−20 dB/decade), leave a wide margin — placing fc around fs / 5 is a reasonable start.

Example: with R = 10 kΩ and C = 100 nF,

fc = 1 / (2 · π · 10000 · 100e-9) ≈ 159 Hz

which nicely rejects mains hum and high-frequency noise for a slow sensor.

Mind the source impedance

The MCU's sample-and-hold circuit has to charge its internal capacitor through your resistor during a short acquisition window. Too large an R (above ~10 kΩ on most MCUs) makes the reading sluggish or inaccurate. Keep R in the 1 kΩ – 10 kΩ range and get your lower cutoff by increasing C instead.

This same RC network also doubles as a noise filter and protects the pin against fast transients — a good habit on any analog input wired to the outside world.

Filtering vs. averaging​

It's tempting to fix noise purely in software with hardware averaging or an extra Average module, but the two tools solve different problems:

  • Averaging (in software, after sampling) reduces random noise: averaging N samples improves the signal-to-noise ratio by roughly √N. It does not undo aliasing, because the false frequencies are already baked into the samples.
  • The RC filter (analog, before sampling) is the only thing that can stop above-Nyquist content from aliasing in the first place.

Use them together: an RC filter for anti-aliasing and transient protection, plus averaging to clean up whatever random noise remains. Remember that every extra averaged sample lowers your effective sampling rate, which in turn lowers your Nyquist frequency — so don't over-average a fast-moving signal.

Now let's move on to analog outputs.