UART on Micro:Bit

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
mkingdom
Posts: 9
Joined: Sun Apr 30, 2017 2:25 pm

UART on Micro:Bit

Post by mkingdom » Sun Apr 30, 2017 2:38 pm

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?

BruinBear
Posts: 27
Joined: Sat Jan 14, 2017 11:59 am

Re: UART on Micro:Bit

Post by BruinBear » Sun Apr 30, 2017 6:13 pm

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.

User avatar
dhylands
Posts: 3227
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: UART on Micro:Bit

Post by dhylands » Sun Apr 30, 2017 6:49 pm

And also be sure that ground is connected between the 2 ends.

mkingdom
Posts: 9
Joined: Sun Apr 30, 2017 2:25 pm

Re: UART on Micro:Bit

Post by mkingdom » Sun Apr 30, 2017 8:31 pm

Thanks both. Another question. Do I need to disconnect the usb to computer cable from the Micro:bit for the UART to work?

BruinBear
Posts: 27
Joined: Sat Jan 14, 2017 11:59 am

Re: UART on Micro:Bit

Post by BruinBear » Mon May 01, 2017 9:39 am

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.

mkingdom
Posts: 9
Joined: Sun Apr 30, 2017 2:25 pm

Re: UART on Micro:Bit

Post by mkingdom » Mon May 01, 2017 10:47 am

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)

BruinBear
Posts: 27
Joined: Sat Jan 14, 2017 11:59 am

Re: UART on Micro:Bit

Post by BruinBear » Mon May 01, 2017 12:12 pm

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)?

Post Reply