10 years, 4 months ago.  This question has been closed. Reason: Off Topic

how to get a beep tone out from AnalogOut (p18)???

I am getting a distance from my ultrasonic sensor and I have created a code that's works fine and it beeps with the speaker in the mbed application board PwmOut spkr(p26) and I am trying to convert the sound and play it in hearphones instead of speakers through AnalogoUT(P18) but I didn't know what I have to change in code to hear the sound from hearphones ??

I am using mbed Lpc1768

1 Answer

10 years, 4 months ago.

For a pure tone you want to output a sine wave on the analog out.

However you can probably get away with something like a triangular wave which is far simpler to calculate.

This code below compiles but I've not actually tested that it makes the expected sounds. It should at least give you a good starting point. Dropping the sample rate number will reduce CPU time, increasing it will give cleaner sounds. Changing the frequency number will change the tone of the beep.

#include "mbed.h"

Ticker tick;
AnalogOut aOut(p18);

const int sampleRate = 16000; // 16kHz output
const int beepFrequency = 1000; // frequency of tone

const int samplesPerCycle = sampleRate / beepFrequency ;

void soundOut()
{
    static int cycleCount = 0;

// triangular wave at required frequency
    float value = 2.0*(float)cycleCount / (float)(samplesPerCycle-1); // value from 0 to 2;
    if (value > 1)                // if over 1 start to ramp back down
        value = 2.0 - value;

    // For a pure tone use the line below for a sine wave but it will need more CPU time.
// float value = 0.5*sin(3.14159*((float)cycleCount / (float)(samplesPerCycle-1))/180) + 0.5;

    aOut = value;

    cycleCount++;
    if (cycleCount == samplesPerCycle)
        cycleCount  = 0;

}

void startSound()
{
    tick.attach_us(soundOut, 1000000/sampleRate);
}

void stopSound()
{
    tick.detach();
}


int main() {
    while (1) {
        startSound();
        wait(5);
        stopSound();
        wait(5);
    }
    return 1;
}

Hello Andy Im using the following code but its still not working?? what I have to adjust in the code to get a proper tone that is like a normal beep sound that used in the car sensor for parking?

AnalogIn input(p15); PwmOut spkr(p26); AnalogOut speaker(p18); int distance;

void interrupt(){ distance = input*512*2.54; }

int main() {

device.baud(9600); pc.baud(9600);

int tone=0; tick.attach(&interrupt,0.1);

while(1) {

switch (distance/100){ case 0 : tone =1000; if the distance is 1m or less speaker = !speaker; 25% duty cycle - mid volume break; case 1 : tone =0; if the distance is between 2m < distance > 1m speaker =0.25; 25% duty cycle - mid volume break;

}

pc.printf("Distance:%dcm Tone:%dHz\n",distance,tone); % is printed as dicemal and m= meter

posted by Mazin AL-Shidhani 11 Jul 2014

Please use <<code>> and <</code>> around any code so that it is readable.

For how to make it work see my original answer, it looks like you've completely ignored the code I gave you to generate the sound waveform.

For a buzzer you can just use a PWM, for an audio output you can't just output a constant value or toggle between two values of analog output. You need to output the waveform of the sound you want to generate.

Realistically that means a ticker triggering at least 4,000 times a second and ideally a lot faster (16kHz in the example) that outputs a constantly changing value.

The original code above is always full volume, to change the volume you would add the following line before setting the analogOut value.

  value = value*volume + 0.5*(1-volume); // volume is a float between 0 and 1. Keep the output centered on 0.5
  aOut = value;

Note that a volume of 0.5 would reduce the size of the output wave to 1/2 of the maximum however the human ear is very non-linear, halving the volume will not sound that much different, you'll need to divide it by about 5 to get a significant reduction in perceived volume.

A couple of other notes on the code: To play safe distance should be volatile since its value is changed in an interrupt. Without that the compiler may optimize things and your main loop may not see the value ever change. And ideally you should have a default: in your switch statement.

posted by Andy A 11 Jul 2014