# How can I shrink my code?

**URL:** <https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375>\
**Category:** Programming\
**Created:** [February 15, 2025, 8:49pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375 "2025-02-15T20:49:55Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![mehmet\_ozi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mehmet_ozi/32/1197289_2.png) [@mehmet\_ozi](https://forum.arduino.cc/u/mehmet_ozi)\
**Post date:** [February 15, 2025, 8:49pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/1 "2025-02-15T20:49:55Z")

</div>

(sorry about the poor english) Hello guys, I was working on a smart greenhouse project that utilize very chars and strings, ntp servers for telling time, bluetooth for app compability etc. I use a WEMOS D1 R32 Esp32. I specifically chose this board because i needed more storage for my projects. But my project is too heavy for my board. The sketch uses 1674221 bytes (127%) of program storage space. The maximum is 1310720 bytes.  
And global variables use 60096 bytes (18%) of dynamic memory, leaving 267584 bytes for local variables. The maximum is 327680 bytes. I do not want to buy an SD card reader and do not want to break my code. Can anyone help me? The code is:

```arduino
`#include <Arduino.h>
#include "DHT.h"
#include <BluetoothSerial.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

const char* ssid = " *******";
const char* password = " **********";

const char* ntpServer = "tr.pool.ntp.org";
#define sulamaPin 26
#define DHTPIN 27    
#define DHTTYPE DHT11
#define veri3169 A0
#define buzzerPin 13
#define soilWet 1699  
#define soilDry 880

int relayOnHour = 18, relayOnMinute = 31;
int relayOffHour = 18, relayOffMinute = 32;
bool relayState = false;

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntpServer, 10800, 3600);
BluetoothSerial SerialBT;
DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(115200);
    SerialBT.begin("AgroBot TR31B");
    pinMode(sulamaPin, OUTPUT);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) delay(500);
    timeClient.begin();
    dht.begin();
}

void loop() {
    delay(1000);
    float h = dht.readHumidity(), t = dht.readTemperature();
    if (isnan(h) || isnan(t)) return;

    int nemPin = analogRead(veri3169);
    SerialBT.printf("Nem: %.1f%% Sıcaklık: %.1f°C Toprak: %d\n", h, t, nemPin);
    digitalWrite(buzzerPin, (nemPin < soilDry) ? HIGH : LOW);

    timeClient.update();
    int currentHour = timeClient.getHours(), currentMinute = timeClient.getMinutes();
    relayState = (currentHour == relayOnHour && currentMinute == relayOnMinute) ? HIGH :
                 (currentHour == relayOffHour && currentMinute == relayOffMinute) ? LOW : relayState;
    digitalWrite(sulamaPin, relayState);

    if (SerialBT.available()) {
        int data = SerialBT.readString().toInt();
        if (data >= 1 && data <= 48) {
            relayOnHour = (data - 1) / 2;
            relayOnMinute = (data % 2) ? 0 : 30;
            relayOffHour = relayOnHour;
            relayOffMinute = relayOnMinute + 1;
        }
    }
}
`

```

If anyone can help me I'll be so grateful.

---

<div class="post-metadata">

**Author:** ![kmin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kmin/32/1147370_2.png) [@kmin](https://forum.arduino.cc/u/kmin)\
**Post date:** [February 15, 2025, 9:39pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/2 "2025-02-15T21:39:02Z")

</div>

Try with Huge App on partition scheme.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/6/2/b/62b7632ee2980aa187aeee93b3fb87c3878a9d79.png)

---

<div class="post-metadata">

