LAB 1 - The Artemis Board

Jan 27th, 2022

Blink it Up

Make the LED blink

In this part, I set the delay between turn the LED on and off to be 100ms. Thus the delay for a whole loop is 200ms, and the blink frequency is 5Hz.

Serial

Read and Write

In this part, the board is able to read the input I give to it and echo the same output to me through the serial port.

AnalogRead

Temp Sensor

In this part, the board continuously outputs temperature detected by its temp sensor through the monitoring panel. And we can see the temperature went up when I was heating it using a hair dryer.

MicrophoneOutput

PDM

In this part, the microphone on the board recorded the voice it recieved, and returned the frequency of the loudest sound source using Fourier Transform. I also tested the frequency of my whistle which is approximately 1200~1700.

Whistle Detection

Turn on the LED when you whistle, and off otherwise

Using the data from the previous step, I know the frequency of my whistle is about 1200~1700. I first initialized digital pin LED_BUILTIN as an output by adding 'pinMode(LED_BUILTIN, OUTPUT);' to void setup( ). Then I added a judgment statement to make the LED turn on when it encounters a sound whose frequency is above 1200, and turn off otherwise.

Code Appendix

Modified from Example

                int stage = 0;

                void setup()
                {
                // initialize digital pin LED_BUILTIN as an output.
                pinMode(LED_BUILTIN, OUTPUT);
                
                Serial.begin(115200);
                Serial.println("SparkFun PDM Example");

                if (myPDM.begin() == false) // Turn on PDM with default settings, start interrupts
                {
                    Serial.println("PDM Init failed. Are you sure these pins are PDM capable?");
                    while (1)
                    ;
                }
                Serial.println("PDM Initialized");

                printPDMConfig();

                void loop()
                {
                if (myPDM.available())
                {
                    myPDM.getData(pdmDataBuffer, pdmDataBufferSize);

                    printLoudest();
                }

                // Go to Deep Sleep until the PDM ISR or other ISR wakes us.
                am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP);
                }

                //*****************************************************************************
                //
                // Analyze and print frequency data.
                //
                //*****************************************************************************
                void printLoudest(void)
                {
                float fMaxValue;
                uint32_t ui32MaxIndex;
                int16_t *pi16PDMData = (int16_t *)pdmDataBuffer;
                uint32_t ui32LoudestFrequency;

                //
                // Convert the PDM samples to floats, and arrange them in the format
                // required by the FFT function.
                //
                for (uint32_t i = 0; i < pdmDataBufferSize; i++)
                {
                    if (PRINT_PDM_DATA)
                    {
                    Serial.printf("%d\n", pi16PDMData[i]);
                    }

                    g_fPDMTimeDomain[2 * i] = pi16PDMData[i] / 1.0;
                    g_fPDMTimeDomain[2 * i + 1] = 0.0;
                }

                if (PRINT_PDM_DATA)
                {
                    Serial.printf("END\n");
                }

                //
                // Perform the FFT.
                //
                arm_cfft_radix4_instance_f32 S;
                arm_cfft_radix4_init_f32(&S, pdmDataBufferSize, 0, 1);
                arm_cfft_radix4_f32(&S, g_fPDMTimeDomain);
                arm_cmplx_mag_f32(g_fPDMTimeDomain, g_fPDMMagnitudes, pdmDataBufferSize);

                if (PRINT_FFT_DATA)
                {
                    for (uint32_t i = 0; i < pdmDataBufferSize / 2; i++)
                    {
                    Serial.printf("%f\n", g_fPDMMagnitudes[i]);
                    }

                    Serial.printf("END\n");
                }

                //
                // Find the frequency bin with the largest magnitude.
                //
                arm_max_f32(g_fPDMMagnitudes, pdmDataBufferSize / 2, &fMaxValue, &ui32MaxIndex);

                ui32LoudestFrequency = (sampleFreq * ui32MaxIndex) / pdmDataBufferSize;

                if (PRINT_FFT_DATA)
                {
                    Serial.printf("Loudest frequency bin: %d\n", ui32MaxIndex);
                }
                
                //modified
                if(ui32LoudestFrequency>1200 && stage==0){
                    digitalWrite(LED_BUILTIN, HIGH);
                    stage = 1;
                    }
                else if(ui32LoudestFrequency<=1200 && stage==1){
                    digitalWrite(LED_BUILTIN, LOW);
                    stage = 0;
                    }

                Serial.printf("Loudest frequency: %d         \n", ui32LoudestFrequency);

                }