0% found this document useful (0 votes)
20 views6 pages

Arduino Voice-Controlled Robot Guide

This Arduino sketch controls a voice-controlled robot. It uses an ultrasonic sensor to measure distance and stop the robot if an object is detected within 30 cm. The robot can move forward, backward, left, right, or stop based on voice commands received over Bluetooth. Pins are defined for controlling two DC motors and reading the ultrasonic sensor. Setup and loop functions initialize serial communication and listen for commands to control motor directions accordingly.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Arduino Voice-Controlled Robot Guide

This Arduino sketch controls a voice-controlled robot. It uses an ultrasonic sensor to measure distance and stop the robot if an object is detected within 30 cm. The robot can move forward, backward, left, right, or stop based on voice commands received over Bluetooth. Pins are defined for controlling two DC motors and reading the ultrasonic sensor. Setup and loop functions initialize serial communication and listen for commands to control motor directions accordingly.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

/*Arduino Voice Control Robot

*Version 1.0

*Created By DIY Builder

*Before Uploading the sketch you must install the required libraries first.

*Unless you'll get compilation error message.

*Also remove the bluetooth and servo connector before uploading the sketch.

*You can contact me here [Link]

*/

String command;

//Right motor

int rightMotorPin1=D5;

int rightMotorPin2=D6;

//Left motor

int leftMotorPin1=D7;

int leftMotorPin2=D8;

#define trigger_pin D1
#define Echo_pin D2

long duration;

int distance;

unsigned long oldtime=0;

void setup() {

[Link](9600);

pinMode(rightMotorPin1, OUTPUT);

pinMode(rightMotorPin2, OUTPUT);

pinMode(leftMotorPin1, OUTPUT);

pinMode(leftMotorPin2, OUTPUT);

pinMode(trigger_pin, OUTPUT); // configure the trigger_pin(D9) as an Output

pinMode(Echo_pin, INPUT); // configure the Echo_pin(D11) as an Inpu

void loop() {

delay(10);

while([Link]()) {

command = "";

command = [Link]();
[Link](command);

if(command == "*move forward#"){

forward();

[Link]("Forward Entering");

}else if(command == "*move backward#"){

backward();

}else if(command == "*turn left#"){

left();

}else if(command == "*turn right#"){

right();

}else if(command == "*stop it#") {

Stop();

command = "";

if((millis()-oldtime)>1000)

digitalWrite(trigger_pin, LOW); //set trigger signal low for 2us

delayMicroseconds(2);

/*send 10 microsecond pulse to trigger pin of HC-SR04 */

digitalWrite(trigger_pin, HIGH); // make trigger pin active high


delayMicroseconds(10); // wait for 10 microseconds

digitalWrite(trigger_pin, LOW); // make trigger pin active low

/*Measure the Echo output signal duration or pulss width */

duration = pulseIn(Echo_pin, HIGH); // save time duration value in "duration variable

distance= duration*0.034/2; //Convert pulse duration into distance

if(distance<30)

digitalWrite( rightMotorPin1,LOW);

digitalWrite(rightMotorPin2,LOW);

digitalWrite(leftMotorPin1, LOW);

digitalWrite(leftMotorPin2 , LOW);}

oldtime=millis();

void forward()

digitalWrite(rightMotorPin1,HIGH);

digitalWrite(rightMotorPin2,LOW);

digitalWrite(leftMotorPin1,HIGH);
digitalWrite(leftMotorPin2,LOW);

void backward()

digitalWrite(rightMotorPin1,LOW);

digitalWrite(rightMotorPin2,HIGH);

digitalWrite(leftMotorPin1,LOW);

digitalWrite(leftMotorPin2,HIGH);

void right()

digitalWrite(rightMotorPin1,HIGH);

digitalWrite(rightMotorPin2,LOW);

digitalWrite(leftMotorPin1,LOW);

digitalWrite(leftMotorPin2,HIGH);

void left()
{

digitalWrite(rightMotorPin1,LOW);

digitalWrite(rightMotorPin2,HIGH);

digitalWrite(leftMotorPin1,HIGH);

digitalWrite(leftMotorPin2,LOW);

void Stop()

digitalWrite(rightMotorPin1,LOW);

digitalWrite(rightMotorPin2,LOW);

digitalWrite(leftMotorPin1,LOW);

digitalWrite(leftMotorPin2,LOW);

Common questions

Powered by AI

Before uploading the sketch, it is advised to install the required libraries to avoid compilation errors and to remove the Bluetooth and servo connectors to ensure a successful upload process .

Challenges include dependency on voice recognition accuracy, the effectiveness of Bluetooth communication, and the responsiveness of the ultrasonic sensor. These elements may not function reliably in noisy environments or with obstructed signals. Improvements could involve using more advanced voice recognition technologies, enhancing the sensor's range and accuracy with better positioning, or integrating more robust wireless communication protocols to enhance connectivity and control .

The distance is measured by sending a 10-microsecond pulse to the trigger pin of the HC-SR04 ultrasonic sensor. The duration of the pulse reflected back to the Echo pin is recorded and converted into distance using the formula distance = duration * 0.034 / 2. This calculation works by determining the time it takes for the sound waves to travel to the obstacle and back, and dividing by 2 to account for the round trip .

The robot stops automatically when it detects an obstacle at a distance of less than 30 cm. The code sets all motor pins to LOW when the distance calculated from the ultrasonic sensor is less than 30, effectively stopping the robot by cutting power to the motors. This functionality is implemented in the main loop function, which continuously checks the distance at intervals defined by a 1-second timer .

The delayMicroseconds(10) function creates a 10-microsecond pulse to the ultrasonic sensor's trigger pin, signaling it to send out a sound wave. This precise timing is crucial for starting the sensor's measurement of the time taken for the emitted sound wave to hit an object and return, which is subsequently used to calculate distance. Inaccurate timing could result in faulty distance readings .

The program reads a command from the serial input and compares it with predefined strings like '*move forward#', '*move backward#', '*turn left#', '*turn right#', and '*stop it#'. Based on the command received, it calls the corresponding function to control the motor pins, thereby moving the robot in the specified direction .

The millis() function keeps track of the time elapsed since the program started running. Within the loop() function, it helps in managing the timing of subsequent ultrasonic measurements by ensuring that the sensor measurements are conducted at 1-second intervals. This prevents continuous and rapid polling, which could lead to inaccurate readings and inefficient use of processing resources .

Resetting the command string after each command processing is a significant design choice that helps ensure the system responds to new commands correctly without being influenced by previous inputs. It clears the string buffer, thus preventing any leftover data from the previous execution from interfering with the current operation. This approach enhances the reliability and responsiveness of the voice-controlled robot .

Both forward and backward motor control are essential for maneuvering the robot effectively in various situations, such as navigating obstacles or reversing direction. In the code, these movements are implemented through separate functions that control the motor pins. The forward() function sets specific motor pins HIGH and others LOW to move the robot forward, while backward() reverses this configuration to make the robot move backward .

The setup() function initializes serial communication at a baud rate of 9600 for reading commands. It sets the pin modes for the motor pins and the ultrasonic sensor's trigger and echo pins. The motor pins are set to OUTPUT to control motor movements, and the echo pin is set to INPUT to receive signal duration for distance measurement .

You might also like