How to: Greenhouse with Arduino UNO

Index:

 

  1. Components
  2. Steps
  3. Code


Components                                                                   

  • Wood
  • 7 Leds
  • 1 Water pump
  • Wires
  • 2 Arduino UNO
  • 1 Humidity Sensor (DHT11)
  • 1 Distance Detector (HC-SR04)
  • 2 Servos
  • 1 Container for water
  • Plastic (for the windows)
  • 1 Circuit board 
  • 6 Straws 
  • 1 LDR 
  • 1 Buzzer 
  • 1 Relay

Steps                                                                                

1. Build an structure

     ·We have to build a structure like a house, to build our greenhouse there. It has to be like this:



















Be sure that your structure has windows made up of plastic in the roof and a platform for the arduinos. (We are going to explain how to do the "water pump" structure later)

2. Illumination

If we want to see the greenhouse when the night comes, we will need lamps made up of straws and leds.
First we pick a straw and then we put inside that a led (we will need wires bellow the straw to connect it, so do the led more longer by melting tin on the led's wires) like this:
Then we put the straws where it illuminates your greenhouse.
But you want to illuminate the greenhouse when the night comes, so you will need a LDR, you have to do a platform to place it at the roof and then connect it to the arduino.

When you have all the straws located in a fix place and the LDR, then you will have to connect it to the arduino, be sure that you connect ground to ground and positive to positive. You must have something like this:
Here is the code for the illumination system:
__________________________________________________________

int LDR = A0;
int led1 = 12;
int led2 = 11;
void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(LDR, INPUT);
  Serial.begin(9600);
}
void loop(){
//LED
  potenciaLDR = analogRead(LDR);
  if(potenciaLDR < 450){
    digitalWrite(led1,HIGH);
    digitalWrite(led2,HIGH);
  }else{
    digitalWrite(led1, LOW);
    digitalWrite(led2,LOW);
  }
  //Fin LEDS
__________________________________________________________ 

3. Alarm system 

No one wants to be rob by someone, so your greenhouse will need an alarm protection system.
You will need to locate the Distance Detector (HC-SR04) in the door of your greenhouse or in a zone that the thieves will walk.
Then you have to connect a Buzzer to the distance detector (HC-SR04). Here is an example of where to locate it:























And here is the code for the alarm system to work: 
__________________________________________________________

long distancia;
long tiempo;

int sonido = 120;
int zumbi2 = 9;
int alarma = 0;
int zumbi = 5; 
int i = 0;
int ultrasonido = 8;
void setup(){
Serial.begin(9600);
  pinMode(zumbi2, OUTPUT);
  pinMode(ultrasonido, INPUT);

}
void loop(){
// Ultrasonido
  sonido = sonido + 3;
  if( sonido >= 440){
    sonido = 120;
  }
  digitalWrite(9,LOW);
  delayMicroseconds(5);
  digitalWrite(9, HIGH);
  delayMicroseconds(10);
  tiempo=pulseIn(8, HIGH);
  distancia= int(0.017*tiempo);
  alarma = distancia;
  if(alarma < 22){
    tone(zumbi, sonido);
    digitalWrite(ledtocho,HIGH);
  } else {
    noTone(zumbi);
    digitalWrite(ledtocho,LOW);
  }
  //Fin Ultrasonido

}
__________________________________________________________
4. Humidity sensor

You will need a sensor if the sky starts raining so you must place the humidity sensor at the roof of the greenhouse and a ventilator to dry it. When it gets wet, the ventilador switch on and it dry the humidity sensor, doing this you know when rains.
First place the humidity sensor in the roof and a ventilator near to it for drying the sensor. You can place it like this:























Now connect it to Arduino UNO and load the library "DHT.h", here is the link to download it: Download.
Here is the code:
__________________________________________________________

 int ventilador = 2;
 int temperatura = 0;
#include <DHT.h>
#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  pinMode(ventilador, OUTPUT); 
  dht.begin();
  //HumiditySensor
int h = dht.readHumidity();// Lee la humedad
int t= dht.readTemperature();//Lee la temperatura
//////////////////////////////////////////////////Humedad
Serial.print("Humedad Relativa: ");                
Serial.print(h);//Escribe la humedad
Serial.println(" %");                    
delay (5);
///////////////////////////////////////////////////Temperatura             
Serial.print("Temperatura: ");
Serial.print(t);
if(h >= 50){
  digitalWrite(ventilador,HIGH);
}else{
  digitalWrite(ventilador,LOW);
}
Serial.println(" C'");                  
delay (5);
//Fin HumiditySensor
}

__________________________________________________________
NOTE: You must load the library "DHT.h", if you don't do this, the humidity sensor and the ventilator will not work. You can download it here: DOWNLOAD

5.Servos

Now you will need the other Arduino UNO, so pick it and connect it to a computer.
The servos allow us to open and close the windows when we want. Place the servos near to the windows and place a thread in order to move the windows when we activate the servos, be careful because the servos only can be moved 180º.We place the servos like this:



















Don't worry if you activate the servos and it doesn't work, that is a signal to move the thread in the correct way to open and close the windows. Here you have the code:
__________________________________________________________

 #include <Servo.h>

