Design and Implementation of an Intelligent Aquarium Monitoring and Control System Based on the 51 Microcontroller

Abstract

This article details the design and implementation of an intelligent aquarium monitoring and control system based on the 51 microcontroller. The system utilizes a DS18B20 temperature sensor, a pH sensor, and a water level sensor to monitor aquarium environmental parameters in real-time. It employs a DAC0832 for analog-to-digital conversion, combined with an LCD1602 display and a button module for human-computer interaction, enabling automatic adjustments of water temperature, water level, and pH value to ensure a stable living environment for fish. The article includes a complete Proteus simulation design approach, hardware circuit connection diagrams, software flowcharts, and source code with detailed comments, providing a comprehensive intelligent aquarium solution for electronics enthusiasts and IoT beginners.

1. System Overall Design

1.1 System Architecture

The intelligent aquarium control system adopts a modular design concept, mainly consisting of the following functional modules:

  1. Sensor Module: Includes DS18B20 digital temperature sensor, pH sensor, and water level sensor, responsible for collecting aquarium environmental parameters.
  2. Signal Conversion Module: Uses the DAC0832 chip to convert the analog signals from the pH sensor and water level sensor into digital signals.
  3. Control Module: The STC89C52 microcontroller serves as the main controller, processing sensor data and outputting control signals.
  4. Execution Module: Includes heating rods, inlet solenoid valve, and outlet solenoid valve, executing temperature adjustment and water level control.
  5. Human-Computer Interaction Module: LCD1602 display and four independent buttons for displaying parameters and setting thresholds.
  6. Alarm Module: LED indicator and buzzer, issuing alarms when pH values are abnormal.

1.2 Hardware Selection and Circuit Design

The main control chip selected is the STC89C52 microcontroller, a classic 51 core microcontroller with 8K bytes of Flash memory and 512 bytes of RAM, fully meeting the program storage and data processing needs of this system.

The temperature detection uses the DS18B20 digital temperature sensor, which features:

  • Measurement range: -55°C to +125°C
  • Accuracy: ±0.5°C (within the range of -10°C to +85°C)
  • Single bus interface, saving IO resources

The pH and water level detection use analog output sensors, converted through the DAC0832. The DAC0832 is an 8-bit D/A converter with dual-buffered input registers, allowing direct interfacing with the microcontroller.

2. Hardware Circuit Implementation

2.1 Sensor Interface Circuit

Temperature Sensor Circuit: The DS18B20 uses a single bus protocol, requiring only one data line to communicate with the microcontroller. A 4.7KΩ pull-up resistor must be added to the data line in the circuit design.

// DS18B20 data line connection
sbit DQ = P1^0; // Temperature sensor data line

pH/Water Level Sensor Circuit: Two analog sensors are connected to the microcontroller via the DAC0832. The DAC0832 operates in single-buffer mode, with ILE connected to high, CS and WR1 grounded, and WR2 and XFER connected to the microcontroller’s P2.7 pin.

// DAC0832 control line definitions
sbit DAC_CS = P2^7; // DAC0832 chip select signal

2.2 Actuator Drive Circuit

The heating rod, inlet valve, and outlet valve are all driven by relays, using a ULN2003 Darlington array to drive the relay coils. The input terminals of the ULN2003 are connected to the microcontroller’s P2.0-P2.2 pins.

// Actuator control pin definitions
sbit HEATER = P2^0; // Heating control
sbit INLET_VALVE = P2^1; // Inlet valve control
sbit OUTLET_VALVE = P2^2; // Outlet valve control

2.3 Human-Computer Interaction Circuit

The LCD1602 uses a 4-bit data bus mode, connected to the microcontroller’s P0.4-P0.7 pins, saving IO resources. The RS, RW, and E control lines are connected to P3.5-P3.7.

// LCD1602 interface definitions
sbit LCD_RS = P3^5;
sbit LCD_RW = P3^6;
sbit LCD_E = P3^7;
#define LCD_DATA P0

Four independent buttons are connected to P3.0-P3.3 for setting parameter thresholds:

// Button definitions
sbit KEY_UP = P3^0; // Increase button
sbit KEY_DOWN = P3^1; // Decrease button
sbit KEY_SET = P3^2; // Set button
sbit KEY_ENTER = P3^3; // Confirm button

3. Software Design and Implementation

3.1 Main Program Flowchart

