0%
0%

# Ball-in-box simulator with REAL gravity

Turning the classic analog physics simulation into an interactive experience with an accelerometer and a handheld CRT display

Similar projects worth following
1.9k views
This is an extension of the classic analog ball-in-box simulator circuit. The traditional version of the circuit makes a bouncing ball appear on an oscilloscope in x-y mode. The bouncing motion looks realistic, thanks to the analog circuit modeling the underlying physics, including gravity, air drag, and collisions with the edges of the display.

Here, we add the ability to pick up the "box" (the display), turn it over, shake it, etc. and watch the ball react in real-time. We achieve this effect by replacing the fixed gravity term in the original simulator with live readouts from an analog accelerometer chip mounted to the display. The display is a small handheld CRT monitor, made from a vintage camcorder viewfinder.

# What is an analog computer

Unlike a digital computer that represents numbers as sequences of 0s and 1s, an analog computer, for the purpose of this discussion, represents numbers as real-valued voltages. For example, the number 2.3 is simply 2.3 Volts at some point in the circuit.

Using op amps, we can build analog circuit elements that perform addition, multiplication, etc. on these voltages. We can also do more sophisticated things, like compute integrals over time, using an op amp integrator:

Imagine a physics simulation where we set the integrator input voltage Vin(t) to the speed x’(t) of a moving object. The object’s position x(t), which is x’(t) integrated over time, is then simply the op amp’s output voltage (scaled by -1/RC). In real-time!

Of course, modern digital computers outperform this ancient method in pretty much every way. But there is something beautiful and satisfying about solving problems with these minimalistic analog circuits.

# The classic ball-in-box simulation

Computing the trajectory of a bouncing ball, subject to gravity and air drag, is a classic demonstration of what analog computers are good at. The German company Telefunken used it as a tutorial for their analog computers in the 1960s.  Below is a screenshot of their “Ball im Kasten” (German for “ball in a box”) manual. The original document is in German, but here is a walkthrough in English.

A similar analog computer was used in the famous Tennis for Two demonstration from 1958. Like Telefunken’s “Ball im Kasten”, Tennis for Two was originally meant to show off the capabilities of analog computers. Remarkably, it is now considered one of the first video games ever created.

Today, the analog ball-in-box simulator still makes for a fun and rewarding electronics project. Here are a few implementations that are worth checking out [analogmuseum.org]  [glensstuff.com] [hackaday2009], and here is a nice video of Eric Archer's build from 2009:

# The goal of this project

The goal of this project is to extend the classic ball-in-box simulator so that the user can pick up the physical “box” (the display), shake it, turn it over, etc., and watch the ball inside react in a physically plausible way.

To achieve this effect, we replace the fixed gravity term in the classic ball simulator with the readout from an accelerometer that is rigidly mounted to the display. The display needs to be small, of course, so that it can be easily picked up and moved around in space. A heavy oscilloscope will not work.

A small cathode ray tube (CRT) is a good fit for this project, since we can drive the beam deflection directly with the position of the ball (or rather, the position of the dot that draws the ball), using minimal circuitry. I decided to repurpose a vintage video camera viewfinder, a Sony CRT DXF-801, because it is small, there are many available on eBay, and I found a free schematic online.

The accelerometer is an ADXL335. It outputs three analog voltages proportional to its acceleration in three dimensions, which is the perfect interface for our analog computer.

# Why this should work

To make the “pick up the box” metaphor work, we replace the constant gravity term in the ball simulator with live measurements from an accelerometer attached to the display, without any additional math. It may seem obvious at first that this works, but it is more nuanced when you think about it, since an accelerometer cannot distinguish earth gravity from acceleration caused by velocity changes of the display / box.

Proper acceleration is the technical term for what an accelerometer measures. The science here gets complex very quickly, at least for me. To convince myself that this hack should, in fact, work, I found it easiest to think about how an accelerometer works...

### bib_schematic_mainboard.pdf

Adobe Portable Document Format - 264.94 kB - 07/15/2023 at 17:09

### bib_schematic_accelboard.pdf

Adobe Portable Document Format - 52.46 kB - 07/15/2023 at 17:08

• ### 7/15 | boards from pcbway, improved sound effects!

wlf64707/15/2023 at 17:51 0 comments

# Moving the circuit to PCB

I decided that this project is worth keeping around on a PCB, so I designed two boards in KiCad. I tried out PCBWay’s KiCad plugin for the first time, and it worked like a charm. It takes literally one click to generate all the fabrication files and upload them to PCBWay. Pretty cool!

