# Nextion and relays programming

**URL:** <https://forum.xod.io/t/nextion-and-relays-programming/4396>\
**Category:** XOD Programming Questions\
**Created:** [September 2, 2020, 10:31pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396 "2020-09-02T22:31:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcod](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marcod](https://forum.xod.io/u/marcod)\
**Post date:** [September 2, 2020, 10:31pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/1 "2020-09-02T22:31:23Z")

</div>

I am very new to xod and arduino, so please forgive me, Could someone help. I need a simple program in xod that thurs a 5v relay on and off on a bread board. I have the nextion all set with a dual state switch so it can toggle the relay. But I can’t get it to communicate with the relay. i am using Wayland library. Thank you so much

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [September 2, 2020, 11:58pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/2 "2020-09-02T23:58:31Z")

</div>

There is a relay node (which is basically just a digital-write). Use it to send 0/false (off) or 1/true (on) to the port the relay is wired to. relay-ON is the pin to determine if relay is on or off. relay-ACT (active) must be true for relay to change (otherwise relay-ON is ignored)

---

<div class="post-metadata">

**Author:** ![marcod](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marcod](https://forum.xod.io/u/marcod)\
**Post date:** [September 3, 2020, 12:18pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/3 "2020-09-03T12:18:06Z")

</div>

![relay](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/f/ffee08eeb4931fc42bc3460c8c50a221f3c92107.jpeg)  
Thank you for the response. This is how I have it set up the leds work with the buttons but the relay still does not. What am I missing?

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [September 3, 2020, 12:24pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/4 "2020-09-03T12:24:43Z")

</div>

Is relay really connected to pin D0? If you swap relay and LED, does LED still work with bt2 & relay not work with bt1?

---

<div class="post-metadata">

**Author:** ![marcod](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marcod](https://forum.xod.io/u/marcod)\
**Post date:** [September 4, 2020, 5:08pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/5 "2020-09-04T17:08:28Z")

</div>

II replaced all the led with relays. I found the issue it was a wiring issue. What seems to happen I bt0 works and drives the relay it is assigned. But bt1 stops driving its relay. bt2 works and dirves its relay. I erase the nodes for bt1 (string constant and read number and put news ones in and then bt0 stops works and bt1 and bt 2 work fine. (even when I cut and paste or put in new node from scratch. it does not matter.) this issues can rotate . is there a bug in Xod when driving more than 1 relay?

---

<div class="post-metadata">

**Author:** ![marcod](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marcod](https://forum.xod.io/u/marcod)\
**Post date:** [September 4, 2020, 5:16pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/6 "2020-09-04T17:16:08Z")

</div>

sorry it does not make any sense. It does this error with both led or relays

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [September 4, 2020, 6:13pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/7 "2020-09-04T18:13:04Z")

</div>

“driving the relay” is just doing a digital write to a specific pin, so more than one is not a problem UNLESS there are multiple “relay” nodes with the same pin specified. If you have more than one node controlling the same pin, then only one can be active at a time. You can use the relay-ACT pin to make only one active at a time, but generally the better solution is to only have one “relay” node for each relay.

If it works fine when using 3 LEDs, but relay doesn’t work, then probably either pin numbers are wrong on your relay nodes, or there is a wiring problem.

---

<div class="post-metadata">

**Author:** ![marcod](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marcod](https://forum.xod.io/u/marcod)\
**Post date:** [September 4, 2020, 6:27pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/8 "2020-09-04T18:27:40Z")

</div>

Thanks for the follow up. I checked all wires no issues. each relay is driven by a different pin, different switch. It just seems like there is a bug it does the same thing on both relays and Led. one switch will work one will not. Then I delete the switch not working on xod and renter the info again and it works and the other one does not. all wiring is staying the same. all pins the same. only does this when i use the nextion switches and wayland read-number. please see example of the program in question

 ![issue](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/b/b796bf1bfd6a01e92fcb7efe587f25dd64b164ea.jpeg)

---

<div class="post-metadata">

**Author:** ![marcod](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marcod](https://forum.xod.io/u/marcod)\
**Post date:** [September 4, 2020, 6:28pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/9 "2020-09-04T18:28:13Z")

</div>

please ignore bt2 thank you

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [September 5, 2020, 12:14am UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/10 "2020-09-05T00:14:22Z")

</div>

You say you are using switches, but you are using analog-read. LED will work with analog input, but relay is expecting digital input (0 or 1, not a number between 0 and 1).

I don’t know what your write-number/read-number nodes are supposed to do, but you are writing val0, then reading bt0. I suspect your problem is with how you are using these. You can verify by connecting the button output directly to the LED or relay.

---

<div class="post-metadata">

**Author:** ![marcod](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marcod](https://forum.xod.io/u/marcod)\
**Post date:** [September 5, 2020, 12:34am UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/11 "2020-09-05T00:34:46Z")

</div>

thank you for following up. I should of mentioned I did directly go to the relay and leds. Like I said it works. The question I have is why does it work and then other times not work? It seems like the switches are good but then one of them stops and once I remove it and readd it, it works again then the other switch stops working

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [September 5, 2020, 3:01am UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/12 "2020-09-05T03:01:19Z")

</div>

You have the same problem without the write-numer, read-number nodes? What kind of switches are you using that you are reading with the analog-read? If they are on/off switches, you should be using digital-read instead of analog-read. Do you have pull-up (or pull-down depending on wiring) resistors on your switches? Without those, the value of the switch will “float” when the switch is off, so the arduino will not know for sure if it is on or off.

---

<div class="post-metadata">

**Author:** ![marcod](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marcod](https://forum.xod.io/u/marcod)\
**Post date:** [September 5, 2020, 9:57am UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/13 "2020-09-05T09:57:17Z")

</div>

the switches I am using are called dual state buttons on the nextion. The nextion is the switches (dual state buttons) in my project. yes they are simple on and off. I don’t know if you can see the code from xod I posted? If so do you still use pull up or pull down nodes? I am so thankful for all your help

---

<div class="post-metadata">

**Author:** ![marcod](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marcod](https://forum.xod.io/u/marcod)\
**Post date:** [September 5, 2020, 10:02am UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/14 "2020-09-05T10:02:09Z")

</div>

I should add do you still also need resistors for the switches (buttons on nextion) for pull up and down or nodes with pull up or down? I am using wayland nextion library example and his nodes

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [September 5, 2020, 1:50pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/15 "2020-09-05T13:50:25Z")

</div>

If they are on/off switches, you should be using digital-read instead of analog-read.

If the resistors are not built in to the switches, you will need to add them to the wiring. If your switch is wired from Arduino pin to V+, then the pin should have a resistor to ground (usually about 10K ohms resistor; exact value doesn’t matter, but it should be pretty high to avoid creating a short). If your switch is wired from pin to ground, then the pin should have a resistor to V+ instead of ground. This gives the Arduino a specific value to read when the switch is off, but the high resistance allows the switch to over ride that value when the switch is on.

---

<div class="post-metadata">

**Author:** ![marcod](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marcod](https://forum.xod.io/u/marcod)\
**Post date:** [September 5, 2020, 3:02pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/16 "2020-09-05T15:02:23Z")

</div>

the switches are designed in nextion tft. The nextion is a touch screen and i am using the switched (dual state button) on the nextions touch screen. the the nextion is the the switch sending rx tx serial signal to the relays. So the read function that wayland has in his example are wrong? there would be no way to put resistors on a tft touch screen switch since it is in image of a switch on a touch screen. does that help explain it more?

---

<div class="post-metadata">

**Author:** ![marcod](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marcod](https://forum.xod.io/u/marcod)\
**Post date:** [September 5, 2020, 3:03pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/17 "2020-09-05T15:03:04Z")

</div>

the library I used is wayland nextion

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [September 5, 2020, 4:26pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/18 "2020-09-05T16:26:29Z")

</div>

Sorry. Resistor and digital-read comments won’t apply to this project. You are using virtual buttons on a display not physical buttons.

The example uses va0.val for write-number. You use val0.val, which could be a problem.

The XOD library isn’t very clear what these nodes are actually doing. Open the nextion-serial1 node and open the not-implemented-in-xod node. At the top of the code, the #pragma XOD require line indicates this is a wrapper for [https://github.com/Seithan/EasyNextionLibrary](https://github.com/Seithan/EasyNextionLibrary). Opening that URL, there are instructions that indicate the write-number nodes are for updating the nextion display and have nothing to do with what is read using read-number nodes. The github page offers some insight, but it is not very clear what CMD options are available beyond giving a few examples. I’m guessing there is nextion usage documents that would provide that information. [https://nextion.tech/](https://nextion.tech/) has some information, but it is focused on their editor for programming the display, not interfacing to it.

If you can run your code in debug mode and still communicate with the nextion, replace the relay/led nodes with watch nodes to see what values are actually being returned. Trying to run serial communication to nextion & your computer for debug at the same time may not work with your Arduino model. I assume you have the display programmed with at least 3 buttons and fields to display the numeric data you are trying to write using write-number…

---

<div class="post-metadata">

**Author:** ![wayland](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/wayland/32/1590_2.png) [@wayland](https://forum.xod.io/u/wayland)\
**Post date:** [September 7, 2020, 10:54pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/19 "2020-09-07T22:54:45Z")

</div>

Hi @marcod,

Sorry it’s taken me a while to respond. If you simply want to control two relays using dual-state buttons on the nextion display, the following patch will work:  
 ![relay-patch](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/5/549f91b3ffdd7e1fa71d5dae6761a50586fbf994.png)

XODBALL:  
[relay-test.xodball](https://forum.xod.io/uploads/short-url/63h8WHTeY0VX009LYmxVispwJH4.xodball) (3.7 KB)

This can be paired with the following Nextion GUI with two buttons named bt0 and bt1:

 ![nextion-gui](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/7/716e43433f7d0aaa29f74d35c62c3e0ae22a5c57.png)

The command sent to read-number comprises two parts separated by a dot. The part before the dot is the name of the object in the gui, in this case a button called **bt0** or **bt1**. The part after the dot is the attribute of that object we would like to read, in this case **val** (value).

I’ve tested the patch on an Arduino mega and Nextion NX8048K070\_11. Hopefully this will work for you, but please let me know if you run into any problems.

---

<div class="post-metadata">

**Author:** ![marcod](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marcod](https://forum.xod.io/u/marcod)\
**Post date:** [September 8, 2020, 1:50pm UTC](https://forum.xod.io/t/nextion-and-relays-programming/4396/20 "2020-09-08T13:50:30Z")

</div>

thank you so much for the follow up Wayland. What I am noticing is that one of the nextion switch seem to stop working. But the other works. I am not sure why. It is hard to explain it on here. Maybe a call would be easier to explain? Are you in the USA? I am and maybe we could could discuss? I have set up the exact same way you have in your example.

[Next page](https://forum.xod.io/t/nextion-and-relays-programming/4396.md?page=2)
