/*
http://engineeringlearning.blogspot.com/2013/10/interfacing-lcd-without-potentiometer.html
Library Originally added by David A Mellis
Library Modified by Limor Fried
Example added by Tom Igoe
Modified by Tom Igoe, Edoctoor.
This example is in the public domain. http://www.arduino.cc.en/Tutorial/LiquidCrystal
Contrast modification by Ahmed Murtaza Qureshi (www.engineeringlearning.blogspot.com)
Note: If you can improve this, please do so.
Also note: The constrast pin must have a ~ because it uses PWM.
Note: I have tested this and the dimming works on real LCD.
That said, I do not know if a LCD can handle
PWM or if the Arduino Pin can handle the draw to power a
LCD.
(It did work when testing, but I doubt that
it is smart to do without transistors )
If a transistor should be used or if this is a bad idea
altogether.
If you would like to offer your suggestions;
just @edoctoor on the wokwi discord server; thanks.
I decided just to toggle power on and off using
a NPN transistor: just to be on the safe side.
BTW I did use a PM2222a NPN Transistor
When reading the PM2222a and the transistor was facing me
the Collector pin on the right goes to GROUND and the middle
Base pin goes to the 470 ohm resistor and then the
pin 4 on the arduino, and the last Emitter pin
goes to 5 volts on the breadboard.
I would have added this, but I could not find
them on wokwi. And I am totally fine with that ;-)
*/
#include <LiquidCrystal.h>
char ch;
int Contrast=15;
// initialize the library with the numbers of the interface pins
int rs = 10;
int en = 9;
int d4 = 8;
int d5 = 7;
int d6 = 6;
int d7 = 5;
int CPin = 11; // contrast ~pin // Normally goes to potentiometer
int PPin = 4; // backlight Power Pin // Normally goes to power
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// LCD PIN MAP from left to right //
// VSS>GRN, VDD>5V, V0>11 CONTRASTPIN, RS>10, RW>GRN, EN>9, D0,D1,D2,D3,
// D4>8, D5>7, D6>6, D7>5, A>4, K>GRN
// LiquidCrystal lcd(6, 7, 8, 9, 10, 12); // above help with the hard wiring //
void setup()
{
Serial.begin(9600);
Serial.println("LCD test with PWM contrast adjustment");
pinMode(13,OUTPUT);
pinMode(CPin, OUTPUT);
pinMode(PPin, OUTPUT);
analogWrite(CPin,Contrast);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("A+ B- N=ON F=OFF");
}
void loop()
{
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13,HIGH);
// 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()/1000);
}
void serialEvent()
{
if (Serial.available())
{
ch= Serial.read();
if(ch == 'A' || ch == 'a' && Contrast<255)
{
Contrast=Contrast+10;
}
if(ch=='B' || ch == 'b' && Contrast>0)
{
Contrast=Contrast-10;
}
if(ch == 'N'|| ch == 'n')
{
analogWrite(PPin,28836);
}
if(ch=='F'|| ch == 'f')
{
analogWrite(PPin,0);
}
analogWrite(CPin,Contrast);
Serial.print("Current contrast ");
Serial.print(Contrast);
Serial.println(" Use > A+ B- N=ON F=OFF ");
}
}