# P5.js serial port and Microbit connection

**URL:** https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986
**Category:** Libraries
**Created:** [December 29, 2018, 3:22pm UTC](https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986 "2018-12-29T15:22:31Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![loretod](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/loretod/32/2974_2.png) [@loretod](https://discourse.processing.org/u/loretod)
#### Post date: [December 29, 2018, 3:22pm UTC](https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986/1 "2018-12-29T15:22:31Z")

</div>

I am working on getting the serial communication to work between a Microbit and p5.js. I’m pretty sure the Microbit side of the project is working since I am able to see the serial write in my terminal.

On the p5.js side, I’ve been following the available documentation and believe the library, variables, and functions are set up correctly however it’s not working.

Here is the Microbit side of things:

[https://makecode.microbit.org/\_UmseYmUumFmf](https://makecode.microbit.org/_UmseYmUumFmf)

Here is the p5.js side of things:

https://repl.it/@loretod/p5-Serial-library?lite=true

I’m running things on a Mac (10.14.2)

Any help is most appreciated!

---

<div class="post-metadata">

### Author: ![dector](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dector/32/2944_2.png) [@dector](https://discourse.processing.org/u/dector)
#### Post date: [December 29, 2018, 7:53pm UTC](https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986/2 "2018-12-29T19:53:05Z")

</div>

Can you please specify what wrong result you’ve got? Just in case. Do you see “server is connected” and “Serial port is open” in console?

---

<div class="post-metadata">

### Author: ![loretod](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/loretod/32/2974_2.png) [@loretod](https://discourse.processing.org/u/loretod)
#### Post date: [December 30, 2018, 2:38am UTC](https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986/3 "2018-12-30T02:38:25Z")

</div>

Yes, I see the server is connected and serial port is open messages. The serial message I’m sending from the Microbit do not show up in the inData variable (I get “undefined”). Printing the inData in the console log also gets me undefined. Using the if(serial.available()\>0) - executes none of the text. I also tried to run it using the web editor and in multiple browsers.

I do not see any of the serial data sent from the Microbit on p5.serialcontrol app. I can, however, see the data that is sent when using screen from the Terminal. Any ideas why it would work in Terminal and not elsewhere?

---

<div class="post-metadata">

### Author: ![dector](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dector/32/2944_2.png) [@dector](https://discourse.processing.org/u/dector)
#### Post date: [December 30, 2018, 9:04pm UTC](https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986/4 "2018-12-30T21:04:56Z")

</div>

> Microbit do not show up in the inData variable (I get “undefined”). Printing the inData in the console log also gets me undefined.

According to the [snippet you’ve share](https://repl.it/@loretod/p5-Serial-library) it will be undefined because of this:

```javascript
// Line 8
serial.on('data', serialEvent);

// Later, in line 15
serial.on('data', gotData);

```

So you are setting `gotData` as your callback function. And in `draw()` function you are calling it manually on each frame.

Try to comment line 15 and change `serialEvent()` function (line 22) to this:

```javascript
var inByte = serial.read();
console.log((inByte & 0xff).toString(16));

```

Also you can check this [basic example](https://github.com/vanevery/p5.serialport/blob/master/examples/basics/sketch.js)

---

<div class="post-metadata">

### Author: ![dector](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dector/32/2944_2.png) [@dector](https://discourse.processing.org/u/dector)
#### Post date: [December 30, 2018, 9:28pm UTC](https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986/5 "2018-12-30T21:28:29Z")

</div>

Also about `serial.readStringUntil('\r\n')`. `\r\n` is line break symbol. But I think that [**write number**](https://makecode.microbit.org/reference/serial/write-number) function will not add it. Probably you should convert it to string first and then send as a string. (If you want to receive data with line breaks only).

---

<div class="post-metadata">

### Author: ![loretod](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/loretod/32/2974_2.png) [@loretod](https://discourse.processing.org/u/loretod)
#### Post date: [December 31, 2018, 4:28am UTC](https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986/6 "2018-12-31T04:28:38Z")

</div>

Thanks @dector for the tips and help. I tried the above with still no serial data coming through (nothing in text or console log). Also tried to re download and run the basic examples provided from local files without luck. I’m starting to think it might be related to WebUSB and permissions ([https://developers.google.com/web/updates/2016/03/access-usb-devices-on-the-web](https://developers.google.com/web/updates/2016/03/access-usb-devices-on-the-web))? Although it’s not working in Safari or Firefox either. I’ll go down this rabbit hole a little and perhaps try and find a PC and Arduino to see if that makes a difference.

---

<div class="post-metadata">

### Author: ![dector](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dector/32/2944_2.png) [@dector](https://discourse.processing.org/u/dector)
#### Post date: [December 31, 2018, 11:51am UTC](https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986/7 "2018-12-31T11:51:45Z")

</div>

But on p5 side you are connecting to port (if I’ve got it right) and just reading from not (not writing)?

Try some serial terminal that is running in browser (e.g. [this](https://github.com/billroy/serial-web-terminal)) and exchange data with microbit.

---

<div class="post-metadata">

### Author: ![loretod](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/loretod/32/2974_2.png) [@loretod](https://discourse.processing.org/u/loretod)
#### Post date: [January 19, 2019, 10:12pm UTC](https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986/8 "2019-01-19T22:12:21Z")

</div>

It works! I think the main culprit may have been the firmware version I was running. The older firmware doesn’t have WebUSB. Updating to [250](https://support.microbit.org/support/solutions/articles/19000019131-how-to-upgrade-the-firmware-on-the-micro-bit) helped. The Microbit also runs at a115200 baudrate so specifying that rate may have helped also:

```auto
serial.open(portName, {baudrate: 115200},gotOpen);

```

Phew…

---

<div class="post-metadata">

### Author: ![totovr76](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/totovr76/32/50_2.png) [@totovr76](https://discourse.processing.org/u/totovr76)
#### Post date: [February 5, 2019, 11:45am UTC](https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986/9 "2019-02-05T11:45:16Z")

</div>

Late but I could read serial data with Chrome:

 ![Screenshot%20from%202019-02-05%2020-43-52](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b2e0503647ee0b476a93d632300c67a04d1dcb95.png)

The code is available [here](https://github.com/totovr/JS/tree/master/Node/SocketBroadcast)

---

<div class="post-metadata">

### Author: ![loretod](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/loretod/32/2974_2.png) [@loretod](https://discourse.processing.org/u/loretod)
#### Post date: [April 10, 2021, 5:59pm UTC](https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986/11 "2021-04-10T17:59:51Z")

</div>

Nice @geert ! I’ll try it out this weekend. The bluetooth version would be sweet!

---

<div class="post-metadata">

### Author: ![geert](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@geert](https://discourse.processing.org/u/geert)
#### Post date: [April 11, 2021, 9:17am UTC](https://discourse.processing.org/t/p5-js-serial-port-and-microbit-connection/6986/12 "2021-04-11T09:17:05Z")

</div>

If you want ‘normal’ serial you can check out this one: [Let Arduino Control Your Browser - Arduino Project Hub](https://create.arduino.cc/projecthub/lemio/let-arduino-control-your-browser-ac76f4?ref=user&ref_id=140850&offset=0)
