Bluetooth Controller (PXT Editor)


This is a bonus article giving Bit:Bot the ability to be controlled from a phone, tablet or laptop.

It’s not possible to program the Bluetooth module using MicroPython therefore I will use Microsoft’s PXT block editor to create a hex file. I will show you how the file was created step by step then I will show you an app that can be downloaded to control the Bit:Bot.

This tutorial will concentrate on using the Bluetooth UART service. There are other tutorials on the internet regarding this subject however most of them use other Bluetooth services and not the UART service. Once you understand how to use the UART service you can do lots of cool stuff through Bluetooth.

Hopefully this will mean that any serial app from the app store or any terminal program (Must be BLE bluetooth 4.0 compatible) can be used to communicate with Bit:Bot. This can be very useful in the robotics world allowing us to pass telemetry data wirelessly via Bluetooth and not have to worry about platform specific issues.

This code was made with Microsoft’s PXT toolset. It can be found at the below link;

Follow the link, select javascript from the top of the page and paste the below code into the javascript window;

[pastacode lang=”javascript” manual=”let%20uartData%20%3D%20%22%22%0Alet%20connected%20%3D%200%0Abluetooth.onBluetoothDisconnected(()%20%3D%3E%20%7B%0A%20%20%20%20basic.showString(%22D%22)%0A%20%20%20%20connected%20%3D%200%0A%20%20%20%20pins.digitalWritePin(DigitalPin.P0%2C%200)%0A%20%20%20%20pins.digitalWritePin(DigitalPin.P8%2C%200)%0A%20%20%20%20pins.digitalWritePin(DigitalPin.P1%2C%200)%0A%20%20%20%20pins.digitalWritePin(DigitalPin.P12%2C%200)%0A%7D)%0Abluetooth.onBluetoothConnected(()%20%3D%3E%20%7B%0A%20%20%20%20basic.showString(%22C%22)%0A%20%20%20%20connected%20%3D%201%0A%20%20%20%20while%20(connected%20%3D%3D%201)%20%7B%0A%20%20%20%20uartData%20%3D%20bluetooth.uartReadUntil(serial.delimiters(Delimiters.NewLine))%0A%20%20%20%20if%20(uartData.charAt(0)%20%3D%3D%20%22S%22)%20%7B%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P0%2C%200)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P8%2C%200)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P1%2C%200)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P12%2C%200)%0A%20%20%20%20%7D%0A%20%20%20%20if%20(uartData.charAt(0)%20%3D%3D%20%22F%22)%20%7B%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P0%2C%201)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P8%2C%200)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P1%2C%201)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P12%2C%200)%0A%20%20%20%20%7D%0A%20%20%20%20if%20(uartData.charAt(0)%20%3D%3D%20%22B%22)%20%7B%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P0%2C%201)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P8%2C%201)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P1%2C%201)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P12%2C%201)%0A%20%20%20%20%7D%0A%20%20%20%20if%20(uartData.charAt(0)%20%3D%3D%20%22L%22)%20%7B%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P0%2C%200)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P8%2C%200)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P1%2C%201)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P12%2C%200)%0A%20%20%20%20%7D%0A%20%20%20%20if%20(uartData.charAt(0)%20%3D%3D%20%22R%22)%20%7B%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P0%2C%201)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P8%2C%200)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P1%2C%200)%0A%20%20%20%20%20%20%20%20pins.digitalWritePin(DigitalPin.P12%2C%200)%0A%20%20%20%20%7D%0A%7D%0A%7D)%0Abasic.forever(()%20%3D%3E%20%7B%0A%20%20%20%20bluetooth.startUartService()%0A%7D)” message=”Javascript Remote Controlled Robot over Bluetooth” highlight=”” provider=”manual”/]

Notice how the code is written in javascript buts its layout is similar to MicroPython. Look at the code either in the javascript editor or the block editor and try to figure out what it does. Once copied to the javascript editor remember to go in to project settings and set the Bluetooth so pairing is not required.

The code will listen for Bluetooth commands when it receives a “F” it will set the pins to make Bit:Bot go forward, when it receives a “L” the pins will be set to go left and so forth.

There are two ways to achieve this, the above way or you can use the GAMEPAD library (not sure if its called this!). I chose to do it this way because I wanted to use the UART functionality and I wanted to control Bit:Bot using a generic serial terminal app. If you would like to see how to achieve the same thing using the gamepad service the following link shows you how;

Please be aware that the above example needs a paid app from Bitty Software to work.

Getting the Serial Software to work

To get the above code to work you will need to download serial Bluetooth Terminal Software. This software must be BLE (Bluetooth 4.0) compatible. If its only Bluetooth 2.0 it will not work. The software I used can be found at the below link;

You could also download BLE terminal software for your laptop and control Bit:Bot from your laptop instead. Once you have the above software installed select the device (ensure that you have Bluetooth LE selected at the top of the devices menu, no pairing required), connect to your Bit:Bot and try typing in different commands to make Bit:Bot work. The commands are;

  • F = forwards
  • B = backwards
  • L = left
  • R = right
  • S = stop

All commands are case sensitive. You could design your own app to control Bit:Bot in the same way that Bitty Software has if you wanted.

During this example we have created code that will allow you to control Bit:Bot using your BLE enabled phone, tablet or laptop. You can use these techniques to send any command data to your Micro:Bit and your Micro:Bit can send telemetry data back to you. The software i suggested above has buttons that can be pre-programmed making controlling your Bit:Bot easier. If you find better terminal programs please let me know in the comments section. Most of the apps I found was only compatible with Bluetooth 2.0 and would not work.


Code Files

The below file is the hex file of the above example.