MicroPython Tutorial XVII

Sensors. Beyond the colour and gyro sensors you have more, two notable ones the ultrasonic and infrared. We used the infrared in an earlier tutorial with the remote in fact, although it has a few more tricks up its sleve.

Of course you you have one or the other depending on which set you buy. I wouldn’t go out and get the missing one in preference to the colour sensor, but if you still some money left, its your next investment.

Both sensors will measure distance, but report their findings differently. The ultrasonic reports in millimetres, the infrared reports as a percentage figure. But beware they don’t behave in the same manner as I discovered by accident a while back. Here is a simple piece of code to invoke them.

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import UltrasonicSensor, InfraredSensor
from pybricks.parameters import Port
from pybricks.tools import wait
ultra = UltrasonicSensor(Port.S1)
infra = InfraredSensor(Port.S2)
while True:
distance = ultra.distance()
distance2 = infra.distance()
print("ultra distance",distance,"infra distance",distance2)
wait(500)

All this does is define the two sensors and report the distances they see every 1/2 a second. I mounted them on my robot and build a lego tower to observe as you can see in this photo.

The infrared returned a value of 90 @ approximately 1200cm, beyond that the results seemed very unreliable. The ultrasonic on the other hand continued to return values until it reached 2550mm. It appeared to be more reliable.

But wait, when I pointed the ultrasonic at my lego tower positioning it so that it split ultrasonic wave, it got the distance very wrong. Square the tower wall to it, than it reported it correctly, faced by a corner it was didn’t work. The infrared on other hand wouldn’t/couldn’t be fooled. I even tried the infrared in bright sun light, it still worked. The finding suggesting, you need to be mindful using the ultrasonic with surfaces that aren’t not unform.

At the other end, the ultrasonic was unable to measure distances as close as 3cm. The infrared on the other had would give me an index value at 1 at 5cm. Unlike the ultrasonic, because it gives an index value it is far less granular than the ultrasonic.

In conclusion, ideally you would use both. The infrared could help sanity check the ultrasonic distance, the ultrasonic used measure distances with more precision. Assuming of course you interested in objects closer than 1m.

Which brings me to the next script. Lets mount both sensors next to each other and build a table that we can use to cross match distances measured.

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import UltrasonicSensor, InfraredSensor, Motor
from pybricks.parameters import Port
from pybricks.tools import wait
from pybricks.robotics import DriveBase
left_motor = Motor(Port.B)
right_motor = Motor(Port.C)
left_motor.reset_angle(0)
robot = DriveBase(left_motor, right_motor, 56, 114)
ultra = UltrasonicSensor(Port.S1)
infra = InfraredSensor(Port.S2)
speed = 5robot.drive(-speed,0)while True:
print("ultra distance",ultra.distance(),"infra distance",infra.distance(),"angle",left_motor.angle())
wait(2000)

With the results coming back looking something like this.

ultra distance 835 infra distance 77 angle 0
ultra distance 839 infra distance 78 angle -18
ultra distance 849 infra distance 77 angle -38
ultra distance 857 infra distance 77 angle -59
ultra distance 864 infra distance 78 angle -80
ultra distance 877 infra distance 78 angle -100
ultra distance 886 infra distance 78 angle -120
ultra distance 901 infra distance 79 angle -140
ultra distance 908 infra distance 79 angle -160
ultra distance 916 infra distance 78 angle -180
ultra distance 929 infra distance 79 angle -200
ultra distance 940 infra distance 80 angle -220
ultra distance 947 infra distance 80 angle -241
ultra distance 955 infra distance 79 angle -261
ultra distance 969 infra distance 81 angle -280

The script reasonable self explanatory; it simply defines a couple of motors, a couple of sensors and drives the robot back, reporting every 2 seconds what the sensors are saying.

More worrying for me a a computer scientist is the report it produces. There is as you can see for yourself a lack of uniformity. The robot is driving at a constant rate, and within one degree of error the angle looks good, with 20/21 degrees every 2 seconds once it gets going. But the infrared measurement/index is jumping back and forth, the index returns 80 than 79, then 81? The ultrasonic keeps increasing, but with inconsistent steps, anything between 9mm difference to as much as 15mm despite the fact we moved the same distance [apparently].

In conclusion ultimately even using both, you’re in the dark. Your best bet if you need how to know how far you travelled isn’t the ultrasonic or the infrared, its the angle reading on the motor itself.

Written by

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store