Measure distances with an ultrasound sensor and show it on VPython


In this post I’m using again the ultrasonic sensor to measure distances. In a previous post I used the sensor combined with a LCD display to show the measured distance. In this case I’m going to create a virtual environment to show the distance to the target using VPython.

1. Arduino with an USB connection and the Arduino IDE installed.
2. An ultrasound sensor, I’m using an Ultrasonic Ranging Module HC-SR04.
3. Python 2.7 installed with VPython and the following library: pyserial.
4. A breadboard and some wires to connect the ultrasound sensor to the Arduino board.

Circuit diagram:
This is a easy circuit, we are just connecting the ultrasound sensor to the Arduino. Remember that we are going to read the Arduino from the serial port, so it has to be connected to the computer by the USB:

Ultrasonic module hooked to the Arduino
Ultrasonic module hooked to the Arduino

Just in case, here is a table explaining the pin connections for the sensor:

LCD Display Pin
VCC (Voltage for the sensor) 5V
Echo Pin 13 of Arduino board
Trig (trigger pin) Pin 11 of Arduino board
GND (Ground GND




Arduino code:
The code needed from the Arduino part is the same used in the previous post. We have to remove just the code that controls the display:

You can check what each part of the code does in this post. Here I will summarize the code: In the first part we declare the pins and the variables needed to the system. In the void setup we start the serial port and set the trigger pin as an output and the the echo pin as an input. Finally, in the void loop we first send the wave and we then measure the time to go to the target and come back. We calculate the distance using the speed of sound and finally we print the value of the targetDistance on the serial port. This is the value that Python will read later.

Python code:

Here is the new part where we have to write a code in python that does the following:
1- It reads the value sent from the Arduino
2- creates a virtual world and reacts in ‘real time’ with the measurements. The code is the following one:

As usual first lines import the required libraries, in this case pyserial and VPython. Then we create an object for the serial data received from the Arduino using the serial port.

Now we start to set up the virtual environment: first we create a measuring rod which is a yellow cylinder with an initial length of 50. We also create a label to show the value of the distance and to show the target we define a green box.

We then create a while loop that will run forever. We have to add now the refresh rate, which is required for the visual library to draw the objects. Then we create an if loop which will be true only when serial data is sent by the Arduino. If loop’s evaluation is true the data is read and then convert the data into a number.

Now that we have the value as a string and as a number, we can refresh our virtual world. The first thing we refresh is our label using my Data to show the current measurement. Instruction in line 16 just creates the label and in line 17 the code updates the label. Then  the code updates the length of the measuring Rod using the distance variable we created. The final instruction  is to move the target box to the correct position.

If you have followed this tutorial, this is what you should see in the VPython window:

VPython window of the distance being measured
VPython window of the distance being measured

You can copy and paste the codes above or you can go to my Github account and find the code for the Arduino here, and the code for Python here.