I had made a few careless mistakes during layout (a handful of vias were tented, even though I wanted all of them to be open), which PCBWay thankfully caught and fixed for me. The boards still arrived within less than a week, and they are flawless:

For the main board, I went with what I would call an “educational” layout :). I imagined a circuit board you might see in electronics class at school. It’s easy to identify the subcircuits and there are lots of labeled test points to hook up your scope and look at waveforms. All components are through-hole because I already had all the parts that way. It is also era-appropriate for this circuit :) The accelerometer is an SMT part, but I’m using the SparkFun module which breaks out the pins to more convenient through-hole pads.

Here is the fully assembled main board:

And here is the fully assembled accelerometer board, mounted to the display:

The full system on PCB:

# Improved sound effects

I couldn’t resist making a few tweaks to the circuit before hardening it on a PCB. The most significant one is better audio. The original approach of playing a 1kHz tone for the duration of the bounce was effective for how simple it was, but I somehow couldn’t dial it in to where lighter bounces really sounded lighter.

So I added a voltage controlled amplifier that varies the volume with the depth of the bounce. You can see the VCA in the bottom right of the main PCB. It’s a basic long tail pair with 3 NPN transistors, the schematic is shown below. The full updated schematics are uploaded here on hackaday.

I also decided to move the speaker from the main board to the display, so that the bounce sound effect comes from the right direction. This is shown in the picture below. I had to play with the positioning of the speaker a bit, since the strong permanent magnet can easily distort the CRT picture.

# Fixed display flicker

I noticed a slight flickering in the display so I took a second look at the brightness signal coming at G1 of the CRT. It turns out the brightness voltage was not actually constant as I had hoped, but was periodically pulled down to completely black/off during what looks like horizontal and vertical blank (retrace) periods.

In retrospect, it makes sense, looking at the rest of the viewfinder schematic. I’m not quite sure why the ~60Hz vertical flicker didn’t show up more clearly in the videos I made earlier? Either way, to fix the issue I disconnected G1 from the video circuit and connected it to 12V DC (display power rail) through a 1M resistor.

# Miscellaneous tuning

• Double checked the resistors that are parallel to the deflection coil to dampen ringing and decided to lower their value from 120 Ohms to 68 Ohms.
• Repurposed unused op amps in as additional virtual GND buffers

# Updated demo video

Finally, I recorded another short video that shows the PCBified system. Enjoy!

• ### 6/5 | Finished system demo, analysis

wlf64706/06/2023 at 05:14 0 comments

It is done! In today's update I will discuss the finished product (finished meaning for the hackaday op amp challenge, ideas for future work are listed below). I have a demo video of the system working and some deeper insights into how component values were picked and how the bounce logic works. I also finally wrote the high-level “Why this works” part in this project’s Details section.

# Attaching the accelerometer to the display

As the final build step, I have replicated the accelerometer circuit on a protoboard and mounted it to the handheld CRT display. I 3D printed a rectangular frame to hold the protoboard and stuck it to the side of the display housing with double-sided tape.

The accelerometer is deliberately positioned close to the front of the display, where the screen is. That way, we always measure linear acceleration on the side of the housing where the simulated “box” is. The placement of the accelerometer wouldn’t matter if we only allowed translational movements or very slow rotational movements of the display. But it is unavoidable in practice, and quite fun actually, to “wiggle” the display (e.g. quickly tilt it up and down) to move the ball around. During these fast rotational movements, measuring linear acceleration at the back of the case would yield the opposite of what we expect to see on the front side where the ball is displayed.

# The finished system

Random side note: I’m kind of proud of this technique I came up with for making a scrappy cable assembly with .1” header connectors and strain relief in a hurry: just cut a paddle-shaped support plate out of 2mm ABS and superglue the header and outer cable jacket to it. Works great :)

# Demo video

Without further ado, here’s a video of the system in action. Make sure you have sound on, I find that the sound effect nicely adds to the illusion. I’m super happy how well this circuit performs!

# Double-checking our simulation parameters

The motion equations for the ball are independent for each dimension (x and y) in our 2D simulation. Each dimension is modeled with two op amp integrators and four passive components, as shown in the schematic snippet below. The circuits for x and y are identical.

Note that this is for ball motion without wall collisions. We look at the bounce logic in the next section below.

I chose the values for R1, R2, C1, C2 by trial and error, such that the simulation “looked right”. The shown values R1=100k, C1=C2=.22u worked well, and I found it useful to leave R1 variable, to control how strongly the ball is affected by inputs from the accelerometer. Around 120k felt pretty good for R1.

