Acoustic Bean Machine

A ‘Bean Machine’, also known as Galton Board, is a simple but appealing way to watch statistics at work. On their way from top to bottom, all (red) ‘beans’ will hit a fixed number N of (green) obstacles before ending up in one of the N+1 collecting bins. A bean’s path is the result of N random choices between left and right at every collision. In a perfect machine, both directions have an equal 50% chance of being chosen by the bean when it hits an obstacle.


When the number of processed beans increases, the pattern of the collected beans will approximate a binomial distribution curve.


I wrote a small Arduino sketch for simulating a Bean Machine on a 480×320 TFT display. Then I decided to add some ‘audio’ by sounding a short beep at every collision. Its frequency depends on the current distance from the center of the machine (the average of the distribution), so different bean paths will have different ‘melodies’.

Here’s a short video impression:


As you can see, N=9 in my sketch. The process stops when one of the 10 bins is completely filled with beans:



Here’s the sketch. All code for drawing the machine is in setup(). Two functions take care of simulating a dropping bean (drop_vert and drop_diag). Every loop() cycle processes one bean.