Build a Sound-Tracking Search and Rescue Robot About

Ask questions about projects relating to: aerodynamics or hydrodynamics, astronomy, chemistry, electricity, electronics, physics, or engineering.

Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators

Post Reply
Serkan444
Posts: 5
Joined: Mon Apr 21, 2025 7:52 am
Occupation: Student

Build a Sound-Tracking Search and Rescue Robot About

Post by Serkan444 »

[Admin note - project is here: https://www.sciencebuddies.org/science- ... king-robot
]

We created the "Build a Sound-Tracking Search and Rescue Robot" project on the website, but it does not respond to sounds. Is there a mistake in the code system or the circuit? Can you help?

The code is like this:
/*
* Starter code for making an autonomous Arduino car with the Science Buddies Bluebot kit
* For introductory video see https://youtu.be/qUo6hXSV1b8
* For circuit diagram and instructions see: https://www.sciencebuddies.org/science- ... riving-car
*
*
* *Important*: this code assumes you use the Arduino pins from the circuit diagram on the Science Buddies website.
* If you wire your circuit differently, you will need to adjust your code accordingly.
*
* This code is intended to help you get started building a self-driving car with the Science Buddies Bluebot kit.
* It assumes you are using the Bluebot chassis along with the two included infrared (IR) sensors, as well as
* an Arduino UNO and a PING ultrasonic distance sensor (purchased separately).
*
* The code will read the sensors and is set up to control the speed and direction of the robot's two motors
* using an H-bridge integrated circuit (the L293D, included in your Bluebot kit). However, *you* must write
* an algorithm that determines what the robot will do based on the sensor readings.
*/

// define pins for H bridge
// to make a motor spin forward, set the forward pin high and the backward pin low
// to make a motor spin backward, set the backward pin high and the forward pin low
// to stop a motor, set both pins low OR set both pins high, OR set the speed to zero (regardless of direction)
// to control motor speed, use the analogWrite() function for a speed control pin
const int Lmotor_forward_pin = 5; // left motor forward pin is Arduino pin 5
const int Lmotor_backward_pin = 4; // left motor backward pin is Arduino pin 4
const int Rmotor_forward_pin = 3; // right motor forward pin is Arduino pin 3
const int Rmotor_backward_pin = 2; // right motor backward pin is Arduino pin 2
const int Lmotor_speed_pin = 9; // left motor speed control pin is Arduino pin 9
const int Rmotor_speed_pin = 10; // right motor speed control pin is Arduino pin 10


// define pins for the ultrasonic sensor
const int triggerPin = 11;
const int echoPin = 7;

// define pins for LEDs
const int leftLEDpin = 6;
const int rightLEDpin = 8;

// define constants for comparing sensor readings
// you will need to calibrate these values for your setup - the sensors can be affected by ambient light
// the Arduino's analog input will read a value between 0-1023. Higher values mean *less* IR light is reflected (dark surface)
const int leftIRthreshold = 200; // threshold for left IR sensor to detect line
const int rightIRthreshold = 200; // threshold for right IR sensor to detect line
const int brakingDistance = 8; // desired automatic stopping distance in centimeters

// define other non-constant variables used in program
// all speeds must be valus between 0 (stopped) to 255 (full speed)
// tweak the speed values to change how fast your robot drives and turns straight (decrease them if it is overshooting turns)
int speedL = 0; // left motor speed
int speedR = 0; // right motor speed
int turnspeed1 = 140; // speed of outer wheel when turning
int turnspeed2 = 25; // speed of inner wheel when turning
int straightspeed = 130; // speed of both motors when driving straight
int leftIR = 0; // left IR sensor reading
int rightIR = 0; // right IR sensor reading
long distance = 0; // distance measured by the ultrasonic sensor in centimeters

void setup() { // setup code that only runs once

// set pin modes
pinMode(Lmotor_forward_pin, OUTPUT);
pinMode(Lmotor_backward_pin, OUTPUT);
pinMode(Rmotor_forward_pin, OUTPUT);
pinMode(Rmotor_backward_pin, OUTPUT);
pinMode(Lmotor_speed_pin, OUTPUT);
pinMode(Rmotor_speed_pin, OUTPUT);
pinMode(leftLEDpin, OUTPUT);
pinMode(rightLEDpin, OUTPUT);
pinMode(triggerPin,OUTPUT);
pinMode(echoPin,INPUT);

// set H bridge pins for robot to drive forward
digitalWrite(Lmotor_forward_pin, HIGH);
digitalWrite(Lmotor_backward_pin, LOW);
digitalWrite(Rmotor_forward_pin, HIGH);
digitalWrite(Rmotor_backward_pin, LOW);

// initialize serial communication for debugging
Serial.begin(9600);
}

