Chapter 10. Arduino in a Real Case – Greenhouse Control

At this point in the book, we have already seen every important aspect of the Arduino platform, from its inputs and outputs to the use of interrupts and communications.

For this final chapter, I would like to propose a complete project that uses as many concepts as possible that have been seen until now, and I thought that a greenhouse controller could be a good example.

We will even meet a final component, the relay, which will help us deal with external devices not directly connected to our circuitry.

So, let's go into our final project and see how much Arduino can do by simply connecting a bunch of electronic components and writing a little sketch.

A greenhouse controller

This project aims to control and automate all aspects in a theoretical greenhouse that has a number of sensors, which provide information regarding the environment and act on a number of devices as a reaction to changes in its environment.

To begin designing, connecting, and programming our controller, let's begin by clearly setting out what we expect of such a system by creating a simple list of the functions it should accomplish.

The controller requirements

In general terms, the controller should monitor and control the most common variables that could affect such an installation:

  • Air temperature
  • Soil humidity
  • Direct solar lighting

Secondly, the controller will also offer an alarm mechanism that could be manually triggered by a person inside the greenhouse in case of an emergency.

Modular design

To accomplish the previous requirements, we will divide the complete greenhouse controller into four main different modules or subsystems:

  • Temperature: It is perhaps the most important subsystem. Its main objective is to constantly monitor the temperature inside the greenhouse and operate a different number of devices to maintain the temperature between the limits of a pre-established secure range.
  • Watering: It will be responsible for keeping the humidity of the soil between the preset limits by operating an external water pump.
  • Lighting: The controller will open a retractable roof during the day and close it at night by using a big servomotor that will rotate an arm that moves the roof panels.
  • Alarm: In case a greenhouse operator needs it, he/she could press a button to activate a visual and acoustics alarm to indicate an emergency situation to others.

Let's take a deeper look at every one of these subsystems to try to understand their functioning and the electronic components we will use to accomplish these tasks.

Temperature control

The temperature control subsystem will receive information through a simple thermistor and acts on up to four different devices to control the temperature:

  • Visual indicator: The temperature should be constantly announced via a visual panel. In our case, the panel is going to be replaced by three simple LEDs that will act as a visual level of the temperature inside the greenhouse.
  • Fan: As the temperature goes up, the controller will act on a fan's speed, a motor in our example, to allow the greenhouse to be ventilated.
  • Retractable roof: The greenhouse roof can be opened or closed, and thus the temperature control subsystem can open it in case the temperature is getting too hot.
  • Watering pump: In case of an excessive temperature, the control system can activate the watering to increase the humidity of the greenhouse or try to avoid a fire inside the greenhouse.

Humidity control

The monitoring of the soil's humidity can be done thanks to special humidity sensors that can be bought in any major gardening store (around $2) or built by simply using a pair of nails as you can see, for example, in http://www.instructables.com/id/Garduino-Gardening-Arduino/step4/Build-Your-Moisture-Sensor/, but for the sake of simplicity in the project schematic and code, I will replace it by a simple potentiometer that will represent any generic resistive sensor.

We will also consider that the greenhouse counts on an external watering system that has its own pump. From the point of view of our controller, we only have to switch it on, and for this purpose we will use a new electronic component, a relay, that will be introduced in the The relay as a mediator section.

Lighting control

Thanks to the retractable roof, the greenhouse allows you to expose the plants to direct solar light during the day. In the night hours, the roof will be closed to avoid freezing.

From the point of view of the controller, we will only have to position the servo at 0 degrees to close the roof or at 180 degrees to open it.

Manual alarm

By using a simple push button, the greenhouse operator can activate the system alarm in case of an emergency.

The alarm routine will trigger up to four actions:

  • A loud sound will be produced to give an acoustic indication of the alarm situation
  • Panel LEDs will blink instead of indicating the temperature level
  • The retractable roof will be closed in case it is open to prevent air from flowing in case a fire breaks out
  • The watering system will be activated to try to increase the humidity in the greenhouse

Input and output devices

Once we know what is expected of such a system, let's make an account of the input and output devices that will be needed to accomplish the aforementioned requirements.

From the point of view of inputs, we will need to connect the following components to act as sensors:

  • Thermistor: To allow for temperature monitoring in a similar way as we have already seen in Chapter 8, Communicating with Others.
  • Photocell: To sense direct sunlight over the greenhouse and allow opening or closing of the retractable roof.
  • Humidity resistive sensor: To sense the soil's humidity and trigger the watering pump. As mentioned before, this sensor will be replaced in the schematics and code by a simple potentiometer to allow for simple testing.
  • Push button: This will allow the greenhouse operator to trigger the general alarm.

On the other side, talking about outputs, our project will use the following components connected to Arduino outputs:

  • Motor: To operate the fan that will ventilate the greenhouse
  • Servomotor: To open or close the retractable roof
  • Relay: To activate the watering pump, as we will see in the next section
  • Buzzer: To produce the alarm sound

