Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
simonc8
Posts: 7
Joined: Sat Nov 30, 2019 9:56 am

I am trying to programme a HC-SR04 ultrasonic sensor mounted on a 4tronix bitbot robot. The following micropython code is from the 4tronix website:

Code: Select all

from microbit import *
from utime import ticks_us, sleep_us

SONAR = pin15

def sonar( ):
SONAR.write_digital(0) # Clear trigger
sleep_us(2)
SONAR.write_digital(1) # Send 10us Ping pulse
sleep_us(10)
SONAR.write_digital(0)
# set pin15 to read, with no voltage applied
SONAR.set_pull(SONAR.NO_PULL)
while SONAR.read_digital() == 0: # ensure Ping pulse has cleared
pass
start = ticks_us() # define starting time
while SONAR.read_digital() == 1: # wait for Echo pulse to return
pass
end = ticks_us() # define ending time
echo = end - start
distance = int(0.01715 * echo) # Calculate cm distance
return distance

while True:
display.scroll(sonar())
sleep(1000)
but this doesn't work, because it gets stuck at the line

Code: Select all

while SONAR.read_digital() == 0: 
If I programme the microbit using PXT the sensor works properly. PXT uses the c++ function pulseIN() to read the digital pin but I can't find a way to get micropython to mimic the same behaviour.

Grateful for assistance.

lujo
Posts: 14
Joined: Sat May 11, 2019 2:30 pm

Hi,

Did you really used the same pin for triggering and reading back the echo?

Code: Select all

from microbit import *
from utime import ticks_us, sleep_us, ticks_diff

def sonar( ):

pin0.write_digital(0)
sleep_us(10)
pin0.write_digital(1)
sleep_us(10)
pin0.write_digital(0)

pass

start = ticks_us()
pass
end = ticks_us()

cm = ticks_diff(end, start) // 58
return cm

while True:
display.scroll(sonar())
sleep(1000)

lujo

simonc8
Posts: 7
Joined: Sat Nov 30, 2019 9:56 am

I have no control over the pins, as that's the way the bitbot is wired up.

I know it works using the same pin for both because this code in Java PXT returns the distance (albeit not very accurately):

Code: Select all

basic.forever(function () {
basic.showNumber(Math.round(bitbot.sonar(BBPingUnit.Centimeters)))
basic.pause(1000)
})

the bitbot PXT library function sonar I assume comes from the github project at

https://github.com/srs/pxt-bitbot

and the code in main.ts includes:

Code: Select all

    export function sonar(unit: BBPingUnit): number {
// send pulse
let trig = DigitalPin.P15;
let echo = DigitalPin.P15;

let maxCmDistance = 500;

pins.setPull(trig, PinPullMode.PullNone);
pins.digitalWritePin(trig, 0);
control.waitMicros(2);
pins.digitalWritePin(trig, 1);
control.waitMicros(10);
pins.digitalWritePin(trig, 0);

let d = pins.pulseIn(echo, PulseValue.High, maxCmDistance * 58);

switch (unit) {
case BBPingUnit.Centimeters: return d / 58;
case BBPingUnit.Inches: return d / 148;
default: return d;
}
}

This uses the function pulseIn() for the sensing but I can't find the source for this function to see exactly what it does to try and duplicate it in micropython.

lujo
Posts: 14
Joined: Sat May 11, 2019 2:30 pm

Hi,

There is a pulse in function. Source is here: https://github.com/bbcmicrobit/micropyt ... ne_pulse.c

Code: Select all

from microbit import *
from utime import sleep_us
from machine import time_pulse_us

def sonar( ):

pin0.write_digital(0)
sleep_us(10)
pin0.write_digital(1)
sleep_us(10)
pin0.write_digital(0)

pin1.set_pull(pin1.PULL_DOWN)
t = time_pulse_us(pin1, 1, 10**5)   # pin, level, time-out

return t // 58

while True:
display.scroll(sonar())
sleep(1000)

lujo

jimmo
Posts: 1435
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Can confirm that lujo's code works on the bit:bot -- here's code that I've seen working (which is basically identical to lujo's snippet above). https://github.com/jimmo/microbit-demos ... -finder.py

simonc8
Posts: 7
Joined: Sat Nov 30, 2019 9:56 am

Thanks for the replies. I loaded lugo's code onto the bitbot and it just returns -1 all the time, which means it's timing out while waiting for the pulse to finish. Not quite sure what this means.

I would suspect the batteries except for the fact that the same bitbot will return ultrasonic distances ok when programmed with PXT blocks in microsoft.makecode.org.

Mysterious.

jimmo
Posts: 1435
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

I went looking through my notes from a summer school I used to teach at -- https://github.com/jimmo/ncss-embedded/ ... opixels.md This is specifically about using the bit:bot with the micro:bit and MicroPython.

There's a code snippet at the end:

Code: Select all

from microbit import *
import machine

def distance_cm():
# Send a pulse on pin 15
pin15.write_digital(1)
pin15.write_digital(0)
# Read from the pin to turn it back to an input.
# Turn on the internal pull-up resistor
pin15.set_pull(pin15.PULL_UP)
pulse_time = machine.time_pulse_us(pin15, 1)
if pulse_time < 0:
return 0
return pulse_time * 0.034 / 2

while True:
print(distance_cm())
sleep(500)


simonc8
Posts: 7
Joined: Sat Nov 30, 2019 9:56 am

jimmo - brilliant - that code works!

Many thanks for your help. Your notes on the bit:bot are extremely useful as well.

On your github pages for ncss-embedded is there something I need to have installed so the equations show properly? In my browser they just show as the text eg

$$V_{tot} = \frac{V}{2}$$

jimmo
Posts: 1435
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

simonc8 wrote:
Wed Dec 11, 2019 9:57 am
On your github pages for ncss-embedded is there something I need to have installed so the equations show properly? In my browser they just show as the text eg

$$V_{tot} = \frac{V}{2}$$
I originally wrote all the formulae using Math URL (see the note in the main readme file) but a friend wanted to make it into a PDF so he set it up to use pandoc and LaTeX etc, so that's why the equations look like that. Here's a copy of the generated PDF https://drive.google.com/file/d/0B-O86M ... sp=sharing

simonc8
Posts: 7
Joined: Sat Nov 30, 2019 9:56 am