Binary compile size so large?

My sketch is 24KB. When I export to binary its over 60KB... Um, what happened?

brolly759:
My sketch is 24KB. When I export to binary its over 60KB... Um, what happened?

It was turned into machine code.
Code size has no reflection on compiled size.

If you want some real advice, rather than a handful of educated/sarcastic guesses. I'd encourage you to post your code.

Here is a snipit of the code. Its all over the place right now:

#include <Arduino.h>
#include "LowPower.h"
#include "Vcc.h"
#include "XBee.h"
#include "SparkFun_Si7021_Breakout_Library.h"
#include <avr/wdt.h>
#include <math.h>
#include <EEPROM.h>
#include <Wire.h>
#include <SPI.h>
#include "Printers.h"
#include <SoftwareSerial.h>
#include <tmp102.h>
#include <VL53L0X.h>

VL53L0X sensor;

SoftwareSerial mySerial(8, 7);

/*  sonarPowerPin = 4, sonarInputPin = 5
    xBeeWakeupPin = A3, xBeeSleepPin = A2
    ledRed = 13, ledGreen = 10, ledBlue = 9
    buzzer = 6, pushButton = 2, /sensorIRQ = 3
*/
// byte hardwareComponent4_Error = 0;  //  Sonar, LV MaxxSonar
// byte hardwareComponent5_Error = 0;  //  System, ATMEGA328P
// byte hardwareComponent8_Error = 0;  //  Reed Switch, CT10
// byte hardwareComponent10_Error = 0; //  Accelerometer, LIS3DH
// byte hardwareComponent15_Error = 0; //  Temperature, Humidity Si7021
// byte hardwareComponent16_Error = 0; //  IR Sensor, VL53L0X
// byte hardwareComponent17_Error = 0; //  Temperature, TMP112
//char hardwareVer = 'D';
//char hardwareVer = 'E';


unsigned int firmwareVersion = 1037;
unsigned int hardwareVersion = 500;
byte payloadVersion = 1;
byte numberOfPresenceDetection;

byte accelerometerSystemCode = 1;
byte accelSpeed;
byte accelPresenceThreshold;
volatile byte accelWake = 0;


Weather Si7021;
byte humidityPercentage;
short tempCtimes;
byte tempHumiditySystemCode = 1;
tmp102 *thermometer = new tmp102(&Wire);
Tmp102ConfFormat conf;

long pulse;
byte sonarSystemCode = 1;
unsigned int distanceInMillimeters;
unsigned int setupDistanceInMillimeters;

XBee xbee = XBee();
String panID = "xxxxxx";
String networkPasswordKey = "xxxxxx";
byte payloadErrorCounter = 0;
uint8_t payload[88];
byte arraySize = 0;

const float VccCorrection = 1.0 / 1.0;
Vcc vcc(VccCorrection);
byte batteryPercentage;
short power;

byte systemCode = 0;
byte presence = 0;
bool debugEnabled = false;

unsigned int outputType_PostIntervalArray[6];
//  outputType_PostIntervalArray[0] =  Output Type 2
//  outputType_PostIntervalArray[1] =  Output Type 4
//  outputType_PostIntervalArray[2] =  Output Type 5
//  outputType_PostIntervalArray[3] =  Output Type 7
//  outputType_PostIntervalArray[4] =  Output Type 8
//  outputType_PostIntervalArray[5] =  Output Type 12

unsigned int outputType_PostIntervalCounter[] = {0,0,0,0,0,0};
//   outputType_PostIntervalCounter[0] =  Output Type 2
//   outputType_PostIntervalCounter[1] =  Output Type 4
//   outputType_PostIntervalCounter[2] =  Output Type 5
//   outputType_PostIntervalCounter[3] =  Output Type 7
//   outputType_PostIntervalCounter[4] =  Output Type 8
//   outputType_PostIntervalCounter[5] =  Output Type 12

byte outputType_Enabled[] = {1,1,1,1,1,1};
//  outputType_Enabled[0] =  Output Type 2
//  outputType_Enabled[1] =  Output Type 4
//  outputType_Enabled[2] =  Output Type 5
//  outputType_Enabled[3] =  Output Type 7
//  outputType_Enabled[4] =  Output Type 8
//  outputType_Enabled[5] =  Output Type 12

