Compilation error: data section exceeds available space in board

I'm a newbie, I decided to write a melody with a buzzer using the Robson Couto code. But the melody turned out to be too big and does not fit into dynamic memory(( how to fix this?

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define REST      0
int tempo = 128;
int buzzer = 11;
int melody[] = {

  NOTE_B3,4,NOTE_B3,4,NOTE_B3,-8,NOTE_D4,16,REST,8,NOTE_C4,8,
  NOTE_B3,4,NOTE_B3,8,NOTE_E3,8,NOTE_B3,-8,NOTE_D4,8,REST,16,NOTE_C4,8,
  NOTE_B3,4,NOTE_B3,4,NOTE_B3,-8,NOTE_D4,16,REST,8,NOTE_C4,8,
  NOTE_B3,8,NOTE_B3,8,NOTE_E3,8,NOTE_B3,8,NOTE_B3,16,NOTE_E3,16,NOTE_E3,16,NOTE_E3,16,NOTE_B3,4,

  NOTE_B3,4,NOTE_B3,4,NOTE_B3,-8,NOTE_D4,16,REST,8,NOTE_C4,8,
  NOTE_B3,4,NOTE_B3,8,NOTE_E3,8,NOTE_B3,-8,NOTE_D4,8,REST,16,NOTE_C4,8,
  NOTE_B3,4,NOTE_B3,4,NOTE_B3,-8,NOTE_D4,16,REST,8,NOTE_C4,8,
  NOTE_B3,8,NOTE_B3,8,NOTE_E3,8,NOTE_B3,8,NOTE_B3,16,NOTE_E3,16,NOTE_E3,16,NOTE_E3,16,NOTE_B3,4,
  
  NOTE_B4,4,NOTE_B4,4,NOTE_B4,-8,NOTE_D5,16,REST,8,NOTE_C5,8,
  NOTE_B4,4,NOTE_B4,8,NOTE_E4,8,NOTE_B4,-8,NOTE_D5,8,REST,16,NOTE_C5,8,
  NOTE_B4,4,NOTE_B4,4,NOTE_B4,-8,NOTE_D5,16,REST,8,NOTE_C5,8,
  NOTE_B4,8,NOTE_B4,8,NOTE_E4,8,NOTE_B4,8,NOTE_B4,16,NOTE_E4,16,N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,
  NOTE_B4,-8,NOTE_B4,-8,NOTE_B4,8,NOTE_A4,4,REST,8,NOTE_G4,8,
  NOTE_B4,-8,NOTE_B4,-8,NOTE_B4,8,NOTE_A4,-8,NOTE_G4,8,REST,16,NOTE_G4,8,
  NOTE_B4,-8,NOTE_B4,-8,NOTE_B4,8,NOTE_A4,8,REST,4,NOTE_G4,8,
  NOTE_B4,-8,NOTE_B4,-8,NOTE_B4,8,NOTE_D5,4,REST,8,NOTE_G4,8,
  NOTE_B4,-8,NOTE_B4,-8,NOTE_B4,8,NOTE_A4,-8,NOTE_G4,8,REST,16,NOTE_G4,8,
  NOTE_B4,-8,NOTE_B4,-8,NOTE_B4,8,NOTE_A4,-4,REST,8,
  NOTE_B4,4,NOTE_B4,4,NOTE_B4,-8,NOTE_D5,16,REST,8,NOTE_C5,8,
  NOTE_C5,4,NOTE_C5,8,NOTE_E4,8,NOTE_B4,2,
  NOTE_D5,8,NOTE_G4,8,NOTE_D5,8,NOTE_G4,8,NOTE_D5,8,NOTE_G4,8,NOTE_D5,8,NOTE_G4,8,
  NOTE_D5,8,NOTE_G4,8,NOTE_D5,8,NOTE_G4,8,NOTE_D5,8,NOTE_G4,8,NOTE_C5,8,NOTE_G4,8,
  NOTE_B4,8,NOTE_G4,8,NOTE_B4,8,NOTE_G4,8,NOTE_B4,8,NOTE_G4,8,NOTE_B4,8,NOTE_G4,8,
  NOTE_B4,-8,NOTE_B4,-8,REST,8,NOTE_B4,8,NOTE_C5,8,NOTE_B4,8,
  NOTE_B4,8,NOTE_B4,8,NOTE_A4,8,NOTE_G4,8,NOTE_A4,8,NOTE_G4,8,REST,16,NOTE_B4,4,
  NOTE_B4,8,NOTE_E4,8,NOTE_B4,-8,NOTE_D5,8,REST,16
};


int notes = sizeof(melody) / sizeof(melody[0]) / 2;


int wholenote = (60000 * 4) / tempo;

int divider = 0, noteDuration = 0;

void setup() {
  
  for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {

    
    divider = melody[thisNote + 1];
    if (divider > 0) {
      
      noteDuration = (wholenote) / divider;
    } else if (divider < 0) {
      
      noteDuration = (wholenote) / abs(divider);
      noteDuration *= 1.5; 
    }

    
    tone(buzzer, melody[thisNote], noteDuration * 0.9);

    
    delay(noteDuration);

    
    noTone(buzzer);
  }
}

