Code to Make a Sound with a Specific Pitch

This packet uses a buzzer attached to two I/O pins, called BUZZERRED and BUZZERBLK. The
sketch defines a function, makesound(), to

makesound() takes 2 arguments, a frequency (float freq) and a duration (float duration).
From these data, it calculates:

half-period (in microseconds) = (1000000/frequency)/2

number-of-vibrations = duration * frequency

In both cases, a floating point result is calculated from the input data. But the
subsequent statements that use these data require integer inputs. So in each
calculation, the resulting float is cast into an int by preceding the
expression with the operator (int).

Make 3 Sounds

This packet calls a user-defined function makesound() 3 times to make 3 notes,
each for a different duration:

Note Frequency Duration Half-Period # Loops
Middle C 261.6 Hz 1 s 1911 262
Middle D, 1.122x higher than Middle C 293.7 Hz 2 s 1702 587
Middle E, 1.122x higher than Middle D 311.1 Hz 0.5 s 1607 156

After that sequence of sounds, the sketch pauses a couple of seconds before repeating the sequence.


void setup(void) {
    pinMode(BUZZERHI, OUTPUT);
    digitalWrite(BUZZERHI, LOW);
    digitalWrite(BUZZERLOW, LOW);

void loop() {
    float freq_c = 261.6;
    float freq_d = 293.7;
    float freq_e = 311.1;

    makesound(freq_c, 1);
    makesound(freq_d, 2);
    makesound(freq_e, 0.5);

void makesound(float freq, float duration) {
    int p = (int)((1000000/freq)/2);
    int nloops = (int)(duration * freq);
    for (int i=0; i<nloops; i++) {
        digitalWrite(BUZZERHI, HIGH);
        digitalWrite(BUZZERLOW, LOW);
        digitalWrite(BUZZERHI, LOW);
        digitalWrite(BUZZERLOW, HIGH);