Sampling and Aliasing
Compare a sine wave sampled at different rates and see why sampling too slowly can make a signal appear different from the original.
Getting started: what you'll need
This section lists everything you need for the worked example below. Equivalent parts will work.
Equipment list
| Part number | Description | Qty |
|---|---|---|
| 10k potentiometer | 10 kΩ slider potentiometer. A 10k pot is also included in the Elegoo starter kit below | 1 |
| ABX00087 | Arduino UNO R4 WiFi | 1 |
| Jumper wire kit | Male-to-male breadboard jumper wires, assorted | 1 |
| BB830 (or equivalent) | 830-point solderless breadboard | 1 |
| USB-C data cable | USB-C to USB-A/C data cable (not charge-only) | 1 |
New to electronics? This Elegoo electronics starter kit includes the Arduino-compatible board, breadboard, jumper wires and 10 kΩ potentiometer, and much more. It has everything you need for this example.
New to Arduino? Start with Arduino's UNO R4 getting-started guide for setting up your board and uploading your first sketch.
Arduino Code
This sketch generates a sine wave on the UNO R4's DAC output, samples it at a rate set by the potentiometer, and scrolls the samples on the LED matrix. Use the Copy button, paste it into the Arduino IDE and upload.
// Nyquist / aliasing demo for Arduino UNO R4 WiFi
//
// Wiring:
// A0 (DAC out) --> jumper --> A1 (ADC in)
// Pot: one end 5V, other end GND, wiper to A2
#include "Arduino_LED_Matrix.h"
// ---------- Settings you may want to tweak ----------
const float SIGNAL_HZ = 2.0; // DAC sine frequency
const float FS_MIN = 1.0; // lowest sample rate (Hz)
const float FS_MAX = 20.0; // highest sample rate (Hz)
const float SIGNAL_AMP_V = 2.3; // sine amplitude (V) around 2.5 V midpoint; max ~2.5
const float DISPLAY_BOT_V = 0.0; // voltage shown on bottom LED row
const float DISPLAY_TOP_V = 5.0; // voltage shown on top LED row
const float ADC_REF_V = 5.0; // R4 ADC reference
const bool HOLD_MODE = true; // true = staircase, false = isolated samples
// ----------------------------------------------------
// Display scroll rate = max sample rate (one column per sample at full rate)
const int SCROLL_COLS_SEC = (int)FS_MAX;
ArduinoLEDMatrix matrix;
uint8_t frame[8][12] = {0}; // [row][col], row 0 = top
int rowForCol[12]; // row lit in each column (-1 = none)
const unsigned long DAC_PERIOD_US = 500; // DAC update every 0.5 ms
unsigned long lastDacUs = 0;
unsigned long lastSampleUs = 0;
unsigned long lastScrollUs = 0;
float phase = 0; // DAC sine phase (radians)
float fs = FS_MIN; // current (smoothed) sample rate
float potSmoothed = 0;
int heldRow = 7; // most recent sample as a row
bool newSampleThisColumn = false;
int voltsToRow(float v) {
float frac = (v - DISPLAY_BOT_V) / (DISPLAY_TOP_V - DISPLAY_BOT_V);
if (frac < 0) frac = 0;
if (frac > 1) frac = 1;
return 7 - (int)(frac * 7.0 + 0.5); // bottom V -> row 7, top V -> row 0
}
void drawFrame() {
memset(frame, 0, sizeof(frame));
for (int c = 0; c < 12; c++) {
if (rowForCol[c] < 0) continue;
frame[rowForCol[c]][c] = 1;
// fill the gap to the previous column so steep edges look like a line
if (c > 0 && rowForCol[c - 1] >= 0) {
int a = rowForCol[c - 1], b = rowForCol[c];
int lo = min(a, b), hi = max(a, b);
for (int r = lo; r <= hi; r++) frame[r][c] = 1;
}
}
matrix.renderBitmap(frame, 8, 12);
}
void setup() {
Serial.begin(115200);
analogWriteResolution(12);
analogReadResolution(12);
matrix.begin();
for (int c = 0; c < 12; c++) rowForCol[c] = -1;
potSmoothed = analogRead(A2);
lastDacUs = lastSampleUs = lastScrollUs = micros();
}
void loop() {
unsigned long now = micros();
// 1) Drive the DAC sine on A0, centred on 2.5 V
if (now - lastDacUs >= DAC_PERIOD_US) {
lastDacUs += DAC_PERIOD_US;
phase += 2.0 * PI * SIGNAL_HZ * (DAC_PERIOD_US * 1e-6);
if (phase >= 2.0 * PI) phase -= 2.0 * PI;
float volts = 2.5 + SIGNAL_AMP_V * sin(phase);
analogWrite(A0, (int)(volts / 5.0 * 4095.0 + 0.5));
}
// 2) Read pot -> sample rate (smoothed to avoid jitter)
potSmoothed += 0.02 * (analogRead(A2) - potSmoothed);
fs = FS_MIN + (potSmoothed / 4095.0) * (FS_MAX - FS_MIN);
// 3) Take a sample at the chosen rate
unsigned long samplePeriodUs = (unsigned long)(1e6 / fs);
if (now - lastSampleUs >= samplePeriodUs) {
lastSampleUs += samplePeriodUs;
if (now - lastSampleUs >= samplePeriodUs) lastSampleUs = now; // catch up after pot jumps
float v = analogRead(A1) * ADC_REF_V / 4095.0;
heldRow = voltsToRow(v);
newSampleThisColumn = true;
}
// 4) Scroll the display at a FIXED real-time rate, independent of fs
const unsigned long scrollPeriodUs = 1000000UL / SCROLL_COLS_SEC;
if (now - lastScrollUs >= scrollPeriodUs) {
lastScrollUs += scrollPeriodUs;
for (int c = 0; c < 11; c++) rowForCol[c] = rowForCol[c + 1];
if (HOLD_MODE) rowForCol[11] = heldRow;
else rowForCol[11] = newSampleThisColumn ? heldRow : -1;
newSampleThisColumn = false;
drawFrame();
// Print sample rate and predicted alias twice a second
static uint8_t n = 0;
if (++n >= SCROLL_COLS_SEC / 2) {
n = 0;
float k = round(SIGNAL_HZ / fs);
Serial.print("fs = "); Serial.print(fs, 1);
Serial.print(" Hz apparent freq = ");
Serial.print(fabs(SIGNAL_HZ - k * fs), 2);
Serial.println(" Hz");
}
}
}The sampling setup
The Arduino generates a sine wave on A0 and reads it on A1. The potentiometer connected to A2 sets the sampling rate.
Pinout wiring
| From | To | Purpose |
|---|---|---|
| Arduino A0 (DAC out) | Arduino A1 (ADC in) | Sine-wave signal; connect with a jumper wire |
| 5V | One end of the 10k pot | Top of the potentiometer divider |
| GND | Other end of the 10k pot | Bottom of the potentiometer divider |
| 10k pot wiper | Arduino A2 | Sets the sampling rate |
Input safety: keep the voltage at analogue input A1 between 0 V and 5 V relative to the Arduino's ground. Do not connect a negative-going or higher-voltage signal directly to the board.
What aliasing means
Sampling records snapshots of a changing voltage. With many samples during each cycle, those points describe the sine wave clearly. As the sampling rate falls, fewer points are captured and the reconstructed shape becomes less reliable.
If the signal changes more than the samples can distinguish, different input frequencies can produce the same sample pattern. This distortion is called aliasing: the sampled signal can appear slower or otherwise unlike the original.
The Nyquist criterion says the sampling rate must be greater than twice the highest frequency in the signal to represent that frequency without aliasing. In practice, sampling faster provides a clearer representation and room for real-world filtering.
Sampling-rate comparison
These results show the progression from a clear sine wave to a poorly represented signal as the sampling rate is reduced: