Hack GraviTrax with Raspberry Pi

By Mike Cook. Posted

GraviTrax is a construction system to make your own marble runs of any complexity. From the famous toy maker Ravensburger, it is aimed at the STEM market as well as kids of all ages. It has lots of extension packs for more basic parts and new fun features. There is only one thing missing, until now: an interface to Raspberry Pi.
We have always wanted to do a marble run project, so when we saw the GraviTrax system we were excited because it had all the ingredients we needed. Also, it is built up on a grid of hexagon spacing, and we are suckers for hexagons. The standardised parts, and wide availability, meant we could make our own parts, or modify existing parts, to feed back events into the Raspberry Pi and have them trigger sounds or animations. We can also trigger lights to make our run more exciting. In this first part, we will concentrate on detecting the balls.

When you get your starter set, you have to prepare the cardboard base by pushing out hexagons to leave holes to mount the tiles in. Do not discard these hexagons, because we are going to use them in our project. If you have already discarded them, then you’ll need to cut out a hexagon from either cardboard of the same thickness or 3 mm plywood. We made two types of sensor: a standalone one, and ones where you have to modify an existing part. Just like the GraviTrax system, our approach is modular and you can make as many of each sensor type as you like.

How does this Gravitrax hack work?

All the sensors in this part use light to detect the presence of a ball – through breaking a beam, detecting reflected light, or simply the presence or absence of ambient light. As such, the circuits are all very similar as shown in Figure 1; the only difference is the physical sensor used, and the current limiting resistor value on the LED. They are connected to the Raspberry Pi by a length of three-wires-wide ribbon cable, with a pull-up resistor going on the board that connects to the GPIO pins. In later parts of this tutorial series, we will look at making a distribution board for them all.

Figure 1

Optical slot sensor

The first sensor we will look at is an optical slot sensor. This fits under a track and the height can be adjusted by using the small or large height tiles. Basically, this is a cardboard hexagon with a small piece of 3 mm plywood stuck on it and the electronics glued to that, going from point to opposite point on the hexagon. The physical construction of the electronics board is shown in Figure 2, with the whole assembly shown in Figure 3. It is best to glue the electronics in place with a track running through it, between two pieces, so you can get it aligned precisely.

Fig 2 Figure 3

Reflective detector

The previous sensor needs to be placed under a track, whereas this one can be placed in an adjacent space and it looks downwards. It is capable of detecting balls on the entrance or exit to any of the tiles, be it basic, curved, or launch pad. The sensor is easy to make, as the LED and transistor symbols are drawn on the part. We used a stack of five cardboard hexagons glued together, followed by a plywood hexagon with a hole in the centre and a 10 mm, M3 countersunk screw sticking through it. This was then placed on the arm, and could be rotated to the required point.

Making the reflective detector

Figure 4 shows the measurements of the plywood arm, and the mounting bracket made from a piece of 12×12 mm angle aluminium. The arm should be notched with the corner of a square file; this is so a cable tie can grip the ribbon cable without slipping. The height of the sensor is adjustable due to a long slot in it, allowing it to be slid up and down. You need a gap of about 2 mm between the sensor and the top of a ball to detect it reliably (Figure 5). You can mount two arms on a stack to get coverage of another position.

Figure 4 Figure 5

A track monitor

The last two sensors have been standalone; the next one modifies a GraviTrax part. The physical layout is shown in Figure 6. The TCRT1010 sensor comes with bent leads which neatly wrap round two holes of stripboard. On the strip side, you need to bend the connectors over again and cut them short. Then bend the middle two so they sit on one track and solder them up. You also need a surface-mount 51Ω resistor to make it small, although a 025 W through-hole resistor could be used. The assembly needs hot-melt gluing onto the track (Figure 7).

Figure 7

Launch pad monitor

Take an OPB706B sensor and wrap it round the centre post in the launch pad tile (Figure 8). Push it down and use a pencil to mark the outline on the wall. Then, using a Dremel, and 1 mm router bit, cut to about 1 mm short of the outline you drew, so the part does not go through the hole. Also, cut a slot in the opposite side to let the wires through when attached to a height tile. Paint black the area the sensor is pointing at, and glue a 10×2 mm piece of 1 mm thick styrene to the green plunger with polystyrene glue (Figure 9).

Figure 8 Figure 9

Switch monitor

This uses ambient light to detect which way the switch is set. Draw in pencil around the switching lever in both positions, showing the area being covered and uncovered. Then drill a 2 mm hole in the middle of this area (Figure 10). Next, paint the underside of the switch tile black, and make sure you paint on the inside of the hole (Figure 11). The schematic of this sensor is somewhat different from the others and is shown in Figure 12. Finally, Figure 13 shows the physical layout of the parts. Position the board so you can see the white sensor through the hole and fix with Sugru.

Figure 10 Figure 11 Figure 12 Figure 13

Software to control the hacked GraviTrax

We have written software, shown in the sound_trigger.py listing, that monitors these sensors, and triggers sounds, either immediately or after a delay.

Click here to visit the Pi Bakery GitHub page and download the code.

The software is modular: line 82 determines what GPIO pins you will use and it automatically generates a window size to accommodate the number of pins in this list. Note if you want to use GPIO 14, you should disable SPI before booting with the sensor attached. The sounds’ names are in a list at line 88; by simply changing or adding to this list, different sounds can be used. The GPIO pins are scanned and when a trigger condition is met, the event is put in a pending list to be actioned at the correct time.

Using the software

The user interface is shown in Figure 14. For each line, you can set what the trigger action will be. These states are: disabled, when the signal goes high, when it goes low, or when it goes either high or low. They are changed by clicking on the trigger icon. The delay column, as you might expect, determines a delay between the trigger and the sound, whereas the sound sample played can be changed by the icons on the right. You can change the GPIO pin and note that one pin can trigger different actions; so, for example, you could have the switch tile generating a different sound depending if it is changed to the left or right.

Figure 14

Choosing sounds

We found short sounds were generally best, but longer sounds can be useful at the beginning or end of your run. We copied a lot of sounds from the Scratch media library from the path /usr/share/scratch/Media/Sounds into our sounds directory. Make sure that are all .wav files, because that suffix gets added automatically to the file names. Note that the slot sensor will read as a logic zero with no ball, whereas a reflective sensor will read high in the absence of a ball. There are lots of suitable sounds available online as well.

We have looked at adding optical sensors to detect where a ball is, be it on a track or a tile. Next month we will look at how to add different sorts of LED displays to enhance your GraviTrax layout. In the meantime, if GraviTrax is new to you, then have a play with the different layouts in the accompanying booklet.

Tip! Track monitor

When gluing the PCB to the track, glue the long edge first, then push the sensor slightly so it is not parallel to the track, and test that it detects the ball before gluing the sides to hold it into position.

GraviTrax quick tip


From The MagPi store


Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.