STM32F407Disc board: Problem with interfacing Ultrasonic Ranger

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

STM32F407Disc board: Problem with interfacing Ultrasonic Ranger

Post by nikhiledutech » Sat Jan 06, 2018 10:37 am

So i am currently interfacing ultrasonic sensor with my board. So i am facing 2 problems.
1) Loop is not continuiosly reading values in input mode
2) The distance value is not correctly calculated.
Below is my code. My sensor is an Seed Studio Grove Ultasonic ranger, having one signal pin, VCC, GND, NC pin. I use SIG pin both as Input and output in my code.

import pyb
from pyb import Pin
import uasyncio as asyncio
import utime as time
from alcd import PINLIST,LCD

start = 0
end = 0
lcd = LCD(PINLIST, cols=16)

def Ultrasonic_Measure_In_CM ():
global start
global end

#configuring pin as output
pin1 = pyb.Pin('PC6')

#Sending Signal

#configuring pin as Input

#Waiting for reflected signal
while (pin1.value() == 0):
start = time.ticks_ms()
while (pin1.value() == 1):
end = time.ticks_ms()

distance = time.ticks_diff(end,start) #divide by 29 (speed of sound is 340 m/s and i.e 29 us/cm).
distance_in_cm = (distance /2)/29 #Divide by 2 to get one way distance
return distance_in_cm

async def ultrasonic_ranger():
dist = Ultrasonic_Measure_In_CM()
for secs in range(20,-1,-1):
lcd[0] = 'Dist {:f}'.format(dist)
await asyncio.sleep(1)

while True:
loop = asyncio.get_event_loop()

1. Here thing is that, some times value get updated slowly. In async def ultrasonic_ranger() function dist variable should continously read the values.But as i change the distance between ultrasonic ranger and wall, dist variable is not reading different values.

2. The equation which i am using to convert values in cm. Is it correct ?

Also sometimes O/P on LCD is displayed slowly. So help me in reading values from ultrasonic continiously.

Post Reply