For my project, i need my Arduino Uno to simulate a GPS. To do this, I need to:

  • Generate a 5V Pulse once a second (no problem)
  • Send a $GPRMC NMEA sentence with the current time over RS-232 (i've got an RS-232 shield)

The GPRMC message i need to send is the following:


Where the first part is the time, and needs to change. I basically implemented it as:

// Setup
String prefix = "$GPRMC,";
String gprmc  = "075832.293";
String suffix = ",A,4042.695,N,07400.796,W,,,080120,000.0,W*63";
String all = prefix + gprmc + suffix;
char* buff = (char*) malloc(sizeof(char)*all.length()+1);

Then, in the loop-code i update the time and want to send the message:

//...inside the condition that gets executed once a second
gprmc = String(gprmc.toFloat()+1);
all = prefix+gprmc+suffix;
all.toCharArray(buff, all.length()+1);

I'm using an RS-232 shield, which shows me when a message is being sent by blinking an LED. However, is only seems to work, if i shorten the all string, i.e.: Works:

all = prefix+gprmc;

Does not work:

all = prefix+gprmc+suffix;

Is it possible that the message is just too long to send? Is there a way I can fix this? Because it's crucial to send the whole message.

Note: I'm currently working on an Arduino Uno, but I'm switching to an arduino Leonardo today. I currently have low memory available (77% used). could this add to the problem?

Thank you so much for your help!! :)

  • What do you mean with "does not work"? What did you expect and what actually happens?
    – chrisl
    Jan 8, 2020 at 10:08
  • On the RS-232 Shield, an LED blinks if a message is sent. By "doesn't work" i mean, no message is sent (led no blinking)
    – MarayJay
    Jan 8, 2020 at 10:14

1 Answer 1


First off, some comments about your code:

  • Never use String unless it is absolutely unavoidable
  • Unless you have a free() to balance your malloc() you will run out of memory almost immediately
  • There is never any need to concatenate strings to send through serial like that.

Instead of what you are currently doing, just scrap the String usage and your buf variable completely. There is no need to concatenate things together - serial sends data one character at a time, so you're gaining nothing by concatenating and only causing yourself more problems.

How you should be doing it:

// The changing value as a float
float gprmc=75832.293;
// Print the prefix
// Pad the value with zeroes
if (gprmc < 10) mySerial.print(F("0"));
if (gprmc < 100) mySerial.print(F("0"));
if (gprmc < 1000) mySerial.print(F("0"));
if (gprmc < 10000) mySerial.print(F("0"));
if (gprmc < 100000) mySerial.print(F("0"));
// Print the value with 3 decimal places
mySerial.print(gprmc, 3);
// Finally print the suffix

Note the use of the F() macro to further reduce RAM usage by keeping the string constants in flash memory.

  • Thank you! Is there any difference on how the message will be transmitted by using mySerial.print(...) or mySerial.write(...)? somehow, the example uses the .write() command, which introduced my problems.
    – MarayJay
    Jan 8, 2020 at 12:03
  • No. print just calls write but accepts more parameter types.
    – Majenko
    Jan 8, 2020 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.