Servo miServo;
Servo miServo2;
int angulo=1;

void setup(){
  miServo.attach(9);
  miServo2.attach(8);
  Serial.begin(9600);
}

void loop(){
  unsigned char comando=0;
  if(Serial.available()){
    comando=Serial.read();
  
    if(comando=='a')angulo+=178;
    else if(comando=='z')angulo-=178;
    angulo=constrain(angulo,1,179);
  }
  miServo.write(angulo);
  miServo2.write(angulo);
  Serial.print("Angulo:");Serial.println(angulo);
   delay(100);
}

__________________________________________________________

5.1 How to move the servos

 Now we are going to explain you how to work the servos. First you have to connect it to the arduino like you see in the code (One to the pin 9 and other to the pin 8) and then you have to open the console of the 
arduino by clicking at the button that the photo shows:

Then you will see a new window and a variable called "angulo=", it shows to you the position of the servos and you can increase it (by writting "a" in the console and pressing "send") or decrease it (by writing "z" in the console and pressing "send").

6. Water pump

This is maybe the most difficult part of the proyect. You must place 2 container with water like this:























The upest container must have a hole in the bottom, like 10mm of diameter. This is to water a plant that is bellow this container.
First we have to prepare a relay by connecting it to 230V and to the arduino. Make sure that you connect it well, be careful because you are working with 230V! Then you have to isolate the zone of the relay to protect it from water or an innocent finger. It must be protected like this:



Now write the code you want by writing "digitalWrite" to activate the water pump. Here you have a simple code example:

 __________________________________________________________

 int motobomba = 2;
void setup(){
pinMode(motobomba,OUTPUT);
}
void loop(){
digitalWrite(motobomba,HIGH);
}
__________________________________________________________

 6.Decoration

Now you have finished the tecnical part!
The decoration step is the funniest one, you can decorate your greenhouse whichever way you want, we decorate our greenhouse like this:



Now you have finished your Greenhouse! Take a look of our greenhouse in this video:
 

Code                                                                                

Here is all the code we compiled in the steps:

Arduino 1:

int temperatura = 0;
int ventilador = 2;
long distancia;
long tiempo;
int led1 = 12;
int led2 = 11;
int sonido = 120;
int LDR = A0;
int i = 0;
int Servopdt = 9;
int enciendete;
int numTones = 10;
int potenciaLDR = 0;
int zumbi = 5;
int alarma = 0;
#include <DHT.h>
#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(LDR, INPUT);
  Serial.begin(9600);
  pinMode(9, OUTPUT);
  pinMode(8, INPUT);
  pinMode(ledtocho, OUTPUT);
  pinMode(ventilador, OUTPUT);
  dht.begin();

}

void loop(){
  //pin de prueba (2)
  //Fin pin de prueba
//LED
  potenciaLDR = analogRead(LDR);
  if(potenciaLDR < 450){
    digitalWrite(led1,HIGH);
    digitalWrite(led2,HIGH);
  }else{
    digitalWrite(led1, LOW);
    digitalWrite(led2,LOW);
  }
  //Fin LEDS
  // Ultrasonido
  sonido = sonido + 3;
  if( sonido >= 440){
    sonido = 120;
  }
  digitalWrite(9,LOW);
  delayMicroseconds(5);
  digitalWrite(9, HIGH);
  delayMicroseconds(10);
  tiempo=pulseIn(8, HIGH);
  distancia= int(0.017*tiempo);
  alarma = distancia;
  if(alarma < 22){
    tone(zumbi, sonido);
    digitalWrite(ledtocho,HIGH);
  } else {
    noTone(zumbi);
    digitalWrite(ledtocho,LOW);
  }
  //Fin Ultrasonido
  //Temperatura
int h = dht.readHumidity();// Lee la humedad
int t= dht.readTemperature();//Lee la temperatura
//////////////////////////////////////////////////Humedad
Serial.print("Humedad Relativa: ");                
Serial.print(h);//Escribe la humedad
Serial.println(" %");                    
delay (5);
///////////////////////////////////////////////////Temperatura             
Serial.print("Temperatura: ");
Serial.print(t);
if(h >= 50){
  digitalWrite(ventilador,HIGH);
}else{
  digitalWrite(ventilador,LOW);
}
Serial.println(" C'");                  
delay (5);
//Fin Temperatura
}


Arduino 2:

#include <Servo.h>

Servo miServo;
Servo miServo2;
int angulo=1;
int motobomba=3;

void setup(){
  miServo.attach(9);
  miServo2.attach(8);
  Serial.begin(9600);
pinMode(motobomba,OUTPUT);
}

void loop(){
  unsigned char comando=0;
  if(Serial.available()){
    comando=Serial.read();
   
    if(comando=='a')angulo+=178;
    else if(comando=='z')angulo-=178;
    angulo=constrain(angulo,1,179);
  }
  miServo.write(angulo);
  miServo2.write(angulo);
  Serial.print("Angulo:");Serial.println(angulo);
  
digitalWrite(motobomba,HIGH);
  delay(100);
}
__________________________________________________________________________________

Blog realized by Arduino Team ( Pablo, Sergio, Carlos & Juancar ) from IES DIEGO DE SILOÉ - 4ºCB