# Codal-microbit-v2: How to instantiate & use a second serial?

**URL:** https://forum.makecode.com/t/codal-microbit-v2-how-to-instantiate-use-a-second-serial/32332
**Category:** PXT
**Created:** [December 9, 2024, 1:46am UTC](https://forum.makecode.com/t/codal-microbit-v2-how-to-instantiate-use-a-second-serial/32332 "2024-12-09T01:46:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![muz](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@muz](https://forum.makecode.com/u/muz)
#### Post date: [December 9, 2024, 1:46am UTC](https://forum.makecode.com/t/codal-microbit-v2-how-to-instantiate-use-a-second-serial/32332/1 "2024-12-09T01:46:31Z")

</div>

Hi!

I am planning to create an extension targetting Micro:bit v2 which would require a second serial interface. As a quick test I had a simple C++ program built against codal-microbit-v2 as following:

> #include “MicroBit.h”  
> #include “samples/Tests.h”
> 
> MicroBit uBit;
> 
> int main()  
> {
> 
> ```
> uBit.init();
> 
> // out_of_box_experience();
> 
> codal::MicroBitIO io = uBit.io;
> NRF52Serial serial2(io.P13, io.P14, NRF_UARTE1);
> serial2.init();
> NVIC_SetPriority(UARTE1_IRQn, 2); // Serial port
> serial2.addComponent();
> 
> while (1)
> {
> serial2.sendChar('!');
> uBit.sleep(1000);
> }
> 
> microbit_panic(999);
> 
> ```
> 
> }

However a HardFault was caught upon the 1st char was sent. Could someone please help?

–  
Muz

---

<div class="post-metadata">

### Author: ![muz](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@muz](https://forum.makecode.com/u/muz)
#### Post date: [December 9, 2024, 3:01pm UTC](https://forum.makecode.com/t/codal-microbit-v2-how-to-instantiate-use-a-second-serial/32332/2 "2024-12-09T15:01:41Z")

</div>

Some updates to the issues described in the thread.

1. This line of code should be deleted. The `addComponent()` method is already called in the constructor.

> [@muz](#):
>
> `serial2.addComponent();`

1. I found that if I remove all the code that causes fiber to go into sleep, like the following code, then the program works fine.

> ```
> while (1)
> {
> serial2.send("hello!\n", codal::SYNC_SPINWAIT);
> uBit.wait(1000);
> }
> 
> ```

However, I think this just avoids the real problem, and doesn’t solve it. I expect the second serial instance to fit into the Micro:bit runtime and work perfectly fine, including when the device goes into a low-power sleep state.

Any help would be appreciated!

–  
Muz

---

<div class="post-metadata">

### Author: ![muz](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@muz](https://forum.makecode.com/u/muz)
#### Post date: [December 9, 2024, 7:16pm UTC](https://forum.makecode.com/t/codal-microbit-v2-how-to-instantiate-use-a-second-serial/32332/3 "2024-12-09T19:16:01Z")

</div>

I managed to get the program to work, although I still don’t understand the source of the problem, like - as the old Chinese saying goes - a blind cat catches a dead mouse (瞎猫抓到死耗子). Anyway, here’s my modified program code:

> #include “MicroBit.h”  
> #include “samples/Tests.h”
> 
> MicroBit uBit;  
> NRF52Serial serial2(uBit.io.P13, uBit.io.P14, NRF\_UARTE1);
> 
> int main()  
> {
> 
> ```
> NVIC_SetPriority(UARTE1_IRQn, 2); // Serial port
> 
> uBit.init();
> 
> while (1)
> {
> serial2.send("hello!\n");
> uBit.sleep(1000);
> }
> 
> microbit_panic(999);
> 
> ```
> 
> }

I hope this will inspire someone in the same situation as me.

Of course, I’d be grateful if someone with bright eyes could tell me the full picture…

–  
Muz