The system software adopts a state machine design concept, with the main program flow including initialization, sensor data acquisition, data processing, control output, and display refresh modules.

3.2 Key Function Implementation Code

3.2.1 Temperature Acquisition and Processing

C

// Read DS18B20 temperature value
float Read_Temperature()

{ unsigned char temp_l, temp_h; int temp; float temperature; DQ = 0; // Pull down the bus to start reset Delay_Us(500); // Delay for more than 480us DQ = 1; // Release the bus Delay_Us(60); // Wait for 15-60us if(!DQ)

{ // Detect DS18B20 presence pulse Delay_Us(240); if(!DQ)

{ // Confirm DS18B20 presence // Send skip ROM command (0xCC) Write_DS18B20(0xCC); // Send temperature conversion command (0x44) Write_DS18B20(0x44); Delay_Ms(750); // Wait for conversion to complete // Reinitialize DQ = 0; Delay_Us(500); DQ = 1; Delay_Us(60); if(!DQ)

{ Write_DS18B20(0xCC); // Skip ROM Write_DS18B20(0xBE); // Send read command temp_l = Read_DS18B20(); // Read low byte temp_h = Read_DS18B20(); // Read high byte temp = temp_h; temp <<= 8; temp |= temp_l; temperature = temp * 0.0625; // Convert to actual temperature value return temperature; } } } } return -1000; // Return error value}

3.2.2 pH Value Acquisition and Processing

// Read pH value
unsigned char Read_PH_Value()

{ unsigned char ph_value; DAC_CS = 0; // Select DAC0832 // Assume pH sensor is connected to channel 0 of DAC P1 = 0x00; // Select channel 0 DAC_CS = 1; // Start conversion Delay_Us(100); // Wait for conversion to complete ph_value = P1; // Read conversion result return ph_value;}

3.2.3 Water Level Control Logic

// Water level control function
void Water_Level_Control(unsigned char current_level)

{ if(current_level < 90)

{ // Water level too low INLET_VALVE = 1; // Open inlet valve OUTLET_VALVE = 0; // Ensure outlet valve is closed } else if(current_level > 100)

{ // Water level too high OUTLET_VALVE = 1; // Open outlet valve INLET_VALVE = 0; // Ensure inlet valve is closed }

else { // Water level normal INLET_VALVE = 0; OUTLET_VALVE = 0; }}

3.2.4 Temperature Control Logic

// Temperature control function
void Temperature_Control(float current_temp)

{ if(current_temp < 25.0)

{ // Temperature too low HEATER = 1; // Turn on heating } else

{ HEATER = 0; // Turn off heating }}

3.2.5 pH Alarm Logic

// pH alarm function
void PH_Alarm(unsigned char ph_value)

{ if(ph_value < 6 || ph_value > 8)

{ // Abnormal pH value ALARM_LED = 1; // Turn on alarm light BUZZER = 1; // Turn on buzzer } else

{ ALARM_LED = 0; // Turn off alarm light BUZZER = 0; // Turn off buzzer }}

3.3 Human-Computer Interaction Implementation

3.3.1 LCD Display Module

// LCD display update function
void Update_Display(float temp, unsigned char ph, unsigned char level)

{ char temp_str[16], ph_str[16], level_str[16]; // First line displays temperature sprintf(temp_str, “Temp:%4.1fC “, temp); LCD_Write_String(0, 0, temp_str); // Second line displays pH value and water level sprintf(ph_str, “PH:%d “, ph); sprintf(level_str, “L:%dcm”, level); LCD_Write_String(0, 1, ph_str); LCD_Write_String(8, 1, level_str);}

3.3.2 Button Handling Module

// Button scanning function
void Key_Scan()

{ static unsigned char key_state = 0; static unsigned char set_mode = 0; if(KEY_SET == 0)

{

// Set button pressed Delay_Ms(10); // Debounce if(KEY_SET == 0)

{ set_mode++; if(set_mode > 3) set_mode = 0; while(!KEY_SET); // Wait for button release } } if(set_mode > 0)

{

// In setting mode switch(set_mode)

{ case 1: // Set temperature threshold if(KEY_UP == 0)

{ temp_threshold += 0.5; if(temp_threshold > 35.0) temp_threshold = 35.0; } if(KEY_DOWN == 0)

{ temp_threshold -= 0.5; if(temp_threshold < 15.0) temp_threshold = 15.0; } break; case 2: // Set upper limit for pH threshold if(KEY_UP == 0)

{ ph_high++; if(ph_high > 14) ph_high = 14;

} if(KEY_DOWN == 0)

{ ph_high–; if(ph_high < ph_low+1) ph_high = ph_low+1;

} break; case 3: // Set lower limit for pH threshold if(KEY_UP == 0)

{ ph_low++; if(ph_low > ph_high-1) ph_low = ph_high-1; } if(KEY_DOWN == 0)

{ ph_low–; if(ph_low < 0) ph_low = 0; } break; } if(KEY_ENTER == 0) { // Confirm button pressed set_mode = 0; // Exit setting mode Save_Thresholds(); // Save thresholds to EEPROM } }}

4. Proteus Simulation Design

4.1 Simulation Circuit Construction

When constructing the simulation circuit in Proteus, the following points should be noted:

  1. Select the STC89C52 or AT89C52 microcontroller.
  2. Use the model provided by Proteus for the DS18B20.
  3. Simulate analog sensors using adjustable potentiometers, connected via the DAC0832.
  4. Connect the LCD1602 in 4-bit data bus mode.
  5. Use LEDs to simulate actuators for easy status observation.

6. Conclusion

This intelligent aquarium control system based on the 51 microcontroller achieves automatic monitoring and control of temperature, pH value, and water level, with the feasibility of the system verified through Proteus simulation. The system features a simple structure, low cost, and high reliability, making it suitable as an introductory project for electronic design or a smart transformation solution for small aquariums. Through further optimization and expansion, a more comprehensive and stable intelligent aquarium product can be developed.

Appendix: Complete Source Code

#include <reg52.h>
#include <stdio.h>
#include <intrins.h>
// Hardware interface definitions
sbit DQ = P1^0; // DS18B20 data line
sbit DAC_CS = P2^7; // DAC0832 chip select
sbit HEATER = P2^0; // Heating control
sbit INLET_VALVE = P2^1; // Inlet valve
sbit OUTLET_VALVE = P2^2; // Outlet valve
sbit ALARM_LED = P2^3; // Alarm light
sbit BUZZER = P2^4; // Buzzer
// LCD1602 interface
sbit LCD_RS = P3^5;
sbit LCD_RW = P3^6;
sbit LCD_E = P3^7;
#define LCD_DATA P0
// Button definitions
sbit KEY_UP = P3^0;
sbit KEY_DOWN = P3^1;
sbit KEY_SET = P3^2;
sbit KEY_ENTER = P3^3;
// Global variables
float temperature = 0;
unsigned char ph_value = 7;
unsigned char water_level = 95;
float temp_threshold = 25.0;
unsigned char ph_low = 6, ph_high = 8;
unsigned char level_low = 90, level_high = 100;
// Function declarations
void Delay_Us(unsigned int us);
void Delay_Ms(unsigned int ms);
void LCD_Init();
void LCD_Write_Command(unsigned char cmd);
void LCD_Write_Data(unsigned char dat);
void LCD_Write_String(unsigned char x, unsigned char y, unsigned char *str);
void DS18B20_Init();
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20();
float Read_Temperature();
unsigned char Read_PH_Value();
unsigned char Read_Water_Level();
void Temperature_Control(float temp);
void PH_Alarm(unsigned char ph);
void Water_Level_Control(unsigned char level);
void Update_Display(float temp, unsigned char ph, unsigned char level);
void Key_Scan();
void Save_Thresholds();
void Load_Thresholds();
void main() { LCD_Init(); DS18B20_Init(); Load_Thresholds(); LCD_Write_String(0, 0, “Smart Aquarium “); LCD_Write_String(0, 1, ” System V1.0 “); Delay_Ms(2000); while(1) { // Read sensor data temperature = Read_Temperature(); ph_value = Read_PH_Value(); water_level = Read_Water_Level(); // Execute control logic Temperature_Control(temperature); PH_Alarm(ph_value); Water_Level_Control(water_level); // Update display Update_Display(temperature, ph_value, water_level); // Button scanning Key_Scan(); Delay_Ms(500); // Main loop delay }}// Other function implementations…// (Some function implementations omitted, please refer to the attachment for complete code)

#Official Account: Focus on Microcontroller Graduation Project Development

Feel free to message me for complete materials ❤️

Leave a Comment