There are a lot many projects which one can do using Arduino. Recently, I was working on a project 'measuring distance of an object and show it using virtually using Arduino and Python'. I am using an Ultrasonic sensor for measuring distance, vpython library for virtual representation.
I was doing this project because I found it interesting and it also needs fewer hardware components and we will have to create a virtual object which is very interesting.
We need pyserial library to provide access to the python to communicate with Arduino. So, download that first. Before that, we would need Arduino IDE and Python. Then, we also need vPython for creating virtual objects using vPython library. You can download all these things from the link I have given at the end of this blog.
So, let's begin:-
We need - Hardware:- Arduino
Ultrasonic sensor
Jumper wires and breadboard
Software:-Download these software from the links given at last.
Arduino IDE
Python
Pyserial
vPython
Pyserial:-
So, I am using pyserial because it is a Python API module to access the serial port. In my code
We called for com4 serial port which is done by pyserial. It is a library which provides access to the serial port connection.
vPython:-
It is the Python programming language plus a 3D graphics module called "Visual". To invoke the Visual module, place the following statement at the start of the file:
from visual import *
we have VIDLE for vPython(we will code here), you can download from the links below.
Now, we can go for code:-Make sure that you read the comments while reading the code. It's quite easy. If you have any doubt about it, just comment below.
python script:-
import serial #Import Serial Library
from visual import * #Import all the vPython library
MyScene=display(title='Project of creating virtual world ') #Created scene and gave it a title 'Project of creating a virtual world”.
MyScene.width=500 #set the dimension of visual box.
MyScene.height= 500
MyScene.autoscale=False #We want to set the range of the scene manually for better control.So,Turn the autoscale off
MyScene.range = (12,12,12) #Set range of your scene to be 10 inches by 12 inches by 12 inches.
arduinoSerialData = serial.Serial('com4', 9600) #Create an object for the Serial port. Adjust 'com4' to whatever port your arduino is sending to.
myBoxEnd=box(length=.1, width=10,height=5, pos=(-4.5,0,0)) #This object is the square that is at the backside of the ultrasonic sensor
myTube2=cylinder(color=color.blue, pos=(-4.5,0,-2.5), radius=1.5,length=2.5 ) #One of the 'tubes' in the front of the ultrasonic sensor
myTube3=cylinder(color=color.blue, pos=(-4.5,0,2.5), radius=1.5,length=2.5 ) #Other tube
myBall=sphere(color=color.red, radius=.2) #just for reference like an origin or something like that
measuringRod = cylinder( radius= .4, length=7, color=color.yellow, pos=(-3,-2,0)) #the yellow coloured rod
lengthLabel = label(pos=(0,5,0), text='Target Distance is: ', box=false, height=30) #label just like the name suggests
target=box(pos=(0,-.5,0), length=.4, width=5, height=4, color=color.green) #It's our target object green in colour
while (1==1): #Create a loop that continues to read and display the data
rate(20) #Tell vpython to run this loop 20 times a second
if (arduinoSerialData.inWaiting()>0): #Check to see if a data point is available on the serial port
myData = arduinoSerialData.readline() #Read the distance measure as a string
print myData #Print the measurement to confirm things are working
distance = float(myData) #convert reading to a floating point number
measuringRod.length=distance #Change the length of your measuring rod to your last measurement
target.pos=(-3+distance,-.5,0)
myLabel= 'Target Distance is: ' + myData #Create label by appending string myData to string
lengthLabel.text = myLabel #display updated myLabel on your graphic
In short :-
i) 1st we will import Serial Library (vPython Library and not pyserial).
ii) Then, we create a scene (you can remove this from code(my scene part).I added it just to adjust the scene manually)
iii) Then, we start creating a virtual object and set serial port and let the IDLE communicate with the serial port of Arduino, this happened because of pyserial library.
iv)And after that, we create a rod of yellow colored and the box or card which would be moving
v)we create infinite loop whose speed of run at 20 /sec
vi)we receive the distance from Arduino Serial Data
vii)After that, we change the length of the yellow rod and the position of card or box
Arduino code:-
int trigPin=13;
int echoPin=11;
float pingTime; //time for ping to travel from sensor to target and return back
float targetDistance; //Distance to Target in inches so will have to convert it
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees
void setup() {
// put your setup code here and setting the mode
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// It's loop so,you know it will repeat
digitalWrite(trigPin, LOW); //Set trigger pin low
delayMicroseconds(2000); //Let signal settle for 2000 milli seconds
digitalWrite(trigPin, HIGH); //Set trigPin high
delayMicroseconds(15); //Delay in high state
digitalWrite(trigPin, LOW); //ping has now been sent
delayMicroseconds(10); //Delay in low state
pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds
pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
targetDistance= speedOfSound * pingTime; //This will be in miles, since speed of sound was miles per hour
targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
targetDistance= targetDistance*63360; //Convert miles to inches by multipling by 63360 (inches per mile)
Serial.println(targetDistance);
delay(100); //delay tenth of a second to slow things down a little.100 is in milli seconds
}
You will have to download this software for this project.
Arduino IDE:-If you got any error in uploading. Try for older versions of Arduino. Because it happened to me and then it worked fine with the older version.
Python:-This project was built using Python 2.7.3. There are some reported problems with PySerial on Windows, using Python 3, so stick to Python 2 and the most important thing is 32-bit python works fine on 64 bit. I did that. Go for 32 bit because there are so many libraries on it.
Pyserial :-
https://learn.adafruit.com/arduino-lesson-17-email-sending-movement-detector/installing-python-and-pyserial
vPython:-
If you have any doubt or want to suggest something, just comment below. Thank you!
Best of Luck and have a nice day!
Comments
Post a Comment