






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







1 shows the two types of display that you will use. The first is an alphanumeric LCD display shield. The second is a 128- × 64-pixel OLED (organic light- emitting diode) graphical display. These two types of display are very popular for the Arduino.
Figure 9.1 An alphanumeric LCD shield (left) and OLED display (right).
This is a book about software, not hardware, but in this chapter, we will have to explain a little about how the electronics of these displays work so that you understand how to drive them.
Alphanumeric LCD Displays
The LCD module that we use is an Arduino shield that can just be plugged on top of an Arduino board. In addition to its display, it also has some buttons. There are a number of different shields, but nearly all of them use the same LCD controller chip (the HD44780), so look for a shield that uses this controller chip.
I used the DFRobot LCD Keypad Shield for Arduino. This module supplied by DFRobot (www.robotshop.com ) is inexpensive and provides an LCD display that is 16 characters by 2 rows and also has 6 pushbuttons. The shield comes assembled, so no soldering is required; you just plug it on top of your Arduino board (see Figure 9-2 ).
Figure 9.2 LCD shield attached to an Arduino board.
The LCD shield uses seven of the Arduino pins to control the LCD display and one analog pin for the buttons. So we cannot use these Arduino pins for any other purpose.
A USB Message Board
For a simple example of a simple use of the display, we are going to make a USB message board. This will display messages sent from the Serial Monitor. The Arduino IDE comes with an LCD library. This greatly simplifies the process of using an LCD display. The library gives you useful functions that you can call:
♦ clear clears the display of any text. ♦ setCursor sets the position in row and column where the next thing that you print will appear. ♦ print writes a string at that position. This example is listed in sketch 9-01:
}
void loop()
{
if (Serial.available() > 0)
{
char ch = Serial.read();
if (ch == '#')
{
lcd.clear();
}
else if (ch == '/')
{
// new line
lcd.setCursor(0, 1);
}
else
{
lcd.write(ch);
}
}
}
As with all Arduino libraries, you have to start by including the library to make the compiler aware of it. The next line defines which Arduino pins are used by the shield and for what purpose. If you are using a different shield, then you may well find that the pin allocations are different, so check in the documentation for the shield. In this case, the six pins used to control the display are D4, D5, D6, D7, D8, and D9. The purpose of each of these pins is described in Table 9-.
Parameter to LCD()
Arduino Pin
Purpose
RS 8 Register Select; this is set to 1 or 0 depending on whether the Arduino is sending data for characters or an instruction. An instruction might make the cursor flash, for example. E 9 Enable; this gets toggled to tell the LCD controller chip that the data on the following four pins is ready to be read. Data 4 4 These four pins are used to transfer data. The LCD controller chip used by the shield can use eight-bit or four-bit data. This shield uses four bits, in which case the bits 4–7 are used rather than 0–7.
Data 5 5 Data 6 6 Data 7 7
Table 9-1 LCD Shield Pin Assignments
The setup function is straightforward. You start serial communications so that the Serial Monitor can send commands and initialize the LCD library with the dimensions of the display being used. You also display the message “Arduino Rules” on two lines by setting the cursor to top-left, printing “Arduino,” then moving the cursor to the start of the second row and printing
number of other functions that you can use:
♦ home is the same as setCursor(0,0) : it moves the cursor to top-left. ♦ cursor displays a cursor. ♦ noCursor specifies not to display a cursor. ♦ blink makes the cursor blink. ♦ noBlink stops the cursor from blinking. ♦ noDisplay turns off the display without removing the content. ♦ display turns the display back on after noDisplay. ♦ scrollDisplayLeft moves all the text on the display one character position to the left. ♦ scrollDisplayRight moves all the text on the display one character position to the right. ♦ autoscroll activates a mode in which, as new characters are added at the cursor, the existing text is pushed in the direction determined by the functions leftToRight and rightToLeft. ♦ noAutoscroll turns autoscroll mode off.
OLED Graphic Displays
OLED (organic light-emitting diode) displays are bright and crisp and are fast replacing LCD displays in consumer appliances. The type of OLED described here uses an interface bus called I2C and has a driver chip called the SD1306. These can be bought on eBay, Adafruit, and many other suppliers around the Internet. Look for a device with just four interface pins as these are easiest to work with. Figure 9-4 shows an Arduino Uno connected to a 0.96-inch OLED display. These boards have a resolution of 128 × 64 pixels and are monochrome—in this case, blue.
Figure 9.4 An Arduino Uno and OLED display.
Connecting an OLED Display
You can connect your OLED display to your Arduino using female-to-male jumper wires. You can buy these from many sources including Adafruit (product 825). You will need to make the following connections:
♦ GND on the display to GND on the Arduino. ♦ VCC on the display to 5V on the Arduino. ♦ SCL on the display to the SCL pin of the Arduino. This is not labeled on the Arduino Uno itself but is indicated in Figure 9-. ♦ SDA on the display to the SDA pin of the Arduino (also see Figure 9-5 ).
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(4); // pick an unused pin
void setup()
{
display.begin(SSD1306_SWITCHCAPVCC, 0x3c); // may need to change this
display.setTextSize(4);
display.setTextColor(WHITE);
}
void loop()
{
static int count = 0;
display.clearDisplay();
display.drawRoundRect(0, 0, 127, 63, 8, WHITE);
display.setCursor(20,20);
display.print(count);
display.display();
count ++;
if (count > 9999)
{
count = 0;
}
delay(1000);
}
The sketch starts by importing the libraries that it needs and then a display variable initialized. The parameter supplied is that of the optional “reset” pin that some OLED displays (including those supplied by Adafruit) have. If your display does not have a reset pin, then set this value to a pin that is not being connected to anything. In this case I chose pin 4. The setup function initializes the display and you may need to change the I2C address supplied as the second parameter from 0x3c to a different value. setup then sets the font size to 4 (large) and the text color to white (anything but black will display in the LED color). The loop function clears the display, draws a round-cornered rectangle, sets the cursor position, and then prints the value of count. The display will not actually be updated until the command display.display() is run. The variable count is then incremented and there is a delay of one second.