With the rapid development of the Internet of Things and smart agriculture, automated plant care systems are gradually entering homes and office environments. Especially for busy modern individuals, prolonged business trips or neglect can easily lead to plant wilting. To address this issue, this article designs and implements an intelligent watering system based on the STC89C52 microcontroller, featuring automatic soil moisture monitoring, threshold setting, automatic/manual dual-mode control, and water pump driving functions, achieving precise and intelligent irrigation for plants.
System Architecture
This system uses the STC89C52 as the core controller, integrating a soil moisture sensor, LCD1602 display, independent buttons, relay module, and micro water pump, forming a complete closed-loop control system. The workflow is as follows:
1. The soil moisture sensor continuously collects soil moisture data;
2. The microcontroller reads the analog signal through the ADC0832 analog-to-digital conversion chip;
3. The processed data is displayed in real-time on the LCD1602;
4. The user sets the upper and lower limits for watering through buttons;
5. When the moisture level falls below the lower limit, the microcontroller outputs a high level to drive the relay, starting the water pump;
6. When the moisture level rises to the upper limit, the water pump is automatically turned off, completing one irrigation cycle. The system supports switching between automatic and manual modes, balancing intelligence and user intervention needs.
Core Hardware Design
1. Main Control Unit: STC89C52
As a classic 51 series microcontroller, the STC89C52 features 8KB of Flash program storage, 512 bytes of RAM, and 32 I/O ports, meeting the control requirements of this system. Its high compatibility and rich development resources facilitate rapid prototyping.
2. Humidity Collection Module: A resistive soil moisture sensor is used, outputting a 0-5V analog voltage signal, connected to the ADC0832 for analog-to-digital conversion. The ADC0832 is an 8-bit serial A/D chip, saving microcontroller I/O resources.
// Read ADC0832 channel 0 (soil moisture)unsigned char Read_ADC0832(unsigned char channel) { unsigned char i, data = 0; // Start conversion, send configuration bits ADC_CS = 0; ADC_CLK = 0; ADC_DI = 1; for(i=0; i<8; i++) { ADC_CLK = 1; delay_us(1); ADC_CLK = 0; if(i==1) ADC_DI = (channel==0)?0:1; // Select channel else ADC_DI = 1; data <<= 1; if(ADC_DO) data |= 0x01; } ADC_CS = 1; return data;}
3. Display and Human-Machine Interaction: The LCD1602 displays the current humidity value and the set threshold in real-time. Three independent buttons (set, increase, decrease) are used for parameter configuration, supporting power-off memory functionality, using the microcontroller’s built-in EEPROM to store set values.
// Initialize LCDvoid LCD_Init() { LCD_WriteCmd(0x38); // 8-bit data, 2-line display LCD_WriteCmd(0x0C); // Turn on display, no cursor LCD_WriteCmd(0x06); // Auto increment LCD_WriteCmd(0x01); // Clear screen}
4. Actuator: An NPN transistor drives the relay to control the start and stop of a 12V DC micro water pump. The relay output is connected to the pump and the water storage container, forming a complete irrigation loop.
// Water pump controlsbit Pump = P2^0;void Water_Control(unsigned char humidity) { if(humidity < lower_threshold) { Pump = 1; // Start water pump Buzzer = 1; // Alarm indication } else if(humidity > upper_threshold) { Pump = 0; // Stop water pump Buzzer = 0; }}
Software Flow Design: The main program uses a polling method, continuously executing data collection, display updates, and control judgments. After powering on, the system reads the saved thresholds from EEPROM and enters automatic operation mode.
void main() { unsigned char humidity; // Initialization ADC_Init(); // ADC conversion LCD_Init(); // LCD initialization Read_Thresholds(); // Read saved thresholds while(1) { humidity = Read_ADC0832(0); // Read humidity Display_Humidity(humidity); // Display if(mode == AUTO) { Water_Control(humidity); } Key_Scan(); // Button detection delay_ms(200); }}
System Features and Expandability:
This design has the following advantages:
• Dual-mode control: Combining automatic irrigation with manual intervention enhances practicality;
• Adjustable parameters: Users can set humidity thresholds according to different plant needs;
• Power-off memory: Set values are permanently saved, eliminating the need for repeated configuration;
• Simple structure: Low cost and low power consumption, suitable for home deployment. Future expansion directions include:
• Adding Bluetooth/WiFi modules for remote monitoring via a mobile app;
• Adding temperature and humidity sensors (e.g., DHT11) for comprehensive environmental monitoring;
• Integrating light detection and supplementary lighting for full environmental intelligent control;
• Introducing GSM modules to support remote control and alarm via SMS.
Conclusion:
The intelligent watering system based on the 51 microcontroller designed in this article has a reasonable structure, complete functions, and low cost, providing good practical value and educational significance. Through the collaborative design of hardware and software, it achieves automation and intelligence in plant care, offering a feasible technical solution for smart homes and small agricultural automation. This system can serve as a learning project for electronics enthusiasts and can also be promoted to practical application scenarios such as homes and offices.