Entropy And The Arduino: When Clock Jitter Is Useful

What do you do, when you need a random number in your programming? The chances are that you reach for your environment’s function to do the job, usually something like rand() or similar. This returns the required number, and you go happily on your way.

A shift register configured as a pseudo-random number generator.
A shift register configured as a pseudo-random
number generator. [by KCAuXy4p CC0 1.0]
Except of course the reality isn’t quite that simple, and as many of you will know it all comes down to the level of randomness that you require. The simplest way to generate a random number in software is through a pseudo-random number generator, or PRNG. If you prefer to think in hardware terms, the most elementary PRNG is a shift register with a feedback loop from two of its cells through an XOR gate. While it provides a steady stream of bits it suffers from the fatal flaw that the stream is an endlessly repeating sequence rather than truly random. A PRNG is random enough to provide a level of chance in a computer game, but that predictability would make it entirely unsuitable to be used in cryptographic security for a financial transaction.

There is a handy way to deal with the PRNG predictability problem, and it lies in ensuring that its random number generation starts at a random point. Imagine the  shift register in the previous paragraph being initialised with a random number rather than a string of zeros. This random point is referred to as the seed, and if a PRNG algorithm can be started with a seed derived from a truly unpredictable source, then its output becomes no longer predictable.

Selecting Unpredictable Seeds

Computer systems that use a PRNG will therefore often have some form of seed() function alongside their rand() function. Sometimes this will take a number as an argument allowing the user to provide their own random number, at other times they will take a random number from some source of their own. The Sinclair 8-bit home computers for example took their seed from a count of the number of TV frames since switch-on.

The not-very-random result of a thousand analogRead() calls.
The not-very-random result of a thousand analogRead() calls.

The Arduino Uno has a random() function that returns a random number from a PRNG, and as you might expect it also has a randomSeed() function to ensure that the PRNG is seeded with something that will underpin its randomness. All well and good, you might think, but sadly the Atmel processor on which it depends has no hardware entropy source from which to derive that seed. The user is left to search for a random number of their own, and sadly as we were alerted by a Twitter conversation between @scanlime and @cybergibbons, this is the point at which matters start to go awry. The documentation for randomSeed() suggests reading the random noise on an unused pin via analogRead(), and using that figure does not return anything like the required level of entropy. A very quick test using the Arduino Graph example yields a stream of readings from a pin, and aggregating several thousand of them into a spreadsheet shows an extremely narrow distribution. Clearly a better source is called for.

Noisy Hardware or a Jittery Clock

As a slightly old-school electronic engineer, my thoughts turn straight to a piece of hardware. Source a nice and noisy germanium diode, give it a couple of op-amps to amplify and filter the noise before feeding it to that Arduino pin. Maybe you were thinking about radioactive decay and Geiger counters at that point, or even bouncing balls. Unfortunately though, even if they scratch the urge to make an interesting piece of engineering, these pieces of hardware run the risk of becoming overcomplex and perhaps a bit messy.

The significantly more random result of a thousand Arduino Entropy Library calls.
The significantly more random result of a thousand Arduino Entropy Library calls.

The best of the suggestions in the Twitter thread brings us to the Arduino Entropy Library, which uses jitter in the microcontroller clock to generate truly random numbers that can be used as seeds. Lifting code from the library’s random number example gave us a continuous stream of numbers, and taking a thousand of them for the same spreadsheet treatment shows a much more even distribution. The library performs as it should, though it should be noted that it’s not a particularly fast way to generate a random number.

So should you ever need a truly random number in your Arduino sketch rather than one that appears random enough for some purposes, you now know that you can safely disregard the documentation for a random seed and use the entropy library instead. Of course this comes at the expense of adding an extra library to the overhead of your sketch, but if space is at a premium you still have the option of some form of hardware noise generator. Meanwhile perhaps it is time for the Arduino folks to re-appraise their documentation.

