MCU Examples.com
PIC Microcontroller Project Examples, free source codes and resources collection.


Greenhouse Automation System

System Design

The hardware unit of the prototype of the system is represented by the block diagram bellow. It contains a PIC16F877A microcontroller as the main processing unit and it gets inputs from the temperature sensor (LM35), LDR (Light dependant resistor) and a soil moisture sensor (simulated using a variable resistor). From the data obtained from the sensors the program controls the actuator components (heater, two cooler fans and solenoid valve) to achieve the system requirements. It also uses a LCD display (WH2004A) to display the data obtained from the sensors and the data obtained from the user.

The heater, cooler fans and light bulb will be connected to the microcontroller using a transistor array and 5V relays (The mechanism used is a normally-open relay switch) since they need an AC power supply to operate. A switch is introduced to manually switch off the light bulbs by cutting off the power supply to the light bulbs.


The program software is developed to follow the system flowchart given below. Several macros were also developed in the program (as given in the Appendix) and they were used to give a delay, for initializing the LCD, displaying characters in the LCD display and sending commands to the LCD. What the macro does is explained by commenting on the instructions as given in the Appendix.



The system consist of three subsystems temperature monitor and control system, light intensity monitor and control system and a soil moisture level monitor and control system. the system operates according to the flow chart show. The light monitor and control system consists of a LM35 temperature sensor a user mode switch and two fans and a heater. The user mode switch is connected to RB5 pin in the microcontroller and tested whether the switch is ON, if it is ON (RB5 read as high) the microcontroller saves the value to the EEPROM set by the user by means of the potentiometer connected to the RA4 pin. The analogue input value is converted to a digital value inside the microcontroller and the value is saved in the variable and displayed. Then it will be written to an address location of the EEPROM which in this case is 0x10. The program then checks again whether the user mode switch is pressed and if it is ON once again the program converts the analogue input value to digital and saves it in the variable and the EEPROM. If the switch is OFF the program goes into automatic mode and regulates the temperature to the value set by the user. In this mode the temperature sensor detects the current temperature value and inputs it to RA0 pin of the microcontroller. The input is an analogue input and it is converted to a digital input and calibrated as follows. The resulting value after A/D conversion is reduced by a constant 1 and divided by 2.Then it is displayed and checked with the user defined value by subtracting the current temperature value from the user defined temperature value saved in the EEPROM. If the result is negative it means that the current temperature value is greater than the user defined temperature value so the microcontroller makes the RC1 pin high to ON the cooler fans and RC2 pin low to turn OFF the heater to bring down the temperature to the user defined value. If both the values are equal the result is zero then both RC1 and RC2 pins are set as low hence both fans and the heater are off. If the result is positive it means that the current temperature value is less than the user defined temperature value so the microcontroller makes the RC1 pin low to off the cooler fans and RC2 pin high to ON the heater to bring up the temperature to the user defined value.

The light intensity of the system is controlled to a value obtained by testing therefore the analogue input value was not calibrated. Instead the sensitivity of the LDR was altered by using appropriate resistor (1kΩ) and was found that in a normal room the light intensity is around 34% and in bright sunlight the value was around (90%) so the ideal light intensity value was taken as 50% and then light was controlled around this ideal value and it is taken as a percentage value since a lux meter was not available to calibrate the sensor properly. Again the analogue input value from the LDR (voltage between the 1kΩ resistor and the ground) is taken at RA1 pin and converted it to a digital value and then this value is subtracted by 50 and if the result is zero the bulbs are turned OFF by setting RC4 pin as low and if the result is negative the bulbs are again turned OFF by setting RC4 pin as low and if the result is positive the bulbs are lit by setting RC4 pin as high.

The soil moisture level is also controlled to a predefined ideal value like the light intensity monitor and control system. The sensor was simulated using a non-linear potentiometer and it is an input to the microcontroller at RA2 pin. The analogue value is converted to a digital value and saved in a variable and displayed in the LCD screen as explained in the program code in the Appendix. Then this value is subtracted from the ideal value which is assumed to be ‘70’ and if the result is zero the solenoid valve is shut by setting RC2 pin as low. When the result is negative again the solenoid valve is shut and if the result is positive the solenoid valve is on by setting RC2 pin as high.

Major costs for building the prototype,

  • PIC16F877A – 5 USD /=
  • LM35 temperature sensor– 2 USD
  • LCD display 25 USD
  • LDR – 0.5 USD
With the cost of the resistors, wires and potentiometers total cost would be around 40 USD for electronics