Tring to build a sound activated servo but code won't work

I am trying to use a sound sensor to activate a servo but I can't figure out what is wrong with my code
I am using an Arduino Nano
And this sound sensor
Jocestyle High Sensitivity Sound Microphone Sensor Detection Module Fit for Arduino
Here is my code:

#include <Servo.h> 
int  sensorAnalogPin = A0;    // Select the Arduino input pin to accept the Sound Sensor's analog output 
int  sensorDigitalPin = 3;    // Select the Arduino input pin to accept the Sound Sensor's digital output
int  analogValue = 0;         // Define variable to store the analog value coming from the Sound Sensor
int  digitalValue;            // Define variable to store the digital value coming from the Sound Senso
int servoPin = 9;                               // When D0 from the Sound Sensor (connnected to pin 7 on the
Servo Servo1;                         // Arduino) sends High (voltage present), L will light. In practice, you
                              // should see LED13 on the Arduino blink when LED2 on the Sensor is 100% lit.
                              

void setup()
{
  Serial.begin(9600);               // The IDE settings for Serial Monitor/Plotter (preferred) must match this speed
  pinMode(sensorDigitalPin,INPUT);  // Define pin 7 as an input port, to accept digital input
   Servo1.attach(servoPin);         // Define LED13 as an output port, to indicate digital trigger reached
}

void loop(){
  analogValue = analogRead(sensorAnalogPin); // Read the value of the analog interface A0 assigned to digitalValue 
  digitalValue=digitalRead(sensorDigitalPin); // Read the value of the digital interface 7 assigned to digitalValue 
  Serial.println(analogValue); // Send the analog value to the serial transmit interface
  
  if(digitalValue>300)      // When the Sound Sensor sends signla, via voltage present, light LED13 (L)
  {
      Servo1.write(0);
  }
  else
  {
     Servo1.write(90);
  }
  
  delay(50);                  // Slight pause so that we don't overwhelm the serial interface
}

We need a description of what happens wen you run this code, not simply it does not work. What happens and what do you expect to happen?

We also need to see a schematic of how you have wired things up ( not a physical layout diagram ) and a link to where you bought the sound sensor.

We can only know what you tell us and code is only half the story when things go wrong.

Your code does not even attempt to do anything with the analogue sound reading. What happens when you just switch pin 3 by hand instead of using the sound module?

when I run this code the servo twitches a lot but the sound sensor didn't effect anything, but when I opened the serial plotter you can see that it spiked when ever I yelled
I got the sound sensor in a kit, and I am not sure how to upload a picture onto this

You simply drag in the photo to the reply box.

That suggest you have not connected the ground from your sensor to the ground of the Arduino. Which we should be able to see once we see the schematic.

https://gm1.ggpht.com/JS4BHbBajeVAdhkfk00F9N6bSTt8FpPul8UEUtMmrqVEw-uHPvSu6YYVGzoOSS1EQmYVbOIr3wHhed1odOUpNCUykRlOjG3KSOxcEyTz3dnQpaSkujwv3TXbV-A7Ydod-9I4XxDyKMSW5yTDMyoqSgGJ8v43oKYoGI1VQi-BO5OpkPfxk5NO1HEsYlYPqQFf06DQPEXce-ckHGY92Yi5XuNlk2abpEvIvoWsxgWqXznE6eTpKW-jW03tQeJJKtWIc1CsuTPj1dfG-dntnz-0T7wa5fuTGywFchOj9MTT-Aq_-wyJl6xhntY-4kMF_jxXSg0-RoyDaXKo7_UjRRZj4sRnJPek21gPKQklkhuCyL3hhsKcnyS5SDoUNee6n_wT3ePM5CcUSHuJgj4AZSxpKiu0ACweHCCz6Qyfb2o7s8Z6Y--5tf41iw2xLTOrKcW53gxvFHZq17gyl7jGmql35mDt6Xq-sHZ9dre8FyybkgxSNn-YhjbCOEjLekwe9QSPqgGLYmyZtnbhwmBTRhNJu_rNT60Q2jhS3hAR5lPxismsMr4J-IbPuOlfpfivdo2iBHPKRD6DrCj25wisZtUgSi_BQzTn5ydFOwC5dcTaLkXCN2VHRRmzumGdPNdrJY5OTnFg1hkvfRqxC_xn7huLDmtU5SASFat_jr76p0y4axuhqg3DlTvFG46vV6T-ZdLHD9QtpIEfF7Nevl89YFihuf_Zd2Fao894ZDizLGO7=s0-l75-ft-l75-ft

hope this is good

what did you mean about not using the analogue sound reading and how would I fix that

When I said about dragging in an image I meant an image that was stored on your computer, not one from some external site.

Still waiting to see your schematic and an explanation of what you are expecting from your code.

You take a measurement and then you do not use that measurement to affect anything.

As we don’t know what you want to do with that information it is hard to say how to fix it.

I want my code to make my servo turn 0 degrees when ever the sound is over 300

Thanks for the diagram, a bit scruffy but much better than nothing.

A digital signal can be 0 or 1 it can never reach 300. You want to be looking at the analogue reading here not the digital one.

You should not be trying to run a servo off the Arduino’s 5V output, yes there are tutorials showing this but it is bad practice and can cause interference problems.

so how would i go about fixing this

Thank you so much it is working now

For information about powering a servo from an external supply see
About Servos

Are you giving the servo enough time to finish moving? delay(50) is 1/20th of a second.

oops I forgot 2 zeros

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.