Seeed Xiao nrf52840 tone library

Hi,

I am using Seeed Xiao BLE nrf52840 MCU and trying to play tone via Arduino tone library, but its loudness is pretty low. I looked at the ToneAC Arduino library but it doesn’t seem to be compatible with Seeed Xiao.

Is there any other alternative I can use?

Have you tried using their code? What error messages have you gotten?

@Citric Below are the errors I am getting. Library’s git page mentions that it is only compatible with ATmega microcontrollers. I am new to Arduino development and it would be awesome if someone can navigate me.

XXXXX/xiao/libraries/toneAC/src/toneAC.cpp: In function 'void toneAC(long unsigned int, uint8_t, long unsigned int, uint8_t)':
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:23:5: error: 'TIMSK1' was not declared in this scope
   23 |     TIMSK1 |= _BV(OCIE1A);            // Activate the timer interrupt.
      |     ^~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:23:19: error: 'OCIE1A' was not declared in this scope
   23 |     TIMSK1 |= _BV(OCIE1A);            // Activate the timer interrupt.
      |                   ^~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:23:15: error: '_BV' was not declared in this scope
   23 |     TIMSK1 |= _BV(OCIE1A);            // Activate the timer interrupt.
      |               ^~~
In file included from XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:8:
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp: In function 'void toneAC_playNote(long unsigned int, uint8_t)':
XXXXX/xiao/libraries/toneAC/src/toneAC.h:106:23: error: 'DDRB' was not declared in this scope
  106 |     #define PWMT1DREG DDRB
      |                       ^~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:31:3: note: in expansion of macro 'PWMT1DREG'
   31 |   PWMT1DREG |= _BV(PWMT1AMASK) | _BV(PWMT1BMASK); // Set timer 1 PWM pins to OUTPUT (because analogWrite does it too).
      |   ^~~~~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.h:104:24: error: 'DDB1' was not declared in this scope; did you mean 'B1'?
  104 |     #define PWMT1AMASK DDB1
      |                        ^~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:31:20: note: in expansion of macro 'PWMT1AMASK'
   31 |   PWMT1DREG |= _BV(PWMT1AMASK) | _BV(PWMT1BMASK); // Set timer 1 PWM pins to OUTPUT (because analogWrite does it too).
      |                    ^~~~~~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:31:16: error: '_BV' was not declared in this scope
   31 |   PWMT1DREG |= _BV(PWMT1AMASK) | _BV(PWMT1BMASK); // Set timer 1 PWM pins to OUTPUT (because analogWrite does it too).
      |                ^~~
