Creating
a Virtual World Using Arduino and Python:
Introduction
to Our Project:
how to create a simple virtual world using vPython
library.In this project we are using Arduino and Ultasonic Sensor for sensing
the virtual world. The virtual world is use in real time based on measurements
from the arduino and ultrasonic sensor. In this project you can use how to
combine Arduino, Python, and vPython library to create a virtual world i.e
virtual object updated based on sensor measurements.
Installation of All Software:
Arduino
IDE:
Installation of Arduino IDE: You can download the
latest Arduino IDE from this link:
https://www.arduino.cc/en/Main/Software
Python:
Download and Install Python 2.7.8.for windows. Link is
given below:
https://www.python.org/downloads/windows/
Pyserial:
Download and Install Pyserial version 2.7 for windows.
Link is given below:
https://learn.adafruit.com/arduino-lesson-17-email...
vPython:
Download and install the Vpython library for windows.
Link is given below:
Component
Used:
1)
Arduino UNO:
Arduino/Genuino Uno is a microcontroller board based on
the ATmega328P (datasheet). It has 14 digital input/output pins (of which 6 can
be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB
connection, a power jack, an ICSP header and a reset button.
2)Ultrasonic
sensor:
This is the HC-SR04 ultrasonic ranging sensor. This
economical sensor provides 2cm to 400cm of non-contact measurement
functionality with a ranging accuracy that can reach up to 3mm. Each HC-SR04
module includes an ultrasonic transmitter, a receiver and a control circuit.
Circuit
Description:
First we are going to connect ultrasonic sensor with
Arduino board. Then according to program our ultrasonic sensor going to
generate a trigger signal which is going to receive by our echo pin of
Ultrasonic Sensor. Then we are going to run our arduino code. According to
Arduino code our Ultrasonic Sensor detect the distance of any object.
Here are the connections of our circuit:
Ultrasonic Sensor---Arduino UNO
GND---------------------GND
Echo------------------- pin 11
Trig..........................pin13
Vcc.........................5v
Code
of the Project:
Step
to run Code:
1) Firstly you can run Arduino code.
2) After that You can run python script, before running
python script you can install Pyserial and vPython.
Arduino
Code:
int trigPin=13; //Sensor Trig pin connected to Arduino
pin 13
int echoPin=11;
//Sensor Echo pin connected to Arduino pin 11
float pingTime;
//time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per
hour when temp is 77 degrees.
void setup() {
// put your
setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin,
INPUT);
}
void loop() {
// put your
main code here, to run repeatedly:
digitalWrite(trigPin, LOW); //Set trigger pin low
delayMicroseconds(2000); //Let signal settle
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.
}
Python
Script:
import serial #Import Serial Library
from visual import * #Import all the vPython library
arduinoSerialData = serial.Serial('com3', 9600) #Create
an object for the Serial port. Adjust 'com11' to whatever port your arduino is
sending to.
measuringRod = cylinder( radius= .1, length=6,
color=color.yellow, pos=(-3,-2,0))
lengthLabel = label(pos=(0,5,0), text='Target Distance
is: ', box=false, height=30)
target=box(pos=(0,-.5,0), length=.2, width=3, height=3,
color=color.green)
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