Robojax

Lesson 15: Sound Sensor Module (analog and digital output)

Lesson 15: Sound Sensor Module (analog and digital output)

Please select other codes for this lecture from the links below.

This is the Arduino code for Sound module for Arduino (basic) This video shows you how to turn an AC light ON with clap or your voice and Turn it off with another clap or shout. This code is basic version where digital output of the module is used. Please see other version where actual Analog sound values is used to control light.


 /*
 * Robojax Arduino Step By Step Course
 * Lesson 15: Sound Sensor via digital pin
 * This is the Arduino code for Sound module for Arduino (basic)
This video shows you how to turn an AC light ON 
with clap or your voice and Turn it off with another clap
or shout. This code is basic version where 
digital output of the module is used.
Please see other version where actual Analog sound values is 
used to control light.

  Please watch video instruction here https://youtu.be/INq8A3dLNzM
 This code is available at http://robojax.com/course1/?vid=lecture11
 
with over 100 lectures Free On  YouTube Watch it here http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339 
 * 
 // Written by Ahmad Shamshiri for Robojax Robojax.com  
 Written on Mar 23, 2019 in  Ajax, Ontario, Canada


 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
 
int soundInPin = 2;// connect output to Sound module DO
int relayPin = 8;// Connected to relay or buzzer (or LED)
int extra5V = 12;// define a pin for extra extra 5V

// do not change values bellow
int val = 0; // sound value from pin 2
int relayON = 0;//light status
int heard = 0;//sound heard status


void setup() {

//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338   
  Serial.begin(9600);
  pinMode(soundInPin, INPUT_PULLUP); 
  pinMode(relayPin, OUTPUT);
  pinMode(extra5V,OUTPUT);// set extra5V as output
  digitalWrite(extra5V,HIGH);// turn the extra5V pin HIGH to get 5V

}

void loop() {
//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  val = digitalRead(soundInPin);// read the sound pin

  if(val == HIGH && relayON == LOW){

    heard = 1-heard;// toggle the value of "heard" from HIGH to LOW or from LOW to HIGH
    delay(100);
  }    
//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  relayON = val;// save the value of pin 2

      if(heard == HIGH){
        Serial.println("Light ON");
        digitalWrite(relayPin, LOW); // turn relay ON
       
      }else{
        Serial.println("Light OFF");
        digitalWrite(relayPin, HIGH);// turn relay OFF
   
      }     


//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  delay(100);
}