How to Generate a Beep?



  • Hello, I am new to using these products. I have a Core2 and I am programming in Arduino. I just cannot figure out how to generate a beep with the speaker? Is this possible to do in Arduino? Should I get an M5 Stack 1 instead? I just need to generate a simple beep. Please let know if you have any input. Thank you.



  • @akaf24
    The arduino library examples folder has a beep demo fore Core2



  • Thanks for the reply. I cannot find any examples for this. I clicked through all the given examples and nothing came up when I searched "beep" or "tone" or "speaker". I looked through the library I have installed on arduino, and also this one that has more examples: https://github.com/m5stack/M5Core2



  • @akaf24

    Well.. so sorry... you are right!
    I was certain i took the code from there but i was wrong. It is on the main m5stack library
    check here:
    https://github.com/m5stack/M5Stack/tree/master/examples/Basics/Speaker

    If i dont remember wrong (again) it worked for me.

    Sorry again.



  • The original m5stack uses an analog speaker amplifier circuit. The core 2 uses digital so you'll need to use something like https://github.com/earlephilhower/ESP8266Audio in order to store files in flash/sd/internet and play them back over i2s.

    I've had great quality and volume coming through the speakers this way. I suggest you look into the github link I posted for examples, they have totally complete examples using internet sources but if you just read the examples on that github you can see how to adopt it for sd/spiffs files with ease.



  • @bricoms Thank you for the reply. I tried that example, but it did not work for me. I am using an M5 Stack core2 and that seems to be a library for just the Stack.



  • @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);