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;

www.makecode.org

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

Javascript Remote Controlled Robot over Bluetooth
let uartData = ""
let connected = 0
bluetooth.onBluetoothDisconnected(() => {
basic.showString("D")
connected = 0
pins.digitalWritePin(DigitalPin.P0, 0)
pins.digitalWritePin(DigitalPin.P8, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P12, 0)
})
bluetooth.onBluetoothConnected(() => {
basic.showString("C")
connected = 1
while (connected == 1) {
uartData = bluetooth.uartReadUntil(serial.delimiters(Delimiters.NewLine))
if (uartData.charAt(0) == "S") {
pins.digitalWritePin(DigitalPin.P0, 0)
pins.digitalWritePin(DigitalPin.P8, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P12, 0)
}
if (uartData.charAt(0) == "F") {
pins.digitalWritePin(DigitalPin.P0, 1)
pins.digitalWritePin(DigitalPin.P8, 0)
pins.digitalWritePin(DigitalPin.P1, 1)
pins.digitalWritePin(DigitalPin.P12, 0)
}
if (uartData.charAt(0) == "B") {
pins.digitalWritePin(DigitalPin.P0, 1)
pins.digitalWritePin(DigitalPin.P8, 1)
pins.digitalWritePin(DigitalPin.P1, 1)
pins.digitalWritePin(DigitalPin.P12, 1)
}
if (uartData.charAt(0) == "L") {
pins.digitalWritePin(DigitalPin.P0, 0)
pins.digitalWritePin(DigitalPin.P8, 0)
pins.digitalWritePin(DigitalPin.P1, 1)
pins.digitalWritePin(DigitalPin.P12, 0)
}
if (uartData.charAt(0) == "R") {
pins.digitalWritePin(DigitalPin.P0, 1)
pins.digitalWritePin(DigitalPin.P8, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P12, 0)
}
}
})
basic.forever(() => {
bluetooth.startUartService()
})

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;

https://ukbaz.github.io/howto/bitbotRemoteControl.html

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;

https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal&hl=en_GB

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.

BluetoothCar