The relay as a mediator

A relay is an electromechanical device also known as an electric switch that, thanks to the magnetic field generated by any electrical current, allows opening or closing a mechanical switch placed very close to a small coil just by powering the coil.

In a typical relay, the coil and the switch are encapsulated in a small case, offering usually four terminals, two for powering the coil and two for the switch.

Some types of relays even offer three terminals for the switch side as follows:

  • A normally closed terminal
  • A normally open terminal
  • A common terminal

In the next figure, you can see the schematic symbol of a relay and an image of the relay I've used for assembling the current project:

The relay as a mediator

A relay symbol and a picture of a real relay

The most common use of relays is to operate external or different voltage circuits without connecting and powering them to the controller circuit. In our case, we will use it to activate an alternate current watering pump by simply connecting the switch part of the relay as a usual manual switch inserted in one of the power wires of the watering pump.

From the point of view of managing a relay from Arduino, a coil is just a new inductive load, like that on a motor or a speaker, so we will connect it just the same way as we did with these other components as you can see in the following schematic:

The relay as a mediator

Connecting a relay through a transistor

I have also used a diode to protect against the possible counter-electromotive force peaks that could be produced as we did with the motor and the speaker.

For the sake of simplicity in the schematic and breadboard connections diagram, I will use only two-wire terminal connectors in the switch part of the relay so that the two wires coming from the watering pump can be easily connected to the relay.

Regarding its programming, we can power the coil on and off by simply setting the pin to which the transistor driver base is connected to HIGH or LOW. It doesn't have to be a PWM pin; any digital pin can do the work, because we are not going to use different voltage levels, just on or off.

For more detailed information relating to relays, its functioning, use, and different types, you could go and visit the Wikipedia entry for the relay at http://en.wikipedia.org/wiki/Relay.

The greenhouse controller circuit

Once we have met the relay as the new component that we will use in this project, here you have the complete schematic of the project.

Once again, don't let the number of components scare you. If you study the circuit one component at a time, you will immediately notice that they are all already known and their connections have been explained and tested before. This has been shown in the following image:

The greenhouse controller circuit

Complete greenhouse controller circuit schematic

At this point and before entering the code explanation of this project, I would like to make a compilation of the pins we will be using for the project, in the assembly of the circuit and in the code, to have a clear understanding about which kind of pin is necessary for every one of the connected components.

Regarding inputs, we will have the photocell, thermistor, and potentiometer connected to analog inputs because they are going to be read as analog values.

The other input device, the push button, will be connected to digital pin 2 because we will read it by using Arduino interrupt 0, which is only available through this pin. This way, we give total preference to the alarm by stopping anything else that the processor would be doing when the button is pressed, and we ensure that the button pressing is recorded even if Arduino is doing anything else.

Talking about outputs, the only device that requires an analog output and thus will be connected to a PWM pin is the motor, because we will be varying its speed in an analog way.

The other devices (relay, speaker, servomotor, and LEDs) can be connected to any digital pin, be it PWM or not, because we will be dealing with them as simple digital outputs and only generating values of HIGH or LOW with the digitalWrite() function.

To simply summarize the pins used for this project, here you have the full connections list:

  • Analog 0: Photocell
  • Analog 1: Thermistor
  • Analog 2: Humidity sensor, the potentiometer in our schematic
  • Digital 2: Button to be read through interrupt 0
  • Digital 3: Servomotor
  • Digital 4: Speaker
  • Digital 6: Motor to be used with PWM
  • Digital 8: Relay
  • Digital 11: Green LED
  • Digital 12: Yellow LED
  • Digital 13: Red LED

It may seem a big number of used pins, but let me say that we still have nine available pins:

  • Three more free inputs in the analog input side
  • Six digital pins, three of which are PWM

You could still include some more subsystems to the controller like, for example, a fire extinguisher operated through a relay just like the watering pump, by using just another digital pin.

Who said Arduino had few available pins? And, of course, you can still use an Arduino Mega if your projects need even more pins.

Breadboard connections diagram

Here, you have the breadboard connections diagram for the complete greenhouse controller project:

Breadboard connections diagram

Greenhouse controller project breadboard connections diagram

For this project diagram, I had to use a full-sized breadboard, and even so there are some components that aren't correctly seen because they are beside others. In any case, I'm sure that at this point of the book you don't really need this diagram and you will be able to assemble your circuit directly from the schematic.

Please notice the two wires connecting the two halves of the power rails just in the middle. In a full-sized breadboard, the upper and lower rails are not connected all along. They are divided into two halves to allow for different power sources. In our example, we don't need this and will power all the different devices from the same power source, so you have to connect both halves of the rails by simply using a small wire or a jumper; otherwise, all the components on the other half won't receive any power.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset