Nano BLE Sense not compatible with pulseIn()

Preface

Currently, I'm making an application on a Arduino 33 Nano BLE Sense. I'm trying to measure the current humidity and send it via BLE to another device. The target is to send a precise value with a high frequency, 8-10 values every second.

For measuring the humidity I connected an external sensor, the P14 Rapid Capacitive Humidity Sensor (it's faster and more accurate than the onboard humidity sensor) with a NE555 Timer IC in Monostable Mode.

To read the output pulse (frequency: ca. 500Hz) I'm using pulseIn().

I managed to calculate the humidity correctly and also the BLE data transfer worked.


Problem

My (minified) sketch:

#include <ArduinoBLE.h>

void setup() {
	// some setup, takes around 10s
}

void loop() {
	BLE.poll(); // polling for new events, takes ca. 10 µs

	value = pulseIn(6, HIGH); // read the high pulse, takes around 500-4000 µs

	// calculating the average value, takes < 1 µs

	if (/* this if is executed every 1 second for testing purposes, targeted is every 100ms*/) {
		// humidity is calculated, takes ca. 20 µs

		characteristic.writeValue(/* calculated value */); // takes ca. 50 µs
	}
}

When I merged the features, the problems began. I couldn't connect to the Arduino anymore.

When I try to connect to it (with nrf Connect App), this happens. When I manage to connect to it before it stops working, I will be disconnected with a GATT_CONN_TIMEOUT exception.

When the Arduino is powered on, the setup is called and it's connectable while the setup is running. When pulseIn() is called for the first time it stops working.

I already tried calling pulseIn() only every 10s or only once, it stops working.

And yes, commenting out the pulseIn() makes BLE work again.


Possible Solution

Next thing I would try is to delete the pulseIn() and use interrupts for measuring the time.

My question is if there is a better way to fix this issue?

I think you are running into a conflict with the Timers that are used for pulseIn and BLE. Looking at the Arduino source code Timer2 is used for pulseIn. And looking at the underlying Cordio transport used by BLE via mbed OS it is using both Timer0 and Timer2. Because Timer2 is used by both libraries I suspect they are conflicting with each other.

I suggest you raise raise as an issue on GitHub against the arduino/ArduinoCore-mbed project for a definitive answer.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.