Today I learned how to receive input from the Serial Monitor in Arduino. This helps the Arduino interact with the user by receiving commands or data typed from the computer.
Serial.available()
to check if data is availableSerial.read()
and Serial.readStringUntil()
to receive dataSerial.available()
– Returns the number of bytes available for readingSerial.read()
– Reads the first available byte of incoming dataSerial.readStringUntil(char)
– Reads characters from the serial buffer until a delimiter is
foundSerial.parseInt()
, Serial.parseFloat()
– Useful for reading numbersI had some confusion with Serial.readStringUntil()
needing a char
instead of a string,
but I fixed it by using single quotes (e.g. '\n'
).
void setup() {
Serial.begin(9600);
Serial.println("Enter your name:");
}
void loop() {
if (Serial.available()) {
String name = Serial.readStringUntil('\n');
Serial.print("Hello, ");
Serial.println(name);
}
}
Receiving input from the Serial Monitor opened up new possibilities for interaction. I’m looking forward to controlling LEDs or sensors using typed commands next.