But since we are measuring actual earth gravity and acceleration of the box, there should be a “correct” choice of parameters. Let’s do the math and see if the above values make physical sense.

Let’s use a(t), x(t) to describe acceleration and position in the real world, in meters, and v_a(t), v_x(t) to describe the corresponding voltages in our simulator. Here is the equation that relates acceleration to position in the real world

How do v_x (t) and v_a(t) relate to x(t) and a(t)?

1. v_x to x: the display is 2x5.1V “wide” in our simulator and 28mm wide in the real world, so x / v_x = 1m / 364V, or x = (v_x / 364V) * m
2. v_a to a: our accelerometer + signal conditioning produces about 1V per g, so we have a / v_a = (9.81m / s^2) / 1V, or a = (v_a / 1V) * 9.81m / s^2

Substituting these relationships into the real world formula we get

Dividing by the 1/V and m units, and combining constants, we get

Now we can compare coefficients to the integral equation of our simulator circuit, and find that 1/R1C1R2C3 must be 3574/s^2. The s^2 units checks out, because RC is in seconds, so RCRC is in s^2.

In our case, with C1=C2=.22u and R2=100k,...

• ### 6/2 | Schematics, full system test

wlf64706/03/2023 at 05:20 0 comments

It’s alive! In today’s update I’ll be describing the complete circuit and post a short video of everything working together for the first time.

First things first, to make the CRT display easier to work with (see last update), I created this wire harness for it. It has 6 conductors, 2 for power and 2 each for horizontal and vertical deflection.

## The circuit

The circuit is built on two separate breadboards, as shown in the photo below. The large main board has everything on it except for the accelerometer, which is on the small board. The small board will eventually be mounted on the display so it can measure the acceleration of the display (i.e. the “box”) as it is moved around in space. I’ll probably rebuild / solder the small board on a perfboard for that.

I have uploaded the full schematics to the schematics section of this hackaday project. I’ll briefly go over the different components now one by one, referencing the colored boxes in the breadboard photo. Each box has a corresponding section in the schematics, which has all the details.

### Split supply and reference voltages (top left, red box)

The circuit uses a split supply with +7.5V, -7.5V, and a 0V ground reference. I don’t have a bench split supply so I’m feeding in 15V from my bench supply and splitting with a resistor divider to create GND halfway between the rails. This ground reference is buffered by an op amp and is only used for small loads, like deriving the VRef+ and VRef- reference voltages with 5.1V Zener diodes.

### Accelerometer board (small board, blue box)

Nothing too much going on here, I use a Zener diode to set up the 3.3V power for the ADXL335 chip. The chip outputs 3 analog voltages proportional to the chip’s acceleration in 3 dimensions. Sensitivity is around 300mV/g, with 0g about halfway between the chip’s positive and negative rail. I use two op amps to first buffer the output of two dimensions, then amplify it x3 and add an offset so that 0g is at GND.

A few more details on the accelerometer board. I played with the lowpass filter capacitors but ended up using the default values from the breakout (100nF), which amounts to 50Hz bandwidth. I also tried another accelerometer chip, the ADXL326. It is the same as the ADXL335, but less sensitive. It detects up to +/-16g, instead of +/-3g. I first thought that shaking the display might create more than 3g acceleration, but that wasn’t really the case. So I stuck with the 335 and its +/-3g range.

### X and Y physics logic (2 orange boxes)

The entire physics simulation happens in the two orange boxes. The physics behind the ball’s motion in X and Y direction are independent in this simulation, so there are two separate circuits/orange boxes, one for X and one for Y.

In each box, the bottom 8DIP chip makes up two op amp integrators: a first one that integrates the ball’s acceleration over time to produce velocity, and a second one that integrates that velocity over time to get the ball’s position.

At the top of each orange box is the “bounce circuit” (2 op amps each). This circuit detects collisions with the top/bottom or left/right edges of the screen and provides the force that accelerates the ball back in the opposite direction, much like a spring. For example, when the ball’s X position goes over the right side of the screen by a few mV (full screen width is set by VRef- and VRef+, or -5.1V to 5.1V), the bounce circuit will produce a large negative voltage, 100x the penetration depth (set by R11 and R15 in the schematics), which is fed to the acceleration input for X and makes the ball bounce back to the left.

I hope this short description makes some sense. I want to quickly cover the overall circuit today and plan to dive deeper into the choice and physical interpretation of components and values in the next update.

### Bounce sound trigger logic (2 green boxes)