void loop() { // code that loops forever

// get all three sensor readings
leftIR = analogRead(A0); // read left IR sensor
rightIR = analogRead(A1); // read right IR sensor
distance = readUltrasonic(); // read ultrasonic sensor and return distance in centimeters

/*
* ********** YOUR ALGORITHM HERE! **********
*
* Based on the sensor readings, write code to control the speed of the left and right motors (the speedL and speedR variables).
* Your robot should drive straight when both motors spin the same speed. It will turn left when the right motor spins faster than
* the left motor, and vice versa. It will stop when the speed of both motors is zero. For example, how can you make your robot:
*
* Turn in the correct direction when only one sensor detects a line?
* Stop if both sensors detect a line (e.g. a crosswalk)?
* Stop if there is an obstacle too close to the front of the robot?
*/


// set motor speeds
analogWrite(Lmotor_speed_pin, speedL);
analogWrite(Rmotor_speed_pin, speedR);

// print variables for debugging purposes - useful for calibrating the IR sensors
Serial.print("Left IR sensor: ");
Serial.print(leftIR);
Serial.print(" Right IR sensor: ");
Serial.print(rightIR);
Serial.print(" Ultrasonic sensor (cm): ");
Serial.print(distance);
Serial.print(" Left motor ");
Serial.print(speedL);
Serial.print(" Right motor: ");
Serial.println(speedR);

}

long readUltrasonic(){
// this code is based on the PING example code, modified for the HC-SR04. File -> Examples --> 06.Sensors --> Ping
long duration;

// The ultrasonic sensor is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(5);
digitalWrite(triggerPin, LOW);

// use pulseIn command to measure the duration of the echo pulse in microseconds
duration = pulseIn(echoPin, HIGH);
//Serial.print(duration);

// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return duration / 29 / 2;
}
amyCC
Site Admin
Posts: 398
Joined: Wed Apr 01, 2020 4:02 pm
Occupation: Moderator
Project Question: *
Project Due Date: *
Project Status: Not applicable

Re: Build a Sound-Tracking Search and Rescue Robot About

Post by amyCC »

Hi - You are working on the Sound-Tracking Search and Rescue Robot? https://www.sciencebuddies.org/science- ... king-robot

Can you please check to make sure you've downloaded the correct starter code. It's linked in step 5.
sound-tracking.png
sound-tracking.png (177.41 KiB) Viewed 2541 times

The code you've pasted into your post is from the Self-Driving Car. The commented section at the top of that code identifies it.

The commented section at the top of the Sound-Tracking Robot example code looks like this:
sound-tracking2.png
sound-tracking2.png (5.5 KiB) Viewed 2541 times
(The file name is: sound_following_robot_starter_code.ino)


If you look through the code, you'll see that there are variables to work with the microphones.

Important: If you didn't already, you'll also want to watch the Arduino tutorial video (shown in the introduction) for using the microphones.


After making sure you have the correct code and have watched the video, if you still have questions, post back here making sure to note any changes you've made to the code and including a photo of your circuit for the Experts to review.

Amy
Science Buddies
Serkan444
Posts: 5
Joined: Mon Apr 21, 2025 7:52 am
Occupation: Student

Re: Build a Sound-Tracking Search and Rescue Robot About

Post by Serkan444 »

Yes I am working on the Sound-Tracking Search and Rescue Robot
https://www.sciencebuddies.org/science- ... king-robot
I'm so sorry. I wrote the wrong code. The correct code is as follows:
/*
* Starter code for a sound-following robot that uses
* two microphones to drive toward a sound source.
*
*/

// declare constant variables for motor control pins
const int leftMotorForwardPin = 7;
const int leftMotorBackwardPin = 8;
const int rightMotorForwardPin = 4;
const int rightMotorBackwardPin = 2;
const int leftMotorSpeedPin = 9;
const int rightMotorSpeedPin = 6;

// constant variables for microphone pins
const int mic1pin = A0;
const int mic2pin = A1;

// other variables
const int sample_window = 3000; // amount of time to sample sound in milliseconds
int mic1; // microphone 1 value from analogRead (0-1023)
int mic2; // microphone 2 value from analogRead (0-1023)
int mic1_max; // maximum value recorded by mic1 during sample window
int mic1_min; // minimum value recorded by mic1 during sample window
int mic2_max; // maximum value recorded by mic2 during sample window
int mic2_min; // minimum value recorded by mic2 during sample window
int amp1; // largest amplitude of mic1 during sample window (max-min)
int amp2; // largest amplitude of mic2 during sample window (max-min)
int difference; // difference between the mic amplitudes
unsigned long start_time; // time in milliseconds at start of sample window (since program started)
unsigned long current_time; // current time in milliseconds (since program started)