byte outputType_Error[] = {0,0,0,0,0,0};
//  outputType_Error[0] = Output Type 2
//  outputType_Error[1] = Output Type 4
//  outputType_Error[2] = Output Type 5
//  outputType_Error[3] = Output Type 7
//  outputType_Error[4] = Output Type 8
//  outputType_Error[5] = Output Type 12



byte outputType5_Sensor;
byte outputType12_Sensor;


unsigned int payloadDefaultPostInterval[] = {60,600};
// payloadDefaultPostInterval[0] = Settings Default Posting Interval
// payloadDefaultPostInterval[1] = Heartbeat Default Posting Interval

unsigned int payloadCounter[] = {0,0};
// payloadCounter[0] = Settings Counter
// payloadCounter[1] = Heartbeat Counter


String fail = "FAIL";
String ok = "OK";
String on = "ON";
String off = "OFF";


void wakeUp() { accelWake++; }
void(* resetFunc) (void) = 0;

void writeRegister(byte reg, byte data) {
  Wire.beginTransmission(0x19);
  Wire.write(reg);
  Wire.write(data);
  Wire.endTransmission();
}

unsigned int readRegister(byte reg) {
  Wire.beginTransmission(0x19);
  Wire.write(reg);
  Wire.endTransmission();
  Wire.requestFrom(0x19, 1);
  return Wire.read();
}

void setup() {

  Serial.begin(115200);
  mySerial.begin(9600);
  mySerial.setTimeout(50);
  xbee.setSerial(mySerial);

  deviceStartup();
}

I presume that you are referring to the .hex output file. That file is a hex representation of your program with some added information about where the bytes will be stored in flash. Each byte of your program becomes two bytes when represented as hex characters. Each line of the output has 16 bytes of the program plus 10 or 11 bytes of address and checksum. So, first your code is doubled in size because of the translation to hex. Then adding the address and checksum info per line means that 16 bytes of program become 32+11 bytes on the line PLUS two bytes for the CR and LF at the end of the line. So 16 bytes becomes 45 bytes in the file. If your code was 24kB, the output file will be about 67kB.

Pete

Does that mean I will use up 67Kb on the chip or does the 2/3rd of that get stripped away when programming?

The .hex file is a different representation of the code. Your program will be 24kB when loaded into ram flash (Thanks Ray).

Pete

el_supremo:
The .hex file is a different representation of the code. Your program will be 24kB when loaded into ram.

Pete

Except the program never gets loaded into RAM....

Regards,
Ray L.

Right. I was thinking "flash" but wrote "ram". Brain obviously not in gear - again.

Pete

Part of the issue is that you are including a bunch of libraries. All that code takes up space, even though the compiler strips out stuff that is never used. f you are using sparkfun, wire, SPI, xbee, and all the rest, it all winds up in the binary. Necessarily so, of course. It's all code that has to be there to deal with what you are working with.

You appear to be using 11 libraries in your program. It is possible that some of those are very large. Are there any of those libraries that you don’t actually need?

#include "LowPower.h"
#include "Vcc.h"
#include "XBee.h"
#include "SparkFun_Si7021_Breakout_Library.h"
#include <EEPROM.h>
#include <Wire.h>
#include <SPI.h>
#include "Printers.h"
#include <SoftwareSerial.h>
#include <tmp102.h>
#include <VL53L0X.h>

If you need all of those libraries you will probably have to switch to a larger Arduino, like the Mega. It’s only a few cm longer than the UNO but has a lot more memory space.

I had an accelerator library that I was using and I removed it by writing the registers directly. I will probably do that for all the sensors to lower the code size. The sketch said 24kb but the hex was larger. Its okay though, the hex being larger doesnt matter because 2/3 of the size gets thrown away after its programmed in. So thats good.

I am looking at Atmega1284P or SAMD21 chips right now. There is more compatibility with 1284P than SAMD21. I would LOVE to use SAMD21 instead but wont be able to do OTA and probably half the code I wrote will break :stuck_out_tongue:

brolly759:
My sketch is 24KB. When I export to binary its over 60KB... Um, what happened?

There is not really a correlation between the amount pf plain text C/C++ source code you write and the compiled program size

Compiled program size depends more on different things like

  • destination platform you are compiling for
  • amount of library code you are including by explicit or implicit including libaries in your sketch.

I don't think that brolly759 is referring to the filesize of the sketch but to the reported size in the output window of the IDE; might be wrong though.