Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Controlled Automated Plant Watering System with Bluetooth and Soil Moisture Sensing

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Automated Plant Watering System with Bluetooth and Soil Moisture Sensing

Circuit Documentation

Summary

This circuit is designed to control a soil moisture monitoring system with automated water pumping and servo actuation capabilities. It uses an Arduino UNO as the central microcontroller to read soil moisture levels, control a DC motor via an L298N motor driver, and manage two servo motors. The system also includes an HC-05 Bluetooth module for wireless communication, allowing manual control over the motor and pump. A 12V battery powers the motor driver, which in turn powers the DC motor and water pump. The servos and soil moisture sensor are powered directly by the Arduino's 5V output.

Component List

Arduino UNO

Servo (x2)

Soil Moisture Sensor

L298N DC Motor Driver

DC Motor

HC-05 Bluetooth Module

Water Pump

12V Battery (mini)

Wiring Details

Arduino UNO

Servo

Soil Moisture Sensor

L298N DC Motor Driver

DC Motor

HC-05 Bluetooth Module

Water Pump

12V Battery (mini)

Documented Code

#include <Servo.h>

// Define pins for L298N motor driver
#define MOTOR_A_IN1_PIN 2   // Motor A input 1
#define MOTOR_A_IN2_PIN 3   // Motor A input 2
#define MOTOR_B_IN3_PIN 4   // Motor B input 3
#define MOTOR_B_IN4_PIN 5   // Motor B input 4
#define MOTOR_A_ENABLE_PIN 9 // Motor A speed control (PWM)
#define MOTOR_B_ENABLE_PIN 10 // Motor B speed control (PWM)

// Define pins for soil moisture sensor and servos
#define SOIL_SENSOR_PIN A0   // Soil moisture sensor input
#define SERVO1_PIN 6         // Servo 1 control
#define SERVO2_PIN 7         // Servo 2 control

// Threshold for soil moisture to start the pump
const int moistureThreshold = 400; // Adjust based on your sensor readings

// Bluetooth command for manual control
char bluetoothCommand;

// Initialize servo objects
Servo servo1;
Servo servo2;

// Timing variable for Servo 1 movement
unsigned long lastServoMoveTime1 = 0;

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

  // Initialize motor driver pins
  pinMode(MOTOR_A_IN1_PIN, OUTPUT);
  pinMode(MOTOR_A_IN2_PIN, OUTPUT);
  pinMode(MOTOR_B_IN3_PIN, OUTPUT);
  pinMode(MOTOR_B_IN4_PIN, OUTPUT);
  pinMode(MOTOR_A_ENABLE_PIN, OUTPUT);
  pinMode(MOTOR_B_ENABLE_PIN, OUTPUT);

  // Initialize servos
  servo1.attach(SERVO1_PIN);
  servo2.attach(SERVO2_PIN);

  // Set initial positions for servos
  servo1.write(0); // Start at 0 degrees
  servo2.write(90); // Initial position for servo2
}

void loop() {
  // Check for Bluetooth commands
  if (Serial.available()) {
    bluetoothCommand = Serial.read();
    handleBluetoothCommand(bluetoothCommand);
  }

  // Read soil moisture value
  int soilMoistureValue = analogRead(SOIL_SENSOR_PIN);
  if (soilMoistureValue < moistureThreshold) {
    startPump();
  } else {
    stopPump();
  }

  // No delay here to keep the loop responsive
}

void startMotor() {
  digitalWrite(MOTOR_A_IN1_PIN, HIGH);
  digitalWrite(MOTOR_A_IN2_PIN, LOW);
  analogWrite(MOTOR_A_ENABLE_PIN, 200); // Motor A speed control
}

void stopMotor() {
  digitalWrite(MOTOR_A_IN1_PIN, LOW);
  digitalWrite(MOTOR_A_IN2_PIN, LOW);
}

void moveMotorForward() {
  digitalWrite(MOTOR_A_IN1_PIN, HIGH);
  digitalWrite(MOTOR_A_IN2_PIN, LOW);
  analogWrite(MOTOR_A_ENABLE_PIN, 200); // Move forward
}

void moveMotorBackward() {
  digitalWrite(MOTOR_A_IN1_PIN, LOW);
  digitalWrite(MOTOR_A_IN2_PIN, HIGH);
  analogWrite(MOTOR_A_ENABLE_PIN, 200); // Move backward
}

void startPump() {
  digitalWrite(MOTOR_B_IN3_PIN, HIGH);
  digitalWrite(MOTOR_B_IN4_PIN, LOW);
  analogWrite(MOTOR_B_ENABLE_PIN, 200); // Pump speed control
}

void stopPump() {
  digitalWrite(MOTOR_B_IN3_PIN, LOW);
  digitalWrite(MOTOR_B_IN4_PIN, LOW);
}

void handleBluetoothCommand(char command) {
  switch (command) {
    case 'M': // Start motor forward
      moveMotorForward();
      break;
    case 'm': // Stop motor
      stopMotor();
      break;
    case 'N': // Start motor backward
      moveMotorBackward();
      break;
    case 'P': // Start pump manually
      startPump();
      break;
    case 'p': // Stop pump manually
      stopPump();
      break;
    case 'S': // Rotate servo1 to -90 degrees and return quickly to 0
      rotateServo(servo1, -90, 0);
      break;
    case 'U': // Rotate servo1 to 90 degrees and return quickly to 0
      rotateServo(servo1, 90, 0);
      break;
    case 'T': // Rotate servo2 to 0 and 90 degrees
      rotateServo(servo2, 0, 90);
      break;
  }
}

void rotateServo(Servo &servo, int targetAngle, int returnAngle) {
  // Move to the target angle
  servo.write(targetAngle);
  delay(0); // Allow some time for the servo to reach the target

  // Immediately return to the original angle
  servo.write(returnAngle);
}

This code is designed to run on an Arduino UNO and controls the various components of the circuit based on sensor inputs and Bluetooth commands. It includes functions for starting and stopping the motor and pump, as well as for moving the servos to specific angles. The loop() function continuously checks for Bluetooth commands and the soil moisture level, activating the pump when necessary.