I'm trying to read a string like this one:


over a serial connection and split this string afterwards by a ";". So the output would be this array:


All the stuff runs on an arduino uno. So I made some code:

String read_input;
char read_input_char[20];
char *read_values;

void setup() {



void loop() {
    read_input = Serial.readString();
    read_input.toCharArray(read_input_char, 20);

    read_values = strtok(read_input_char, ";");

However now instead of outputting the 3rd array element it outputs only an '1'. What's wrong?

  • strtok() doesn't do that. And the characters available to Serial.readstring() may not have the full string in the buffer when loop() first calls it.
    – Dave X
    May 10, 2016 at 17:51

2 Answers 2


This is a C question, not an arduino question, but anyway:

From the man page:

   The  strtok()  function  breaks a string into a sequence of zero or more nonempty tokens.  On the first call to strtok() the
   string to be parsed should be specified in str.  In each subsequent call that should parse the  same  string,  str  must  be

You are only calling strtok() once, so it only parses the first value. You need to keep calling it until there is nothing left to parse.


Your code as is checks for a non-empty Serial buffer (the Serial.available() bit), and then reads it all into a String. Be careful doing this, as if this happens to execute part-way through the data arriving, you will get a partial data String. This may be cut off part way through one of your integers.

Also, the String object is very memory-intensive. Maybe not an issue for you, but try to use char arrays, or read directly from the Serial buffer.

In your case, there is a great function that you could use; parseInt().

Details are at; https://www.arduino.cc/en/Serial/ParseInt

More or less, this reads successive chars from the serial buffer, and once digits are encountered, forms an int. This continues until a non-digit char, or a (configurable) timeout. Something like this might do the job for you;

int data[10]; // Or however long you need it to be!
int arrayPointer = 0;

void setup() {

void loop() {
  if (Serial.available()) {
    int incomingInt = Serial.parseInt();
    data[arrayPointer] = incomingInt;

Hope this helps!

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.