1

I'm trying to translate the python code for Raspberry to MicroBit MicroPython for driving a Grove - Ultrasonic Ranger module with MicroPython.

http://wiki.seeedstudio.com/Grove-Ultrasonic_Ranger/

https://github.com/Seeed-Studio/grove.py/blob/master/grove/grove_ultrasonic_ranger.py

I did this, the syntaxe is okay:

from microbit import *
import time


_TIMEOUT1 = 1000
_TIMEOUT2 = 10000


def _get_distance():
    pin0.write_digital(0)
    time.sleep_us(2)
    pin0.write_digital(1)
    time.sleep_us(10)
    pin0.write_digital(0)


    t0 = time.ticks_us()
    count = 0
    while count < _TIMEOUT1:
        if pin0.read_digital():
            display.set_pixel(1, 1, 5)
            break
        count += 1
    if count >= _TIMEOUT1:
        return None

    t1 = time.ticks_us()
    count = 0
    while count < _TIMEOUT2:
        if not pin0.read_digital():
            display.set_pixel(0, 0, 5)
            break
        count += 1
    if count >= _TIMEOUT2:
        return None

    t2 = time.ticks_us()



    dt = int(time.ticks_diff(t1,t0) * 1000000)
    # The problem is upside !


    if dt > 530:
        return  None

    distance = (time.ticks_diff(t2,t1) * 1000000 / 29 / 2)    # cm


    return distance


def get_distance():
    while True:
        dist = _get_distance()
        if dist:
            return dist

#Appel de la fonction get_distance(void) et affichage sur le display
display.scroll(get_distance())

But i have a big value for dt i don't know why... Thanks for your help !

2 Answers2

1

The Seeed Studio code does timing using Python's time.time() function. From the help:

time.time() → float

Return the time in seconds since the epoch as a floating point number.

Your code uses MicroPython's time.ticks_us() function. From its help:

utime.ticks_ms()

Returns an increasing millisecond counter with an arbitrary reference point, that wraps around after some value.

...

utime.ticks_us()

Just like ticks_ms() above, but in microseconds.

So the numbers you get in your version will be 10^6 times larger than the original Python code. It looks as if you're already multiplying the time differences by 10^6 to turn them into microseconds, so just remove this coefficient from your calculations.

nekomatic
  • 5,988
  • 1
  • 20
  • 27
0

To run Ultrasonic Ranger module with MicroPython, you can just use this library. where I modularized a distance function into a module. So you can import it and call the distance function as in this example below:

from microbit import *
from ultrasonic import Ultrasonic

# default pins in Ultrasonic library are:
# trigger: pin13
# echo: pin15

ultrasonic_sensor = Ultrasonic()
# or
# ultrasonic_sensor = Ultrasonic(trig=pin13, echo=pin15)

while True:
    distance_value = ultrasonic_sensor.measure_distance_cm()
    display.scroll(str(int(distance_value)))
    sleep(2000)

I've tested it on micro:bit v2 with an Ultrasonic module from Elecrow and their Crowtail-Base Shield for Microbit.

Meqdad Dev
  • 131
  • 1
  • 2
  • 10