# Serial communication from node-red to arduino

**URL:** <https://discourse.nodered.org/t/serial-communication-from-node-red-to-arduino/40136>\
**Category:** General\
**Created:** [31 January 2021 22:00 UTC](https://discourse.nodered.org/t/serial-communication-from-node-red-to-arduino/40136 "2021-01-31T22:00:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stevenvdw](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@Stevenvdw](https://discourse.nodered.org/u/Stevenvdw)\
**Post date:** [31 January 2021 22:00 UTC](https://discourse.nodered.org/t/serial-communication-from-node-red-to-arduino/40136/1 "2021-01-31T22:00:22Z")

</div>

Hello

Hope you guys understand my pore English. Sorry for that.

I have made a button in node-red that sends a 0 or 1 through serial communication to my arduino. (Represents the button state). In arduino I have an if statemant when the value is 1, do something. But i can not let it work.

The button is a simple ui-switch connected to a serial-out-node

I receive the message send to the arduino. When i output this message back i get 48 and 49.  
I know this is the decimal code for 0 and 1. The problem is i can not solve the problem to compare this value in arduino.

```auto
if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    if (incomingByte ==1 ){
      puzzelstate=2;
      }
    
  }

```

I tried different things, equal to 1, equal to 49 or even equal to 110001, send it as string, number,...  
But the if statement never come true.

What am I doing wrong?

Thanks  
Steven

---

<div class="post-metadata">

**Author:** ![Stevenvdw](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@Stevenvdw](https://discourse.nodered.org/u/Stevenvdw)\
**Post date:** [31 January 2021 22:04 UTC](https://discourse.nodered.org/t/serial-communication-from-node-red-to-arduino/40136/2 "2021-01-31T22:04:27Z")

</div>

Maybe i found the solution. I am checking my post on the forum and see that my if-statement is in the serial.available statement. Maybe this is the mistake. I try it out and let you know. My brains must be tired.

---

<div class="post-metadata">

**Author:** ![Stevenvdw](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@Stevenvdw](https://discourse.nodered.org/u/Stevenvdw)\
**Post date:** [31 January 2021 22:11 UTC](https://discourse.nodered.org/t/serial-communication-from-node-red-to-arduino/40136/3 "2021-01-31T22:11:09Z")

</div>

Yeps that solved my problem. Crazy i overlooked that. I changed my code to

```auto
if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    
    }
    if (incomingByte == 49 ){
      puzzelstate=2;
      }

```

It works now, but how can i output from node-red, so i receive a 0 or 1?

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [31 January 2021 22:44 UTC](https://discourse.nodered.org/t/serial-communication-from-node-red-to-arduino/40136/4 "2021-01-31T22:44:21Z")

</div>

To make it more readable

```auto
if (incomingByte =='1' )

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 April 2021 22:44 UTC](https://discourse.nodered.org/t/serial-communication-from-node-red-to-arduino/40136/5 "2021-04-01T22:44:35Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
