Page 1 of 1
pin digital_read
Posted: Wed Dec 04, 2019 10:18 am
by simonc8
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
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.
Re: pin digital_read
Posted: Fri Dec 06, 2019 8:31 am
by lujo
Hi,
Did you really used the same pin for triggering and reading back the echo?
If I use two different micro:bit pins, it works. Using your code to start with:
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)
while pin1.read_digital() == 0:
pass
start = ticks_us()
while pin1.read_digital() == 1:
pass
end = ticks_us()
cm = ticks_diff(end, start) // 58
return cm
while True:
display.scroll(sonar())
sleep(1000)
lujo
Re: pin digital_read
Posted: Fri Dec 06, 2019 10:25 am
by simonc8
Thanks for your reply.
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);
// read pulse
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.
Re: pin digital_read
Posted: Sat Dec 07, 2019 1:44 am
by lujo
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
Re: pin digital_read
Posted: Sat Dec 07, 2019 11:12 am
by jimmo
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
Re: pin digital_read
Posted: Tue Dec 10, 2019 7:01 pm
by simonc8
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.
Re: pin digital_read
Posted: Wed Dec 11, 2019 2:58 am
by jimmo
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.
pin15.read_digital()
# 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)
Re: pin digital_read
Posted: Wed Dec 11, 2019 9:57 am
by simonc8
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
\begin{equation} V_{tot} = \frac{V}{2} \end{equation}
Re: pin digital_read
Posted: Tue Dec 17, 2019 1:46 am
by jimmo
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
\begin{equation} V_{tot} = \frac{V}{2} \end{equation}
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
Re: pin digital_read
Posted: Tue Dec 17, 2019 5:06 pm
by simonc8
Many thanks - that is a really useful document.