# Sending Bytes to Arduino via Serial Node

**URL:** <https://discourse.nodered.org/t/sending-bytes-to-arduino-via-serial-node/60113>\
**Category:** General\
**Created:** [20 March 2022 16:08 UTC](https://discourse.nodered.org/t/sending-bytes-to-arduino-via-serial-node/60113 "2022-03-20T16:08:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rayank97](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@rayank97](https://discourse.nodered.org/u/rayank97)\
**Post date:** [20 March 2022 16:08 UTC](https://discourse.nodered.org/t/sending-bytes-to-arduino-via-serial-node/60113/1 "2022-03-20T16:08:01Z")

</div>

Hello everyone,

I have a project where I need 6 function blocks, each performing the functions of a button (0/1), to send their value in binary via the Serial node. As of now, I'm testing this with just 1 such function block. I have the function block for button 1 set up to send out 00000100 as ON and 00000000 as OFF. My question is, how do I configure the Serial Node to send this as a byte so that it will be compatible with my sketch on Arduino (send as uint8). Here is the bit of the Arduino sketch which is reading the button state:

```auto
int switch2;
int switch3;
const uint8_t virtual_switch_masks[8] = {
    0,
    0,   
    1<<0, // pin 2: virtual switch 0
    1<<1, // pin 3: virtual switch 1
    1<<2,
    1<<3,
    1<<4,
    1<<5
    // etc...
}; 
uint8_t virtual_switch_states = 0;
int virtualDigitalRead(uint8_t pin) {
    uint8_t mask = virtual_switch_masks[pin];

    // If this is not a virtual switch, do a regular digitalRead().
    if (!mask)
        return digitalRead(pin);

    // For a virtual switch, extract the relevant bit
    // from virtual_switch_states.
    if (virtual_switch_states & mask)
        return HIGH;
    else
        return LOW;
}

void loop(){
  if (Serial.available()){
    virtual_switch_states = Serial.read();
  }
  switch2=virtualDigitalRead(2);
  digitalWrite(9,switch2);
}

```

Here's a screenshot of my flow right now.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/498084583b96c8fa810db226d099827a5a852687.png)

What you need to know about the pink nodes: The first 2 are simply for initialization. The next one, returns a value between 0-100. Here's a screenshot of my function block 'Button 1':

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/218293c51e0a4694d8389399cd07067f32868065.png)

As per my limited understanding, my LED connected to pin 9, should light up, when the payload from the pink node goes above 50, and the function payload becomes [0,0,0,0,0,1,0,0]. I've set the Serial node output to be binary buffers as well. I don't understand what's wrong.

Please help.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [21 March 2022 07:23 UTC](https://discourse.nodered.org/t/sending-bytes-to-arduino-via-serial-node/60113/2 "2022-03-21T07:23:02Z")

</div>

In my opinion you write not an uint8- byte - your buffer is 8 bytes long instead of 1 byte

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 March 2022 08:04 UTC](https://discourse.nodered.org/t/sending-bytes-to-arduino-via-serial-node/60113/3 "2022-03-21T08:04:21Z")

</div>

As your sketch is expecting a single number, you should send a single number

try

```javascript
const all_off = 0x0;
const pin2 = 0x4; // hex 4 = dec 4, bit3 on, all others off : 00000100
// or const pin2 = 0b00000100; //binary 4 in JS is written as 0b00000100
const pin3 = 0x8; // hex 8 = dec 8, bit4 on, all others off : 00001000
// or const pin3 = 0b00001000; //binary 8 in JS is written as 0b00001000
const pin2and3 = pin2 | pin3;// the | operator is bitwise OR 
msg.payload = pin2;
return msg;

```

In fact, no need for function nodes in your whole flow, you could just use a switch node with 2 entries (Output 1: determine \>50, Output 2: "otherwise") and 2 change nodes to set payload to a number - 0 (for all off), 4 (for pin2), 8 (for pin3), 16 (for pin4) etc.

---

<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:** [20 May 2022 08:04 UTC](https://discourse.nodered.org/t/sending-bytes-to-arduino-via-serial-node/60113/4 "2022-05-20T08:04:28Z")

</div>

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