Randomness via an ADC
A much higher level of randomness can be obtained by observing some aspect of the real physical world, and capturing a noisy signal (“noise” == “random” in physics … sort of).
With an ADC, this is easy to do: leave one ADC input pin floating, and measure its analog voltage. The least-significant bit will be the noisiest, always fluctuating due to electrical noise, e.g. the omni-present thermal brownian motion. If we repeat this 32 times and keep only the lower bit, we should get nicely random results:
ADC<0> adc; PinA<0> floatingPin; uint32_t randomNumber () { uint32_t r = 0; for (int i = 0; i < 32; ++i) r = (r << 1) | (adc.read(floatingPin) & 1); return r; }
https://jeelabs.org/2018/random-numbers/
alias pru='pio run -t upload'
[env:bluepill] platform = ststm32 board = bluepill_f103c8 framework = stm32cube upload_protocol = blackmagic upload_port = /dev/cu.usbmodemE0C2C5A7 lib_deps = jeeh
pio device monitor --quiet -b 115200 -p /dev/cu.usbmodem469
picocom -q -b 115200 --imap lfcrlf /dev/cu.usbmodem469
https://jeelabs.org/2018/getting-started-bp/
https://github.com/gd32v-rust/gd32vf103-example
https://how2electronics.com/interfacing-ds18b20-temperature-sensor-with-stm32/