void setup() { // setup code that only runs once
// set motor control pins as outputs
pinMode(leftMotorForwardPin,OUTPUT);
pinMode(leftMotorBackwardPin,OUTPUT);
pinMode(rightMotorForwardPin,OUTPUT);
pinMode(rightMotorBackwardPin,OUTPUT);
pinMode(leftMotorSpeedPin,OUTPUT);
pinMode(rightMotorSpeedPin,OUTPUT);

// set the H-bridge enable pins for speed control HIGH
// so motors will always run at full speed. You can
// replace these with analogWrite to make the motors
// run at different speeds. You can also put analogWrite
// commands in the various steering functions to make
// the speed variable.
digitalWrite(leftMotorSpeedPin,HIGH);
digitalWrite(rightMotorSpeedPin,HIGH);

Serial.begin(9600); // initialize serial communication for debugging
}

void loop() { // code that loops forever

// recommended: stop driving so motor noise does not
// affect sound sample
stopDriving;
delay(500);

// sample the microphones for sample_window milliseconds
// and calculate the maximum amplitude for each mic.
// you could also put this part of the code inside
// a function.

// reset max and min values
mic1_min = 1023;
mic1_max = 0;
mic2_min = 1023;
mic2_max = 0;

start_time = millis(); // start time for this sample window
current_time = millis();
Serial.println("Beginning sample");
while( millis() - start_time < sample_window){
// read mic values
mic1 = analogRead(mic1pin);
mic2 = analogRead(mic2pin);

// assign new min and max values
mic1_min = min(mic1, mic1_min);
mic1_max = max(mic1, mic1_max);
mic2_min = min(mic2, mic2_min);
mic2_max = max(mic2, mic2_max);

current_time = millis(); // update current time
}

// done sampling - calculate max amplitude for each mic
amp1 = mic1_max - mic1_min;
amp2 = mic2_max - mic2_min;
difference = amp1-amp2;

// Print amplitudes to serial monitor

Serial.print("Mic 1 amplitude: ");
Serial.print(amp1);
Serial.print(" | Mic 2 amplitude: ");
Serial.print(amp2);
Serial.print(" | Difference = ");
Serial.print(difference);

/*
* YOUR ALGORITHM HERE!
* What should the robot do based on the microphone
* amplitudes and the difference between them?
* You can make use of the pre-defined functions below
* to control the robot, or write your own.
*/
}

void driveForward(){ // set pins to make robot drive forward
digitalWrite(leftMotorForwardPin,HIGH);
digitalWrite(leftMotorBackwardPin,LOW);
digitalWrite(rightMotorForwardPin,HIGH);
digitalWrite(rightMotorBackwardPin,LOW);
}

void driveBackward(){ // set pins to make robot drive backward
digitalWrite(leftMotorForwardPin,LOW);
digitalWrite(leftMotorBackwardPin,HIGH);
digitalWrite(rightMotorForwardPin,LOW);
digitalWrite(rightMotorBackwardPin,HIGH);
}

void turnRight(){ // set pins to make robot turn right
digitalWrite(leftMotorForwardPin,HIGH);
digitalWrite(leftMotorBackwardPin,LOW);
digitalWrite(rightMotorForwardPin,LOW);
digitalWrite(rightMotorBackwardPin,HIGH);
}

void turnLeft(){ // set pins to make robot turn left
digitalWrite(leftMotorForwardPin,LOW);
digitalWrite(leftMotorBackwardPin,HIGH);
digitalWrite(rightMotorForwardPin,HIGH);
digitalWrite(rightMotorBackwardPin,LOW);
}

void stopDriving(){ // set pins to make robot stop
digitalWrite(leftMotorForwardPin,LOW);
digitalWrite(leftMotorBackwardPin,LOW);
digitalWrite(rightMotorForwardPin,LOW);
digitalWrite(rightMotorBackwardPin,LOW);
}
bfinio
Expert
Posts: 964
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Lead Staff Scientist, Science Buddies
Project Question: Expert
Project Due Date: n/a
Project Status: Not applicable

Re: Build a Sound-Tracking Search and Rescue Robot About

Post by bfinio »

Hi - please make sure you read through all the code and comments, particularly this part:

/*
* YOUR ALGORITHM HERE!
* What should the robot do based on the microphone
* amplitudes and the difference between them?
* You can make use of the pre-defined functions below
* to control the robot, or write your own.
*/

The sample code provided does not do anything to make the robot drive. It provides the code to get the sound amplitude for each microphone and the difference between them (the amp1, amp2, and difference variables), and functions that can make the robot drive forward, backward, turn left, and turn right. But it does not do anything to connect these things. It is your job to write code to make the robot react to the sound and drive accordingly.

