Skip to content

Code Overview and Integration πŸ’»

In this section, you will find the code used to run the sensors for our AgroLink project 🌱. Each sensor contributes vital data to the system, allowing for real-time decisions based on environmental factors. Here’s a breakdown of each sensor’s code, what it does, and how the data helps the user optimize their indoor garden.

We’ll start by looking at the individual codes for each sensor and explain how they come together in the merged code.

Use the three backticks to separate code.

To get started with the AgroLink system, you’ll need the Arduino IDE software to upload the code to the microcontrollers (Arduino Uno and Arduino MKR WiFi 1010).

β–ͺ️ Steps:

  1. Connect the Arduino board to your computer via USB.
  2. Open the Arduino IDE and paste the code.
  3. Select the correct board (Arduino Uno or MKR WiFi 1010) and COM port.
  4. Click Upload to transfer the code to the microcontroller.
  5. Use the Serial Monitor (9600 baud rate) to see real-time sensor readings and debugging messages.

Water Level Sensor 🌊

The water level sensor detects the amount of water in the bird sink. It outputs different readings depending on whether the sink is empty, low, medium, or full.

Code Explanation: This code reads the analog signal from the water level sensor and prints its state to the serial monitor.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
}

void loop() {
  int value = analogRead(A1);

  if (value == 0){ 
    Serial.println("Empty");
  } else if (value > 1 && value < 200) {
    Serial.println("Low");
  } else if (value > 201 && value < 220) {
    Serial.println("Medium");
  } else if (value > 220){
    Serial.println("High");
  }
}

What the User Sees:

β–ͺ️ The water level (empty, low, medium, or full) is printed in the Serial Monitor.

Pin Arrangement:

β–ͺ️ Sensor Pin: A1

Temperature & Humidity Sensor (DHT11) 🌑️

This code reads data from the DHT11 sensor to measure the temperature and humidity in the environment.

#include <DHT11.h>

DHT11 dht11(2);

void setup() {
    Serial.begin(9600);
}

void loop() {
    int temperature = 0;
    int humidity = 0;
    int result = dht11.readTemperatureHumidity(temperature, humidity);

    if (result == 0) {
        Serial.print("Temperature: ");
        Serial.print(temperature);
        Serial.print(" Β°C\tHumidity: ");
        Serial.print(humidity);
        Serial.println(" %");
    } else {
        Serial.println(DHT11::getErrorString(result));
    }
}

What the User Sees:

β–ͺ️ Temperature and humidity values are printed in degrees Celsius and percentage.

Pin Arrangement:

β–ͺ️ Data Pin: Digital Pin 2

Light Intensity Sensor 🌚/🌝

The light intensity sensor measures ambient light levels, indicating whether it’s light or dark.

void setup() {
  Serial.begin(9600);
}
void loop() {
  int analogValue = analogRead(A3);
  Serial.print("Analog reading: ");
  Serial.print(analogValue);   

  if (analogValue < 400) {
    Serial.println(" - Light");
  }
   else (analogValue < 800); {
    Serial.println(" - Dark");
  }
}

Last update: October 12, 2024