C

This code base on language C have the propose to get information about this sensor and display it.

First part that we need to write on our code is the libraries that we are going to use. In this case we are going to use

BCDS_BSP_Mic_AKU340.h

/************************************************************
*   AcousticSensor.c
************************************************************/

/* system header files */
#include 
/* additional interface header files */
#include "FreeRTOS.h"
#include "timers.h"

#include "KnowledgebaseAcousticSensor.h"
#include "BCDS_CmdProcessor.h"
#include "BCDS_Assert.h"

Currently, the API does not provide a built-in function to convert these values from their byte representation into the representation of the output voltage of the AKU340 or the measured sound pressure. To calculate the sound pressure level, we use the next part of the code:

** First, the sound pressure to output voltage conversion ratio of the AKU340 is calculated using the math.c library into a linear value from its logarithmic data sheet specification. Afterwards, the ADC conversion ratio is calculated by with the maximal input voltage of the ADC and the 4096 representable states. Additionally the ratio is multiplied by 1000 for a presentation in milli volts.

Then, the function calcSoundPressure() is used to calculate the sound pressure representation in milli pascal by using the previous calculated conversion ratios for the ADC and the AKU340 and the passed measured raw value read from the acoustic sensor.

/* own header files */
#include "BCDS_BSP_Mic_AKU340.h"
#include "math.h"

const float aku340ConversionRatio = pow(10,(-38/20));

const float adcConversionRatio = 2.5*1000/4096;

// Function to calculate the sound pressure representation from the read acoustic sensor values
float calcSoundPressure(uint32_t acousticRawValue){
    return ((acousticRawValue/aku340ConversionRatio)*adcConversionRatio);
}

BSP_Mic_AKU340_Sense() is used and data can be read as follows from the acoustic sensor.

// Timer function to read out the acoustic sensor
void readAcousticSensor(xTimerHandle xTimer){

    BCDS_UNUSED(xTimer);

    int32_t microphoneSample;

    microphoneSample = BSP_Mic_AKU340_Sense();

    printf("Sound pressure: %f \r\n", calcSoundPressure(microphoneSample));
}

// Function to initialize the acoustic sensor

void initAcousticSensor(void){
    BSP_Mic_AKU340_Connect();
    BSP_Mic_AKU340_Enable();
}

Finally we declared the main part of the program where we need to call all the function that we previously defined. Also define the timers.

Acoustic sensor requires a timer or operating task, which runs on a very short sampling period, such as 1 to 10 ms approximately to produce appropriate results, when measuring ambient noise.

void appInitSystem(void * CmdProcessorHandle, uint32_t param2)
{
    if (CmdProcessorHandle == NULL)
    {
        printf("Command processor handle is null \n\r");
        assert(false);
    }
    BCDS_UNUSED(param2);

    initAcousticSensor();

    uint32_t timerBlockTime = UINT32_MAX;
    uint32_t timerAutoReloadOn = UINT32_C(1);
    uint32_t timerPeriod = UINT32_C(100) / portTICK_RATE_MS;

    xTimerHandle TimerHandle = xTimerCreate(
                (const char* const) "TimerFunc",
                timerPeriod,
                timerAutoReloadOn,
                (void*) NULL,
                readAcousticSensor);

    xTimerStart(TimerHandle, timerBlockTime);
}

Last updated

Was this helpful?