In file included from XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:8:
XXXXX/xiao/libraries/toneAC/src/toneAC.h:105:24: error: 'DDB2' was not declared in this scope; did you mean 'D2'?
  105 |     #define PWMT1BMASK DDB2
      |                        ^~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:31:38: note: in expansion of macro 'PWMT1BMASK'
   31 |   PWMT1DREG |= _BV(PWMT1AMASK) | _BV(PWMT1BMASK); // Set timer 1 PWM pins to OUTPUT (because analogWrite does it too).
      |                                      ^~~~~~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:33:27: error: 'CS10' was not declared in this scope
   33 |   uint8_t prescaler = _BV(CS10);                  // Try using prescaler 1 first.
      |                           ^~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:36:21: error: 'CS12' was not declared in this scope
   36 |     prescaler = _BV(CS12);                        // Set the 256 prescaler bit.
      |                     ^~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:40:3: error: 'ICR1' was not declared in this scope; did you mean 'IR1'?
   40 |   ICR1   = top;                                     // Set the top.
      |   ^~~~
      |   IR1
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:41:7: error: 'TCNT1' was not declared in this scope
   41 |   if (TCNT1 > top) TCNT1 = top;                     // Counter over the top, put within range.
      |       ^~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:42:3: error: 'TCCR1B' was not declared in this scope
   42 |   TCCR1B = _BV(WGM13)  | prescaler;                 // Set PWM, phase and frequency corrected (top=ICR1) and prescaler.
      |   ^~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:42:16: error: 'WGM13' was not declared in this scope
   42 |   TCCR1B = _BV(WGM13)  | prescaler;                 // Set PWM, phase and frequency corrected (top=ICR1) and prescaler.
      |                ^~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:43:3: error: 'OCR1A' was not declared in this scope
   43 |   OCR1A  = OCR1B = top / _tAC_volume[volume - 1];   // Calculate & set the duty cycle (volume).
      |   ^~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:43:12: error: 'OCR1B' was not declared in this scope
   43 |   OCR1A  = OCR1B = top / _tAC_volume[volume - 1];   // Calculate & set the duty cycle (volume).
      |            ^~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:44:3: error: 'TCCR1A' was not declared in this scope
   44 |   TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1B0); // Inverted/non-inverted mode (AC).
      |   ^~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:44:16: error: 'COM1A1' was not declared in this scope
   44 |   TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1B0); // Inverted/non-inverted mode (AC).
      |                ^~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:44:30: error: 'COM1B1' was not declared in this scope
   44 |   TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1B0); // Inverted/non-inverted mode (AC).
      |                              ^~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:44:44: error: 'COM1B0' was not declared in this scope
   44 |   TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1B0); // Inverted/non-inverted mode (AC).
      |                                            ^~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp: In function 'void noToneAC()':
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:48:3: error: 'TIMSK1' was not declared in this scope
   48 |   TIMSK1 &= ~_BV(OCIE1A);        // Remove the timer interrupt.
      |   ^~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:48:18: error: 'OCIE1A' was not declared in this scope
   48 |   TIMSK1 &= ~_BV(OCIE1A);        // Remove the timer interrupt.
      |                  ^~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:48:14: error: '_BV' was not declared in this scope
   48 |   TIMSK1 &= ~_BV(OCIE1A);        // Remove the timer interrupt.
      |              ^~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:49:3: error: 'TCCR1B' was not declared in this scope
   49 |   TCCR1B  = _BV(CS11);           // Default clock prescaler of 8.
      |   ^~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:49:17: error: 'CS11' was not declared in this scope
   49 |   TCCR1B  = _BV(CS11);           // Default clock prescaler of 8.
      |                 ^~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:50:3: error: 'TCCR1A' was not declared in this scope
   50 |   TCCR1A  = _BV(WGM10);          // Set to defaults so PWM can work like normal (PWM, phase corrected, 8bit).
      |   ^~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:50:17: error: 'WGM10' was not declared in this scope
   50 |   TCCR1A  = _BV(WGM10);          // Set to defaults so PWM can work like normal (PWM, phase corrected, 8bit).
      |                 ^~~~~
In file included from XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:8:
XXXXX/xiao/libraries/toneAC/src/toneAC.h:107:23: error: 'PORTB' was not declared in this scope
  107 |     #define PWMT1PORT PORTB
      |                       ^~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:51:3: note: in expansion of macro 'PWMT1PORT'
   51 |   PWMT1PORT &= ~_BV(PWMT1AMASK); // Set timer 1 PWM pins to LOW.
      |   ^~~~~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.h:104:24: error: 'DDB1' was not declared in this scope; did you mean 'B1'?
  104 |     #define PWMT1AMASK DDB1
      |                        ^~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:51:21: note: in expansion of macro 'PWMT1AMASK'
   51 |   PWMT1PORT &= ~_BV(PWMT1AMASK); // Set timer 1 PWM pins to LOW.
      |                     ^~~~~~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.h:105:24: error: 'DDB2' was not declared in this scope; did you mean 'D2'?
  105 |     #define PWMT1BMASK DDB2
      |                        ^~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:52:21: note: in expansion of macro 'PWMT1BMASK'
   52 |   PWMT1PORT &= ~_BV(PWMT1BMASK); // Other timer 1 PWM pin also to LOW.
      |                     ^~~~~~~~~~
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp: At global scope:
XXXXX/xiao/libraries/toneAC/src/toneAC.cpp:55:4: error: expected constructor, destructor, or type conversion before '(' token
   55 | ISR(TIMER1_COMPA_vect) {                 // Timer interrupt vector.
      |    ^

Compilation error: exit status 1

It appears to be a compatibility issue, and XIAO BLE does not define these elements, making them unusable.

Thanks @Citric for the response. Do you have any suggestions on other ways to achieve louder buzzer?

My advice is to solve this problem by getting an “extra loud” buzzer. A hardware solution is often the most effective.

Getting an “extra loud” buzzer is not a proper solution to this problem. If you only create half the sine wave (+ and GND instead of + and -), the sound quality is really bad, apart from half the power, and apart from damaging the speaker earlier.
So, what we really need is something like toneAC for nrf52.