Tuesday, August 2, 2016

Buzzer Test with Edison

This is a rapid sketch where I make sound using a buzzer connected to the PWM pin 9 of the Intel Edison. In the part “Test 5” of the code, the sound is like a siren that I am going to add to my WiFi_Car.

The buzzer is connected to the pin SERVO_2 (which is internally connected to the Intel Edison pin 9) through a 680 ohms resistor.
 
Testing the Buzzer with Intel Edison.

/* Info:https://developer.mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/
 *
 * Using PWM hardware to generate a simple audio tone
 *
 * The simplest way to generate an audio signal to play on the speaker is to use a hardware PWM output.
 * Set the PWM period to 1/frequency of the desired sound. The PWM duty cycle is set to 0.5.
 * A lower duty cycle setting produces lower volume, but keep in mind that since square waves or
 * pulses are generated there will also be a lot of higher frequency harmonics produced.
 * Not quite a pure tone, but then again most instruments also generate some harmonics.
 * The advantage of using the PWM hardware is that it takes minimal memory and no processor
 * time to output an audio tone. If only a beep, click, or siren alarm sound is needed this may be the appropriate choice.
*/
//Makefile: g++ -lmraa -o SPEAKER_EDISON SPEAKER_EDISON.CPP

#include <iostream>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <mraa.h>
#include <mraa/pwm.h>

#define ON  1
#define OFF 0

using namespace std;

sig_atomic_t volatile isrunning = 1;
void sig_handler(int signum);

int main()
{
       signal(SIGINT, &sig_handler);
       float    duty = 0.5;     //50% duty cycle - max volume
       mraa_init();
       mraa_pwm_context PWM_9_pin;
       PWM_9_pin = mraa_pwm_init(9);
       if (PWM_9_pin == NULL)            cout << "Error in PWM_9_pin...\n";

             int i;
             cout << "test 1" << endl;
             // generate a 500Hz tone using PWM hardware output
             mraa_pwm_period_us(PWM_9_pin, 2000);//500hz period, 2ms
             mraa_pwm_write(PWM_9_pin, duty);//50% duty cycle - max volume     
             mraa_pwm_enable(PWM_9_pin, ON);// turn on audio
             sleep(3);
             mraa_pwm_enable(PWM_9_pin, OFF);// turn off audio
             sleep(2);//little pause

             cout << "test 2:";
             // generate a short 150Hz tone using PWM hardware output
             // something like this can be used for a button click effect for feedback
             duty = 0.25;
             for (i = 0; i<10; i++)
             {           
                    mraa_pwm_period_us(PWM_9_pin, 6);//6ms period
                    mraa_pwm_write(PWM_9_pin, duty);//25% duty cycle - mid range volume
                    mraa_pwm_enable(PWM_9_pin, ON);// turn on audio
                    usleep(20000);//20ms
                    mraa_pwm_enable(PWM_9_pin, OFF);// turn off audio
                    usleep(50000);//500ms
                    cout << " ";
                    cout <<i;
             }

             sleep(2);//little pause

             cout << "\ntest 3" << endl;
             duty = 0.25;
             // sweep up in frequency by changing the PWM period
             for (i = 0; i<8000; i = i + 100)
             {
                    mraa_pwm_period_us(PWM_9_pin, (1.0 / float(i)) * 1000);
                    mraa_pwm_enable(PWM_9_pin, ON);// turn on audio
                    usleep(10000);
             }
             mraa_pwm_enable(PWM_9_pin, OFF);
             sleep(2);

             cout << "test 4"<<endl;
             // two tone police siren effect -  two periods or two frequencies
             // increase volume - by changing the PWM duty cycle
             mraa_pwm_enable(PWM_9_pin, ON);// turn on audio
             for (i = 0; i < 26; i = i + 2)
             {
                    mraa_pwm_period_us(PWM_9_pin, 1031);//(1.0 / 969.0)
                    duty = (float(i) / 50.0);
                    mraa_pwm_write(PWM_9_pin, duty);
                    usleep(500000);
                    mraa_pwm_period_us(PWM_9_pin, 1250);//(1.0 / 800.0)
                    usleep(500000);
             }
             // decrease volume
             for (i = 25; i >= 0; i = i - 2)
             {
                    mraa_pwm_period_us(PWM_9_pin, 1031);
                    duty = (float(i) / 50.0);
                    mraa_pwm_write(PWM_9_pin, duty);
                    usleep(500000);
                    mraa_pwm_period_us(PWM_9_pin, 1250);
                    usleep(500000);
             }
             mraa_pwm_enable(PWM_9_pin, OFF);
             sleep(2);
      
       mraa_pwm_enable(PWM_9_pin, ON);
       cout << "test 5" << endl;
       while (isrunning)
       {
             mraa_pwm_period_us(PWM_9_pin, 1031);//(1.0 / 969.0)
             duty = 0.05;
             mraa_pwm_write(PWM_9_pin, duty);
             usleep(500000);
             mraa_pwm_period_us(PWM_9_pin, 1250);//(1.0 / 800.0)
             usleep(500000);
       }
       mraa_pwm_write(PWM_9_pin, 0);
       mraa_pwm_enable(PWM_9_pin, OFF);
       cout << "\nClosing application..." << endl;
       sleep(1);
       return MRAA_SUCCESS;
}

void sig_handler(int signum)
{
       if (signum == SIGINT) isrunning = 0;
}