Yes, there is a bouncing sound effect :) I’ve seen...

• ### 5/29 | Initial log entry, CRT bring-up

wlf64705/29/2023 at 22:26 0 comments

This is my first log entry. At this point I’ve spent a couple evenings testing the feasibility of the idea with a 1D version of the circuit on a breadboard. It doesn’t perform too well yet, but it does render a ball on my tiny CRT, and the ball can bounce off the floor and ceiling. Over the next few days I plan to optimize and clean up the circuit, so I can draw the schematics and share them here.

For this update I’ll focus on the part that is more “done” already, which is the display. The display is a modified Sony DXF-801 viewfinder. You can find them for \$20 on eBay. To fit our purposes, I cut most of eyepiece off with a hacksaw. I also cut off the clamp in the back.

Using a service manual I found online, I looked up the power pins on the 20-pin connector (it needs 12V, 2.1W). From the schematic it appears fine to leave all other pins disconnected. Indeed, it powered up fine, and showed static on the screen. Yay!

To connect the CRT to our simulator circuit, we need to drive the horizontal and vertical deflection coils. The picture below shows the coils disconnected from the main board, highlighted in green. We’ll leave the other CRT connections alone (highlighted orange and red), those are for biasing the tube with high voltage:

The orange connector also carries the heater and grid 1 (H1 and G1) voltages, which determine the brightness of the dot on the screen. I will leave that part of the circuit unmodified for now, and just use the external “bright” control knob, since it lets me turn down the brightness to basically darkness. Brightness control is important, because in normal viewfinder operation, the energy in the electron beam lights up the entire picture. If we instead make the CRT draw just one dot, the brightness of a full image is now concentrated to one point. We need to set the brightness very low, or we could damage the CRT.

Let's look at the deflection coils next. Our analog computer produces X and Y voltages that normally connect to an oscilloscope in x-y mode. For example, 0 Volts for X means horizontal center of the screen, more positive is more right, more negative is more left, etc.

With magnetic deflection coils, the beam angle is proportional to the current, not voltage. And since deflection coils are inductors, the current through them is i(t) = 1/L * \int v(t) dt. So we can’t just apply our X and Y voltages to the coil and expect the current to be proportional, like in a resistor. Instead, we need to produce a drive voltage that causes the coil current to follow the path we want. This is the perfect job for an op amp in the configuration below:

The scope traces here show the desired deflection waveform Vin (yellow), the resulting drive current waveform Vsense (cyan), and the drive voltage Vdrive (magenta) produced by the op amp. The yellow and cyan waveforms are identical because the negative feedback loop ensures that Vsense = Vin, and therefore, Idrive is proportional to Vin.

NOTE: the circuit above is simplified to illustrate the feedback mechanism. The real circuit doesn’t drive the coil directly from the op amp; there is a push-pull transistor stage in between. A fair amount of current is needed to deflect the beam all the way to the edges: about 50mA vertically and about 200mA horizontally. I’ll post the full schematics once I have finalized them.

Finally, we have a ball on the screen! To waveform used to draw the ball in the picture below is a 1.3 kHz sine wave, with 90 degrees phase shift between X and Y.

Ok that’s it for today's update. Here’s what I’m planning to do next.

• During testing I destroyed a 2N3904 transistor driving the CRT deflection coils. I ordered beefier transistors, they came today.
• The ball on the display is not perfectly round near the corners of the screen. It’s not the most important thing to fix right now, but it bugs me, so I will try.
• Finish the rest of the circuit, e.g. add missing bounce logic in 2nd dimension.
• Dial in...

Share

## Discussions

Kuba Sunderland-Ober wrote 05/30/2023 at 14:08 point

Repurposing the viewfinder CRT is just icing on the cake. Real gravity is a wonderful twist!

Are you sure? yes | no

wlf647 wrote 06/06/2023 at 05:49 point

That's very kind. Thank you so much!

Are you sure? yes | no

Kuba Sunderland-Ober wrote 06/06/2023 at 17:39 point

I love the idea and the execution. Keep those analog gizmos alive and well :)

Are you sure? yes | no

## Similar Projects

Project Owner Contributor

### Neopixel Facemask With Embedded Media & Phone

Morning.Star

Project Owner Contributor

### Magic Eye Vacuum Tube Display Bluetooth Speaker

Jay Riedl

Project Owner Contributor

### 25-Hour Digital Myst Clock/Chronometer

RIUM+ (Mike Ando)

Project Owner Contributor

sky-guided

# Does this project spark your interest?

Become a member to follow this project and never miss any updates