This is the sixth post of the projects related to reading a potentiometer, it really is another variant to show the value of the analog pin reading from an Arduino, now using a 1604 LCD.
Previous post are here: 8xLEDs, 8x8 LEDs matrix,7-segments display,4-digit 7-segment display and 16x2 LCD.
Diagram
It is important to note that the pins on the 16x4 LCD are identical to the 16x2 LCD. See imagen from LastMinuteEngineers.
So once again, we make the connections similar to the 16x2 LCD.
Here we go again, many cables mean more trouble or a more careful check at the time of a problem, so I prefer to simplify with the i2c adapter with the PCF8574 chip. See the information from Techtonions
It's time to add the library to use, LiquidCrystal_I2C.h offers a basic solution but with great detail: It does not work correctly on the 16x4 LCD due to a slippage of the characters in the third and fourth rows. There is even information about this detail in the Arduino's community and a simple adjustment by placing negative values at the cursor position or simply changing the library, but I'll stick with the library and use the detail to my advantage. This is how the definitive connections remain.
Remember to perform the necessary test to verify that everything is fine and that you must know the i2C address of your LCD, the information on this is here. It is useful to use the i2C scanner.
The atom into the codeWe always start by including the libraries that we will use.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,16,4);
Now I create the custom characters to generate a triangular scale.
byte b1[] = { B00000, B00000, B00000, B00000, B00000, B00000, B00000, B11111 } ;
byte b2[] = { B00000, B00000, B00000, B00000, B00000, B00000, B11111, B11111 } ;
byte b3[] = { B00000, B00000, B00000, B00000, B00000, B11111, B11111, B11111 } ;
byte b4[] = { B00000, B00000, B00000, B00000, B11111, B11111, B11111, B11111 } ;
byte b5[] = { B00000, B00000, B00000, B11111, B11111, B11111, B11111, B11111 } ;
byte b6[] = { B00000, B00000, B11111, B11111, B11111, B11111, B11111, B11111 } ;
byte b7[] = { B00000, B11111, B11111, B11111, B11111, B11111, B11111, B11111 } ;
byte b8[] = { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 } ;
The setup is simple: start the screen and name the custom characters.
void setup() {
lcd.init();
lcd.backlight();
lcd.createChar(0,b1);
lcd.createChar(1,b2);
lcd.createChar(2,b3);
lcd.createChar(3,b4);
lcd.createChar(4,b5);
lcd.createChar(5,b6);
lcd.createChar(6,b7);
lcd.createChar(7,b8);
}
Now if there are details to understand in the Loop. First we read the analog pin and interpolate the value between 0 and 15, this is because we only have 16 characters for each row on the LCD.
void loop() {
int value = analogRead(A0); // read of potentiometer value
int level = map(value, 0, 1000, 0, 15);
Now I check if the level variable is less than 9 or if it requires half the characters. If it is correct, I can present each character in the corresponding position.
if (level<9){
for (int i=0;i<level;i++){
lcd.setCursor(i, 1);
lcd.write(i);
}
}
En el caso contrario, genero dos ciclos FOR, uno para cada mitad de la pantalla: A la izquierda genero cada caracter en la segunda fila y en su orden respectivo, para la segunda mitad, simplemente ubico la posicion del cursor en la primera fila y desde la columna 8 para mostrar los caracteres, ahora, debajo de la segunda mitad coloco los caracteres full (char(255)).
In the opposite case, I generate two for loops, one for each half of the screen: On the left I generate each character in the second row and in its respective order, for the second half, I simply place the cursor position in the first row and from column 8 to show the characters, now, under the second half I put the full characters (char(255)).
else {
for (int i=0;i<8;i++){
lcd.setCursor(i, 1);
lcd.write(i);
}
for (int i=0;i<level-8;i++){
lcd.setCursor(i+8, 0);
lcd.write(i);
lcd.setCursor(i+8, 1);
lcd.print(char(255));
}
}
Finally I must present the text in the third row and the reading value in the fourth row. Remark: the characters are moved 4 positions by the detail of the library.
lcd.setCursor(0,2);
lcd.print("Value:");
lcd.setCursor(1,3);
lcd.print(value);
delay(200);
lcd.clear();
}
ConclusionThis graphic presentation seems very attractive to me due to its triangular shape, you can appreciate the growth of the value only by the height of the column, of course there is a separation between each character. The good question is, have you seen a similar scale in any artifact? If you have the answer, please share with us in the comments. Remember to let us know how useful this information has been and remember to get a head start on the bookstores if you can. See ya!!!
Comments