The subject of entropy and generating random numbers is one that has appeared on these pages many times. [Voja Antonic] made a in-depth study using uninitialized RAM as an entropy source for microcontrollers. If you have an insatiable appetite for understanding Linux entropy, we point you at [Elliot Williams]’ comprehensive examination of the subject.

[Arduino image: DustyDingo Public domain]

The Majesty Of Saturn’s Rings Lighting Your Abode

[Modustrial Maker] is at it again with another seriously cool LED visualizer. This time around, he’s built pair of pendant lights inspired by the rings of Saturn.

The rings are made mostly of walnut plywood using a circle router jig to make the cut easier. If you are inspired to make these for yourself, [Modustrial Maker] is clear — the order in which you cut out the pieces of the rings is absolutely critical. The pieces are glued together — with any edges sanded smooth — and edgebanding applied using a hot air gun due to the curved surface before staining. Duplicate for the second (or more if you so choose!) rings. Be forewarned — a little geometry will be needed to find anchor points that will keep the rings properly balanced.

[Modustrial Maker] suggests an off-the shelf LED controller to handle the visualizations and lighting effects, but he used an Arduino Mega clone as the brains — code available here, a MonkeyJack MAX9814 electret mic, and a four-channel RF remote/transceiver to control the different modes. Pulsing along to the music, these rings make for sleek lighting indeed.

Continue reading “The Majesty Of Saturn’s Rings Lighting Your Abode”

GhettoLED boombox with LED strips lighting up speakers

This Boombox Hack Is Lit

Old boomboxes make great hacks. Their design is iconic; yes they look dated but that really just builds on the nostalgic urge to have one hanging around. Plus their big cases simply invite adding things inside in a way impossible with contemporary electronics.

[Danc0rp] hacked his JVC M70 boombox to make the speakers glow with animated light, bumping VU meters, and a pulsing horizontal bar above the tape deck. The effect is superb. The cones of the speakers act like a projection surface and the grilles hide the LEDs until they activate, and enhance the effects once unleashed. It is one of the best LED speaker hacks we’ve ever seen.

Custom board with Arduino UNO
Custom board with Arduino UNO

The light effects are provided by LED strips, which for the speakers are attached just inside the outer rim. The brains behind it all is an Arduino UNO. To connect to it, he soldered components to a blank Arduino prototyping board. That board takes input from the boombox’s line-out and does some filtering (an attempt to address some ground noise) before passing the signal on to the Arduino. That board also interfaces between the Arduino and the LED strips. The schematic is available on his GitHub page. He’d like to replace the board with a custom PCB instead and is looking for design help.

The result is not only beautiful but professional looking too. This makes us wonder why boomboxes don’t come this way. See it for yourself in the video below.

Continue reading “This Boombox Hack Is Lit”

OpenBraille Is An Impressive DIY Embosser

In 2024, the Braille system will have been around for 200 years. What better way to mark the occasion than with an open source project devoted to making embossing equipment affordable for the visually impaired? This long overdue cause became the plight of [ccampos7], who couldn’t find a DIY embosser kit and set out to build one himself.

While other embossers forcibly punch the letters in one go, OpenBraille takes a more gradual approach to ensure a clean impression with a rolling motion. Paper is placed between a mechanical encoder with moving pins and a dimpled roller that provides resistance and a place to land. The embossing head is driven by an Arduino Mega and a standard RAMPS board, as the rest of the system relies on Cartesian movement.

The encoder and roller.

The encoder mechanism itself is pretty interesting. A micro servo drives a 3D printed wheel with three distinct tracks around half of the edge. The peaks and valleys encoded in these plastic tracks actuate the embossing pins, which are made from nails embedded through the sides of hex nuts. There’s a quick demo of the encoder movement after the break, and another video of it in action on the OpenBraille Facebook page.

[ccampos7] has all the files up on Thingiverse and plans to post the software soon. You should also check out this compact embosser that was recognized in the first round of the 2017 Hackaday Prize which is a nice all-print Braille concept. Continue reading “OpenBraille Is An Impressive DIY Embosser”

SID Organ Pulls Out All The Stops

