How to adjust the backlight of LCD?
Some aren’t adjustable; refer to the ‘documents’ tab of the product for its associated datasheets and example code to determine if the one you are using is adjustable
I just received the LCD-16396 SparkFun 16x2 SerLCD [Qwiic] RGB Backlight and the bigger ‘brother’ LCD-16398 20x4 SerLCD [Qwiic] RGB Backlight. I tried to get them working OK for a few days now, together with an Arduino Uno WiFi Rev2. I used a SparkFun BOB12009 Logic Level Converter. The past five years I built experience with using Hitachi like LCD’s 16x2 and 20x4. Recently a Grove-LCD 16x2 RGB backlight. So, I can make comparisons. I ordered the SparkFuns because they have these nice little Qwiic connectors. I got confused by the examples that SparkFun gives. On one side the real Arduino sketches but on the other side examples using the OpenLCD commands. Finally I managed to get the Backlight contrast working as needed and I got the backlight colored in RGB. I discovered, unfortunately, that, beside the Qwiic interface I needed one more wire to run from (in my case) Pin 11 of the Arduino to the “RX” pin on the LCD. Then inside the Arduino sketch I did not use the Wire library but the HardwareSerial library and use the SerLCD library from SparkFun. To set the contrast and RGB colors for the backlight the only way (as far as I discovered) was thru the functions of the HardwareSerial library, e.g. “mySerial.write(‘|’) ;” to activate the command modus (my words) followed by the appropriate code to execute the command you need. – I think it is stupid to have a nice system like Qwiic and then have to use an extra wire to send commands to the OpenLCD firmware. With the Grove RGB LCD everything is commanded via that Grove I2C cable (also 4 wires) but the commands for the backlight go to another I2C address. When I ran a I2C-scan sketch I saw that the scan result also reported two I2C addresses for the LCD-16396 (or 8). I think it should be possible to command OpenLCD via the 2nd I2C address. I go to investigate that.
Addition to my 1st reply:
The script I copied from an example that I found in the SparkFun examples for these LCD’s.
For this, I copy here the intro and copyright info and info about the authors of the SerLCD and OpenLCD libraries.
My thanks goes to them all!. The two examples below contain modifications/additions to the original example. The comments are for my account. They reflect my experiences of the past few days.
/*
SerLCD Library - Hello World
Gaston Williams - August 29, 2018
This sketch prints “Hello World!” to the LCD
and shows the time over I2C using the Qwiic system.
The circuit:
SparkFun RGB OpenLCD Serial display connected through
a Sparkfun Qwiic adpater to an Ardruino with a
Qwiic shield or a Sparkfun Blackboard with Qwiic built in.
The Qwiic adapter should be attached to the display as follows:
Display / Qwiic Cable Color
GND / Black
RAW / Red
SDA / Blue
SCL / Yellow
Note: If you connect directly to a 5V Arduino instead, you MUST use
a level-shifter to convert the i2c voltage levels down to 3.3V for the display.
This code is based on the LiquidCrystal code originally by David A. Mellis
and the OpenLCD code by Nathan Seidle at SparkFun.
License: This example code is in the public domain.
More info on Qwiic here: https://www.sparkfun.com/qwiic
AVR-Based Serial Enabled LCDs Hookup Guide
https://learn.sparkfun.com/tutorials/avr-based-serial-enabled-lcds-hookup-guide
*/
At the global definitions part I first set the following include’s and definitions:
#include <SerLCD.h> //Click here to get the library: http://librarymanager/All#SparkFun_SerLCD
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); //RX, TX (Note: only Arduino’s Pin 11 (TX) is connected (to “RX” on the LCD), but the call has to include both RX and TX)
#define bright 100 // used in the colors_t definition
#define contrast_default 120 // contrast range 0~255
enum colors {red, green, blue, white, black};
static int colors_t[5] = {
{bright, 0, 0}, // red
{0, bright, 0}, // green
{0, 0, bright}, // blue
{bright, bright, bright}, // white
{0, 0, 0} }; // black
Example 1:
void defltContrast_ser() {
mySerial.write(‘|’); //Put LCD into setting mode
mySerial.write(0x18);
mySerial.write(0x78); // = 120 decimal
Serial.println(“Setting contrast to default value 120”);
}
Example 2: set color of backlight:
void setRGB_ser(int clr) {
mySerial.write(‘|’); //Put LCD into setting mode
mySerial.write(0x2B); // = ‘+’ char
if (clr == red) {
mySerial.write(colors_t);
mySerial.write(colors_t);
mySerial.write(colors_t);
}
else if (clr == green) {
mySerial.write(colors_t);
mySerial.write(colors_t);
mySerial.write(colors_t);
}
else if (clr == blue) {
mySerial.write(colors_t);
mySerial.write(colors_t);
mySerial.write(colors_t);
}
else if (clr == white) {
mySerial.write(colors_t);
mySerial.write(colors_t);
mySerial.write(colors_t);
}
else {
mySerial.write(colors_t);
mySerial.write(colors_t);
mySerial.write(colors_t);
}
}
// In setup() one needs at least these two commands otherwise nothing will happen with the backlight:
void setup() {
mySerial.begin(9600);
lcd.begin(mySerial); //Set up the LCD for I2C communication
}
// Next, in loop() calls to set the backlight color alternatingly
void loop() {
lcd.clear(); //Clear the display - this moves the cursor to home position as well
lcd.print(“OpenLCD example code”);
delay(3000);
for (int i = 0; i < 4; i++) {
Serial.print("Setting backlight to: ");
if (i==0) Serial.println(“red”);
else if (i == 1) Serial.println(“green”);
else if (i == 2) Serial.println(“blue”);
else if (i == 3) Serial.println(“white”);
setRGB_ser(i);
lcd.setCursor(0, 2);
lcd.print(" "); // Clear the line
lcd.setCursor(0, 2);
cycles++; //Counting cycles! Yay!
lcd.print("Cycle: "); //These serial.print statements take multiple miliseconds
lcd.print(cycles);
Serial.print("Value of cycles is: ");
Serial.println(cycles);
lcd.setCursor(0,3);
// Print the number of seconds since reset:
lcd.print("Millis: ");
lcd.print(millis() / 1000);
delay(5000);
}
}
I hope this reply makes ‘visual’ what I wrote in my 1st reply.
Here an image of my test setup:
Addition to my 1st reply: I2C scanner script reported the following I2C addresses found: 0x60 and 0x72. The latter is mentioned in the examples and explanations from SparkFun. I expect the 0x60 address could be the address to send commands to OpenLCD.
My (final) reply: I have to correct what I wrote in my first reply “unfortunately” having to add an extra wire (outside of the Qwiic connection). That is not necessary. I now found this example:
https://github.com/sparkfun/SparkFun_Se … klight.ino. I ran it on the setup shown in the image in my post above, and it works great!
That means that we can do without an extra wire, the HardwareSerial library and use the combo of the Wire and the SerLCD libraries (for Arduino, that is). The RGB backlight colors in the example of the link mentioned in this reply demonstrate more colors like: grey, indigo, violet, orange and yellow (outside the ‘standard’: Red, Green and Blue).
I was not anymore allowed to edit my last post.
I made modifications to the Example2 mentioned in my last reply. Here is the sketch. It now has just one function call in loop(), but an extra function and a little ‘overhead’ by defining two arrays.
/*
SerLCD Library - Backlight
Gaston Williams - August 29, 2018
This sketch changes the backlight color and displays text using
the OpenLCD functions. This works with the original version of
SerLCD. See FastBacklight example for version 1.1 and later.
The circuit:
SparkFun RGB OpenLCD Serial display connected through
a Sparkfun Qwiic adpater to an Ardruino with a
Qwiic shield or a Sparkfun Blackboard with Qwiic built in.
The Qwiic adapter should be attached to the display as follows:
Display / Qwiic Cable Color
GND / Black
RAW / Red
SDA / Blue
SCL / Yellow
Note: If you connect directly to a 5V Arduino instead, you MUST use
a level-shifter to convert the i2c voltage levels down to 3.3V for the display.
License: This example code is in the public domain.
*/
#include <Wire.h>
#include <SerLCD.h> //Click here to get the library: http://librarymanager/All#SparkFun_SerLCD (link broken!)
// instead use: https://github.com/sparkfun/SparkFun_Se … klight.ino
SerLCD lcd; // Initialize the library with default I2C address 0x72
char static colorT[10][12] = {
“Black (off)”,
"Red ",
"Orange ",
"Yellow ",
"Green ",
"Blue ",
"Indigo ",
"Violet ",
"Grey ",
"White " };
long static colorN[10] = {
{0, 0, 0}, // black is off
{255, 0, 0}, // bright red
{0xFF8C00, 0, 0}, // orange
{255, 255, 0}, // bright yellow
{0, 255, 0}, // bright green
{0, 0, 255}, // bright blue
{0x4B0082, 0, 0}, // indigo
{0xA020F0, 0, 0}, // violet
{0x808080, 0, 0}, // grey
{255, 255, 255}}; // white
void colorDemo() {
int le = (sizeof(colorT) / sizeof(colorT[0]));
for (int i = 0; i < le; i++) {
if (colorN*[0] > 255) // Check if we have to do with a hex value like 0xFF8C00 which are all > 255.*
lcd.setBacklight(colorN[0] );
else
lcd.setBacklight(colorN[0], colorN[1], colorN[2]); // set r,g,b
lcd.clear();
lcd.print(colorT); // print the text
delay(3000);
}
}
void setup() {
Wire.begin();
lcd.begin(Wire);
//By default .begin() will set I2C SCL to Standard Speed mode of 100kHz
Wire.setClock(400000); //Optional - set I2C SCL to High Speed Mode of 400kHz
}
void loop() {
colorDemo();
}
Link to an image of the rewired test setup. https://imgur.com/gallery/ar3iY7N.
paulsk:
Here an image of my test setup:The image has been deleted. It has been replaced by an image of an updated setup. See my previous post with the new link.
Thanks for sharing!
Does this method work for all displays? I am using a [serial screen.](https://www.stoneitech.com/)
@Jackey. I don’t know. What I shared were my experiences with the SparkFun serLCD models and they are ‘specific’ in a sense that they use this Qwiic interface.