How to make like megaphone with Arduino?

How can I write a code that will simultaneously receive the data from the microphone sensor as sound from the speaker? So, I want to make something like a megaphone, but when I write the code, the sound only comes out as a crackling and incomprehensible sound.

İ use this code:

const int microphonePin = A6;
const int speakerPin = 5;     

void setup() {
  pinMode(speakerPin, OUTPUT); 
}

void loop() {
  int sensorValue = analogRead(microphonePin); 
  analogWrite(speakerPin, sensorValue / 4);    
}

Sorry I cannot follow what you are asking. Using a microphone to listen to something it is sending to a speaker is prone to feedback (squealing is what you will hear). Post a simple block diagram explaining what you want to do. Realize the Arduino such as the UNO is not very good at working with sound.

I want to do this to transmit the sound from the sound sensor to the speaker simultaneously, like a megaphone.

Then You better use a simple audio amplifier. No need for coding.

1 Like

I think the sound sensor is broken and I'll replace it.

I replaced the sensor and the problem was not solved. I am getting variable frequencies from the sensor depending on the sound with this code.

const int sound_sensor = A6;
const int button = A0;
int distance;

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  
  distance = analogRead(button);

  if (distance > 700) {
    int soundValue = 0;
    for (int i = 0; i < 32; i++) {
      soundValue += analogRead(sound_sensor);
    }
    soundValue = soundValue / 32;
    Serial.println(soundValue);
  }
  
  delay(5);
}

What you're trying to do there isn't going to make very recognizable sound.

Hook the microphone output to an audio amplifier input. Hook the amplifier output to the speaker.

There's nothing there for the Arduino to do unless you want it to turn the thing on and off. Not every electronic gadget needs a microcontroller. Many work better without them. A megaphone is a great example.

2 Likes

My sensör is this:

Doesn't the sensor amplify the sound? I don't have a circuit for the amplifier and I think Arduino can do this because it is a simple circuit.

Just because it's a simple circuit doesn't mean Arduino can help. An amplifier is an analog circuit. The Arduino doesn't do analog things very fast or with very much resolution.

You can build a simple amplifier circuit with a transistor

If it amplifies it enough to drive the speaker then hook it directly to the speaker. I doubt that is the case.

Either way, the Arduino doesn't make a good amplifier. It doesn't even make a so-so amplifier.

I don't want to make a megaphone, I just want to hear the sound through my in-ear headphones at the same time, so I don't want to increase the volume

And yet this entire thread you've spoken of nothing but a megaphone.

If you don't even know what you want to build then I don't know how to help you except.to tell you that the Arduino is terrible for audio.

Good luck with your project.

3 Likes

An Arduino cannot safely drive a speaker directly.

It may work for a little while before the Arduino output pin burns out, and what you've already heard is about what to expect.

1 Like

There is a PCM.h library that gives very good sound output, please do not write if you are not going to give an idea.

Darkmour,
I've done some tests on your code from post #1, to try and explain why it doesn't work.

I modified your code slightly, as follows:

  • I used pin12 to monitor the frequency at which the ADC was taking samples.
  • I used direct port manipulation to change the state of pin 12, this hardly affects the sampling frequency.
  • Pin 12 goes high immediately before the ADC starts its conversion, and low again when the conversion has finished.
  • I changed the analogue pin to A0. This was simply for convenience, and has no effect on the results.

Here is the modified code:

const int microphonePin = A0;
const int speakerPin = 5;
const int monitorPin = 12;

void setup() {
  pinMode(speakerPin, OUTPUT);
  pinMode(monitorPin, OUTPUT);
}

void loop() {
  PORTB = B00010000;           // take pin12 high
  int sensorValue = analogRead(microphonePin);
  PORTB =B00000000;           // take pin 12 low
  analogWrite(speakerPin, sensorValue / 4);
}                                                                           

I used a function generator to feed a 2kHz sinewave into the analogue pin A0.
I used an oscilloscope to monitor the signals going to and from the Arduino Uno R3.

Here is an oscilloscope trace showing what is happening:
The oscilloscope trace shows:-

  • Channel 1 - yellow trace - sinewave input to A0.
  • Channel 2 - red trace - PWM output from pin 5.
  • Channel 4 - green trace - output from pin 12.

The red trace shows the PWM output of pin 5. The PWM frequency is around 980Hz.

The green trace shows that the ADC is taking samples at a frequency of 8.9kHz.
When the green trace goes low the code is doing it's analogWrite().

This should change the pulse width of the red trace, but if the frequency of the PWM is only 980Hz, then you can't change the pulse width at a frequency of 8.9kHz.

The PWM signal just changes pulse width when it is able to, and so it is not a representation of the input signal.

1 Like

Which you are not using. The PCM.h library functions at a much higher frequency than analogWrite(), which makes all the difference in the world.

Do have fun, though. Arduinos are cheap and easy to replace.

Thank you, I will try other ways then

I gave you the best and simplest idea. You need very little to make it work. Why do you insist that there must be an Arduino involved.

Oh well, I suppose you must know a lot more about this than me. I'm only some guy who has done what you want and it worked. If you don't want to do it in a way that works then keep trying things that dont. No worries. I'll just mute the thread and you won't hear from me again. Best of luck.

1 Like

After showing in post #15 that the PWM could not change fast enough to respond to a 2kHz signal, I did some further tests with lower frequency inputs.

Using a 20Hz signal, the PWM pulse width does in fact change fast enough to respond to the input.

I have included a fourth trace - Channel 3 (blue) which is the output of a low pass filter I have added to pin 5 output.
The filter consists of a 10kΩ resistor and 1µF capacitor.
1st_Order_Lowpass_Filter_RC

The blue trace is a good representation of the yellow trace.

You can see that the duty cycle of the red trace varying from a low value to a high value.