# Code, possible conflict (OLED Display + Buzzer troubles)

**URL:** <https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089>\
**Category:** Sensors\
**Created:** [March 21, 2025, 11:28pm UTC](https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089 "2025-03-21T23:28:46Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [March 22, 2025, 5:57pm UTC](https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089/21 "2025-03-22T17:57:35Z")

</div>

> [@onrguneyli](#):
>
> When I changed (memory usages) values as you find above, voice commands doesn't work as I want(as must be).

Again, your descriptions are not detailed enough. What are these "voice commands" ? What do you want them to do? What do they actually do?

---

<div class="post-metadata">

**Author:** ![onrguneyli](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/onrguneyli/32/1130509_2.png) [@onrguneyli](https://forum.arduino.cc/u/onrguneyli)\
**Post date:** [March 22, 2025, 10:05pm UTC](https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089/22 "2025-03-22T22:05:10Z")

</div>

okay let me tell you clearly. Watch the [video](https://www.youtube.com/shorts/K78VqB-n1wE) to understand. Buzzer beeping different frequencies depending to pressure changes. here is the code without screen. its works perfect when its coded without screen.

```arduino
#include <toneAC.h>
#include <Wire.h>
#include <MS5611.h>

MS5611 ms5611; // MS5611 sensörü nesnesi

/*
Donanım bağlantıları:

- (GND) GND'ye
+ (VDD) 3.3V'ye
SCL A5 pinine
SDA A4 pinine

D9 ve D10 pinlerine Piezo Buzzer (toneAC.h kütüphanesi ile)
*/

#define NUM_PRESSURES 64
#define NUM_TOTALS 16
uint32_t pressure[NUM_PRESSURES];
uint32_t old_total[NUM_TOTALS];
int pressure_index = 0;
int total_index = 0;
uint32_t total;
int current_tone = 0;
int beep_time = 0;

void setup() {
  // Seri iletişimi başlat
  Serial.begin(9600);
  
  // Başlangıç sesi çal
  toneAC(388, 4);
  delay(70);
  toneAC(0);
  delay(30);
  toneAC(590, 4);
  delay(70);

  // MS5611 sensörünü başlat
  if (!ms5611.begin()) {
    Serial.println("MS5611 sensörü tespit edilemedi. Bağlantıları kontrol edin.");
    while (1);
  }

  uint32_t p = ms5611.readPressure();
  total = p * NUM_PRESSURES;
  
  // İlk değeri kullanarak basınç dizisini başlat
  for (int i = 0; i < NUM_PRESSURES; i++) {
    pressure[i] = p;
  }

  // İlk toplam değeri kullanarak eski toplam dizisini başlat
  for (int i = 0; i < NUM_TOTALS; i++) {
    old_total[i] = total;
  }
  
  toneAC(0);
}

void loop() {
  // Yeni basıncı oku ve toplam basıncı güncelle
  total -= pressure[pressure_index];
  pressure[pressure_index] = ms5611.readPressure(); // MS5611'den güncel basıncı oku
  total += pressure[pressure_index];

  // Basınç değişim hızını hesapla
  int32_t rate = total - old_total[total_index];
  old_total[total_index] = total;

  // Basınç dizisinin ve toplam dizisinin indekslerini sıfırla
  pressure_index++;
  total_index++;

  if (pressure_index >= NUM_PRESSURES) pressure_index = 0;
  if (total_index >= NUM_TOTALS) total_index = 0;

  // Basınç değişim hızına göre ton frekansını ayarla
  if (rate < -50) {
    if (beep_time < 5) {
      toneAC(1000 - rate / 2); // Basınç değişimine bağlı olarak frekansı artır
    } else {
      toneAC(0); // Belirli bir süre sonra tonu kapat
    }
  } else if (rate > 50) {
    // Basınç değişim hızına bağlı olarak frekansı belirle
    float f = 1000.0 + 40000.0 * 1.0 / ((float)rate); // Yüksek frekanslar için formül
    toneAC((int)f);
  } else {
    // Basınç değişim hızı küçükse tonu kapat
    toneAC(0);
  }

  // beep_time'ı artır ve sıfırla
  beep_time++;
  if (beep_time >= 10) beep_time = 0;
}

```

And this code(WITHIUT BUZZER) OLED screen and ms5611 sensor ;  
Screen shows current altitude, temprature, pressure.

```arduino
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <MS5611.h> // https://github.com/jarzebski/Arduino-MS5611
 
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // See datasheet for Address
 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
MS5611 ms5611;
 
double referencePressure;
 
void setup() {
  Serial.begin(9600);

  // Initialize MS5611 sensor
  Serial.println("Initialize MS5611 Sensor");
  while (!ms5611.begin()) {
    Serial.println("Could not find a valid MS5611 sensor, check wiring!");
    delay(500);
  }

  // Initialize OLED Display
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  display.clearDisplay();
  display.display();
  delay(500);

  // Get reference pressure for relative altitude
  referencePressure = 101250;

  // Check sensor settings
  checkSettings();
  delay(2000);
}

void checkSettings() {
  Serial.print("Oversampling: ");
  Serial.println(ms5611.getOversampling());
}

void loop() {
  // Read true temperature & pressure
  double realTemperature = ms5611.readTemperature();
  long realPressure = ms5611.readPressure();
  double realPressureKPa = realPressure / 1000.0; // Pa -> kPa dönüşümü

  // Calculate altitude
  float relativeAltitude = ms5611.getAltitude(realPressure, referencePressure);

  // Print to Serial Monitor
  Serial.println("--");
  Serial.print("Temperature: ");
  Serial.print(realTemperature);
  Serial.println(" *C");

  Serial.print("Pressure: ");
  Serial.print(realPressureKPa, 2); // 2 ondalık basamak gösterimi
  Serial.println(" kPa");

  Serial.print("Relative Altitude: ");
  Serial.print(relativeAltitude);
  Serial.println(" m");

  // Display on OLED
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);

  display.setCursor(0, 0);
  display.setTextSize(1);
  display.print("Temp: ");
  display.print(realTemperature);
  display.print(" C");

  display.setCursor(0, 20);
  display.print("Pressure: ");
  display.print(realPressureKPa, 2); // 2 ondalık basamak hassasiyeti
  display.print(" kPa");

  display.setCursor(0, 40);
  display.print("Altitude: ");
  display.print(relativeAltitude);
  display.print(" m");

  display.display();
  delay(2000);
}

```

---

<div class="post-metadata">

**Author:** ![onrguneyli](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/onrguneyli/32/1130509_2.png) [@onrguneyli](https://forum.arduino.cc/u/onrguneyli)\
**Post date:** [March 22, 2025, 10:20pm UTC](https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089/23 "2025-03-22T22:20:56Z")

</div>

> [@PaulRB](#):
>
> `#define NUM_PRESSURES 32`

> [@PaulRB](#):
>
> I have now changed this line:
> 
> ```arduino
> #define NUM_PRESSURES 32
> 
> ```

I did that also and screen started shown by "merhaba" on the screen. However, the response of the buzzer to pressure changes is very delayed. This is an undesirable situation.

---

<div class="post-metadata">

**Author:** ![onrguneyli](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/onrguneyli/32/1130509_2.png) [@onrguneyli](https://forum.arduino.cc/u/onrguneyli)\
**Post date:** [March 22, 2025, 10:27pm UTC](https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089/24 "2025-03-22T22:27:10Z")

</div>

> [@PaulRB](#):
>
> When the code runs, at least 1024 bytes will be reserved by the OLED library, leaving only perhaps 150 bytes remaining. Far too little for reliable operation.

I ordered gps module and sd-card module for this project to save flight time data to a sdcard. now im stuck here ☹

---

<div class="post-metadata">

**Author:** ![onrguneyli](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/onrguneyli/32/1130509_2.png) [@onrguneyli](https://forum.arduino.cc/u/onrguneyli)\
**Post date:** [March 22, 2025, 10:40pm UTC](https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089/25 "2025-03-22T22:40:50Z")

</div>

> [@onrguneyli](#):
>
> I did that also and screen started shown by "merhaba" on the screen. However, the response of the buzzer to pressure changes is very delayed. This is an undesirable situation.

you can watch the videos. you can understand what happens when I changed

> [@PaulRB](#):
>
> `#define NUM_PRESSURES 32`

values.  
[Downloads.zip](https://forum.arduino.cc/uploads/short-url/wnraf7nnrTumPIokKn15DDkrJoF.zip) (2.9 MB)

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [March 23, 2025, 12:04am UTC](https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089/26 "2025-03-23T00:04:06Z")

</div>

> [@onrguneyli](#):
>
> I ordered gps module and sd-card module for this project to save flight time data to a sdcard.

You really need a board with more ram than an atmega328 based Nano. The SD card will need 512 bytes of memory for a buffer, and the GPS generally needs serial data, so either SoftwareSerial on a Nano, or it can be done with Serial if you don't need the serial monitor.

You cannot use the Adafruit\_SSD1306 library, it needs too much memory. There is the SSD1306Ascii library that needs no buffer in memory, but is limited to text. The U8g2 library has an u8x8 variant that allows text and limited bitmap images, or the full U8g2 library with a page buffer that only needs about 128 bytes of ram for the buffer.

See if this modified version of your code runs using the U8g2 library (you may need to install that through the IDE's library manager:

```arduino
#include <Wire.h>
////#include <Adafruit_GFX.h>
////#include <Adafruit_SSD1306.h>
#include <toneAC.h>
#include <MS5611.h>

#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

MS5611 ms5611; // MS5611 sensor object

#define SCREEN_WIDTH 128 // Screen width
#define SCREEN_HEIGHT 64 // Screen height
#define OLED_RESET -1 // Screen reset pin, this screen doesn't have a reset pin

#define NUM_PRESSURES 64
#define NUM_TOTALS 16
uint32_t pressure[NUM_PRESSURES];
uint32_t old_total[NUM_TOTALS];
int pressure_index = 0;
int total_index = 0;
uint32_t total;
int current_tone = 0;
int beep_time = 0;

// Create the OLED screen object
////Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
U8G2_SSD1306_128X64_NONAME_1_HW_I2C display(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

void setup() {
  // Start serial communication
  Serial.begin(9600);

  // Initialize OLED screen (using address 0x3C)
  ////if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C
  //// Serial.println(F("Failed to initialize OLED display"));
  //// while (1); // Stop if the screen fails to initialize
  ////}
  display.begin();

  // Clear the OLED screen
  ////display.clearDisplay();

  // Play startup sound
  toneAC(388, 4);
  delay(70);
  toneAC(0);
  delay(30);
  toneAC(590, 4);
  delay(70);

  // Initialize the MS5611 sensor
  if (!ms5611.begin()) {
    Serial.println("MS5611 sensor not detected. Check the connections.");
    while (1); // Stop if the sensor is not detected
  }

  uint32_t p = ms5611.readPressure();
  total = p * NUM_PRESSURES;

  // Initialize the pressure array with the first value
  for (int i = 0; i < NUM_PRESSURES; i++) {
    pressure[i] = p;
  }

  // Initialize the old_total array with the first total value
  for (int i = 0; i < NUM_TOTALS; i++) {
    old_total[i] = total;
  }

  toneAC(0);

  // Display "Merhaba" (Hello) on the screen
  ////display.setTextSize(1); // Set text size
  ////display.setTextColor(SSD1306_WHITE); // Set text color to white
  ////display.setCursor(0, 0); // Set starting position (top left corner)
  ////display.print(F("Merhaba")); // Print "Merhaba" (Hello)
  ////display.display(); // Update the display

  display.setFont(u8g2_font_6x10_tr);
  display.firstPage();
  do {
    display.setCursor(0, 8);
    display.print(F("Merhaba in setup"));
  } while (display.nextPage());
  delay(2000); // Display for 2 seconds
}

void loop() {
  // Read new pressure and update the total pressure
  total -= pressure[pressure_index];
  pressure[pressure_index] = ms5611.readPressure(); // Read current pressure from MS5611
  total += pressure[pressure_index];

  // Calculate the pressure change rate
  int32_t rate = total - old_total[total_index];
  old_total[total_index] = total;

  // Reset the indices for the pressure and total arrays
  pressure_index++;
  total_index++;

  if (pressure_index >= NUM_PRESSURES) pressure_index = 0;
  if (total_index >= NUM_TOTALS) total_index = 0;

  // Adjust tone frequency based on the pressure change rate
  if (rate < -50) {
    if (beep_time < 5) {
      toneAC(1000 - rate / 2); // Increase frequency based on pressure change
    } else {
      toneAC(0); // Turn off tone after a certain period
    }
  } else if (rate > 50) {
    // Set frequency based on the pressure change rate
    float f = 1000.0 + 40000.0 * 1.0 / ((float)rate); // Formula for higher frequencies
    toneAC((int)f);
  } else {
    // Turn off the tone if the pressure change rate is small
    toneAC(0);
  }

  // Increase beep_time and reset if necessary
  beep_time++;
  if (beep_time >= 10) beep_time = 0;

  // Continuously display "Merhaba" on the screen
  ////display.clearDisplay(); // Clear the screen
  ////display.setCursor(0, 0); // Set starting position
  ////display.print(F("Merhaba")); // Print "Merhaba" (Hello)
  ////display.display(); // Update the display display.firstPage();
  display.firstPage();
  do {
    display.setCursor(0, 8);
    display.print(F("Merhaba in loop"));
  } while (display.nextPage());
  delay(500); // Wait for 500ms
}
```

---

<div class="post-metadata">

**Author:** ![onrguneyli](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/onrguneyli/32/1130509_2.png) [@onrguneyli](https://forum.arduino.cc/u/onrguneyli)\
**Post date:** [March 23, 2025, 8:27am UTC](https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089/27 "2025-03-23T08:27:16Z")

</div>

> [@david\_2018](#):
>
> You really need a board with more ram than an atmega328 based Nano

Any advice you have? What should I use? It must be small as nano

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [March 23, 2025, 12:01pm UTC](https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089/28 "2025-03-23T12:01:15Z")

</div>

There are multiple boards that are the same size as the Nano, often with the word "Nano" as part of their name. Adafruit also has their "Feather" line of boards of a similar size, including one with an ESP32 processor and a TFT display on the back, if you really want something compact.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [March 23, 2025, 12:49pm UTC](https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089/29 "2025-03-23T12:49:18Z")

</div>

I have done some analysis on your code that produces the beeps.

> [@onrguneyli](#):
>
> the response of the buzzer to pressure changes is very delayed. This is an undesirable situation.

If `NUM_PRESSURES` is half of its previous value (32 not 64), then `total` will be approximately half also. Not exactly half, because the pressure readings will vary. But `total` is now the sum of 32 pressure values instead of 64 pressure values, so on average it will be about half what it would be for 64 values.

This means that `rate` will also be approximately half what it was previously. This makes it much less likely to be \< -50 or \> 50, and will also affect the frequency of the beeps, because they are calculated using `rate`.

For a quick fix, you could double the value of `rate` to compensate:

```arduino
int32_t rate = 2 * (total - old_total[total_index]);

```

However, I think it would be better to re-design this code, because it has many faults:

1. Changing the value of `NUM_PRESSURES` and `NUM_TOTALS` breaks the buzzer code, as we have seen.
2. Pressure readings are taken at maximum rate which loop() can execute, which is determined by the other code in loop(). For example, updating the OLED slows down loop() and this will also break the buzzer code.
3. If you are able to save data to the SD card, this will also slow down loop() and break the buzzer code.
4. If you upgrade the Nano to a faster Arduino with more memory, this will increase the speed of loop() and break the buzzer code.

If/when you re-design the buzzer code, I recommend:

1. it should include some timing using millis() or micros(), so that the pressure readings are taken at a constant rate, not at whatever speed loop() executes.
2. Use average pressure readings, not totals, to calculate the buzzer frequencies, so that you can change `NUM_PRESSURES` and `NUM_TOTALS` as you like without affecting the buzzer frequencies.
3. Calculate `rate` as a value which represents a change in Pascals per second, regardless of how many readings were averaged and how fast those readings were taken.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 19, 2025, 12:49pm UTC](https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089/30 "2025-09-19T12:49:30Z")

</div>

Closed on September 19, 2025, 12:49pm UTC

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

[Previous page](https://forum.arduino.cc/t/code-possible-conflict-oled-display-buzzer-troubles/1366089.md?page=1)
