Here is an example (Fig-1) of a multitasking environment with three tasks/threads managed by UNO Q (Zephyr RTOS):
- Task 1: Acquires and displays the temperature from a DS18B20 sensor
- Task 2: Blinks LED1 at a 2-second interval
- Task 3: Blinks LED_BUILTIN at 1 sec interval in the
loop()function
My issue is that the DS18B20 sensor delivers zero signal when run as a separate task in a multi-tasking environment (Fig-1). Currently, I am performing this task in the loop() function; where, the DS18B20 sensor is working fine by virtue of @robtillaart modified delayMicroseconds() function in the zephyrCommon.cpp file.
I would greatly appreciate guidance on how to resolve this issue.
Figure-1;
Sketch: (tested in IDE 2.3.7)
#include <zephyr/kernel.h>
#include <OneWire.h>
#include <Arduino_RouterBridge.h>
#define ONE_WIRE_PIN D8
#define LED1_PIN D3
byte addr[8];
byte data[9];
OneWire oneWire(ONE_WIRE_PIN);
// -------------------- Thread configuration ----------
#define STACK_SIZE 2048
#define THREAD1_PRIO 5 //DS
#define THREAD2_PRIO 5 //ledExternal
K_THREAD_STACK_DEFINE(ds18b20_stack, STACK_SIZE);
static struct k_thread ds18b20_thread;
K_THREAD_STACK_DEFINE(led1_stack, STACK_SIZE);
static struct k_thread led1_thread;
// -----------Thread-1: DS18B20 task (shifted in loop() function----
void ds18b20_task(void *, void *, void *)
{
while (true)
{
; //codes are placed in loop() function as they do not work here; gives 0
}
}
// ----------- Thread-2: External LED1 blinking (at 2 s interval)-------
void led1_task(void *, void *, void *)
{
while (true)
{
digitalWrite(LED1_PIN, HIGH);
k_sleep(K_SECONDS(2));
digitalWrite(LED1_PIN, LOW);
k_sleep(K_SECONDS(2));
}
}
void setup()
{
Bridge.begin();
Monitor.begin();
k_sleep(K_SECONDS(5)); //delay(5000); let bridge hardware initiate
pinMode(LED1_PIN, OUTPUT);
oneWire.reset(); //reading 8-byte ROM_code of DS18B20
oneWire.search(addr);
for (int i = 0; i < 8; i++)
{
byte y = addr[i];
if (y < 0x10)
{
Monitor.print('0');
}
Monitor.print(y, HEX);
Monitor.print(' ');
}
Monitor.println();
// Create DS18B20 thread-1
/* k_thread_create(&ds18b20_thread,
ds18b20_stack,
STACK_SIZE,
ds18b20_task,
NULL, NULL, NULL,
THREAD1_PRIO,
0,
K_NO_WAIT);*/
// Create LED1 thread-2
k_thread_create(&led1_thread,
led1_stack,
STACK_SIZE,
led1_task,
NULL, NULL, NULL,
THREAD2_PRIO,
0,
K_NO_WAIT);
}
// --------- Thread-3: Arduino loop() → blink LED_BUILTIN at 1sec interval
void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
k_sleep(K_SECONDS(1)); //
digitalWrite(LED_BUILTIN, LOW);
k_sleep(K_SECONDS(1));
//---------------------
oneWire.reset();
oneWire.select(addr);
oneWire.write(0x44); // Start conversion
k_sleep(K_MSEC(750)); //maximum conversion delay
oneWire.reset();
oneWire.select(addr);
oneWire.write(0xBE); // Read scratchpad memory
for (int i = 0; i < 9; i++)
data[i] = oneWire.read();
int16_t raw = (data[1] << 8) | data[0]; //compute temp
float tempC = raw / 16.0;
Monitor.print("DS18B20: ");
Monitor.print(tempC);
Monitor.println(" °C");
}
Output: (DS18B20 is running from within loop() function)
28 AD 47 65 09 00 00 2F //8-byte ROM-Code
DS18B20: 28.00 °C
DS18B20: 29.50 °C
DS18B20: 29.00 °C
DS18B20: 30.00 °C
DS18B20: 31.00 °C