void loop() {
  // no need to repeat the melody.
}

storing the data into progmen
or
use a microcontroller with more RAM
or
use an SD-Card to store the data

or
saving RAM by defining the notes only once as an array of int.
And for the melody storing the notes as an array of byte which needs half the RAM

Try this: I shifted the melody array into PROGMEM. It compiled for Uno at 17% instead of 113%

Source:
https://www.arduino.cc/reference/en/language/variables/utilities/progmem/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define REST      0
int tempo = 128;
int buzzer = 11;
const int melody[] PROGMEM = {

  NOTE_B3,4,NOTE_B3,4,NOTE_B3,-8,NOTE_D4,16,REST,8,NOTE_C4,8,
  NOTE_B3,4,NOTE_B3,8,NOTE_E3,8,NOTE_B3,-8,NOTE_D4,8,REST,16,NOTE_C4,8,
  NOTE_B3,4,NOTE_B3,4,NOTE_B3,-8,NOTE_D4,16,REST,8,NOTE_C4,8,
  NOTE_B3,8,NOTE_B3,8,NOTE_E3,8,NOTE_B3,8,NOTE_B3,16,NOTE_E3,16,NOTE_E3,16,NOTE_E3,16,NOTE_B3,4,

  NOTE_B3,4,NOTE_B3,4,NOTE_B3,-8,NOTE_D4,16,REST,8,NOTE_C4,8,
  NOTE_B3,4,NOTE_B3,8,NOTE_E3,8,NOTE_B3,-8,NOTE_D4,8,REST,16,NOTE_C4,8,
  NOTE_B3,4,NOTE_B3,4,NOTE_B3,-8,NOTE_D4,16,REST,8,NOTE_C4,8,
  NOTE_B3,8,NOTE_B3,8,NOTE_E3,8,NOTE_B3,8,NOTE_B3,16,NOTE_E3,16,NOTE_E3,16,NOTE_E3,16,N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E_B4,8,
  NOTE_B4,8,NOTE_B4,8,NOTE_A4,8,NOTE_G4,8,NOTE_A4,8,NOTE_G4,8,REST,16,NOTE_B4,4,
  NOTE_B4,8,NOTE_E4,8,NOTE_B4,-8,NOTE_D5,8,REST,16
};


int notes = sizeof(melody) / sizeof(melody[0]) / 2;


int wholenote = (60000 * 4) / tempo;

int divider = 0, noteDuration = 0;

void setup() {
  
  for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {

    
    divider = melody[thisNote + 1];
    if (divider > 0) {
      
      noteDuration = (wholenote) / divider;
    } else if (divider < 0) {
      
      noteDuration = (wholenote) / abs(divider);
      noteDuration *= 1.5; 
    }

    
    tone(buzzer, melody[thisNote], noteDuration * 0.9);

    
    delay(noteDuration);

    
    noTone(buzzer);
  }
}

void loop() {
  // no need to repeat the melody.
}

For those about to rock, I :saluting_face: you!

1 Like

Thanks for the answer, but another problem has arisen.
Let's start with the fact that my board is Arduino nano.
I ran your code and got a very strange result. Moreover, when loading a sketch and when pressing the reset button, the sound is different. The buzzer just beeps like an old modem.

Sound when loading script via Arduino IDE

Sound when pressing the reset button

and... i dont know what to do...

As far as I see, you connected the speaker directly to arduino pin. This may damage the board.
What is the speaker resistance?

8 Ω :point_right: :point_left:

This speaker must be used with amplifier.
To test your code, use the arduino buzzer module .

Do a basic check with uploading the example-sketch "tone" from the arduino-ide

if you do a basic calculation about the current
this is

5V / 8 Ohm = 0,625 A
where an arduino-IO-pin can only withstand 0,040 A
and you should not exceed 0,020A = 20 mA

Calculating the resistance for 20 mA

5V / 0.02A = 250 Ohms
So if you want to continue driving this 8-Ohm-speaker directly from an IO-pin

insert a 270 Ohm-resistor in series

IO-pin-----270 Ohm--------8-Ohm-speaker-------GND

If you want the speaker to be louder you will have to use a transistor which can drive 800 mA like a BC338
https://alltransistors.com/transistor.php?transistor=22966

1 Like

The code from post #3 has an error. The data is being stored in PROGMEM, but the code is reading from RAM, not PROGMEM. Special methods need to be used to read from PROGMEM.

void setup() {
  for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {

    //divider = melody[thisNote + 1];
    divider = (int)pgm_read_word(&melody[thisNote + 1]);
    if (divider > 0) {

      noteDuration = (wholenote) / divider;
    } else if (divider < 0) {

      noteDuration = (wholenote) / abs(divider);
      noteDuration *= 1.5;
    }

    //tone(buzzer, melody[thisNote], noteDuration * 0.9);
    tone(buzzer, (int)pgm_read_word(&melody[thisNote]), noteDuration * 0.9);

    delay(noteDuration);

    noTone(buzzer);
  }
}
2 Likes

I'd switch to a ESP based board and leave all these AVR specific resource issues behind.

--- bill

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.