Im trying to build an arduino based noise traffic light for classrooms
green=low noise
yellow=moderate noise
red=high noise- a buzzer turns on when it is red
im using an rgb led and a sound sensor
My rgb led wont light up and my sound sensor doesn't seem to work
Here is my code:
// Pins for RGB LED
const int redPin = 9; // Red pin of the RGB LED
const int greenPin = 10; // Green pin of the RGB LED
const int bluePin = 11; // Blue pin of the RGB LED
// Sound sensor pins
const int analogPin = A0; // Analog output of the sound sensor
const int digitalPin = 2; // Digital output of the sound sensor (optional)
// Buzzer pin
const int buzzerPin = 3; // Pin connected to the buzzer
// Noise thresholds (tune these values based on your environment)
const int greenThreshold = 30; // Low noise level
const int yellowThreshold = 35; // Moderate noise level
const int redThreshold = 40; // High noise level
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(digitalPin, INPUT); // Optional: Use digital output to detect peaks
Serial.begin(9600); // For debugging
}
void loop() {
// Read sound levels from the analog pin
int soundLevel = analogRead(analogPin);
// Debugging: Print sound level to Serial Monitor
Serial.println(soundLevel);
// Set RGB LED color based on sound level
if (soundLevel < greenThreshold) {
setColor(0, 30, 0); // Green for acceptable noise
digitalWrite (greenPin, HIGH);
noTone(buzzerPin); // Turn off buzzer
} else if (soundLevel < yellowThreshold) {
setColor(35, 35, 0); // Yellow for moderate noise
noTone(buzzerPin); // Turn off buzzer
} else if (soundLevel >= redThreshold) {
setColor(40, 0, 0); // Red for high noise
tone(buzzerPin, 1000); // Turn on buzzer (1kHz tone)
}
}
// Function to set the RGB LED color
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
Can you provide a wiring diagram and a little more about the components that you are using? Also, your code should be in code tags. You can easily edit the original post to fix that.
In the IDE click on Edit then Copy For Forum, that will copy your code. Then come here and just do a paste.
A plain copy and paste does not work right.
RGBLEDs have a "common" pin, which can be "common cathode" or "common anode"... determine this polarity of your RGBLED is using GROUND, a 500 ohm resistor, and VCC.
It will not matter if tou get the sound sensor to work because the project as a whole will not work.
How do I know? Well there has been a couple of dozens of this sort of project and none have been successful. It turns out that a simple volume based sensors do not respond to what is perceived.
You forgot to mention which sound module you have. If you have a cheap one without pre-amp then the returned sound levels are very low. And it likely has some bias voltage. And you have to sample sound for a period of time. Just taking a snapshot with analogRead won't work.
Try this sketch, and see which values you get. Only use the analogue out of your module and connect it to A0. I tested it with a bare mic capsule and a 6k8 pull up resistor, because I didn't have a mic module. Got 1-2 making loud noises, and 3-10 with very loud noises.
Leo..
void setup() {
Serial.begin(115200);
}
void loop() {
int negativePeak = 1023, positivePeak = 0; // reset
unsigned long prevMillis = millis(); // mark start
while(millis() - prevMillis < 1000){ // measure for one second
int rawValue = analogRead(A0);
if (rawValue > positivePeak) positivePeak = rawValue;
if (rawValue < negativePeak) negativePeak = rawValue;
}
int level = positivePeak - negativePeak;
Serial.println(level);
}
Edit: Not happy with the sensitivity, so I changed the mic's pull up resistor, powered from the cleaner 3.3volt pin, to a value so the mic capsule's bias voltage dropped below 1volt.
22k got me to ~0.75volt.
OP can do the same by adjusting the pot on the mic module to 0.75volt on A0.
Again, assuming a cheap one, with a blue pot.
That change enabled me to use 1.1volt Aref, with a higher sensitivity.
Now speaking normal got me peak A/D values of about 10, and loud noises in the hundreds.
The updated code for that:
void setup() {
Serial.begin(115200);
analogReference(INTERNAL);
}
void loop() {
int negativePeak = 1023, positivePeak = 0; // reset
unsigned long prevMillis = millis(); // mark start
while(millis() - prevMillis < 250){ // measuring time
int rawValue = analogRead(A0);
if (rawValue > positivePeak) positivePeak = rawValue;
if (rawValue < negativePeak) negativePeak = rawValue;
}
int level = positivePeak - negativePeak;
Serial.println(level);
}
Sound level print with a healthy dose of Breaking Benjamin at 8m distance
I now removed the rgb led and used normal leds instead, how ever it still wont light up
// Pins for LEDs
const int redLED = 9; // Red LED pin
const int yellowLED = 10; // Yellow LED pin
const int greenLED = 11; // Green LED pin
// Pin for buzzer
const int buzzer = 12; // Buzzer pin
// Pin for sound sensor
const int analogPin = A0; // Analog output of the sound sensor
// Noise thresholds (adjust based on your environment)
const int greenThreshold = 300; // Low noise level
const int yellowThreshold = 500; // Moderate noise level
const int redThreshold = 700; // High noise level
void setup() {
// Set LED pins as OUTPUT
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
// Set buzzer pin as OUTPUT
pinMode(buzzer, OUTPUT);
Serial.begin(9600); // For debugging
}
void loop() {
// Read the sound level from the sound sensor
int soundLevel = analogRead(analogPin);
// Debugging: Print sound level to Serial Monitor
Serial.println(soundLevel);
// Control LEDs and buzzer based on sound level
if (soundLevel < greenThreshold) {
// Low noise: Green LED ON, others OFF
digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(buzzer, LOW); // Buzzer OFF
} else if (soundLevel < yellowThreshold) {
// Moderate noise: Yellow LED ON, others OFF
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(redLED, LOW);
digitalWrite(buzzer, LOW); // Buzzer OFF
} else {
// High noise: Red LED ON, others OFF, Buzzer ON
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, HIGH);
digitalWrite(buzzer, HIGH); // Buzzer ON
}
delay(100); // Small delay for stability
}
It sounds like a hardware or wiring fault. Have you got the LEDs the correct way round? Is there a series resistor for each LED? If so what value are you using?
Yes the leds are in the correct position and i'm using a 220 ohm resistor
only the red led is functioning but it keeps blinking
I also adjusted the threshold
```cpp
// Noise thresholds (adjust based on your environment)
const int greenThreshold = 5; // Low noise level
const int yellowThreshold = 10; // Moderate noise level
const int redThreshold = 15; // High noise level
There are two types of buzzer, active and passive.
An active buzzer will make a sound when you just connect a voltage, to test if you have one of these simply connect the buzzer between 5V and ground and it will make a sound.
If all you here is a click then it is a passive buzzer and in order for it to make a noise it needs driving with a rapid on/off voltage from one of the pins. This is best done by using the tone library. Note that the sound of the buzzer will be picked up by your microphone and so might not be able to turn the tone off if the low sound level is reached.
Anyway with a passive buzzer make sure the tone is turned off in the low sound state.