This tutorial shows how to spin a DC MOTOR using ESP32, L293D motor driver, and SOUND SENSOR(CZN-15E).
TABLE OF CONTENT
PREREQUISITES :
We will require the following components for spinning the DC motor using the sound sensor.REQUIRED COMPONENTS:
*ESP32 development board.
*Sound sensor (CZN-15E).
*L293D motor driver.
*DC motor.
*Connecting wires.
We will use Arduino IDE to program our ESP32 development board. You should have the latest version of Arduino IDE. Additionally, you also need to install the ESP32 plugin.
CZN-15E SOUND SENSOR:
In this fully automated world, we want to operate things through sound for our easiness. To make this automation, we need a sound sensor. Basically, the sound is a waveform of energy which is produced by the form of mechanical vibration. The type of sound determines its frequency.
CZN-15E SOUND SENSOR
This CZN-15E Electret microphone-based module can be used to detect sound intensities using an adjustable potentiometer to adjust the trigger level. Its output is binary, the output goes high if sound intensity level crosses preset trigger level.
L293D MOTOR DRIVER:
L293D MOTOR DRIVER
L293D motor driver module is a medium power motor driver perfect for driving DC motors and stepper motors. It uses the popular L293 motor driver IC. It can drive motors up to 12V with a total DC current of up to 600mA.
CIRCUIT CONNECTIONS:
PIN CONFIGURATION:
This sensor includes three pins and they are,
*Pin 1(VCC): 3.3V DC to 5V DC.
*Pin 2(GND): This is a ground pin.
*Pin 3(OUT): This is an output pin; we will connect ADC pin 34 with AO. You can use any appropriate analog pin of ESP32.
ESP32 | SOUND SENSOR |
---|---|
VIN | VCC |
GROUND | GROUND |
GPIO34 | AO |
Since we’re just driving one motor for this tutorial, we won’t be using all the motor driver’s pins.
*Startup by connecting input pin1 and input pin2 with ESP32 GPIO26 and GPIO27.
*Next give VS pin to 5V, which supplies power to the motor and ground to ground of ESP32.
*Connect both the motor pins to motor1 pins of the L293D motor driver, if we use two DC motors then connect the second motor to motor2 pins of the L293D motor driver.
PROGRAM:#define Sound_sensor 34
#define motorPin1 26
#define motorPin2 27
const int sampleWindow = 50; //sample window width in ms(50mS=20Hz)
const int dbMax=70;
unsigned int volume;
void setup()
{
pinMode(Sound_sensor, INPUT);
pinMode(motorPin1,OUTPUT);
pinMode(motorPin2,OUTPUT);
Serial.begin(115200);
}
void loop()
{
unsigned long startMillis= millis(); //start of sample window
int peakToPeak = 0; //peak-to-peak level
unsigned int signalMax = 0; //minimum value
unsigned int signalMin = 4096; //maximum value
while (millis() - startMillis < sampleWindow) //collect data for 50ms
{
volume = analogRead(Sound_sensor); //get reading from microphone
if (volume < signalMin) { if (volume > signalMax)
{
signalMax = volume;
}
}
}
peakToPeak = signalMax;
int db = (peakToPeak/10);
Serial.print("Db = ");
Serial.println(db);
if(db>dbMax){
digitalWrite(motorPin1,HIGH);
digitalWrite(motorPin2,LOW);
}
else{
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,LOW);
}
}
Download the code directly from Github
Upload the code to your ESP32. Make sure you have the right board and COM port selected. In this tutorial shows you how to spin a DC motor based on sound frequency using an ESP32, L293D motor driver and a sound sensor.