Arduino siren sound generator code

This is a simple Arduino code to generate a wailing audio tone. The code can be used to generate tones similar to a police siren, ambulance siren, warning siren, etc.
arduino tone generator, arduino siren
This circuit works using the tone function of the Arduino. The tone() function can generate a square wave with the specified frequency at a particular pin.

To generate a wailing sound two for loop are used in the program to modulate the frequency; one (for loop) for to increment from lower to a higher frequency and another one(for loop) to decrement from higher to a lower frequency. In the given program the output frequency range is between 700-800 HZ.

A variety of sound effects can be generated from this siren by precisely adjusting the frequency range and time delay values in the code.

Arduino siren code

int i=0;
void setup() {
  pinMode(9, OUTPUT);
}

void loop() {
  for(i=700;i<800;i++){
  tone(9,i);
  delay(15);
  }
  for(i=800;i>700;i--){
  tone(9,i);
  delay(15);
  }
  }

In the setup() function, the pin 9 is declared as an output.

In the loop() function, the code uses two for loops. The first for loop starts with the value of i set to 700, and increases it by 1 in each iteration until it reaches 800. Inside the loop, the tone() function is called with the pin 9 and the current value of i as arguments. This will cause the pin 9 to output a tone with a frequency equal to the value of i. The delay() function is called with a value of 15 milliseconds, which will cause the loop to pause for 15 milliseconds before moving on to the next iteration.

The second for loop starts with the value of i set to 800, and decreases it by 1 in each iteration until it reaches 700. Inside the loop, the tone() function is called with the pin 9 and the current value of i as arguments, producing a tone with a frequency equal to the value of i. The delay() function is called with a value of 15 milliseconds, which will cause the loop to pause for 15 milliseconds before moving on to the next iteration.

The effect of this code will be a continuous change of tone on pin 9 in a range of 700 to 800 hertz. The delay of 15 milliseconds between each iteration of the for loop will cause the tone to change smoothly.

You may also like...

5 Responses

  1. Priya says:

    Is this a Piezzo Transducer??

  2. eddy says:

    Moi j’ai toujours des message en rouge avec les programmes copie coller.?

  3. KenF says:

    How did you increase the tone volume? What would be a good way to increase the volume of the tone and not increase the tone’s pitch?

    • admin says:

      You can use an audio amplifier module, an audio amplifier circuit using op-amps or IC’s like LM386, or a simple transistor amplifier circuit (input impedance match needs to be considered).

Leave a Reply

Your email address will not be published. Required fields are marked *