Hi i have a problem with the back light of the LCD i2c.So i am using a servo WHICH IS CONNECTED TO 4AA BATTERIES.I changed the pins of the servo but to no avail.When the servo is removed from the schematic the display still does not work.So i think it is something from the code. I'll add circuit later
CODE:#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Инициализация на LCD с I2C адрес 0x27
Servo servo;
const int coinPin = 2; // Пинът, към който са свързани две жички за монета
const int button2Pin = 3;
const int servoPin = 9;
bool coinInserted = false;
bool button2Pressed = false;
void setup() {
lcd.begin(16, 2);
lcd.print("Insert a coin");
pinMode(coinPin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
servo.attach(servoPin);
servo.write(0); // Позиция на сервото в началото
}
void loop() {
// Проверка за пусната монета
if (digitalRead(coinPin) == LOW && !coinInserted) {
coinInserted = true;
lcd.clear();
lcd.print("Press button 2");
}
// Проверка за натискане на бутон 2
if (digitalRead(button2Pin) == LOW && coinInserted && !button2Pressed) {
button2Pressed = true;
lcd.clear();
lcd.print("Thank you");
// Задвижване на сервото
servo.write(90);
delay(1000); // Изчакване 1 секунда
servo.write(0);
// Рестартиране на променливите
coinInserted = false;
button2Pressed = false;
delay(2000); // Изчакване 2 секунди
lcd.clear();
lcd.print("Insert a coin");
}
// Проверка за липса на пусната монета
if (digitalRead(button2Pin) == LOW && !coinInserted && !button2Pressed) {
lcd.clear();
lcd.print("No coins");
// Рестартиране на променливите
coinInserted = false;
button2Pressed = false;
delay(2000); // Изчакване 2 секунди
lcd.clear();
lcd.print("Insert a coin");
}
}`
Have you run an I2C scan to confirm the LCD address??? Better option is to use the hd44780 library which has the ability to auto setup and complete test of the LCD through your I2C bus .
upload this code to check the I2C address of your LCD
[code]
// --------------------------------------
// i2c_scanner
//
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26 2013
// V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
// by Arduino.cc user Krodal.
// Changes by louarnold removed.
// Scanning addresses changed from 0...127 to 1...119,
// according to the i2c scanner by Nick Gammon
// https://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
// As version 4, but address scans now to 127.
// A sensor seems to use address 120.
// Version 6, November 27, 2015.
// Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(115200);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(1000); // wait 5 seconds for next scan
}
[/code]
or use this as a template to use the hd44780 library which you will need to install via the library manager
[code]
// how to convert "non-bperrybap lcd code" to "bperrybap lcd code"
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip
const int LCD_COLS = 16; // LCD geometry
const int LCD_ROWS = 2;
// end of stuff for bperrybap library
void setup()
{
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.print(" blah blah");
lcd.setCursor(0, 1);
}
void loop()
{
your code goes here
}
[/code]
Not sure which LiquidCrystal_I2C library you are using (there is more than one using the same file name), but it seems odd to have the LCD dimensions (16, 2) in both the constructor and the begin() function. Have you looked at the library example sketches to see if begin() is used without any arguments?
I'll try to do it and send it because I'm tired of trying to fix them. +code is from Chat gpt. I don't know how true it is. If someone wants to write similar code that can work, I would be happy. I will try to test the display. But again I say the problem is only with this code
I've been doing electronics for about a year and a half. For programming I've been using Arduino for a while. I've done a few projects who worked perfectly. The time has come to do this project. I use sg90 micro servo (I think it was called that) to rotate at certain degrees. The things that are in this project are: the Servo, lcd with i2c, arduino, button. About testing the codes in the library, I tested them at the beginning and it works. The problem is with this project.I'm sure the display works properly