Sound sensor servo code

Hi,

Just after some help if I could. I bought a sound sensor for my Arduino uno that I have got to work with the code below. I have played around with the code with no success and was hoping someone here may be able to help me with my request if it’s possible to do.

I was hoping to use the one sound sensor LM383 to operate 2 servos with the same parameters is this something that’s possible?

Thanks Daniel

/*

#include <Servo.h>

#define SENSOR_PIN A0 // Arduino pin connected to sound sensor’s pin
#define SERVO_PIN 9 // Arduino pin connected to servo motor’s pin

Servo servo; // create servo object to control a servo

// variables will change:
int angle = 0; // the current angle of servo motor
int lastSoundState; // the previous state of sound sensor
int currentSoundState; // the current state of sound sensor

void setup() {
Serial.begin(9600); // initialize serial
pinMode(SENSOR_PIN, INPUT); // set arduino pin to input mode
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object

servo.write(angle);
currentSoundState = digitalRead(SENSOR_PIN);
}

void loop() {
lastSoundState = currentSoundState; // save the last state
currentSoundState = digitalRead(SENSOR_PIN); // read new state

if (lastSoundState == HIGH && currentSoundState == LOW) { // state change: HIGH → LOW
Serial.println(“The sound has been detected”);

// change angle of servo motor
if (angle == 0)
  angle = 90;
else if (angle == 90)
  angle = 0;

// control servo motor arccoding to the angle
servo.write(angle);

}
}

Hi Daniel.

I think your asking if you could use the same sound detecting module to control two separate motors?
Maybe something like this?

// UNTESTED CODE!!!

#include <Servo.h>

// YOUR DEFINES GO HERE!

Servo servoOne; // a servo object
Servo servoTwo; // a second servo object
int lastState;

void setup() {
lastState = digitalRead(SENSOR_PIN);
pinMode(SENSOR_PIN, INPUT); 
servoOne.attach(SERVO_PIN_ONE);
servoTwo.attach(SERVO_PIN_TWO);
}

void loop() {
bool soundDetected = (lastState != digitalRead(SENSOR_PIN));
lastState = digitalRead(SENSOR_PIN);
servoOne.write((int)soundDetected * 90); //Drive servo one by the sensor
servoTwo.write((int)soundDetected * 180); //Drive servo two by the same sensor
}

Yep that’s exactly what I was asking. Unfortunately I couldn’t get that code to work

As in the code I just wrote.
That’s fair, I wrote it blind.
I don’t have your sensors or motors so I can’t test anything.

Th concept I was trying to convey is that you can create two Servo objects and drive them with the same input. Is there something your struggling with in specific?

That’s fair and I appreciate your help. I’m new to coding so just trying to get the coding right is my issue. I’m still learning and playing with it so I’ll keep trying

1 Like

Oh! So sorry I shouldn’t have been so hand wavy.
If this is your first time coding can i suggest breaking it down into steps.

For example:
Put aside the sound sensor for a moment, have you managed to get a single motor working by itself? (for instance, could you turn it to 90 degrees)
Might look something like

#include <Servo.h> //A library: code someone wrote so you don't have to

#define SERVO_PIN 9 //Tell the device where the motor is plugged in.
Servo myMotor; //Create a servo and give it a name.

void setup() {
   servoOne.attach(SERVO_PIN); //Marry your new servo with it's pin
   servo.write(90); //set to 90 degrees //use the servo
}
1 Like

Do you mean this one?

What do you mean by ‘I couldn’t get that code to work’? Does it show an error when you compile, does it fail to upload, does it not move the servos, or move only one, or move both but not like you expected, or something else?

Also, show the way you have got everything connected together. And confirm that the power LED on the sensor is lit and that the sensor detect LED blinks when it hears a sound.

I would strongly recommend a decent delay at the end of each loop, because with a variable sound source the sensor is likely to trigger at a much higher rate than the servos can react.

2 Likes

Hi Jeff,

Sorry didn’t mean to sound so vague. I have attached a picture of the sensor I have and the diagram from where I have copied it from. I can get some actual photos as well of my board if needed. Only difference is that I’m running a sensor shield connected to 9v.

The original code I posted all works fine to run the sound sensor and servo. What I was asking is how do I go about changing the code to run 2 servos with the same code off the one sensor? If that’s possible.



Jonny’s code was the one I was referring to that I couldn’t get to work.

The setup that is important is the one that’s not working - in this case it would be the one with the second servo. There are several versions of the code available - post the one that you are currently having trouble with. Also, describe your problem - what you have done, what you expected to happen, and what actually happened. If you already had one servo working then I am sure the solution for a second one is simple.

1 Like

@Daniel209871 The way to approach this is in steps.
You have working one servo and sensor.
Try this with each servo to ensure they both work.

Remove the sensor and try with both servos, create two servo objects and try to drive each with the code. @Pixmusix has shown you how to do this.
The original code used Pin 9 which is one of the UNO PWM Pins. The servo driver used PWM to set the servo position. The only pins that can be used for PWM are 3,5,6,9,10,11 (see pic below).

The next step would be to add the sensor and try to control just one servo.
The final step would be to control both.

Your wiring diagram shows the servo using the UNO 5V pin.
If you are connecting to a USB socket the 5V would be provided by the USB, usually limited to 500mA.
If you are connecting to a plug pack power supply, the 5V would be supplied by the onboard regulator. This should not exceed 400mA.

Check the servo specs for how much current they draw.

Regards
Jim

EDIT: As @Jeff105671 suggested post any code here to help us help you, cheers

2 Likes

Hi Daniel

Just connect the 2 servo signal wires to the same servo signal output.
Cheers Bob
PS don’t forget to connect all the grounds together.
Also the Arduino 5V might not be enough to drive 2 servos. You might need a separate servo supply.

1 Like

Hi Daniel,

To echo the rest of the comments it would be great to see your code. If you want the servos to do the same thing you can do as @Robert93820 said and wire them off the same pin. If you want them moving separately that’s where you will need to create 2 instances of servo

#include <Servo.h>

Servo servo1; // create servo object to control a servo
Servo servo2;

void setup()
{
    servo1.attach(9);  // attaches the servo on pin 9 to the servo object
    servo2.attach(10);
}

Then have it do something in the loop. Once you can get both servos moving then start integrating the sound sensor