I have a project for school to make a soil moisture sensor with an LCD to display it. I turn it on but nothing appears on the screen. I created the code in tinkercad and used an identical diagram of my actual build and it worked their.
// C++ code
//
#include <Adafruit_MCP23X08.h>
#include <Adafruit_LiquidCrystal.h>
int moisture = 0;
Adafruit_LiquidCrystal lcd_1(0);
void setup()
{
pinMode(A0, OUTPUT);
pinMode(A1, INPUT);
Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
lcd_1.begin(16, 2);
}
void loop()
{
// Apply power to the soil moisture sensor
digitalWrite(A0, HIGH);
delay(10); // Wait for 10 millisecond(s)
moisture = digitalRead(A1);
// Turn off the sensor to reduce metal corrosion
// over time
digitalWrite(A0, LOW);
Serial.println(moisture);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
if (moisture < 200) {
digitalWrite(12, HIGH);
} else {
if (moisture < 400) {
digitalWrite(11, HIGH);
} else {
if (moisture < 600) {
digitalWrite(10, HIGH);
} else {
if (moisture < 800) {
digitalWrite(9, HIGH);
} else {
digitalWrite(8, HIGH);
}
}
}
}
delay(100); // Wait for 100 millisecond(s)
if (moisture <= 720 && (moisture >= 170 && moisture > 170)) {
lcd_1.print("I am happy!");
delay(3000); // Wait for 3000 millisecond(s)
lcd_1.clear();
lcd_1.print("Thank you!");
delay(3000); // Wait for 3000 millisecond(s)
lcd_1.clear();
} else {
if (moisture >= 720) {
lcd_1.print("I am sad. ");
delay(3000); // Wait for 3000 millisecond(s)
lcd_1.clear();
lcd_1.print("Too much water!");
delay(3000); // Wait for 3000 millisecond(s)
lcd_1.clear();
} else {
if (moisture <= 170) {
lcd_1.print("I am sad. ");
delay(3000); // Wait for 3000 millisecond(s)
lcd_1.clear();
lcd_1.print("I need water!");
delay(3000); // Wait for 3000 millisecond(s)
lcd_1.clear();
} else {
}
}
}
}
Check if the address of your display's I2C module is correct.
To do this, run the I2c Scanner code:
" Arduino Playground - I2cScanner
PS: Why are you using this library? " <Adafruit_MCP23X08.h> "
This is a library for the MCP23008/17 I2C and MCP23S08/17 SPI Port Expanders.
I'm not sure, I tried to run it and it said it was missing so I installed it.
I tried to run the scanner and got this error:
avrdude: ser_open(): can't open device "\.\COM5": The system cannot find the file specified.
Its display is a basic 16x2 type display.
I saw in the photo that you are using an I2C module coupled to it.
So the recommended library for it is one of:
etc..
I tried the second one because I am not to good with GitHub, same result.
I used it and got this result,
Scanning...
I2C device found at address 0x27 !
done
Scanning...
I2C device found at address 0x27 !
done,
what does this mean?
This means that the I2C address of your display is 0x27.
This is the most common address found on modules of this type.
Now you need to use the correct library for your display, using one of the ones I posted the link to.
Try this code:
Obs: Don't forget to install the library
" LiquidCrystal_I2C.h ".
// C++ code
//
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd_1(0x27, 16, 2); // Set the LCD address and dimensions (16x2)
int moisture = 0;
void setup()
{
pinMode(A0, OUTPUT);
pinMode(A1, INPUT);
Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
lcd_1.init(); // Initialize the LCD
}
void loop()
{
// Apply power to the soil moisture sensor
digitalWrite(A0, HIGH);
delay(10); // Wait for 10 millisecond(s)
moisture = digitalRead(A1);
// Turn off the sensor to reduce metal corrosion
// over time
digitalWrite(A0, LOW);
Serial.println(moisture);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
if (moisture < 200) {
digitalWrite(12, HIGH);
} else {
if (moisture < 400) {
digitalWrite(11, HIGH);
} else {
if (moisture < 600) {
digitalWrite(10, HIGH);
} else {
if (moisture < 800) {
digitalWrite(9, HIGH);
} else {
digitalWrite(8, HIGH);
}
}
}
}
delay(100); // Wait for 100 millisecond(s)
if (moisture <= 720 && (moisture >= 170 && moisture > 170)) {
lcd_1.print("I am happy!");
delay(3000); // Wait for 3000 millisecond(s)
lcd_1.clear();
lcd_1.print("Thank you!");
delay(3000); // Wait for 3000 millisecond(s)
lcd_1.clear();
} else {
if (moisture >= 720) {
lcd_1.print("I am sad. ");
delay(3000); // Wait for 3000 millisecond(s)
lcd_1.clear();
lcd_1.print("Too much water!");
delay(3000); // Wait for 3000 millisecond(s)
lcd_1.clear();
} else {
if (moisture <= 170) {
lcd_1.print("I am sad. ");
delay(3000); // Wait for 3000 millisecond(s)
lcd_1.clear();
lcd_1.print("I need water!");
delay(3000); // Wait for 3000 millisecond(s)
lcd_1.clear();
} else {
}
}
}
}
Alternatively, change your original code to the correct address:
Adafruit_LiquidCrystal lcd_1(7);
" <Adafruit_LiquidCrystal.h> "
Does this library work correctly with the PCF8574 chip?
It may not, the library was written for the Adafruit display that uses an MCP23008.
I think OP module uses PCF8574.
- Follow the GitHub link... click the green CODE dropdown, click the DOWNLOAD ZIP link
- In IDE, click SKETCH >> INCLUDE LIBRARY >> ADD ZIP LIBRARY... then navigate to the downloaded zip from step 1.
thank you both for all the help! last question. How do I turn the backlight on?
and also, the moisture sensor won't change, even when i water the plant.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.