We will need to start off by installing two Adafruit libraries. These are the Adafruit GFX Library and the Adafruit PCD8544 Nokia 5110 LCD library. These libraries are installed as we will need to include them and the SPI library. We can do this by adding the following include statements at the beginning of the sketch:
#include <SPI.h> #include <Adafruit_GFX.h> #include <Adafruit_PCD8544.h>
We will not want to initiate an instance of the Adafruit_PCD8544 type using the following code:
Adafruit_PCD8544 display = Adafruit_PCD8544(13, 11, 5, 4, 3);
The parameters are the Arduino pin numbers that the CLK, DIN, DC, CE and RST pins respectively are connected too.
In the setup() function, we will want to add the following code to set up the Adafruit_PCD8544 instance:
Serial.begin(9600);
display.begin(); display.setContrast(40);
Now the rest of the code can go in the setup() function for test purposes or in the loop() function. Let's start off by seeing how to light up a single pixel on the display. This can be accomplished by using the drawPixel() function as shown in the following code:
display.clearDisplay(); display.drawPixel(10, 10, BLACK); display.display();
Before we draw anything to the screen, we will want to clear the display and buffer. We do this with the clearDisplay() function. Next, we use the drawPixel() function to light up a single pixel located at the X coordinate 10 and the Y coordinate 10. Before anything is displayed on the LCD, we need to run the display() function as shown in the preceding code. It is important to remember to run the clearDisplay() function before we draw anything to the LCD, and we run the display() function after we draw everything to the screen to display it.