Hello everyone, in this tutorial I will cover how to program and wire an arduino LCD Screen. Essentially how this works is the arduino prints to the LCD, similar to how it would print to the serial monitor, except this is a more easy to see visual.
So, First a parts list.
You will need:
1 LCD screen, there is one included in your starter pack. For this tutorial I have a slightly larger one.
1 Solderless breadboard
1 330 Ohm resistor
14 – 26 Wires,
If you are using the starter kit LCD, or have attached pin headers, you will only need 14
If you are not using the starter LCD, and have not attached pin headers, you will need 26 wires because you need to jump from the breadboard over to your LCD with another 12 wires.
10K Ohm potentiometer (Optional)
This is how it should look at the end. As you can see there is a first set of wires, then wires that jump from those and lead off to the LCD screen.
This image below is how the breadboard should look before you attach the wires to jump to the LCD, or, before you attach the starter LCD with the pin headers.
Here we have the schematic, and breadboard view of the project.
I decided in the schematics and in these two images, the schematic and breadboard view that it would be easier to go with the standard LCD. And I have included the optional potentiometer just to demonstrate how this part would be wired in as well.
So, basically how this works is just like the Serial Monitor that the arduino itself has. You can write values to this, and it will display in a visual manner that allows for easier interpretation. Basically you can program the arduino to write to the LCD screen, and you can do this using straight print commands, or utilizing read commands. For the purposes of this tutorial I will include both types of code. The first, and simplest of the code types, is just the basic write as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// include the library code: #include <LiquidCrystal.h> // initialize the library by associating any needed LCD interface pin // with the arduino pin number it is connected to const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print("hello, world!"); } void loop() { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 1); // print the number of seconds since reset: lcd.print(millis()/ 100); } |
This code is simple, and prints the words “hello, world!” to your arduino LCD screen.
The next block of code, reads sensor data, and prints it on the LCD screen. In this case I have taken the smoothing function to write averages, and I have transferred it to the LCD screen to write an average to the LCD.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include<LiquidCrystal.h> int readings[numReadings]; // the readings from the analog input int readIndex = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average int inputPin = A0; const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. Serial.begin(9600); } void loop() { sensorValue = analogRead(sensorOne); // read sensorOne value. lcd.setCursor(0, 1) lcd.print("Your Current Electric Usage = "); // subtract the last reading: total = total - readings[readIndex]; // read from the sensor: readings[readIndex] = analogRead(inputPin); // add the reading to the total: total = total + readings[readIndex]; // advance to the next position in the array: readIndex = readIndex + 1; // if we're at the end of the array... if (readIndex >= numReadings) { // ...wrap around to the beginning: readIndex = 0; } // calculate the average: average = total / numReadings; // send it to the computer as ASCII digits lcd.print(average); delay(1); // delay in between reads for stability lcd.print("Your Current Electric Usage = "); } |
This code reads data from a sensor, and creates an index of readings out of the readings it gathers. After four readings in this case, the index average is calculated and printed to the screen above the heading your current electrical usage.