How to Generate a Beep?



  • @jesse-schein haha wow, there's certainly a lot there. Thanks for this info. I just need to generate a simple beep once every few seconds, but it seems like I can only generate sound from from a wav or other file type? I was hoping there would be a tone() function, but I guess i'll just have to go through a few extra steps to get what I need.
    Edit: I just tried uploading some of these examples and they would not generate sound. I'll have to take a closer look at what's going on



  • @akaf24 You can try look at this:
    https://github.com/m5stack/M5Atom/tree/master/examples/ATOM_BASE/ATOM_SPK/PlayRawPCM
    It use the same chip as Core2 and play beeps ;-) so you migth be able to just change the i2S pins and the you go ;-)



  • Just did a test, it will/can play a Beep om CORE2, just change Atom to CORE2 include, change the i2s pins and make one line a comment, the line not supported by CORE2.

    And yes, it is far to much code to get a Beep ;-)



  • thank you for the reply. I think I'm stuck on pin assignment. So on the back of my core2, it says BCLK is 12, LRCK is 0 and DATA is 0. But the example code also has "CONFIG_I2S_DATA_IN_PIN" and "SPEAKER_I2S_NUMBER" but I am not sure what to assign these as.



  • @akaf24 DATA should be 2 not 0, you dont need to change the last two items.
    ( "CONFIG_I2S_DATA_IN_PIN" and "SPEAKER_I2S_NUMBER" )



  • @agreedk thank you for reply. I keep getting a compile error. A beep isn't totally needed in my project so i'm just going to leave it out.



  • I haven't tried this but in the .h file there is something called Speaker.setBeep . That might just work.

    in the M5Stack.h file there is this.

    Speaker:
        M5.Speaker.tone(uint32_t freq);
        M5.Speaker.tone(freq, time);
        M5.Speaker.beep();
        M5.Speaker.setBeep(uint16_t frequency, uint16_t duration);
        M5.Speaker.mute();
    

    And if you open up the example "FactoryTest" there is a section in the code that uses dacWrite to the speaker pin.

    void startupLogo() {
    static uint8_t brightness, pre_brightness;
    uint32_t length = strlen((char*)m5stack_startup_music);
    M5.Lcd.setBrightness(0);
    M5.Lcd.pushImage(0, 0, 320, 240, (uint16_t *)gImage_logoM5);
    for(int i=0; i<length; i++) {
    dacWrite(SPEAKER_PIN, m5stack_startup_music[i]>>2);
    delayMicroseconds(40);

    brightness = (i/157);
    if(pre_brightness != brightness) {
    pre_brightness = brightness;
    M5.Lcd.setBrightness(brightness);
    }
    }

    The startup_music.c is found on another tab when you open the example code.

    // SAMPLES: 40000,
    // BITSPERSAMPLE: 8,
    // CHANNELS: 1,
    // SAMPLERATE: 20000,
    // NORMALIZED: FALSE,
    const unsigned char m5stack_startup_music[] = {

    Lots of HEX here*



  • @AgreeDK Thanks a million for your solution, it does work perfectly with the Core2 !
    Just a shame there's no simple beep method in the core2 lib :(

    @Shift1313 as far as I understand, the .Speaker method you are referring to is not part of the M5Core lib, it is working with the Core but not with the Core2



  • 0_1639904411062_Screenshot 2021-12-19 at 08.59.45.png

    in UIFlow you use this block to make a bleep



  • @ajb2k3 Thanks for the tip. Unfortunately it is not that easy when not using UIFlow.
    I am using VSCode + PIO with the Arduino framework



  • I could not here any sound on my system by using the method described above by @AgreeDK. I figured out that you have to switch on the power supply for the integrated amplifier. This is controlled (like all other things needing power inside the Core2) by the APX192. To activate the Power supply use :

    M5.Axp.SetSpkEnable(1);



  • I too had this need with my Core 2 Stack, so I wrote a few little routines that make sounds based on the M5Stack Atom Speaker code examples.

    The code is at,
    https://github.com/Hagtronics/M5Stack-Core2/tree/main/Sound