Start a new topic

Use slider as dimmer

For several days now, I've been trying to write a code for a Nextion slider to control the brightness of a LED.

But I can't find any solutions that work. It's mainly how to receive the slider value from the Nextion on the Arduino and using that value to control the LED brightness.

Any suggestions of examples that work?


I have a similar problem. Adding a slider to a layout it brings up a touch move event. Unlike the touch press and touch release events it has no checkbox. I cannot find anywhere how to setup the touch move event to send data to the Arduino.

I am sure its a simple one line of code but cant find any examples.

Any help appreciated.



I think I have solved our problem. Using the touch move event is not the way to go.

Setup a slider at h0

Set the touch release event tick box then add "print h0.val" as a second event.

This way the Nextion transmits to the Arduino a  touch release event followed by a second line which is the value at which it was released. You will need to crunch the output a bit to get a usable value but easily dooable in the Arduino and I am sure I will be able to adjust my light levels with this output.


Neil, Thank you, so far I think this will have to work for now. I looked at the CompSlider example, works almost the same as you found out. I hope there is a way for a touch move value to read with MCU. Must be possible, right? I want to add a slider to control a RGB LED as well, so it would be nice to be able sliding while you set the desired color. Thanks so far Neil

If you change my suggestion so it sends a command on touch press then move the print h0.val to the Touch Move even.

In this way you know which slider is touched and then you get a live stream of the values. I am not sure it will be a smooth stream of data but it does seem to work.


1 person likes this
Neil, Thank you, I will try that. Which command line do you use on Arduino to call/read the h0 value?

I have to say I find the whole Nextion library a bit of a juggernaut. I feel its way more complicated than necessary so  I have moved across to a smaller library you can find hear.

by Bently Born

This one I could understand very quickly as the examples and the library are well documented and I suspect it will be much easier to do what you want.

Word of warning. If you are using a Mega or another Arduino with Hardware interrupt you will need to modify the library a little bit, but again its well documented.

using the above library and setting up the display as we discussed above it should "push" everything you need to the Arduino so you can setup a listening loop and decode what is sent. Although the examples dont cover sliders I am pretty sure you will be able to make it work by modifying the examples in this Library when you see the data you are getting at the Arduino.

I have got to get exactly this working for myself in the next few weeks so will post some code when its done.


2 people like this

Nice Neil!

Thank you for the link to the alternative library. Looking forward to see your code!

Programming has many methods.  Another way accomplishing this:

In the Slider components, check the Send Component ID in the Press Release event.

This will be sent to be caught by your Arduino in the format:
0x65 0x06 0x01 0x00 0xFF 0xFF 0xFF.

0x65 says this is a press release event. 0x06 says this component is on page 6 in my HMI file. The 0x01 says that this event comes from component 1 (my red slider), and the next 0x00 is the event type (1 for press and 0 for release) the last three 0xff bytes are the end of data.

In my project I have three such sliders, red/green/blue. h0, h1 and h2.  I also have text values underneath that allowed me to see the 0 to 255 value for R/G/B and it is in the sliders OnMove Event that I calculate for the Nextion side to display R/G/B Hex values, the 565 equivalent value, and set a text box background to my selected color.

Something that could be done is when you your Arduino as caught the release event, is to create your block of code around that release event (rather than catch events and also catch codes).

Since you know the slider component for the event, your design is already stating you are going to respond, you could send (get t0.txt / get h0.val) within your block and catch the result from the 0x70 (string) or 0x71 (number) data that will result from the get command. 

It isn't that we end up with more programming, but if someone presses several things at once, the value of printh h0.val may become ambigious if not with the block but in a general catch - from which component is the printh for.  While still in the block you can deal with changing the LED color.  Everything for that slider press event has been taken care of before moving on.

This could be improved on, by calculating the exact value needed for the LED in (merging h0/h1/h2 values) in a variable on the nextion, making it not necessary to be calculated on the Arduino, and also better graphics are always a plus.

Like your approach Patrick, you mind sharing your HMI and INO file? I would like to see how you implement this into your code. :)

Hey Fritz,

My implementation was not Arduino but rather in Linux on an Intel Edison, my source code was pascal. 

I am unable to post the HMI to this as the above although similar in concept, was an RGB to 565 utility from an earlier project long since morphed.  What I could do is create another HMI and provide pseudo on the comms as per the Nextion Instruction Set, but I don't do Arduino.  This portion you would be responsible for.

Give me a while to put that HMI together for you.  I would also need to know exactly what your LED would be looking for as far as format (RGB hex FFFFFF, or decimal, three parameters or what)

Ok Fritz,

It is close to the older one, leaving out the hex conversions on the screen, doesn't require so many variables and fixed up some math in the calculations in the HMI.  I also changed the sending component to make it less complicated on the host Arduino. So the gist on the user interface is they have three sliders for R,G,B and as they change the slider, it updates the color the led should produce.

In the HMI, Page PostInitializaton Event, the sliders are initialized to default, slider values h0,h1,h2 are sent to their numeric value n0,n1,n2 under the slider, and the slider value is stored in a variable c0,c1,c2, and give the crgb and c565 their initial default values in case called before a slider move.

In each of the slider TouchMove Events, the slider value is again sent to its c variable and updates its n component for this slider.  This is followed by the recalculations for crgb and c565 variables, changes to the background color of the big text box and refreshes the textbox with the new color.

Making things less complicated is a single Send Component ID in the textbox Touch Release Event.  So the user can play with the sliders all they want till the find a color they like without creating a flurry of traffic over the RX/TX lines.  When they are ready to set the LED, they tap the big textbox (that is showing the color they are to receive) and that sends 0x65 0x00 0x04 0x00 0xFF 0xFF 0xFF over the wire to your Arduino.  The Arduino coding is up to you, as I don't do Arduino.

The nextion library link that Neil posted, I did take a glance and found the format of the Message truncates leading zeros, creates an FFFF instead of FF (of which byte(FFFF) would be FF, but if I am waiting for 255 and get 65535 or vice versa yeah, and that is why a detest Arduino code)  so my code is not actual Arduino code, but the gist would be something like ...

int function convert_response_to_integer (b : array); {

  int rgbval;

  if (b[0] = 0x71) {  // b[0] first byte as 0x71 is numberic data received.

     rgbval = b[3]*65536+b[2]*256+b[1]; 

     // b[3] forth byte: red, b[2] third byte:green, b[1] second byte: blue;

     return rgbval;

  } else {

   // deal with error



if message_received {

  if message = '65 00 04 00 FF FF FF' {

    serial.send('get crgb.val');

    // response in RGB numeric value 0x71 0xA6 0x2E 0xBA 0x00 0xFF 0xFF 0xFF for 8 bytes

    myRGB := convert_response_to_integer(response);


    return ack;

    set_the_led(myRGB);  // assumes led is looking for 24 bit RGB number from 0 to 16777215


if message = 'xx xx xx xx xx xx xx' { // other events we are expecting


} // end of message_received

(15.4 KB)

2 people like this

when controlling a RGB LED, you normally do this by 3 separate PWM channels on 3 separate I/O Pins (1 Pin = 1 PWM = 1 LED). It is maybe a good idea to directly send the 3 values for each PWM channel rather than send one RGB value which must be split again on MCU to get the single PWM values ...

Just send out the sliderbar value as a percentage value for PWM ...

Just another dea :-)

Hey Gerard

Do you have an .ino example for Fritz?  I don't do Arduino yet.  LOL

I also have NO .ino plans ...  :-)


Login or Signup to post a comment