Skip to content

Instantly share code, notes, and snippets.

@ednisley
Created October 26, 2017 23:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ednisley/a2b935a1e6b38e238ff07f3569752e41 to your computer and use it in GitHub Desktop.
Save ednisley/a2b935a1e6b38e238ff07f3569752e41 to your computer and use it in GitHub Desktop.
Arduino Pseudo-Random White Noise Source
// Quick test for random bit generation timing
// Ed Nisley KE4ZNU - 2017-10-25
// Observe output bit on an oscilloscope
// LFSR info https://en.wikipedia.org/wiki/Linear-feedback_shift_register
// This code uses the Galois implementation
// Coefficients from https://users.ece.cmu.edu/~koopman/lfsr/index.html
#define PIN_RND 13
#include <Entropy.h>
uint32_t Rnd;
byte LowBit;
void setup() {
Serial.begin(57600);
Serial.println("Random bit timing");
Serial.println("Ed Nisley KE4ZNU - 2017-10-25");
Entropy.initialize();
pinMode(PIN_RND,OUTPUT);
uint32_t Seed = Entropy.random();
Serial.print("Seed: ");
Serial.println(Seed,HEX);
randomSeed(Seed);
do {
Rnd = random();
} while (!Rnd); // get nonzero initial value
}
void loop() {
// digitalWrite(PIN_RND,Rnd & 1); // about 55 us/bit
// Rnd = random();
LowBit = Rnd & 1;
digitalWrite(PIN_RND,LowBit); // about 6 us/bit
Rnd >>= 1;
Rnd ^= LowBit ? 0x80000057ul : 0ul;
}
@ednisley
Copy link
Author

More details on my blog at http://wp.me/poZKh-78y

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment