ESP-32 with KY-050 sensor provides wrong values within task

JazzJackR
Posts: 1
Joined: Sat Jul 29, 2023 9:34 am

ESP-32 with KY-050 sensor provides wrong values within task

Postby JazzJackR » Sat Jul 29, 2023 9:50 am

First of all,

hi to everyone this is my first post in this forum.

I use a ultrasonic sensor KY-050 to measure the distance.
If I use it in the main loop it works fine (e.g 17 cm)
When I move the measurement to a separate FreeRTOS task (same code) and communicate via queues, I get a distance of 26cm.

Why do I not get the same distance, shouldn’t it be the same whether is use it in the main loop or a separate task?

Here is the code which I use from the examples:

Code: Select all

// Distance measurement will be started with a 10us long trigger signal
digitalWrite(Trigger_OutputPin, HIGH); 
delayMicroseconds(10);
digitalWrite(Trigger_OutputPin, LOW);
 
// Now it will be waited at the echo input till the signal was activated
// and after that the time will be measured how long it is active
duration = pulseIn(Echo_InputPin, HIGH);

// Now the distance will be calculated with the recorded time
distance = duration/58.2;

Who is online

Users browsing this forum: Majestic-12 [Bot] and 133 guests