10 months ago

Magnetic Bounce: a magnet interface for Raspberry Pi

The magnetic bouncer is a unique computer interface based on the repulsive force of magnets. Magnetic attraction is all very well, but is a bit limited. Bringing two like magnetic poles together so that they repel, however, is fascinating: you can feel the magnetic field as you move them closer together and around one another. A simple way to demonstrate this effect is with ring magnets on a pole: they will sit still with the upper magnet suspended in mid-air, but disturb them and they bounce around in a most satisfying manner. We wanted to be able to capture this bouncing action and feed it into our computer to act as a controller.

This tutorial was written by Mike Cook, author of Raspberry Pi Projects for Dummies, and first appeared in The MagPi #69. Download your free copy of The MagPi magazine #69.

Magnetic Bounce: Design a magnet interface for Raspberry Pi

The problem was how to convert this movement into an electrical signal. First off, we tried using a coil between the two magnets, because the bouncing caused a variation in the overall field, and magnetic field variation close to a coil generates an electrical signal. This worked quite well, but only when the magnets were moving, and we felt a direct measure of the magnetic field would be better. Therefore, we used a miniature Hall effect sensor between the two magnets. This meant we had to use an amplifier to remove any offset and increase the voltage from the sensor.

Also, for added flexibility, we made two such systems and mounted them on top of a box. Finally, we thought it would be fun to have this unique controller drive some LEDs in a sort of ‘executive toy’, so we mounted some LEDs on a small box to sit between the two magnetic bouncers.

The hardware using in Magnetic Bounce

The schematic of the bouncer electronics is shown in Figure 1. This shows the two SS39ET linear Hall effect sensors in a dotted circle to indicate they’re on a separate board between the two magnets. The rest of the circuit is built on a small piece of stripboard and is located inside the mounting box. This circuit connects to two analogue-to-digital converter channels connected to the Raspberry Pi’s GPIO pins. If you don’t have one already then you can simply use the one described in the MIDI Drum Sequencer project in The MagPi #68.


Figure 1 Schematic of the magnetic bounce controller

The LED part of the project is shown in Figure 2. It consists of a 16-LED ring and a 14-LED strip of the WS2812B type. You need to amplify the 3V3 logic signal out of the Raspberry Pi to be 5 V, to drive this. Figure 2 shows two ways to do so. The lower circuit uses the 74LS14 inverter IC and was used in our Infinity Mirror project in The MagPi #43. The circuit above is a simple FET drive circuit, although you do need a FET that will switch off 3V3. Note that due to the number of LEDs, you’ll need an external power supply: if all these LEDs were on fully then it would take 1.8 amps, which is too much to take from the Pi’s 5 V line.

Construction details for both circuits are in the step-by-step guide, with extra photos on the GitHub page.


Figure 2 Schematic of the LED display with two possible methods

Magnetic Bounce: The software

The first thing we need to do is to look at signals from the sensors and to adjust the preset pots to get the right offset. The code in the bounce_test.py listing will do that and display the results like an oscilloscope. It is written in the Pygame framework and simply reads the two sensors’ values and plots the result as a graph. The result is shown in Figure 3 – note how the top trace shows a magnet configuration of two opposed floating magnets and you can see that it basically consists of two frequencies. The bottom trace has three magnets all stuck together bouncing; due to the greater magnetic field and mass of this configuration, the trace does not decay as quickly as the top trace. The two amplifiers’ offset trim pots should be adjusted so that the trace just begins to rise from its lowest point.


Figure 3 Top trace, left controller; bottom trace, right controller

Applications for Magnetic Bounce

First, we made a drawing application, a sort of Etch A Sketch arrangement. The program can use the two controllers as conventional Cartesian controls – x and y – or as polar co-ordinates: angle and radius. This code can be found on our GitHub page.

Instead of printing this, we thought of using the bounce controller to drive some LEDs in a sort of executive toy style. The idea was to use an LED ring combined with an LED strip placed vertically in the middle of the ring, a sort of totem pole.

One of the controllers determines the colour of the LED and the other controls the position. So a single lit LED rotates around the ring at a speed determined by one controller’s movement and each time it passes in front of the totem strip, it adds the current colour of LED to the bottom of the strip and shifts all the others up one. When the totem strip is full, there is a display animation. If at any time you stop bouncing the controllers, the totem slowly loses lit LEDs until all LEDs are off.

The code to do this is shown in the neopixel_bounce.py listing. You need to have installed the software for Pimoroni’s Unicorn HAT to run it. You also need to be in supervisor mode, so use gksudo idle3 or equivalent.

Magnetic Bounce: Kit You’ll need

  • 2 × Linear Hall effect sensors
  • 8 × Ring magnets
  • 1 × MPC602 amplifier
  • 2 × 10 kΩ trim pots
  • 1 × 8-pin DIL IC socket
  • 1 × A/D converter, minimum of two channels
  • 12 mm diameter dowel
  • Various resistors, stripboard, and wood

Optional LED display:

  • 16 LED WS2812B ring
  • 14 LED WS2812B strip
  • Empty toothpaste pump dispenser
  • Sugru mouldable glue
  • Plastic box

Magnetic Bouncer: Taking it further

There are many variations you can make to Neopixel_Bounce.py. Maybe the simplest is to change the display animation, or alter the drain animation to run into the ring. You could also build a Pi Zero into the box to make it self-contained. However, the application we are itching to try is to turn this controller into a weird type of theremin, which we shall show you next issue.

Step-01: Making the sensors

Make two sensor boards, for the SS39ET Hall effect sensor and capacitor. Cut two washers from 1 mm thick foam to prevent the magnets breaking if they bang into each other. Mount this above the sensor board on the dowels.


Step-02: Prepare the amplifier stripboard

Take a piece of 14×15-hole stripboard and cut the tracks as marked. Also drill a 3 mm hole in the top-left corner for mounting to the underside of the box.


Step-03: Building the amplifier board

Wire up the circuit as shown in the diagram (right). We used a 5-way DIN socket for the connection between the bounce controller and the A/D converter mounted on the GPIO pins.


Step-04: Build the base

We used 12 mm MDF board to make a box, 300 mm by 120 mm by 40 mm high. Drill two 12 mm holes 190 mm apart and cut the two dowel rods at 165 mm long, insert, and glue. Smooth the dowel with the finest sandpaper you can get and then apply a little beeswax polish to make it glide against the magnets.


Step-05: Building the LED box

We made a ridged strip of LEDs by soldering up 14 of the type that come on their own small PCB with tinned copper wire. We drilled several 2 mm holes in the box to allow the Sugru to get a good grip when mounting the strip vertically in the centre of the circle.


Step-06: Building the LED box

A Colgate toothpaste dispenser was disassembled so we just got the tube and top. The base exactly fits the LED ring. Slip some greaseproof paper or polyester sheet on the inside of the dispenser to act as a light diffuser. Glue this onto the LED ring with a PVA-type glue that dries transparent. Fix the LED box to the main box with self-adhesive Velcro strips.