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
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.
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.