Skip to Content
This is the Beta version of our new Learning Paths approach. Please email feedback.

Sound Sensor

sound_sensor.ino
/* Project: Smart Home Circuit: Arduino Ek R4 Sketch Description: This code reads the analog signal from a sound sensor. Read this for details on how Arduino compiles sketches distributed across multiple files. https://arduino.github.io/arduino-cli/1.0/sketch-build-process/#pre-processing All .ino files in the sketch folder are concatenated together, starting with the file that matches the folder name followed by the others in alphabetical order. The .cpp filename extension is then added to the resulting file. Author: STEMVentor Educonsulting This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs. This code is for educational purposes only and is not for production use. */ // LIBRARIES // PIN DEFINTIONS #define SOUND_SENSOR_PIN A0 // GLOBAL CONSTANTS // GLOBAL VARIABLES // INITIALIZE OBJECTS /* Libraries usually follow an object-oriented approach that requires * an instance of the class to call its methods. */ // LOCAL FUNCTIONS void soundSensorSetup() { pinMode(SOUND_SENSOR_PIN, INPUT); } // This will read both the digital and analog values. void readSoundSensorValue() { // The sound sensor returns an analog value between 0 and 1024. uint8_t sound_sensor_value = analogRead(SOUND_SENSOR_PIN); Serial.println("Sound Value: " + sound_sensor_value); // Convert the analog value to a % value using the map function. uint8_t sound_sensor_percent_value = map(sound_sensor_value, 0, 1024, 0, 100); Serial.println("Percent Value: " + sound_sensor_percent_value); }