mahdi syed
Published © GPL3+

Speech Controlled Servo With Firmata and Python

Control your servo motor with your voice!

BeginnerProtip30 minutes498
Speech Controlled Servo With Firmata and Python

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
PyCharm

Story

Read more

Schematics

Servo wiring diagram

Wire your servo as shown

Code

Python Code for Speech Recognition

Python
Put this into the Python IDE you are using and run it while connected to the Arduino. Make sure to upload the Arduino code onto the board before doing this!
import speech_recognition
from pyfirmata import Arduino, SERVO, util
from time import sleep

#Define port, board, and servo pin
port = 'COM4'
servoPin = 9
myBoard = Arduino(port)
myBoard.digital[servoPin].mode = SERVO

#Define the speech recognizer
recognizer = speech_recognition.Recognizer()

#Infinite loop
while True:
    try:
        #Collect audio and turn it into text using a speech recognizer from Google
        with speech_recognition.Microphone() as mic:
            recognizer.adjust_for_ambient_noise(mic, duration=0.0)
            audio = recognizer.listen(mic)

            text = recognizer.recognize_google(audio)
            text = text.lower()
            print(text)

            #Check if the word spoken was a number. If it was, write it to the servo
            if text.isnumeric():
                myBoard.digital[servoPin].write(int(text))
                sleep(0.015)


    except speech_recognition.UnknownValueError:

        recognizer = speech_recognition.Recognizer()
        continue

Credits

mahdi syed
3 projects • 1 follower
I'm a passionate programmer with extensive experience in robotics, app design, web design, IOT, and AI.
Contact

Comments

Please log in or sign up to comment.