Skip to content

Arduino analogRead Noise for Random Not Working

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about Arduino analogRead Noise for Random Not Working. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Using analogRead on a floating pin for random values produces repeated numbers.

Quick Fix

Wrong

int r = analogRead(A0) % 100;  // Floating pin reads similar each time
r is the same or very similar each call.
int getRandom() {
  // XOR multiple readings for better entropy
  int val = 0;
  for (int i = 0; i < 10; i++) {
    val ^= analogRead(A1) << (i % 16);
    delayMicroseconds(100);
  }
  return val % 100;
}

void setup() {
  Serial.begin(9600);
  randomSeed(getRandom());
}

void loop() {
  Serial.println(getRandom());
  delay(500);
}
Different random values each time (0-99).

Prevention

Floating analog pin readings have limited entropy — adjacent readings are correlated. XOR multiple readings with varying delays. For true randomness on ESP32, use esp_random() or the RNG peripheral. On AVR, the best approach is still randomSeed(analogRead(A0)) called once in setup().

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### How much entropy does analogRead have?

About 1-3 bits of entropy per reading on AVR. The lower 2-3 bits vary due to noise; the upper bits are stable.

Can I use two floating pins?

XOR-ing readings from two floating pins improves randomness. Each pin picks up different noise patterns.

What is the best random source on Arduino?

ESP32 hardware RNG > external TRNG > multiple floating analogReads > single analogRead.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro