Raspberry Pi, libnotify, and Arduino LCD
Arduino and LCD Display
The RGB LCD shield and Arduino (Figure 3) combine to make an LCD display that accepts messages via serial communication on the Arduino's USB port. The LCD from Adafruit is 16x2 characters with an RGB backlight [2]. The LCD has a companion library available on GitHub that provides convenient functions to control the display. The Arduino code that talks to the LCD is shown in Listing 2. Make sure you have installed the RGBLCDShield
library and followed the instructions on GitHub. See the "Installing the Arduino IDE" box for more information.
Listing 2
arduino.ino
01 #include <Wire.h> 02 #include <Adafruit_RGBLCDShield.h> 03 #include <utility/Adafruit_MCP23017.h> 04 05 Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield(); 06 07 // Backlight color definitions from the example code 08 #define RED 0x1 09 #define YELLOW 0x3 10 #define GREEN 0x2 11 #define TEAL 0x6 12 #define BLUE 0x4 13 #define VIOLET 0x5 14 #define WHITE 0x7 15 16 void setup() { 17 Serial.begin(9600); 18 lcd.begin(16, 2); 19 20 lcd.clear(); 21 lcd.setCursor ( 0 , 0 ); 22 lcd.setBacklight(WHITE); 23 } 24 25 void loop(){ 26 char sBuffer [ 64 ]; 27 String sBufferString; 28 29 Serial.readBytesUntil ( '\n' , sBuffer , 64 ); 30 31 if ( sBuffer [ 0 ] == '1' ) 32 { 33 lcd.clear(); 34 lcd.setCursor(0,0); 35 lcd.print ( String ( sBuffer ).substring ( 1 , 17 ) ); 36 sBuffer [ 0 ] = 0; 37 } 38 else if ( sBuffer [ 0 ] == '2' ) 39 { 40 lcd.setCursor(0,1); 41 lcd.print ( String ( sBuffer ).substring ( 1 , 17 ) ); 42 sBuffer [ 0 ] = 0; 43 } 44 else if ( sBuffer [ 0 ] == 'C' ) 45 { 46 if ( sBuffer [ 1 ] == '0' ) lcd.setBacklight ( BLUE ); 47 if ( sBuffer [ 1 ] == '1' ) lcd.setBacklight ( GREEN ); 48 if ( sBuffer [ 1 ] == '2' ) lcd.setBacklight ( RED ); 49 50 sBuffer [ 0 ] = 0; 51 } 52 }
Installing the Arduino IDE
To program the Arduino, you first need to install the Arduino IDE (Figure 4). You can download it by running
sudo apt-get install arduino
from a terminal. Next, either download the arduino.ino
code from the Raspberry Pi Geek FTP site [3] or type it in from Listing 2. You'll need to get the Adafruit LCD library from GitHub [4] and follow the instructions there to install it.
Arduino Code
The #include
is Arduino's version of Python's import
. Wire.h
defines the Arduino pins (line 1), the Adafruit_RGBLCDShield.h
library supports the LCD (line 2), and utility/Adafruit_MCP23017.h
supports the Inter-Integrated Circuit (I2C) port expander on the LCD board (line 3). Line 5 initializes the variable lcd
as an instance of Adafruit_RGBLCDShield
, allowing the rest of the code to talk to the shield by simply referring to lcd
.
Each color definition (lines 7-14) associates a named color with a numerical value, which just makes the code easier to read later when I start asking the backlight for different colors.
Arduino code has three sections. Definitions (everything I've described so far), setup()
(code that runs once at the start of the program), and loop()
(code that runs indefinitely until power is removed).
In the setup
function (lines 16-23), line 17 initializes the Arduino's serial port. Because an Arduino only has one serial port, all I have to provide is the baud rate. Line 18 initializes the LCD library: The arguments 16
and 2
let the library know that it is a 16x2 LCD. Finally, lines 20-22 clear the display, set the cursor to the upper left position, and set the backlight color to WHITE
(as defined earlier).
At the top of the loop
function (lines 25-52), I define sBuffer
(line 26), where the incoming characters from the serial port will be stored. Line 29 reads characters from the serial port until a newline ('\n'
) is encountered, or 64 characters have been received. Once one of those conditions have been met, the code continues.
The blocks of code in lines 31-37 and lines 38-43 are almost identical. The if
statements (lines 31 and 38) look at the first character of the serial string. If it is a 1
, the text goes to the first line of the display. If it is a 2
, the text belongs to the second line.
The text for the first line is cleared (line 33), the cursor is set to the upper left (line 34), and the message is printed to the display (line 35); substring
makes sure that I don't try to display more than 16 characters. By starting with index 1
, I remove the first character that told me which line to write to.
Text for the second line works the same way, but lcd
is not cleared; otherwise, the code is identical. Strings in Arduino are terminated with a null (0) character, so by setting sBuffer [ 0 ] = 0
(a blank string) in both code blocks, any further characters are ignored. As the strings are redefined, the null will move down the string automatically.
The next section (lines 44-51) sets the color and works similar to setting the text. If the first character is a 'C'
, I'm setting the color of the backlight. Lines 46, 47, and 48 all work the same way, looking at the second character in the buffer. Depending on the value, each line calls lcd.setBacklight
to change the color as requested.
« Previous 1 2 3 Next »