Using the microbit to control switch access software

What is switch access software

Many disabled people use specialist software to create speech or to interact with the environment (e.g. turn on lights). Some are unable to use keyboards or mice to operate this software, so use a variety of ‘switches’, such as push buttons. These buttons act like keys on a keyboard, or pretend to be a mouse click.

Wikipedia has a page (of course) explaining what an assistive technology (AT) switch is here. Copying the one line summary at the start of that page: “A switch is an assistive technology device that replaces the need to use a computer keyboard or a mouse.”

The system we developed allows the BBC microbit to pretend to be an AT switch, so that movement sensor devices I make using the microbit can be used to control switch accessible communication software. For instance, Grid 3 by Smartbox.

The picture below shows a typical button switch and an Joybox switch to USB adapter that allows the button switch to be connected to a USB port. This allows the button to act as if a key is pressed on a keyboard to interact with switch enabled software. The standard connector for a switch is the venerable 3.5mm audio plug. The 3.5mm plug is on the end of the cable attached to the button switch. A 3.5mm socket is attached to the USB to switch converter.

My task was to enable a microbit board to connect with a 3.5mm plug and act as a switch. How hard could this be?

Button switch and interface dongle.

How does the switch work? The 3.5mm connector has 2 contacts inside of it. When the switch is operated, these are connected together internally. So, the contacts are normally open and closed when the switch is pressed. How do I recreate this switch electronically?

I used a Grove M281 Optocoupler Relay.

Grove M281 optocoupler relay board. The control pins are on the left, the relay pins are on the right.

This acts as an electronically controlled switch. When the CTR pin on the board goes high, the two connectors with the screws on top are connected. When the CTR pin is low, they are disconnected. The CTR pin can be seen on the left of the photo. There are connections for ground (GND), power (VCC) as well. The NC pin is Not Connected.

I could maybe lash up something cheaper using a transistor or two, but for around £6 I had an off the shelf solution that I got tested and running within a day. The microbit connects to the pins on the left of the board in the photo. The 3.5mm plug connects to the screw top terminals on the right of the photo.

The advantage of using optocoupler is that the microbit is isolated from the communication device that the 3.5mm plug is connected to. My slight worry was creating a ground loop. If I didn’t have any isolation between the microbit and the 3.5mm plug, if the microbit is powered from a USB source – say another computer – and then the microbit is connected to a communication device that is also connected to the mains, we may create a ground loop. The optoisolator prevents this. I don’t think this is a likely scenario with the tiny currents involved, but I am working with a vulnerable user group, so am a little more cautious than usual.

The optocoupler relay board is specced at 3.3V, but worked with the 2xAAA battery pack powered microbit at 2.4V. Nobody is more surprised than I am when something I build works!

The photo below shows all of the parts of the system. The microbit board slides into a Kitronix break out board:

microbit board slid inside a Kitronix break out board connected to a solid state relay board connected to a 3.5mm plug. The plug is unscrewed so you can see the two connectors.

The Kitronix break out board allows all the signal pins on the microbit board to be accessed. I used digital pin 16 on the microbit board to connect to the CTR pin on the M281 board, as it allowed for the neatest wiring. The photo below shows the wiring on the right. Pin 16 connects to the yellow wire. Ground is the black wire and the 3V output is connected to the red wire. Ignore the connectors and resistor on the lower left of the photo – these are used for connecting a motion detecting sensor, which I will write up on a different post.

The two screw top terminal connectors on the M281 are connected to each of the two contacts in the 3.5mm plug. The wires connected to the plug by soldering and are connected to the M281 by screwing down the terminals.

I wrote a small program to toggle pin 16 on the microbit high to simulate the action of the sensor detecting a hand motion, which is what we intend to use to act as a switch.

Pin 16 is connected to the solid state relay board using the yellow wire. The red and black wires connect to +3V and ground.

I tested my final creation using the free software at:

Unfortunately this is Windows only software, so I used a Windows 10 virtual machine on my Linux system to check that everything works as it should. Which it did. Screen shot of the software below. Hurrah.

The final code used for this project can be found on my github page in the hand_wave folder.

Smartbox Switch Driver software used to test the switch.

Checking the resonant frequency of a piezoelectric crystal

Having failed to get a tactile response from a small piezoelectric crystal, I wanted to check that I was hitting it with the correct driving frequency. This is the frequency that makes the crystal resonate. I found a test circuit and method on this website. The schematic for the testing circuit shown below is copied from that website.


Piezoelectric circuit resonance frequency testing circuit from

I breadboarded the circuit and used my Digilent Analog Discovery 2 as a network analyser to check on the resonant frequency. What does a network analyser do? This inputs sine waves with a range of frequencies across R1 and ground. R1 and R2 create a restor bridge, with the piezoelectric crystal (the ‘ceramic element’ in the diagram above) in parallel with R2. The output from the piezo crystal in series with resistor R3 measured for each of the input frequencies. The magnitude of the output and input waveforms and their phase difference is plotted by the network analyser module. This tool is often used with filters to characterise their behaviour. In this case I wanted to see if the output of the test circuit changed at what should be the resonant frequency of the crystal – quoted as 40 kHz by the manufacturer.

A photo of the testing circuit is shown below. This implements the schematic shown above. I used 100 Ohm resistors for R1 and R2 and a 120 Ohm resistor for R3. I did not implement R4 – the variable resistor as I am not looking to measure the equivalent resistance of the piezoelectric crystal at resonance. I am just verifying what the resonant frequency of the crystal is. The bread board circuit has the input on the right and the output on the left of the photo. Two crysals are soldered on to the green circuit board. I tested both and got similar results.

Piezoelectric crystal frequency response testing circuit

The input and output leads for the network analyser can be seen connected to the board. The test signal is input using the yellow lead on the right. The orange lead is oscilloscope channel 1 which measures the input signal. The blue lead in the middle is the second oscilloscope channel, which is used to measure the relative amplitude and phase of the output signal relative to the input signal. The three wires on the left of the photo are all connected to the circuit ground.

The output from the network analyser can be seen in the screen grab below. The amplitude of the output channel is the blue line in the top half of the screen and the phase relative to the input signal is shown in the bottom half.

Network analyser output, red cursor at 40 kHz, top amplitude, bottom phase.

We can see a hump, which using the horizontal cursor (the vertical red line) I can measure is pretty much at 40 kHz. Now you might say the hump looks small. It is roughly 10 dB in height, which is a factor of 3 in amplitude. The phase is all over the place. Naturally, I wanted to ‘scope the input signal to see what the network analyser was doing. As I was out of ‘scope channels on the Discovery 2, I hooked up a Tektronic TBS1104 ‘scope. I can see that the input signal is a sine wave. As the network analyser ramps up the input frequency, I can see the sine wave also increasing in frequency. Kind of like watching a spring compress.

The full testing rig can be seen below. The ‘scope on the left shows the input signal – a smooth sine wave. The board with the crystals is on the bottom edge of the photo at the left. You can never have too many wires on your desk.

Piezo crystal testing arrangement.

The Discovery 2 claims a resolution of 0.32mV. The input to the circuit from the network analsyser is 8 V peak to peak.

8/0.32×10-3 = 25,000 = 87.9 dB

So the amplitude measurements of -56 dB to -80 dB are within the Discovery 2’s stated resolution.

To be more thorough, I could use the amplifying circuit I designed in to generate an input of >100 V to the crystal, which should give me better resolution as the output signal would also be much higher. However, life is short and I’ve sunk enough time in this project. I was hitting the crystal with the correct signal. I just couldn’t make it vibrate enough to be tactile.