# Serial.read() problem

**URL:** <https://forum.arduino.cc/t/serial-read-problem/1388203>\
**Category:** Programming\
**Created:** [June 11, 2025, 10:06am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203 "2025-06-11T10:06:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonczydlo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bonczydlo/32/1129860_2.png) [@bonczydlo](https://forum.arduino.cc/u/bonczydlo)\
**Post date:** [June 11, 2025, 10:06am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/1 "2025-06-11T10:06:21Z")

</div>

Hi, i'm trying to find a solution to my problem with serial communicationvia arduino.  
First of all i want to communicate and automate this process with bt module.

```arduino
while(btModule.available()>0){
  if (Serial.available()) {
    static uint8_t choice = Serial.read();
    char x = "AT+";
        if (wybor == 1) {
      z = btModule.read(); 
      btModule.print((char)Serial.read());
       choice=0;
      }
    }
  }

```

Code i wrote. And i want to do is press button on keyboard and bt module will answer to AT commands.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 11, 2025, 10:10am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/2 "2025-06-11T10:10:15Z")

</div>

Welcome to the forum

We will need more information in order to help

Start by posting the full sketch

Which Arduino board are you using ?

```arduino
char x = "AT+";

```

There is an obvious problem here because a char variable can only hold a single character

---

<div class="post-metadata">