Someone left this organ out in the rain, but [Tinkartank] rescued it and has given it a new life as a SID controller. What’s a SID, you ask? That’s the sound chip Commodore used in the C64, a remarkable chip revered among retro gamers that was way ahead of its time.

He threw out everything but the keyboard assembly for the build. Each key press now drives a momentary button, and those are all wired up to an Arduino Mega through some I/O expansion boards left over from another project. The Mega drives the MOS6581 SID chip which generates those sweet chiptunes. There are four CV outs for expanding the organ’s horizons with Eurorack modules.

Our favorite part is the re-use of the stop knobs — particularly that they are actuated the same way as before. The knobs still technically control the sound, but in a new way — now they turn pots that change the arpeggio, frequency, or whatever he wants ’em to do.

The plans for the future revolve around switching to a Teensy to help out with memory issues. Although it’s a work in progress, this organ already has a ton of features. Be sure to check them out after the break.

Once you dive down the chiptunes rabbit hole, you might want to take them everywhere.  When you get to that point, here’s a portable SID player. A SIDman, if you will.

Continue reading “SID Organ Pulls Out All The Stops”

A Clear Christmas Tree Means More Lights!

For all the hustle and bustle of the holiday season, people still find ways to make time for their passions. In the lead up to Christmas, [Edwin Mol] and a few co-workers built themselves an LED Christmas tree that adds a maker’s touch to any festive decor.

Before going too far, they cut out a cardboard mock-up of the tree. This an easy step to skip, but it can save headaches later! Once happy with the prototype, they printed off the design stencils and cut the chunks of clear acrylic using power tools — you don’t need a laser cutter to produce good stuff — and drilled dozens of holes in the plastic to mount LEDs, and run wires.

A Raspberry Pi 3 and Arduino Uno make this in league with some pretty smart Christmas trees. MAX6968 5.5V constant-current LED driver chips and MOFSETs round out the control circuit. During the build, the central LED column provided a significant challenge — how often do you build a custom jig to solder LEDs? That done, it’s time for a good ol’-fashioned assembly montage! The final product can cycle through several different lighting animations in a rainbow of colours — perfect for a festive build. Continue reading “A Clear Christmas Tree Means More Lights!”

Arduino Trivia Box Is A Gift Unto Itself

There’s something about impressing strangers on the Internet that brings out the best in us. Honestly, we wouldn’t be able to run this site otherwise. A perfect example of this phenomenon is the annual Reddit Secret Santa, where users are challenged to come up with thoughtful gifts for somebody they’ve never even met before.

For his entry into this yearly demonstration of creativity, [Harrison Pace] wanted to do something that showcased his improving electronic skills while also providing something entertaining to the recipient. So he came up with a box of goodies which is unlocked by the successful completion of a built-in trivia game tailored around their interests. If this is how he treats strangers, we can’t wait to see what he does for his friends.

Hardware packed into the lid so the box itself remains empty.

There’s quite a bit of hardware hidden under the hood of this bedazzled gift box. The primary functions of the box are handled by an Arduino Nano; which runs the trivia game and provides user interaction via a 16×2 LCD, three push buttons, and a buzzer. Once the trivia game is complete, a servo is used to unlock the box and allow the recipient access to the physical gifts.

But that’s not the only trick this box has hidden inside. Once the main trivia game is complete, a ESP8266 kicks into action and advertises an access point the user can connect to. This starts the second level of challenges and gifts, which includes a code breaking challenge and gifted software licenses.

The project wasn’t all smooth sailing though. [Harrison] admits that his skills are still developing, and there were a few lessons learned during this project he is unlikely to forget in the future. Some Magic Smoke managed to escape when he connected his 5V Arduino directly to the 3.3V ESP8266, but at least it was a fairly cheap mistake and he had spares on hand to get the project completed anyway.

This project is reminiscent of reverse geocache boxes which only open when moved to a certain location, but the trivia aspect makes it perfect even for those of us who don’t want to put pants on just to receive our Internet gifts.

Continue reading “Arduino Trivia Box Is A Gift Unto Itself”