Go Down

Topic: sound trigger relay on arduino nano (Read 448 times) previous topic - next topic

Soleen

HI.

How make arduino code? (and wiring diagram would be really helpful)

i need relay sound triggered ON and on another sound trigger relay OFF, parts i have:

arduino nano 328

sound sensor KY-038

2 relay module ( 2PH63091A )


many thanks

groundFungus

Google "sound sensor KY-038 arduino tutorial" to see how to use the sound sensor.  Some of the tutorials will show how to turn an LED on and off using the sound sensor.  Controlling the state of an LED is much like controlling a relay.  Google "arduino relay tutorial" to read about relays.

There will be sample code that you can use to write your code.  Give it a try and if you have trouble, post the code and describe what it does and how that is different from what you want to do.  Also a schematic of your setup.

This is not a difficult project

Soleen

i can make relay light go On or off after sound trigger but relay itself does not switches any ideas why?

here code


Arduino_Sound_Sensor_Relay.ino
const int relayPin = 11;
const int soundPin = 7;

int soundVal = 0;
int relayToggle = LOW;
int previousState = HIGH;

void setup ()
{
 pinMode (relayPin, OUTPUT);
 pinMode (soundPin, INPUT);
}

void loop ()
{
 soundVal = digitalRead(soundPin);
 if (soundVal == LOW && previousState==HIGH)
 {
   relayToggle=!relayToggle;
   digitalWrite (relayPin, relayToggle);
 }
 previousState=soundVal;
}

groundFungus

I need to know about your relay and how it is wired to the Arduino.  A datasheet for the relay would help.  Can you post a photo and/or a schematic of the project?

Soleen


Soleen

also if code is correct i would like to know how add 4 sound sensors for 4 relays control. where to change code.

Soleen

hmm... u don't know how to .... ok never mind

Soleen

HI,

i have 1 woking scetch with sound sensor and relay on digi pins 3 and 4

how to copy code for all available digital pins ( make 5 sound trigger relay sensors run from 1 arduino nano) namy thanks


int soundSensor = 3;
int relay = 4;
int claps = 0;
long detectionSpanInitial = 0;
long detectionSpan = 0;
boolean lightState = false;
 
void setup() {
  pinMode(soundSensor, INPUT);
  pinMode(relay, OUTPUT);
}
 
void loop() {
 
  int sensorState = digitalRead(soundSensor);
 
  if (sensorState == 0)
  {
    if (claps == 0)
    {
      detectionSpanInitial = detectionSpan = millis();
      claps++;
    }
    else if (claps > 0 && millis()-detectionSpan >= 50)
    {
      detectionSpan = millis();
      claps++;
    }
  }
 
  if (millis()-detectionSpanInitial >= 400)
  {
    if (claps == 2)
    {
      if (!lightState)
        {
          lightState = true;
          digitalWrite(relay, HIGH);
        }
        else if (lightState)
        {
          lightState = false;
          digitalWrite(relay, LOW);
        }
    }
    claps = 0;
  }
}

PaulRB

Please read the forum guide in the sticky post, then modify your post above and put in the missing code tags.

PaulRB

The short answer is to use arrays.

But please clarify your idea. Will there be 5 sound sensors, or one sound sensor and 5 "sounds" (different numbers of claps)?

johnwasser

#10
Jul 20, 2019, 03:20 pm Last Edit: Jul 20, 2019, 03:21 pm by johnwasser
Arrays and loops.
Code: [Select]
const byte Channels = 5;
const byte SoundSensorPins[Channels] = {3,4,5,6,7};
const byte RelayPins[Channels] = {2,8,9,10,11};

void setup()
{
  for (byte i=0; i<Channels; i++)
  {
    pinMode(SoundSensorPins[i], INPUT);
    pinMode(RelayPins[i], OUTPUT);
  }
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Soleen

Good day,

i need full sketch for arduino nano: sound sensor on trigger (about 4 sec beep ) switches relay ON and keeps it on until another identical 4 sec beep is received.

 if no sound trigger received withing 2 hours when relay ON, switch relay off ( for safety - optional)

apreciate if it could work with 4-8 sound sensors and 4-8 relays total (1 sound sensor control just 1 relay)

many thanks

meltDown

<settles back with popcorn>


Soleen

do you know how to do it ?


KASSIMSAMJI

do you know how to do it ?


wait for the AWOL to throw you the sketch please
Expert To Be: Machine Learning And Computer/Machine Vision (ML/CV/MV)

Go Up