**Author:** ![bonczydlo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bonczydlo/32/1129860_2.png) [@bonczydlo](https://forum.arduino.cc/u/bonczydlo)\
**Post date:** [June 11, 2025, 10:15am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/3 "2025-06-11T10:15:10Z")

</div>

So I'm using arduino uno.

```arduino
#include "Arduino.h"
#include "SoftwareSerial.h" 
#define DPIN_BLUESRL_RX 2  
#define DPIN_BLUESRL_TX 3  

SoftwareSerial btModule(DPIN_BLUESRL_RX, DPIN_BLUESRL_TX);  

void setup() {

  Serial.begin(115200);  
  Serial.println("Bluetooth works);
  btModule.begin(115200);  

void loop() {
  static uint8_t menuPrinted = 0;

  if (0 == menuPrinted) {
    menuPrinted = 1;
    Serial.println("1 - Scan bt devices");
  }
  int x= Serial.read();

  /**/
  while(btModule.available()>0){
  if (Serial.available()) {
    static uint8_t choice = Serial.read();
    char x = "AT+";
        if (choice == 1) {
      z = btModule.read(); 
      btModule.print((char)Serial.read());
       choice=0;
      }
    }
  }
} 

```

That's the whole code. Im trying to understand serial.read() but its not going well.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [June 11, 2025, 10:15am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/4 "2025-06-11T10:15:33Z")

</div>

> [@bonczydlo](#):
>
> Code i wrote.

The code looks the mess. Could you please explain it line by line?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 11, 2025, 10:18am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/5 "2025-06-11T10:18:31Z")

</div>

```arduino
SoftwareSerial btModule(DPIN_BLUESRL_RX, DPIN_BLUESRL_TX);

```

The pin numbers for SoftwareSerial are Rx pin then Tx pin. The BT module Rx pin should be connected to the serial Tx pin and vice versa.

Have you got the BT module connected correctly ?

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [June 11, 2025, 10:18am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/6 "2025-06-11T10:18:36Z")

</div>

suggest you try using `readBytesUntil()`

```arduino
// -----------------------------------------------------------------------------
void
loop ()
{
    if (Serial.available ()) {
        char buf[10];
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0'; // terminate with nul

        Serial.println (buf);
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);
}

```

---

<div class="post-metadata">

**Author:** ![bonczydlo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bonczydlo/32/1129860_2.png) [@bonczydlo](https://forum.arduino.cc/u/bonczydlo)\
**Post date:** [June 11, 2025, 10:19am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/7 "2025-06-11T10:19:37Z")

</div>

Yes i am. I've tested if the serial communication works and bt module answer to my AT commands.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [June 11, 2025, 10:21am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/8 "2025-06-11T10:21:21Z")

</div>

The code you posted in the first message not looks as the part of your "full code" posted later.

For example - the tested variable now the `choice` rather than `wybor` in the first code.

> [@bonczydlo](#):
>
> ```arduino
> static uint8_t choice = Serial.read();
> char x = "AT+";
> if (choice == 1) {
> 
> ```

What version do you actually load into the board ?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 11, 2025, 10:21am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/9 "2025-06-11T10:21:43Z")

</div>

```arduino
 int x= Serial.read();

```

and

```arduino
char x = "AT+";

```

Even if a char could hold multiple characters things could get very messy if you use the same variable name for two different variables

Just noticed something else.  
Where is the closing brace for the code in setup() ?

---

<div class="post-metadata">

**Author:** ![bonczydlo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bonczydlo/32/1129860_2.png) [@bonczydlo](https://forum.arduino.cc/u/bonczydlo)\
**Post date:** [June 11, 2025, 10:22am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/10 "2025-06-11T10:22:09Z")

</div>

Why use this specific?  
I'm a begginer (yes you can easily tell that) and don't know why this if bt module return char.

---

<div class="post-metadata">

**Author:** ![bonczydlo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bonczydlo/32/1129860_2.png) [@bonczydlo](https://forum.arduino.cc/u/bonczydlo)\
**Post date:** [June 11, 2025, 10:23am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/11 "2025-06-11T10:23:34Z")

</div>

Oh, sorry

```arduino
int x = Serial.read();

```

its just a typo that i was to delete earlier

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [June 11, 2025, 10:24am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/12 "2025-06-11T10:24:43Z")

</div>

> [@bonczydlo](#):
>
> don't know why this if bt module return char.

Do you understand the difference between 1, '1' and "1" ? What value do you sending to the Serial when making a "choice" ?

> [@bonczydlo](#):
>
> ```arduino
> static uint8_t choice = Serial.read();
> 
> if (choice == 1) {
> 
> ```

---

<div class="post-metadata">

**Author:** ![bonczydlo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bonczydlo/32/1129860_2.png) [@bonczydlo](https://forum.arduino.cc/u/bonczydlo)\
**Post date:** [June 11, 2025, 10:25am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/13 "2025-06-11T10:25:31Z")

</div>

I was copying in a parts, the code is in mess and a lot of comments. The closing brace for the code in setup() is, i just didnt catch that in

---

<div class="post-metadata">

**Author:** ![bonczydlo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bonczydlo/32/1129860_2.png) [@bonczydlo](https://forum.arduino.cc/u/bonczydlo)\
**Post date:** [June 11, 2025, 10:27am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/14 "2025-06-11T10:27:43Z")

</div>

"1" is string and '1' is character

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [June 11, 2025, 10:28am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/15 "2025-06-11T10:28:16Z")

</div>

> [@bonczydlo](#):
>
> I was copying in a parts,

It is not a good idea.

Copy the code to the empty arduino sketch, fix the typos and try to compile.  
ONLY post the code to the forum if it compiled fine.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 11, 2025, 10:28am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/16 "2025-06-11T10:28:21Z")

</div>

> [@bonczydlo](#):
>
> its just a typo that i was to delete earlier

If you don't post the code that you are actually using it is difficult to help

Please post your actual code in a new reply. DO NOT edit your previous reply and post it there

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [June 11, 2025, 10:29am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/17 "2025-06-11T10:29:07Z")

</div>

> [@bonczydlo](#):
>
> "1" is string and '1' is char

and what is 1 in your line below?

> [@bonczydlo](#):
>
> `if (choice == 1) {`

---

<div class="post-metadata">

**Author:** ![bonczydlo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bonczydlo/32/1129860_2.png) [@bonczydlo](https://forum.arduino.cc/u/bonczydlo)\
**Post date:** [June 11, 2025, 10:29am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/18 "2025-06-11T10:29:27Z")

</div>

to be honest i dont really know what i was thinking in this part of code

```arduino
static uint8_t choice = Serial.read();

if (choice == 1) {

```

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [June 11, 2025, 10:30am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/19 "2025-06-11T10:30:27Z")

</div>

Is it means that now you changed your mind about these lines? - if so, edit them.

---

<div class="post-metadata">

**Author:** ![bonczydlo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bonczydlo/32/1129860_2.png) [@bonczydlo](https://forum.arduino.cc/u/bonczydlo)\
**Post date:** [June 11, 2025, 10:37am UTC](https://forum.arduino.cc/t/serial-read-problem/1388203/20 "2025-06-11T10:37:27Z")

</div>

```arduino
#include "Arduino.h"
#include "SoftwareSerial.h"

#define DPIN_BLUESRL_RX 2
#define DPIN_BLUESRL_TX 3

SoftwareSerial btModule(DPIN_BLUESRL_RX, DPIN_BLUESRL_TX);

void setup() {

  Serial.begin(115200);
  Serial.println("Bluetooth works");
  btModule.begin(115200);
}

void loop() {
  static uint8_t menuPrinted = 0;

  if (0 == menuPrinted) {
    menuPrinted = 1;
    Serial.println("1 - Scan bt devices");
  }

  while (btModule.available() > 0) {
    if (Serial.available()) {
      static uint8_t choice = Serial.read();
      char x = "AT+";
      if (choice == 1) { //in my mind i wanted to press 1 and arduino will read my choice and send it to bt module
        int z = btModule.read();
        btModule.print((char)Serial.read());
        choice = 0;
      }
    }
  }
}

```

[Next page](https://forum.arduino.cc/t/serial-read-problem/1388203.md?page=2)
