Read more then 1 character from PC/serial?


I have gotten my Arduino to detect and understand single characters from my PC serial but it won't take more then just 1 character..

If I enter the character BC and hit send from the serial monitor no such luck but if I enter A or C it works fine...

the code

int ledPin = 9; // LED connected to digital pin 9 void setup() { // initialize the serial communication: Serial.begin(115200); }

void loop() { char srlcmd; if (Serial.available()) { srlcmd =;

if (srlcmd == 'A' ) { analogWrite(ledPin, 1); } else if (srlcmd == 'BC' ) { analogWrite(ledPin, 25); } else if (srlcmd == '0' ) { analogWrite(ledPin, 0); } } delay(30); }

end Code

Thanks for any help! Shane

You might try searching a phrase like "arduino serial read string".

Reading more than 1 character comes up very (very, very very) often.

One simple way to capture a string:

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
        Serial.println("serial test 0021"); // so I can keep track of what is loaded

void loop() {

        while (Serial.available()) {
      if (Serial.available() >0) {
        char c =;
        readString += c;}
      if (readString.length() >0) {

ok i can now get it to read in the string but no luck in comparing them... I have tried on my own a bit with such code as:

  • If (strcmp(readString, "hello") == 0)

But i get error: "error: cannot convert 'String' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'"

so how do compare the string coming from the serial port to a predefined string in code?

Thanks for the help! Shane

You might look at some of the operations below to see if they might be of use.