If you are not sure how to do that, we recommend going through at least our first 7 Arduino tutorials: https://www.sciencebuddies.org/science- ... an-arduino and looking up tutorials about how if/else statements work with Arduino. This is an advanced project and previous experience with Arduino is recommended before attempting it, so you will need to do additional tutorials and learn about Arduino programming if this is your first Arduino project.
Serkan444
Posts: 5
Joined: Mon Apr 21, 2025 7:52 am
Occupation: Student

Re: Build a Sound-Tracking Search and Rescue Robot About

Post by Serkan444 »

I need the code to move the robot in the direction of the sound. I don't have any information on how to write the code for that part. I need to deliver this project in 10 days. Can you help me?
bfinio
Expert
Posts: 964
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Lead Staff Scientist, Science Buddies
Project Question: Expert
Project Due Date: n/a
Project Status: Not applicable

Re: Build a Sound-Tracking Search and Rescue Robot About

Post by bfinio »

Hi,

Since this is a science project we cannot just write the code for you. I would start by doing some research about how IF/ELSE statements work in computer code, for example by reading this page:

https://docs.arduino.cc/built-in-exampl ... nditional/

and as I suggested previously going through our first 7 Arduino tutorials. You need that background information in order to understand what you need to do for this project. After you have done that we can provide more help if you have more specific questions.
Serkan444
Posts: 5
Joined: Mon Apr 21, 2025 7:52 am
Occupation: Student

Re: Build a Sound-Tracking Search and Rescue Robot About

Post by Serkan444 »

Thank you.
I wrote the following code, but our robot still does not respond. Where am I making a mistake?
---------------------------
if(amp1 > amp2 + difference){

turnRight();
}
else if(amp2 > amp1 + difference){

turnRight();
}
else {

driveForward();
}

delay(100);
bfinio
Expert
Posts: 964
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Lead Staff Scientist, Science Buddies
Project Question: Expert
Project Due Date: n/a
Project Status: Not applicable

Re: Build a Sound-Tracking Search and Rescue Robot About

Post by bfinio »

Hi,

You have the right idea, but the issue is how you have the conditions for your IF statements. Since the code assigns

difference = amp1-amp2;

The two conditions in your IF statements:

(amp1 > amp2 + difference)
or
(amp2 > amp1 + difference)

will never be true.

For example, say that amp1 = 100 and amp2 = 75. Then

difference = amp1 - amp2 = 100 - 75 = 25.

Now plug in those numbers to evaluate your first IF condition:

amp1 > amp2 + difference

100 > 75 + 25 is not true. 100 is exactly EQUAL to 75 + 25, but not greater than it. So your code will always go to the "else" part if the if/else statement with the driveForward function, and (assuming your motors are wired properly), the robot will always just drive forward and not respond to sound.

Think about how you could change your if statements to just depend on the "difference" variable. If amp1 is greater than amp2, then "difference" will be positive. If amp1 is less than amp2, then "difference" will be negative. However, you probably don't just want to write code that does this:

if(difference>0){
turnRight();
}
else if(difference<0){
turnLeft();
else{
driveForward;
}

because then the robot will only drive forward if the difference is exactly zero, which will almost never happen in reality - there will always be small differences in the microphone readings, even in a mostly quiet room.

So instead of zero, you could enter a different number there - a positive number for the first condition (10, 20, etc) and a negative number for the next condition (-10, -20, etc) and experiment to see what number works best.

Hope that helps!
Serkan444
Posts: 5
Joined: Mon Apr 21, 2025 7:52 am
Occupation: Student

Re: Build a Sound-Tracking Search and Rescue Robot About

Post by Serkan444 »

Thank you very much.
Is there a discrepancy between the pins in the circuit assembly on the website and the pin sockets given in the sample code? Or am I reading it wrong?
Image
bfinio
Expert
Posts: 964
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Lead Staff Scientist, Science Buddies
Project Question: Expert
Project Due Date: n/a
Project Status: Not applicable

Re: Build a Sound-Tracking Search and Rescue Robot About

Post by bfinio »

Apologies for that, you are correct - thank you for catching that error! We will get the correct code file uploaded to the website ASAP. In the meantime, you can change the pin assignments in your code as follows:

const int leftMotorForwardPin = 4;
const int leftMotorBackwardPin = 5;
const int rightMotorForwardPin = 2;
const int rightMotorBackwardPin = 3;
const int leftMotorSpeedPin = 9;
const int rightMotorSpeedPin = 10;

If either motor spins backwards, you have three options - do ONE of the following things:
1) Reverse the red and black wires that go to the motor
2) Reverse the physical "forward" and "backward" wires on the breadboard (e.g. swap the wires between Arduino pins 2 and 3)
3) Swap the variable assignments in the code (e.g. set rightMotorForwardPin = 3 and rightMotorBackwardPin = 2)

Sorry again for the confusion!
Post Reply

Return to “Grades 6-8: Physical Science”