LED lights maybe not detecting sound input (Arduino Uno board).

조회 수: 5 (최근 30일)
Wyatt
Wyatt 2023년 12월 8일
답변: Deep 2024년 9월 12일
I'm struggling with a part of my project. I'm using a microphone sensor AVR (manufacturer DQZSY, ASIN B08HQY9J5Y) and made a program to function much like how a microphone's sensitivity does. To determine how loud any sound is, I used 5 LED lights (the more lights on, the louder it is). All 5 LED lights are wired to pins D2 to D6.
I think it's reading the voltage instead, though. If I move the wires in the arduino board (A5 and D0), more LEDs light up, like a broken cellphone charger. If I speak into the microphone sensor, it doesn't change. How can I make it so that my LEDs light up when they hear me/any noise in the environment?
Solutions I've tried: using different pins, changing the microphone sensor's sensitivity, changing the resistors used for the LEDs (440 Ohms to 330 Ohms), changing the pause as needed.
My code is relatively short, so I can post it all below!
a = arduino();
% Define pin configurations
soundAnalogPin = 'A5'; % Analog pin for the sound sensor/microphone
ledPins = [2, 3, 4, 5, 6]; % Digital pins for the LEDs
% Set up LED pins
for i = 1:length(ledPins)
configurePin(a, sprintf('D%d', ledPins(i)), 'DigitalOutput');
writeDigitalPin(a, sprintf('D%d', ledPins(i)), 0); % Initialize LEDs to off
end
% Main loop
while true
% Read the analog signal from the sound sensor
sensorValue = readVoltage(a, soundAnalogPin);
% Map the analog signal to the number of LEDs
numLedsToLight = round(interp1([0, 5], [0, length(ledPins)], sensorValue));
% Display the number of LEDs based on the sound level
displayLeds(a, ledPins, numLedsToLight);
% Display the sensor value in the MATLAB Command Window
disp(sensorValue);
pause(0.1); % Adjust the pause as needed
end
clear a;
clear;
% Function to control LEDs
function displayLeds(a, ledPins, num)
for i = 1:length(ledPins)
writeDigitalPin(a, sprintf('D%d', ledPins(i)), i <= num);
end
end

답변 (1개)

Deep
Deep 2024년 9월 12일
Hi Wyatt,
As per my understanding, your sound-meter prototype is not providing the desired outputs and the microphone readings appear to lack robustness. I suspect that the microphone sensor might not be outputting the full range of 5 volts as your code assumes, with the “interp1” function using [0, 5] as arguments. Your sensor may only be outputting readings up to 1-2 volts.
To address this, I recommend plotting the values returned by “readVoltage” at different sound levels. This will help you visually determine appropriate voltage thresholds for lighting up the LEDs. Here is a small MATLAB snippet to help you plot these values while your prototype is running:
figure;
h = animatedline;
axis([0, 100, 0, 5]);
counter = 0;
while true
sensorValue = readVoltage(a, soundAnalogPin);
counter = counter + 1;
addpoints(h, counter, sensorValue);
drawnow;
pause(0.1);
end
This plot will animate the sensor readings in real-time as your script is running, you can read more about this MATLAB capability here: https://www.mathworks.com/help/matlab/animation-1.html
For more robust readings, you can try averaging multiple sensor readings. From personal experience, using around 30 samples on an Arduino Uno can stabilize the readings. This approach might help with the unexpected LED behaviour when moving the jumper wires. Averaging the readings can filter out transient noise and provide more consistent results.
Hope this helps!

카테고리

Help CenterFile Exchange에서 Instrument Control Toolbox에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by