My smartfarming arduino code can't run due to an unknown error. i used several sensors in my project this time

my smartfarming arduino code can't run due to an unknown error. i used several sensors in my project this time, including dht22, soil moisture, photosensitive sensor for lcd i2c output. please someone help me.

#include <DHT.h>  // DHT library for DHT22 sensor
#define DHTPIN 2     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 sensor model

// Define analog pins for soil moisture and photosensitive sensor
const int soilMoisturePin = A0;
const int photoSensorPin = A1;

// Initialize LCD (replace with your specific LCD library and pin connections)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  // Example for I2C LCD with specific pins

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // Initialize serial communication (optional for debugging)
  Serial.begin(9600);

  // Set LCD display and clear screen
  lcd.begin(16, 2);
  lcd.clear();
}

void loop() {
  // Read temperature and humidity from DHT sensor
  float temp = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Check if any reads failed and exit early (to avoid NaN values)
  if (isnan(temp) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Read analog values from soil moisture and photosensitive sensors
  int soilMoisture = analogRead(soilMoisturePin);
  int photoSensorValue = analogRead(photoSensorPin);

  // Convert sensor readings to meaningful values (adjust based on sensor specifications)
  float moisturePercentage = map(soilMoisture, 0, 1023, 0, 100); // Map analog value to moisture percentage
  float lightIntensity = map(photoSensorValue, 0, 1023, 0, 100); // Map analog value to light intensity percentage

  // Display readings on LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temp);
  lcd.print("°C  Humidity: ");
  lcd.print(humidity);
  lcd.print("%");

  lcd.setCursor(0, 1);
  lcd.print("Soil Moisture: ");
  lcd.print(moisturePercentage);
  lcd.print("%  Light: ");
  lcd.print(lightIntensity);
  lcd.print("%");

  // Optional: Print sensor readings to serial monitor for debugging
  Serial.print("Temp: ");
  Serial.print(temp);
  Serial.print("°C, Humidity: ");
  Serial.print(humidity);
  Serial.print("%, Soil Moisture: ");
  Serial.print(moisturePercentage);
  Serial.print("%, Light: ");
  Serial.println(lightIntensity);
  
  // Delay between readings (adjust as needed)
  delay(2000);
}

Hi, @pamski
Welcome to the forum.

What do you mean by unknown, what does the IDE give as an error.

What model Arduino are you using?

Did you write your code in stages?
That is get each peripheral working ON ITS own, before combining your code.

If so what bit of basic code produces your error.

It looks like you have not included the LiquidCrystal Library in your code.

Have you written code that JUST makes your LCD display "Hello World" from the library examples?

Tom... :smiley: :+1: :coffee: :australia:

2 Likes

Missing some info, like the entire sketch, and error listing.

Warm greetings Tom, my name is Pamungkas. Thank you for responding to my problem in my college project this time.

I used Arduino Uno model in this project.

I've tried to use the code in each sensor I use but I'm not good enough at combining code yet. Maybe it's because I'm new to this field.

For some of the libraries in the sensors that I use, I have entered the sensor.h libraries but it doesn't work.

The errors that always appear include
In file included from C:\Users\Acer\Documents\Arduino\libraries\DHT_sensor_library\DHT_U.cpp:15:0:
C:\Users\Acer\Documents\Arduino\libraries\DHT_sensor_library\DHT_U.h:36:10: fatal error: Adafruit_Sensor.h: No such file or directory
#include <Adafruit_Sensor.h>
^~~~~~~~~~~~~~~~~~~
compilation terminated.

You need to include the libraries like #include <Adafruit_Sensor.h>

Arduino IDE >> TOOLS >> MANAGE LIBRARIES >> Adafruit Sensor (no underscore, no .h) >> INSTALL

1 Like