**Author:** ![MrY](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mry/32/624544_2.png) [@MrY](https://forum.arduino.cc/u/MrY)\
**Post date:** [February 15, 2025, 9:51pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/3 "2025-02-15T21:51:49Z")

</div>

You may want to visit [this Arduino page](https://support.arduino.cc/hc/en-us/articles/360013825179-Reduce-the-size-and-memory-usage-of-your-sketch), which describes how to reduce your sketch size and memory usage.

You may also want to consider replacing your current board with an ESP32-S3 board that provides a more significant amount of flash. Adafruit has several boards with 8MB of flash. The downside is that they are S3-type boards that support BLE but not Bluetooth Classic. There is a [BLE serial library](https://github.com/Uberi/Arduino-HardwareBLESerial) that implements Nordic UART.

I have used the HardwareBLESerial library for one of my projects, which works without issue.

---

<div class="post-metadata">

**Author:** ![MrY](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mry/32/624544_2.png) [@MrY](https://forum.arduino.cc/u/MrY)\
**Post date:** [February 15, 2025, 9:59pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/4 "2025-02-15T21:59:41Z")

</div>

@kmin had a great suggestion. I selected large partition (no OTA), and here is the result:

```arduino
Sketch uses 1679524 bytes (80%) of program storage space. Maximum is 2097152 bytes.
Global variables use 61484 bytes (18%) of dynamic memory, leaving 266196 bytes for local variables. Maximum is 327680 bytes.

```

---

<div class="post-metadata">

**Author:** ![ledsyn](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ledsyn/32/1203827_2.png) [@ledsyn](https://forum.arduino.cc/u/ledsyn)\
**Post date:** [February 15, 2025, 10:32pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/5 "2025-02-15T22:32:16Z")

</div>

Still, would be interesting to know what take up that much memory. 80% of 4 MB is crazy, your sketch isn't that big, unless I've overlooked something.

---

<div class="post-metadata">

**Author:** ![mehmet\_ozi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mehmet_ozi/32/1197289_2.png) [@mehmet\_ozi](https://forum.arduino.cc/u/mehmet_ozi)\
**Post date:** [February 15, 2025, 10:34pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/6 "2025-02-15T22:34:18Z")

</div>

Thank you guys very much, I tried out the code and it worked with no issues, also gonna state that I cannot afford another board right now because the econamical state in my country is bad so things get overpriced from our perspective. Still ty very much, appreciate it.

---

<div class="post-metadata">

**Author:** ![mehmet\_ozi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mehmet_ozi/32/1197289_2.png) [@mehmet\_ozi](https://forum.arduino.cc/u/mehmet_ozi)\
**Post date:** [February 15, 2025, 10:35pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/7 "2025-02-15T22:35:39Z")

</div>

Well I have so much char and strings as I stated (as i remember) so it could be that.  
My normal code was just over 600 lines long but i shortend the code a little.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 15, 2025, 10:41pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/8 "2025-02-15T22:41:10Z")

</div>

So the problem was in the code that you didn't post

It would be interesting to see the full sketch that caused the problem

---

<div class="post-metadata">

**Author:** ![ledsyn](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ledsyn/32/1203827_2.png) [@ledsyn](https://forum.arduino.cc/u/ledsyn)\
**Post date:** [February 15, 2025, 10:48pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/9 "2025-02-15T22:48:20Z")

</div>

Well remember to mark the answer that solved the problem as Solution, have a nice night and come again 🙃

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [February 15, 2025, 11:58pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/10 "2025-02-15T23:58:48Z")

</div>

> [@MrY](#):
>
> `Sketch uses 1679524 bytes (80%) of program storage space. Maximum is 2097152 bytes.`

> [@ledsyn](#):
>
> 80% of 4 MB is crazy

Well, it's 80% of only 2MB. Apparently half the flash is "reserved" for OTA upgrades, or something. (although, even the non-OTA, no FS, partition schemes seem to max out at 3MB. I don't know whether that's a device limitation, or just a lack of menu options...)

---

<div class="post-metadata">

**Author:** ![ledsyn](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ledsyn/32/1203827_2.png) [@ledsyn](https://forum.arduino.cc/u/ledsyn)\
**Post date:** [February 16, 2025, 12:04am UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/11 "2025-02-16T00:04:39Z")

</div>

Aha, a bit better but still a lot of code. I bet those wireless libs wants much memory..?

---

<div class="post-metadata">

**Author:** ![mehmet\_ozi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mehmet_ozi/32/1197289_2.png) [@mehmet\_ozi](https://forum.arduino.cc/u/mehmet_ozi)\
**Post date:** [February 16, 2025, 10:49am UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/12 "2025-02-16T10:49:24Z")

</div>

You got it. I did not have an real time module so I just used a ntp server for the time. Its not efficient but hey, it works.

---

<div class="post-metadata">

**Author:** ![mehmet\_ozi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mehmet_ozi/32/1197289_2.png) [@mehmet\_ozi](https://forum.arduino.cc/u/mehmet_ozi)\
**Post date:** [February 16, 2025, 10:49am UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/13 "2025-02-16T10:49:55Z")

</div>

It IS the full code.

---

<div class="post-metadata">

**Author:** ![pert](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pert/32/217111_2.png) [@pert](https://forum.arduino.cc/u/pert)\
**Post date:** [March 8, 2025, 3:32pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/14 "2025-03-08T15:32:41Z")

</div>

Split this topic on March 8, 2025, 3:32pm UTC

3 posts were split to a new topic: [Bluescreen of death while using IDE: "Stop code: DRIVER\_IRQL\_NOT\_LESS\_OR\_EQUAL"](https://forum.arduino.cc/t/bluescreen-of-death-while-using-ide-stop-code-driver-irql-not-less-or-equal/1361689)

---

<div class="post-metadata">

**Author:** ![sara\_morrison](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sara_morrison/32/1198005_2.png) [@sara\_morrison](https://forum.arduino.cc/u/sara_morrison)\
**Post date:** [February 18, 2025, 5:20pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/16 "2025-02-18T17:20:07Z")

</div>

Using the "Huge App" partition scheme indeed helps reduce the firmware size. However, if your code still occupies too much space, consider reviewing and optimizing the libraries you're using. Some libraries can be replaced with lighter alternatives or even custom implementations. Also, be cautious with the use of `String` variables, as they can consume a lot of memory; replacing them with character arrays (`char[]`) might be beneficial.

---

<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 4, 2025, 3:33pm UTC](https://forum.arduino.cc/t/how-can-i-shrink-my-code/1354375/17 "2025-09-04T15:33:20Z")

</div>

Closed on September 4, 2025, 3:33pm UTC

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