I use I2C 4 PIN 16 x 2 LCD.
I compiled the following code and it was compiled sucessfully
// C++ code
//
#include <Adafruit_LiquidCrystal.h>
int trigger = 7;
int echo = 6;
Adafruit_LiquidCrystal lcd_1(0);
void setup()
{
Serial.begin(9600);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
pinMode(echo, INPUT);
lcd_1.begin(16, 2);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
pinMode(2, INPUT);
}
void loop()
{
float cm = 0;
double time = 0;
digitalWrite(7, HIGH);
delayMicroseconds(2);
digitalWrite(7,LOW);
time = pulseIn(echo, HIGH);
cm = time*29/2*0.0473/100*2.511111;
Serial.println(cm);
lcd_1.setCursor(0, 0);
lcd_1.setBacklight(1);
lcd_1.print("cm: ");
lcd_1.setCursor(0, 1);
lcd_1.print(cm);
lcd_1.print("cm +-0.1cm");
delayMicroseconds(30);
}
and for circuit, I used following circuit
but it doesnt work any ideas?
it worked on simulation on tinkercad but it didnt work on real arduino uno
ec2021
October 18, 2024, 2:52pm
4
Hi @jasonbebrilliant ,
install the library "LiquidCrystal I2C" and try this
#include <LiquidCrystal_I2C.h>
const byte I2C_ADDR = 0x27;
const byte LCD_COLUMNS = 16;
const byte LCD_LINES = 2;
LiquidCrystal_I2C lcd_1(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
and outcomment the existing lines as follows
//#include <Adafruit_LiquidCrystal.h>
//Adafruit_LiquidCrystal lcd_1(0);
Good luck!
ec2021
P.S.: I looked into the Adafruit_Liquidcrystal Library and found this
/*!
* @brief LiquidCrystal constructor for connection over i2c
* @param i2cAddr Address of the display. Can use either actual I2C address
* (0x20, 0x21, etc.) or offset from 0x20 base address (0, 1, etc.).
* @param wire Optional pointer to Wire instance to use. Defaults to Wire.
*/
Adafruit_LiquidCrystal(uint8_t i2cAddr, TwoWire *wire = &Wire);
So with the right call the lib you are using should support I2C as well.
It might be sufficient to try
Adafruit_LiquidCrystal lcd_1(0x27);
P.P.S.: Neither (0) nor (0x27) does work in the Wokwi Online simulation while the lib LiquidCrystal_I2C works without problems ...
@LarryD : Do you have experience with the Adafruit lib and I2C?
1 Like
ok so i found that the address is 0x27 and changed
Adafruit_LiquidCrystal lcd_1(0);
to
Adafruit_LiquidCrystal lcd_1(0x27);
now it starts to display this:
is this a good sign or bad sign?
*also i removed the switch
oh thanks!
ill try
#include <LiquidCrystal_I2C.h>
const byte I2C_ADDR = 0x27;
const byte LCD_COLUMNS = 16;
const byte LCD_LINES = 2;
LiquidCrystal_I2C lcd_1(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
bro i love you
bro i love you
bro i love you
bro i love you
bro i love you
bro i love you
bro i love you
bro i love you
bro i love you
bro i love you
bro i love you
bro i love you
bro i love you
bro i love you
ec2021
October 18, 2024, 3:07pm
9
That's too much ...
Actually the example for i2c displays on github
https://github.com/adafruit/Adafruit_LiquidCrystal/blob/master/examples/HelloWorld_i2c/HelloWorld_i2c.ino
uses the address 0
// Connect via i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);
so you used a correct call but it seems not to work in simulation and reality (where reality is more relevant).
Have fun!
ec2021
ec2021
October 18, 2024, 3:51pm
11
I understand that it should work for standard 0x27 address if the call is
Adafruit_LiquidCrystal lcd(7);
or
Adafruit_LiquidCrystal lcd(0x27);
Using zero would address 0x20 as I looked up in the library:
Adafruit_LiquidCrystal::Adafruit_LiquidCrystal(uint8_t i2caddr, TwoWire *wire) {
_i2cAddr = i2caddr <= 0x07 ? 0x20 | i2caddr : i2caddr;
Both options obviously do not work on Wokwi for (at least for me ) unknown reasons.
I made a test sketch using both libs depending on a define.
https://wokwi.com/projects/412097242415977473
Maybe a bug in the Wokwi simulation ... ?!?
P.S.: Just found this on github after searching for "Wokwi Adafruit_LiquidCrystal":
It's from 2021 and found in a thread regarding the ATtiny85
https://github.com/wokwi/wokwi-features/issues/152
but probably still valid ...
Think that's enough investigation for today
1 Like
xfpd
October 18, 2024, 6:52pm
12
Please, refrain from starting multiple topics with the same subject.
When I turn the power while the slideswitch is on, it works but if i turn the slideswitch off and on again, it displays some white lines
the video link
my code
// C++ code
//
// #include <Adafruit_LiquidCrystal.h>
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
// #include <string>
const byte I2C_ADDR = 0x27;
const byte LCD_COLUMNS = 16;
const byte LCD_LINES = 2;
LiquidCrystal_I2C lcd_1(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
int trigger = 7;
int echo = 6;
//…
When I turn the power while the slideswitch is on, it works but if i turn the slideswitch off and on again, it displays some white lines
the video link
my code
// C++ code
//
// #include <Adafruit_LiquidCrystal.h>
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
// #include <string>
const byte I2C_ADDR = 0x27;
const byte LCD_COLUMNS = 16;
const byte LCD_LINES = 2;
LiquidCrystal_I2C lcd_1(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
int trigger = 7;
int echo = 6;
// Adafruit_LiquidCrystal lcd_1(0x27);
void setup()
{
Serial.begin(9600);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
pinMode(echo, INPUT);
lcd_1.begin(16, 2);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
pinMode(2, INPUT);
pinMode(8,INPUT);
}
void loop()
{
float cm = 0;
double time = 0;
digitalWrite(7, HIGH);
delayMicroseconds(2);
digitalWrite(7,LOW);
time = pulseIn(echo, HIGH);
cm = time*29/2*0.0473/100*2.511111;
Serial.println(cm);
lcd_1.setCursor(0, 0);
lcd_1.setBacklight(1);
lcd_1.print("cm: ");
lcd_1.setCursor(0, 1);
lcd_1.print(cm);
lcd_1.print("cm +-0.1cm");
delayMicroseconds(1000);
}
my circuit
I know it worked perfectly without switches, but I want switches for for some reason
is there any ways too do this???
To do what, exactly???? What are you trying to accomplish by turning the power off to the display and then never initializing the display in your code?
I tried to turn off the display and turn it on again
xfpd
October 18, 2024, 6:52pm
18
Please, refrain from creating multiple topics with the same subject.
I use I2C 4 PIN 16 x 2 LCD.
I compiled the following code and it was compiled sucessfully
// C++ code
//
#include <Adafruit_LiquidCrystal.h>
int trigger = 7;
int echo = 6;
Adafruit_LiquidCrystal lcd_1(0);
void setup()
{
Serial.begin(9600);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
pinMode(echo, INPUT);
lcd_1.begin(16, 2);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
pinMode(2, INPUT);
}
void loop()
{
float cm = 0;
double time = 0;
digitalWrite(7, HIGH);
delay…
So, just testing and the result is what you can expect if you do not follow through by initializing the controller inside the LCD display.