Difference between sending '123' and "123" over Serial.

Hi All!

I am working on simple data acquisition device using Arduino. I have been testing different sampling rates I can get out of it, and I run into one peculiarity while sending data using Serial protocol.

Here is my sketch:

void setup()
{
  Serial.begin(115200);   
  
  unsigned long t1 = micros();
  for (int i=0; i< 100; i++)
  {
   Serial.println("123");
  }
  unsigned long t2 = micros() - t1;
  
  Serial.println();
  Serial.println(t2);  
}

void loop()
{}

In line 8 I am sending string formated with double quotation marks: "123". When I change it to single quotation marks '123' the time to send it increases significantly. See the data in the attached table. Anyone have an idea what is the reason?

Thanks!

table.png

HazardsMind:
Obviously from your title, you're not aware of the difference between single and double quotes and how the serial monitor interacts with them. Double quotes means it's a string and single quotes means it's a char. So "123" sends out individual chars, ('1' , '2' , '3'), and '123' sends out a single char, which is why it is much faster.

Almost there, '123' is a multi byte character constant. Also its an int.
On arduino the int is 2 bytes, so the longest multibyte char can be two characters wide. '123' is truncated from 32 bits to 16.

Consider a list of values ( on 8 bit AVR )

'a' == 0x61
'aa' == 0x6161
'aaa' == 0x6161
'aaaa' == 0x6161
'aaaaa' == 0x6161

Yes but the OP is saying it is the other way round as regards speed

When I change it to single quotation marks '123' the time to send it increases significantl

Yes but the OP is saying it is the other way round as regards speed

It should increase, like I said, its an int. The print library does a conversion like itoa which is far more involved than printing 3 chars in a string "123"

Got it. Thanks guys - I wasn't aware of this difference. It makes sense now - for example doing Serial.print('123',DEC) gives 12851. Therefore with "123" I am sending only 5 bytes (including "\r\n"), while '123' is in fact 7 bytes.

teoqba:
Got it. Thanks guys - I wasn't aware of this difference. It makes sense now - for example doing Serial.print('123',DEC) gives 12851. Therefore with "123" I am sending only 5 bytes (including "\r\n"), while '123' is in fact 7 bytes.

Sending the data should be relatively close in timing, as its only two more characters. The algorithm to convert the integer 12851 into a printable string "12851" would be responsible for the bulk of the overhead.

Even though 2 years later..... and after spending a long time trying to figure how 12851 came about, I eventually understood it. For a bit of 'closure' (I hate that word), and if anybody stumbles across this thread.... I'll explain it.

Serial.print, we usually use double quotes to print a string eg. "123" will result in characters 123 printed.
But if we use single quotes, the input is interpreted differently. How is it interpreted?

'1' interpreted as the ascii character decimal 49 (see ASCII character map). '2' interpreted as decimal 50. '3' is decimal 51. Each is converted into 8 bit binary and strung (concatenated) together. Get 24 bits, 00110001 00110010 00110011. However, int data type in arduino is signed 16 bit. So truncated binary will be 110010 00110011, which is:

00110010 00110011 = decimal 12851

To check.... can use an online converter... such as.. click here....

1 Like