Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8303317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:29:54+00:00 2026-06-08T17:29:54+00:00

Alright, I’ve googled getting a string from Serial with Arduino and I’ve had no

  • 0

Alright, I’ve googled getting a string from Serial with Arduino and I’ve had no luck even copy and pasting examples.

I’m trying to get a string from the Serial. Here’s my code:

void setup() {
    Serial.begin(9600);
    Serial.write("Power On");
}

void loop()
{
    while(!Serial.available());

    while (Serial.available() > 0) {
      Serial.write(Serial.read());
    }
    Serial.println();
}

And it’s printing out character by character.

I also tried

char* read(int len) {
  while (!Serial.available());
  char str[len];
  int i = 0;
  while (i < len) {
    str[i] = '\0';
    int inByte = Serial.read();
    Serial.println(inByte);
    if (inByte == -1) {
        return str;
    } else {
      str[i++] = inByte;
    }
  }
  return str;
}

And it returns 1 character at a time (serial.print(inByte) gives -1 every other time). Why is the Serial splitting each character?

If I enter ‘hello’ and I call serial.read() it gives a character then says there’s nothing, then gives another character and says there’s nothing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:29:56+00:00Added an answer on June 8, 2026 at 5:29 pm

    I figured it out.

    When you open a Serial with 9600 baud (Serial.begin(9600);), it’s reading/writing at 9600 bytes per second. That means at fastest it can get just under 10 bytes per millisecond. I don’t know what the operating speed is, but it seems like the Arduino gets alerted of and reads the first byte before the second one arrives. So, you must add a delay(1) to “wait” for another byte in the “same stream” to arrive.

    String read() {
        while (!Serial.available()); //wait for user input
        //there is something in the buffer now
        String str = "";
        while (Serial.available()) {
            str += (char) Serial.read();
            delay(1); //wait for the next byte, if after this nothing has arrived it means the text was not part of the same stream entered by the user
        }
        return str;
    }
    

    You may ask, well since you’re delaying how do you know if the user is just typing very fast? You can’t avoid it here, since the Serial is essentially limited at a certain speed. However, the user must be typing virtually-impossibly-fast for two inputs to be confused as one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, here I am again trying to write code from scratch and I can't
Alright so I have no idea how to even begin doing this But basically
Alright I created some custom classes for my project sourced from this tutorial ,
Alright, here's what I'm trying to do. I'm attempting to write a quick build
Alright, here is my problem i'm trying to solve. I have an index page
Alright I'm pretty new to programming and stuff and I'm now trying to code
Alright, so I've been trying to implement a simple binary search tree that uses
Alright, so here is the property I have public List<String> names{ get{ lock(_names) return
Alright so i have been working on this Dynamic load of a spinner from
Alright so I have recently made the decision to put every string in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.