Page 1 of 1

UART on Micro:Bit

Posted: Sun Apr 30, 2017 2:38 pm
by mkingdom
I have just started with my Micro:Bit having used Arduino previously.

I am trying to connect an Xbee wireless module to the Micro:bit (I've used them successfully before with Arduino). I'm using the instructions in the BBC micro:bit MicroPython Documentation, Release 0.0.1. Section 2.1.8 UART. Pin0 = Tx and Pin1 = RX as per instructions.

The wireless module is configured and tested working to pass data straight through to another Xbee attached to my computer.

When I execute code, the Xbee 'data in' led is not flickering. It appears the Microbit is not communicating through Pin0.

I've tested the connections and Pin0 and 1 are working with write.digital.

My code follows:

from microbit import *
buffer = 'Hello World'
uart.init()
while True:
uart.write(buffer)
sleep(2000)

Any thoughts?

Re: UART on Micro:Bit

Posted: Sun Apr 30, 2017 6:13 pm
by BruinBear
Possible causes of no data at all:

1. Tx -> Rx
Rx -> Tx
not connected correctly

2. Baud rates/stop bits don't match at both ends.

Re: UART on Micro:Bit

Posted: Sun Apr 30, 2017 6:49 pm
by dhylands
And also be sure that ground is connected between the 2 ends.

Re: UART on Micro:Bit

Posted: Sun Apr 30, 2017 8:31 pm
by mkingdom
Thanks both. Another question. Do I need to disconnect the usb to computer cable from the Micro:bit for the UART to work?

Re: UART on Micro:Bit

Posted: Mon May 01, 2017 9:39 am
by BruinBear
If you initialise the uart without parameters, I think that the USB-UART TX/RX pins are used - which connect to the USB serial convertor on the micro:bit.

If you initialise the uart and specify which pins to use, like this

Code: Select all

uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin0, rx=pin1)
then I think the USB serial is disconnected, so you could leave the cable connected (e.g. to power the micro:bit). To get the USB serial back again you have to issue a uart.init(115200) without tx,rx parameters.

Re: UART on Micro:Bit

Posted: Mon May 01, 2017 10:47 am
by mkingdom
Thanks. This works, with one small problem. I am only receiving 'buffer' contents minus the last two characters. In this case I get 'Hello Wor'
Can't understand why I'm losing the last two characters.

The good news is that the microbit reconnects the usb!


Code follows:

from microbit import *
buffer = "Hello World"
uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin0, rx=pin1)
uart.write(buffer)
uart.init(115200)

Re: UART on Micro:Bit

Posted: Mon May 01, 2017 12:12 pm
by BruinBear
I don't know. Have you tried putting a sleep(x) command in before the last uart.init(), where x is a delay in mSecs (e.g. 10)?