In our previous tutorial we learnt how to install python on our windows machine and how to interface Arduino with python using a simple LED control project. If you are new, I would strongly recommend you to fall back to the previous tutorial since this tutorial is a continuation of the same.

You might have already started to wonder why we would need python with Arduino if all that it could do is simply communicate over serial port. But, Python is very strong development platform on which a lot of cool applications in which machine learning, computer vision and much more can be integrated. In this tutorial we will learn Howwe can Create a Small Graphical Interface Using Python. To do this we will need a module called Vpython. The following tutorial is applicable only for windows user since for Mac or Linux user, the procedure is different.

At the end of this tutorial we will learn how we can create simple GUI using Python. We will make a small animation which responds to the value of Ultrasonic sensor that is attached to the Arduino. This application tracks the object using Ultrasonic sensor and displays it in Graphical form on computer using VPython. As we move the object, Ultrasonic sensor senses the distance and sends this information to Python program using Arduino and it will move the object in the computer too. Sounds interesting right! So let get started...

Pre-requisites:

Installing VPython on your Computer:

In our previous tutorial we have already learnt how to install python on your machine and how to move around it and create a simple program with Arduino. Now we have install Visual Python (VPython) on top of this so that we can create cool Graphics using Python for Arduino. For the simple steps below to get started with VPython

Step 12. Now type “pip install --upgrade numpy” and press enter. The new version of Numpy should get installed on your machine. You might have to wait for some time if your internet connection is slow.

Step 13. Once done you can fall back to step number 4 and try an example program and you should be able to get it working.

Programming VPython:

Next we start programming into our VPython window. In this program we will create two 3D rectangular objects one will be placed in the centre of the screen reference to the stationary Ultrasonic sensor and the other will be at a dynamic location based on the distance between the US sensor and the object (paper).

The complete Python code can be found at the end of this page. Further down, I have explained this python code by splitting them into small meaningful junks.

The first line would be to import the visual Library so that we can create 3D objects. The below line does the same.

from visual import *

You should be familiar with the next four lines, since we have used them already in our previous tutorial. They are used to import Serial and time library and also establish a serial connection with Arduino at COM18 with 9600 as baudrate

import serial #Serial imported for Serial communication
import time #Required to use delay functions
ArduinoSerial = serial.Serial('com18',9600) #Create Serial port object called arduinoSerialData
time.sleep(2) #wait for 2 secounds for the communication to get established

Now, it is time to create objects. I have created two 3d rectangles named as obj and wall. The wallL is a stationary wall in cyan colour placed at the centre of the screen and the obj is the movable object in white colour. I have also placed a text “US sensor” near the wall object.

I am sure that the above three lines would have appeared as Greek and Latin for most of the first time readers, but with time you would be able to understand it. Everything that is mentioned inside brackets is (x,y,z) co-ordinates. And these co-ordinates are very similar to the ones that we find in our high school geometry class as shown below.

Now, the graphics and serial port is ready all that we have to do is read the data and place the “obj” (white rectangle) in a place according to the data coming from the Arduino. This can be done by the following lines, where obj.pos.x controls the X co-ordinate position of the object (White rectangle).

Getting your Arduino Ready:

The Python script is ready to listen for values from COM port and animate the graphics accordingly, but our Arduino is not ready yet. First we have to connect the Ultrasonic sensor to the Arduino according to the following Circuit Diagram. If you are completely new to US sensor and Arduino, then you have to fall back to Arduino & Ultrasonic Sensor Based Distance Measurement tutorial.

Then upload the Arduino Program given at the end of this page. The program is self explained using comment lines. We know that ultrasonic sensor works by calculating the time taken for the pulse to hit an object and return back. This value is calculated by using the PulseIn function in Arduino. Later the time taken is converted into distance using the below line.

dist = (timetaken/2) / 2.91;

Here the distance is calculated in terms of millimetres (mm).

Working:

The working of the project is simple. Launch the Python program and place an object before the US sensor as shown below:

Now launch the python program and you should be able to notice the white rectangle move along with your paper, the distance between your paper and sensor will also be displayed in the shell window as show in the image below.

This is how we can track the motion of object using Ultrasonic sensor and Python with Arduino.

Hope you understood the project and enjoyed building one. This is just one subtle step towards python but you can build a lot more creative things using this. If you have any idea of what to build with this post them on the comment section and use the forums for technical help. See you with another interesting python project.