Sunday, June 24, 2018

Using Bitty Data Logger with a serial device connected to the micro:bit edge connector

I was asked how to connect a sensor which has a UART interface to a micro:bit and to collect and graph its data in Bitty Data Logger. To answer the question, I did some experimenting, got something working, using my PC as a source of serial data, typed in by me. Here's a video showing the experiment:


Connecting the Device

I don't have one of the sensors I was asked about so I set about connecting my PC to the edge connector of the micro:bit instead, the idea being that I would type data into a serial terminal application like Putty. To make the connection, I used an adapter board which has USB at one end (for my PC) and 4 wires at the other acting as TX, RX, GND and 3.3V. I connected those wires to micro:bit edge connector pins 1, 0, GND and 3.3V respectively.

micro:bit code

My code had to do several things;
  1. map the serial interface to pins 1 and 0 instead of the usual USB port on the micro:bit
  2. read comma delimited strings from the serial interface
  3. convert each string into a number (I performed no validation in my prototype code btw)
  4. send the data over Bluetooth in the format that Bitty Data Logger would understand. See for more information and for example code.

Here's what the code I wrote ended up looking like, first in JavaScript:

let reading = ""
function transmitSensorData()  {
    let pin_shifted = pin << 14;
let ev = pin_shifted | analog_value;
bluetooth.onBluetoothConnected(() => {
bluetooth.onBluetoothDisconnected(() => {
let pin = 0
let analog_value = 0
basic.forever(() => {
    reading = serial.readUntil(serial.delimiters(Delimiters.Comma))
    pin = 1
    analog_value = parseInt(reading)

And in fact, here's the code in full:

MakeCode Project

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.