|
| 1 | +/* |
| 2 | + * This sketch changes the backlight color and displays text using |
| 3 | + * the OpenLCD functions. |
| 4 | + * |
| 5 | + *The circuit: |
| 6 | + * Sparkfun RGB OpenLCD Serial display connected through |
| 7 | + * a Sparkfun Qwiic adpater to an Ardruino with a |
| 8 | + * Qwiic shield or a Sparkfun Blackboard with Qwiic built in. |
| 9 | + * |
| 10 | + * The Qwiic adapter should be attached to the display as follows: |
| 11 | + * |
| 12 | + * Display Qwiic Qwiic Cable Color |
| 13 | + * GND GND Black |
| 14 | + * RAW 3.3V Red |
| 15 | + * SDA SDA Blue |
| 16 | + * SCL SCL Yellow |
| 17 | + * |
| 18 | + * Note: If you connect directly to a 5V Arduino instead, you *MUST* use |
| 19 | + * a level-shifter to convert the i2c voltage levels down to 3.3V for the display. |
| 20 | +
|
| 21 | + OpenLCD is an LCD with Serial/I2C/SPI interfaces. |
| 22 | + By: Nathan Seidle |
| 23 | + SparkFun Electronics |
| 24 | + Date: April 19th, 2015 |
| 25 | + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). |
| 26 | + OpenLCD gives the user multiple interfaces (serial, I2C, and SPI) to control an LCD. SerLCD was the original |
| 27 | + serial LCD from SparkFun that ran on the PIC 16F88 with only a serial interface and limited feature set. |
| 28 | + This is an updated serial LCD. |
| 29 | + |
| 30 | + This example shows how to change brightness of the three backlight controls. |
| 31 | + |
| 32 | + Please Note: 0x72 is the 7-bit I2C address. If you are using a different language than Arduino you will probably |
| 33 | + need to add the Read/Write bit to the end of the address. This means the default read address for the OpenLCD |
| 34 | + is 0b.1110.0101 or 0xE5 and the write address is 0b.1110.0100 or 0xE4. |
| 35 | + For more information see https://learn.sparkfun.com/tutorials/i2c |
| 36 | + Note: This code expects the display to be listening at the default I2C address. If your display is not at 0x72, you can |
| 37 | + do a hardware reset. Tie the RX pin to ground and power up OpenLCD. You should see the splash screen |
| 38 | + then "System reset Power cycle me" and the backlight will begin to blink. Now power down OpenLCD and remove |
| 39 | + the RX/GND jumper. OpenLCD is now reset. |
| 40 | + |
| 41 | + To get this code to work, attached an OpenLCD to an Arduino Uno using the following pins: |
| 42 | + SCL (OpenLCD) to A5 (Arduino) |
| 43 | + SDA to A4 |
| 44 | + VIN to 5V |
| 45 | + GND to GND |
| 46 | + |
| 47 | + The OpenLCD has 4.7k pull up resistors on SDA and SCL. If you have other devices on the |
| 48 | + I2C bus then you may want to disable the pull up resistors by clearing the PU (pull up) jumper. |
| 49 | + |
| 50 | + OpenLCD will work at 400kHz Fast I2C. Use the .setClock() call shown below to set the data rate |
| 51 | + faster if needed. |
| 52 | + Command cheat sheet: |
| 53 | + ASCII / DEC / HEX |
| 54 | + '|' / 124 / 0x7C - Put into setting mode |
| 55 | + Ctrl+c / 3 / 0x03 - Change width to 20 |
| 56 | + Ctrl+d / 4 / 0x04 - Change width to 16 |
| 57 | + Ctrl+e / 5 / 0x05 - Change lines to 4 |
| 58 | + Ctrl+f / 6 / 0x06 - Change lines to 2 |
| 59 | + Ctrl+g / 7 / 0x07 - Change lines to 1 |
| 60 | + Ctrl+h / 8 / 0x08 - Software reset of the system |
| 61 | + Ctrl+i / 9 / 0x09 - Enable/disable splash screen |
| 62 | + Ctrl+j / 10 / 0x0A - Save currently displayed text as splash |
| 63 | + Ctrl+k / 11 / 0x0B - Change baud to 2400bps |
| 64 | + Ctrl+l / 12 / 0x0C - Change baud to 4800bps |
| 65 | + Ctrl+m / 13 / 0x0D - Change baud to 9600bps |
| 66 | + Ctrl+n / 14 / 0x0E - Change baud to 14400bps |
| 67 | + Ctrl+o / 15 / 0x0F - Change baud to 19200bps |
| 68 | + Ctrl+p / 16 / 0x10 - Change baud to 38400bps |
| 69 | + Ctrl+q / 17 / 0x11 - Change baud to 57600bps |
| 70 | + Ctrl+r / 18 / 0x12 - Change baud to 115200bps |
| 71 | + Ctrl+s / 19 / 0x13 - Change baud to 230400bps |
| 72 | + Ctrl+t / 20 / 0x14 - Change baud to 460800bps |
| 73 | + Ctrl+u / 21 / 0x15 - Change baud to 921600bps |
| 74 | + Ctrl+v / 22 / 0x16 - Change baud to 1000000bps |
| 75 | + Ctrl+w / 23 / 0x17 - Change baud to 1200bps |
| 76 | + Ctrl+x / 24 / 0x18 - Change the contrast. Follow Ctrl+x with number 0 to 255. 120 is default. |
| 77 | + Ctrl+y / 25 / 0x19 - Change the TWI address. Follow Ctrl+x with number 0 to 255. 114 (0x72) is default. |
| 78 | + Ctrl+z / 26 / 0x1A - Enable/disable ignore RX pin on startup (ignore emergency reset) |
| 79 | + '-' / 45 / 0x2D - Clear display. Move cursor to home position. |
| 80 | + / 128-157 / 0x80-0x9D - Set the primary backlight brightness. 128 = Off, 157 = 100%. |
| 81 | + / 158-187 / 0x9E-0xBB - Set the green backlight brightness. 158 = Off, 187 = 100%. |
| 82 | + / 188-217 / 0xBC-0xD9 - Set the blue backlight brightness. 188 = Off, 217 = 100%. |
| 83 | +For example, to change the baud rate to 115200 send 124 followed by 18. |
| 84 | + '+' / 43 / 0x2B - Set Backlight to RGB value, follow + by 3 numbers 0 to 255, for the r, g and b values to set. |
| 85 | + For example, to change the backlight to yellow send + followe by 255, 255 and 0. |
| 86 | + |
| 87 | + |
| 88 | +*/ |
| 89 | + |
| 90 | +#include <Wire.h> |
| 91 | + |
| 92 | +#define DISPLAY_ADDRESS1 0x72 //This is the default address of the OpenLCD |
| 93 | + |
| 94 | +void setup() |
| 95 | +{ |
| 96 | + Wire.begin(); //Join the bus as master |
| 97 | + |
| 98 | + //By default .begin() will set I2C SCL to Standard Speed mode of 100kHz |
| 99 | + Wire.setClock(400000); //Optional - set I2C SCL to High Speed Mode of 400kHz |
| 100 | + |
| 101 | + Serial.begin(9600); //Start serial communication at 9600 for debug statements |
| 102 | + Serial.println("OpenLCD Example Code"); |
| 103 | + |
| 104 | + //Send the reset command to the display - this forces the cursor to return to the beginning of the display |
| 105 | + Wire.beginTransmission(DISPLAY_ADDRESS1); |
| 106 | + Wire.write('|'); //Put LCD into setting mode |
| 107 | + Wire.write('-'); //Send clear display command |
| 108 | + Wire.endTransmission(); |
| 109 | + Wire.print("Testing Set RGB"); |
| 110 | + delay(2000); |
| 111 | +} |
| 112 | + |
| 113 | +void loop() |
| 114 | +{ |
| 115 | + //Turn off backlight (black) |
| 116 | + Serial.println("Setting RGB backlight to black"); |
| 117 | + Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1 |
| 118 | + Wire.write('|'); //Put LCD into setting mode |
| 119 | + Wire.write('-'); //Send clear display command |
| 120 | + Wire.write('|'); //Put LCD into setting mode |
| 121 | + Wire.write('+'); //Send the Set RGB command |
| 122 | + Wire.write(0x00); //Send the red value |
| 123 | + Wire.write(0x00); //Send the green value |
| 124 | + Wire.write(0x00); //Send the blue value |
| 125 | + Wire.print("Black (Off)!"); |
| 126 | + Wire.endTransmission(); //Stop I2C transmission |
| 127 | + |
| 128 | + delay(2000); |
| 129 | + |
| 130 | + |
| 131 | + //Set red backlight |
| 132 | + Serial.println("Setting RGB backlight to red"); |
| 133 | + Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1 |
| 134 | + Wire.write('|'); //Put LCD into setting mode |
| 135 | + Wire.write('-'); //Send clear display command |
| 136 | + Wire.write('|'); //Put LCD into setting mode |
| 137 | + Wire.write('+'); //Send the Set RGB command |
| 138 | + Wire.write(0xFF); //Send the red value |
| 139 | + Wire.write(0x00); //Send the green value |
| 140 | + Wire.write(0x00); //Send the blue value |
| 141 | + Wire.print("Red!"); |
| 142 | + Wire.endTransmission(); //Stop I2C transmission |
| 143 | + |
| 144 | + delay(2000); |
| 145 | + |
| 146 | + //Set Orange backlight |
| 147 | + Serial.println("Setting RGB backlight to orange"); |
| 148 | + Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1 |
| 149 | + Wire.write('|'); //Put LCD into setting mode |
| 150 | + Wire.write('-'); //Send clear display command |
| 151 | + Wire.write('|'); //Put LCD into setting mode |
| 152 | + Wire.write('+'); //Send the Set RGB command |
| 153 | + Wire.write(0xFF); //Send the red value |
| 154 | + Wire.write(0x8C); //Send the green value |
| 155 | + Wire.write(0x00); //Send the blue value |
| 156 | + Wire.print("Orange!"); |
| 157 | + Wire.endTransmission(); //Stop I2C transmission |
| 158 | + |
| 159 | + delay(2000); |
| 160 | + |
| 161 | + //Set yellow backlight |
| 162 | + Serial.println("Setting RGB backlight to yellow"); |
| 163 | + Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1 |
| 164 | + Wire.write('|'); //Put LCD into setting mode |
| 165 | + Wire.write('-'); //Send clear display command |
| 166 | + Wire.write('|'); //Put LCD into setting mode |
| 167 | + Wire.write('+'); //Send the Set RGB command |
| 168 | + Wire.write(0xFF); //Send the red value |
| 169 | + Wire.write(0xFF); //Send the green value |
| 170 | + Wire.write(0x00); //Send the blue value |
| 171 | + Wire.print("Yellow!"); |
| 172 | + Wire.endTransmission(); //Stop I2C transmission |
| 173 | + |
| 174 | + delay(2000); |
| 175 | + |
| 176 | + //Set green backlight |
| 177 | + Serial.println("Setting RGB backlight to green"); |
| 178 | + Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1 |
| 179 | + Wire.write('|'); //Put LCD into setting mode |
| 180 | + Wire.write('-'); //Send clear display command |
| 181 | + Wire.write('|'); //Put LCD into setting mode |
| 182 | + Wire.write('+'); //Send the Set RGB command |
| 183 | + Wire.write(0x00); //Send the red value |
| 184 | + Wire.write(0xFF); //Send the green value |
| 185 | + Wire.write(0x00); //Send the blue value |
| 186 | + Wire.print("Green!"); |
| 187 | + Wire.endTransmission(); //Stop I2C transmission |
| 188 | + delay(2000); |
| 189 | + |
| 190 | + //Set blue backlight |
| 191 | + Serial.println("Setting RGB backlight to blue"); |
| 192 | + Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1 |
| 193 | + Wire.write('|'); //Put LCD into setting mode |
| 194 | + Wire.write('-'); //Send clear display command |
| 195 | + Wire.write('|'); //Put LCD into setting mode |
| 196 | + Wire.write('+'); //Send the Set RGB command |
| 197 | + Wire.write(0x00); //Send the red value |
| 198 | + Wire.write(0x00); //Send the green value |
| 199 | + Wire.write(0xFF); //Send the blue value |
| 200 | + Wire.print("Blue!"); |
| 201 | + Wire.endTransmission(); //Stop I2C transmission |
| 202 | + delay(2000); |
| 203 | + |
| 204 | + //Set violet backlight |
| 205 | + Serial.println("Setting RGB backlight to violet"); |
| 206 | + Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1 |
| 207 | + Wire.write('|'); //Put LCD into setting mode |
| 208 | + Wire.write('-'); //Send clear display command |
| 209 | + Wire.write('|'); //Put LCD into setting mode |
| 210 | + Wire.write('+'); //Send the Set RGB command |
| 211 | + Wire.write(0xA0); //Send the red value |
| 212 | + Wire.write(0x20); //Send the green value |
| 213 | + Wire.write(0xF0); //Send the blue value |
| 214 | + Wire.print("Violet!"); |
| 215 | + Wire.endTransmission(); //Stop I2C transmission |
| 216 | + delay(2000); |
| 217 | + |
| 218 | + //Turn on all (white) |
| 219 | + Serial.println("Setting RGB backlight to white"); |
| 220 | + Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1 |
| 221 | + Wire.write('|'); //Put LCD into setting mode |
| 222 | + Wire.write('-'); //Send clear display command |
| 223 | + Wire.write('|'); //Put LCD into setting mode |
| 224 | + Wire.write('+'); //Send the Set RGB command |
| 225 | + Wire.write(0xFF); //Send the red value |
| 226 | + Wire.write(0xFF); //Send the green value |
| 227 | + Wire.write(0xFF); //Send the blue value |
| 228 | + Wire.print("White!"); |
| 229 | + Wire.endTransmission(); //Stop I2C transmission |
| 230 | + delay(2000); |
| 231 | + |
| 232 | + //Set to Gray |
| 233 | + Serial.println("Setting RGB backlight to gray"); |
| 234 | + Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1 |
| 235 | + Wire.write('|'); //Put LCD into setting mode |
| 236 | + Wire.write('-'); //Send clear display command |
| 237 | + Wire.write('|'); //Put LCD into setting mode |
| 238 | + Wire.write('+'); //Send the Set RGB command |
| 239 | + Wire.write(0x80); //Send the red value |
| 240 | + Wire.write(0x80); //Send the green value |
| 241 | + Wire.write(0x80); //Send the blue value |
| 242 | + Wire.print("Gray!"); |
| 243 | + Wire.endTransmission(); //Stop I2C transmission |
| 244 | + delay(2000); |
| 245 | +} |
0 commit comments