Devices & Components
Arduino Uno Rev3
Ultrasonic Sensor - HC-SR04 (Generic)
Jumper wires (generic)
Project description
Code
Python3 code
python
1import serial #serial import for serial communications 2import time 3import pyttsx3 4import speech_recognition as sr 5 6engine = pyttsx3.init('sapi5') 7voices = engine.getProperty('voices') 8#print(voices[1].id) 9engine.setProperty('voice',voices[1].id) 10 11ArduinoSerial = serial.Serial('com3',9600) #Create Serial port object called arduinoSerialData 12 13 14def speak(audio): 15 engine.say(audio) 16 engine.runAndWait() 17 18def takecommand(): 19 #It takes microphone input from the user and returns string as output 20 21 r = sr.Recognizer() 22 with sr.Microphone() as source: 23 print("Listening....") 24 r.pause_threshold = 1 25 audio = r.listen(source) 26 27 try: 28 print("Recogninsing....") 29 query = r.recognize_google(audio, language = 'en-in') 30 print(f"User said: {query}\ 31") 32 33 except Exception as e: 34 print(e) 35 print("Say that again please...") 36 return "None" 37 return query 38 39if __name__ == "__main__": 40 while True: 41 42 query = takecommand().lower() 43 #logic for executing tasks based on query 44 45 distance = int(ArduinoSerial.readline()) #read the serial data and print it as line 46 dist=str(distance) 47 print(dist) 48 if "where is the object" in query: 49 if (distance<20): 50 speak("StopStop, Close object in front.") 51 52 53 elif (20<distance<60): 54 speak("object is quite a distance away.") 55 56 elif (40<distance<100): 57 speak("object is 1 meter away.") 58 59 else: 60 speak("Object is far away.") 61 62 else: 63 print("nothing") 64
Arduino IDE code
arduino
1const int trig = 2; //Trigger pin of ultrasonic Sesnor 2const int echo = 3; //Echo pin of ultrasonic Sesnor 3long time_taken; 4int dist,distance; 5 6void setup() 7{ 8 Serial.begin(9600); //tells the arduino to get ready to exchange messages with the serial monitor at a data rate of 9600 bits/sec. 9 pinMode(trig, OUTPUT); //configures the specified pin to behave as output/input. 10 pinMode(echo, INPUT); 11} 12 13/*###Function to calculate distance###*/ 14void calculate_distance(int trigger, int echo) 15{ 16 digitalWrite(trigger, LOW); 17 delayMicroseconds(2); 18 digitalWrite(trigger, HIGH); 19 delayMicroseconds(10); 20 digitalWrite(trigger, LOW); 21 22 time_taken = pulseIn(echo, HIGH); 23 dist= time_taken*0.034/2; 24 25} 26 27void loop() 28{ 29 calculate_distance(trig, echo); 30 distance=dist; //getting distance of ultrasonic sensor 31 Serial.println(distance); 32 delay(1000); 33}
Python3 code
python
1import serial #serial import for serial communications 2import time 3import 4 pyttsx3 5import speech_recognition as sr 6 7engine = pyttsx3.init('sapi5') 8voices 9 = engine.getProperty('voices') 10#print(voices[1].id) 11engine.setProperty('voice',voices[1].id) 12 13ArduinoSerial 14 = serial.Serial('com3',9600) #Create Serial port object called arduinoSerialData 15 16 17def 18 speak(audio): 19 engine.say(audio) 20 engine.runAndWait() 21 22def takecommand(): 23 24 #It takes microphone input from the user and returns string as output 25 26 27 r = sr.Recognizer() 28 with sr.Microphone() as source: 29 print("Listening....") 30 31 r.pause_threshold = 1 32 audio = r.listen(source) 33 34 try: 35 36 print("Recogninsing....") 37 query = r.recognize_google(audio, 38 language = 'en-in') 39 print(f"User said: {query}\ 40") 41 42 except 43 Exception as e: 44 print(e) 45 print("Say that again please...") 46 47 return "None" 48 return query 49 50if __name__ == "__main__": 51 52 while True: 53 54 query = takecommand().lower() 55 #logic 56 for executing tasks based on query 57 58 distance = int(ArduinoSerial.readline()) 59 #read the serial data and print it as line 60 dist=str(distance) 61 print(dist) 62 63 if "where is the object" in query: 64 if (distance<20): 65 66 speak("StopStop, Close object in front.") 67 68 69 70 elif (20<distance<60): 71 speak("object 72 is quite a distance away.") 73 74 elif (40<distance<100): 75 76 speak("object is 1 meter away.") 77 78 else: 79 80 speak("Object is far away.") 81 82 else: 83 84 print("nothing") 85
Downloadable files
Circuit Diagram #1
connecting arduino o ultrasonic sensor
Circuit Diagram #1

Diagram #2
Mounting sensor on glasses
Diagram #2

#4
#4

#3
#3

Diagram #2
Mounting sensor on glasses
Diagram #2

#3
#3

#4
#4

Circuit Diagram #1
connecting arduino o ultrasonic sensor
Circuit Diagram #1

Comments
Only logged in users can leave comments