My code for an Automated Greenhouse

So as a part of my project, I had to assemble a Greenhouse using an Arduino Uno (and now a nano) which is connecting to a Grove Moisture Meter, Grove Light Sensor, a Adafruit DHT22 and a Optical Water Level Sensor. These sensors would send data to the Arduino that would tell me about the plants conditions and uses various mechanisms to maintain these conditions.

Here is my main code:


#include <DHT.h>;
#include <Adafruit_NeoPixel.h>;

#define LEDPIN 6 //Pin RGB LED is attached to
#define LED_COUNT 30 //The No. of LED on a strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LEDPIN, NEO_GRB + NEO_KHZ800);

//Constants
#define DHTPIN 7  //What pin we're connected to
#define DHTTYPE DHT22 //DHT 22(AM2302)
DHT dht(DHTPIN,DHTTYPE); ////Initialize DHT sensor for normal16mhz Arduino

#define PUMP 5 //Pin for Pump

int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value

int sensorPin = A0;  //Pin for Moisture sensor
int sensorValue = 0;


//Pins
const int LIQUID_SENSOR_PIN = 8; //Optical Liquid Lvl Sensor
const int LED_PIN = 6;
const int DRY = 400;              


#include <math.h>
const int ledPin=9;                   //Connect the LED Grove module to Pin7, Digital 7
const int thresholdvalue=10;          //The threshold for which the LED should turn on.
float Rsensor;  //Resistance of sensor in k

void setup() 
{   
    strip.begin();                     //INITIALIZE NeoPixel strip object 
    
    pinMode(LIQUID_SENSOR_PIN, INPUT);
    pinMode(LED_PIN, OUTPUT);
    pinMode(PUMP,OUTPUT);
    digitalWrite(LED_PIN, LOW);
    
    Serial.begin(9600);               //start the Serial connection
    pinMode(ledPin,OUTPUT);            //Set the LED on Digital 12 as an OUTPUT
    dht.begin();

    
}
void loop() 
{
  for (int i = 0; i < LED_COUNT; i++)
  {
    strip.setPixelColor(i, strip.Color(255, 0, 0));  //Set the values to create a colour on the Red-Green-Blue spectrum
    //Colour is set to yellow since the 
  }
  strip.show(); 
  
  // Read sensor. If liquid touches the tip, the sensor will 
  // read 0V. Turn on LED if liquid is present.
  int isDry = digitalRead(LIQUID_SENSOR_PIN);
  if ( isDry ) {
    digitalWrite(LED_PIN, LOW);
  } else {
    digitalWrite(LED_PIN, HIGH);
  }

  delay(200);
  
  delay(2000);
  //Read data and store it to variables hum and temp
  hum = dht.readHumidity();
  temp = dht.readTemperature();
  //Print temp and humidity values to serial monitor
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" % Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");
  
  int sensorValue = analogRead(0);
  Rsensor=(float)(1023-sensorValue)*10/sensorValue;
  if(Rsensor>thresholdvalue)
  {
    digitalWrite(ledPin,HIGH);
  }
  else
  {
    digitalWrite(ledPin,LOW);
  }
  Serial.println("the analog read data is ");
  Serial.println(sensorValue);
  Serial.println("the sensor resistance is");
  Serial.println(Rsensor,DEC);//show the light intensity on the serial monitor;
  delay(1000);
    
  // read the value from the moisture sensor:
  sensorValue = analogRead(sensorPin);
  if(sensorValue<400)
  {
    digitalWrite(PUMP,HIGH);
    delay(3000);
    digitalWrite(PUMP,LOW);
  }
  else
  {
    digitalWrite(PUMP,LOW);
  }
  Serial.print("Moisture = " );
  Serial.println(sensorValue);
  delay(1000);



}

I was comparing the Grove Light Sensor package code to my own and I noticed that it uses A0 rather than the Digital pins:

#include <Grove_LED_Bar.h>
 
Grove_LED_Bar bar(3, 2, 0);  // Clock pin, Data pin, Orientation
 
void setup()
{
  // nothing to initialize
  bar.begin();
  bar.setGreenToRed(true);
}
 
void loop()
{
 
  int value = analogRead(A0);
  value = map(value, 0, 800, 0, 10);
 
  bar.setLevel(value);
  delay(100);
}

And just the other day, I worked on an HC-06 bluetooth device and copied someone elses project and it works like a charm, and I want to see if there is any way I can get it to put data in excel or on a web server (I'm open to suggestions)

I wanted to integrate a code that would allow the LED strip lights to turn on in the dark and for now I only had them on without any connection to anything except the Water Level sensor.

Any suggestions? Also feel free to make any other suggestions to improve on my code. I am growing a plant that is supposedly takes 2 weeks to germinate.

EDIT 24/03/2022: Okay, I was able to sort out most of my issues, however, I am looking for a way for my Grove Light Sensors to turn on my LED strip light when the Light Sensor detects that it is dark or at least below a certain value

You may want to add an RTC (DS3231) eventually.
And, maybe an LCD ?

In your spare time :wink: , study how to use the State Machine techniques when writing sketches.

You should never use delay( ) in your sketches.
Well you can but only if you understand why you shouldn’t :wink:

Used to have a greenhouse in our youth, lots of fun.


Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


Sometimes soaking seeds in hydrogen peroxide 3% for a day can speed germination.
Also kills pathogens.


  • For Excel look for PLX-DAQ.
  • Maybe add data logging to an SD card the plug SD card into PC computer to get data into Excel.

Hello cathalmurphy
Take a view to get some additional ideas.
https://www.instructables.com/howto/Greenhouse+arduino/
Have a nice day and enjoy coding in C++.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.