## Video link [【Automatic watering device (based on CW32F030C8T6) - Bilibili】](https://b23.tv/IPfocXz)
## **Description:**
#### I often forget to water the flowers on my balcony, which causes some flowers to wither due to lack of water. For this reason, I came up with the idea of making an automatic watering device. It just happened to catch up with the IoT solicitation activity of EasyEDA, and combined with the "CW32 Developer Support Program" launched by Wuhan Xinyuan Semiconductor, a watering device with timed automatic watering, humidity detection automatic watering, and networked remote control watering was made based on ESP01S and CW32 microcontrollers.
## **Version description:**
#### This project has two versions. A problem was found during the physical verification of the first version. When using a 1S lithium battery to boost the drive motor, the battery voltage will instantly drop a lot, causing the networking module to restart due to insufficient power supply. It has been abandoned at present. The second version improves the power supply solution, adopts 2-3S lithium battery, and DC-DC step-down to power the motor and other equipment, solving the problem of power failure of the networking module when the motor starts.
## V1.0 (first version) Main hardware:
#### 1. MCU: CW32F030C8T6
#### 2. WiFi module: ESP01S
#### 3. Ambient light collection: GL5566 photoresistor
#### 4. Lithium battery and water pump voltage collection: high-precision resistor (resistance voltage divider)
#### 5. Current detection: INA226A
#### 6. Button: 3 6*6mm touch switches
#### 7. Motor drive circuit: PC817 optocoupler, WSP9926A N-channel MOS tube
#### 8. Lithium battery charging circuit: TP4056 charging chip
#### 9. Boost circuit: XL6008E1 DC-DC boost chip (input voltage: 3.6V~32V, output current (maximum): 3A)
#### 10. Buck circuit: ME6211C33M5G-N buck chip, 2 3.3V outputs, 1 for the main control and peripheral circuits, 1 for ESP01S
#### 11. Buzzer: 12A05 active buzzer
#### 12. Display: SPI and I2C interfaces are reserved, and an external 1.7-inch 12864 black and white screen is installed.
#### 13. Expansion interface: 3 expansion interfaces are reserved, which can be connected to other devices such as temperature and humidity sensors, soil moisture sensors, etc.
#### 14. Water pump: Use a 6V tea machine small water pump.
#### 15. Water pipes and water pipe shunts: 8mm and 4mm water pipes, 8 to 4 water pipe shunts (4 holes or 6 holes).
#### 16. Battery: 2 18650 batteries.
#### 17. Battery box: 2 18650 parallel battery boxes.
#### 18. Extension module 1: soil moisture sensor.
#### 19. Extension module 2: SHT30 temperature and humidity sensor (optional).
## V2.0 (Second Edition) Main Hardware:
#### 1. MCU: CW32F030C8T6
#### 2. WiFi module: ESP01S
#### 3. Ambient light collection: GL5566 photoresistor
#### 4. Lithium battery and water pump voltage collection: high-precision resistor (resistance voltage divider)
#### 5. Current detection: INA226A
#### 6. Button: 3 6*6mm touch switches
#### 7. Motor drive circuit: PC817 optocoupler, WSP9926A N-channel MOS tube
#### 8. Lithium battery charging and discharging circuit: 2 strings of 18650 lithium battery protection board (plan to connect to a single-chip 10V Taineng board to directly charge the lithium battery)
#### 9. DC-DC step-down circuit: LM2596S-ADJ DC-DC buck chip, input voltage range 4.5V-35V, output 1.23V-35V adjustable, output current: 3A (7-12V input, 6.0V output in the project)
#### 10.LDO buck circuit: AMS1117-3.3 LDO buck chip, maximum input voltage: 18V output voltage: 3.3V output current: 1A
#### 11.Buzzer: 12A05 active buzzer
#### 12.Display: SPI and I2C interfaces are reserved, external 1.7-inch 12864 black and white screen.
#### 13.Extension interface: 3 expansion interfaces are reserved, which can be connected to other devices such as temperature and humidity sensors, soil moisture sensors, etc.
#### 14.Water pump: Use a 6V tea machine small water pump.
#### 15. Water pipes and water pipe manifolds: 8mm and 4mm water pipes, 8 to 4 water pipe manifolds (4 holes or 6 holes).
#### 16. Batteries: 2 18650 batteries.
#### 17. Battery box: 2 18650 battery boxes in series.
#### 18. Extension module 1: Soil moisture sensor.
#### 19. Extension module 2: SHT30 temperature and humidity sensor (optional).
## Schematic diagram and PCB:
![微信图片_20230425200019.bmp]
![微信图片_20230425200010.jpg]
![微信图片_20230425200016.jpg]
## Function introduction:
#### 1. Use CW32 internal RTC clock to set the automatic watering interval (X days), watering time (X:00), watering duration (X minutes) and watering start date through buttons or mobile phone; #### 2.
Collect ambient light and soil humidity, and automatically turn on the water pump when the light and humidity are lower than the threshold, and turn off the water pump after watering for a period of time (adjustable in the program). The program continuously detects humidity, and continues watering when humidity is below the threshold, until watering stops when humidity is above the threshold.
#### 3. ESP01S connects to the Internet of Things, and the user remotely controls watering with a mobile phone, sets watering parameters, and ESP01S sends control commands and parameters to CW32 through the serial port.
#### 4. ESP01S obtains network time and sends it to the CW32 microcontroller at regular intervals to calibrate the RTC clock of CW32 (this function has not yet been implemented).
#### 5. Detect the voltage and current of the output motor, stop the motor if the voltage is too high or too low, and stop the motor if the no-load current (no water in the water tank) is low. #### 6. Monitor
the lithium battery voltage, and turn off the automatic watering function when the battery is low.
#### 7. The ESP01S power supply can be turned on or off by the CW32 microcontroller to reduce power consumption (automatically connected to the Internet by default when powered on).
#### 8. The output voltage and current abnormal alarm and lithium battery low voltage alarm can be set.
#### 9. Screen display time and related setting parameters;
## Software:
### The program is divided into two parts
#### 1. CW32 MCU is developed using KEIL;
#### 2. ESP01S is developed using ARDUINO IDE;
## Shell:
### The finished waterproof junction box purchased online, with a size of 158X90X60, has holes opened in the corresponding positions of the top cover to install the control board, display screen, and switch. The battery box is glued to the inside of the top cover with 3M tape. Only the water pump is installed on the bottom cover, and the two water inlet and outlet interfaces are extended through the holes;
## Progress update
### On March 25, the circuit board welding was completed, the program was burned normally, the lithium battery charging circuit worked normally, the two-way ldo voltage regulation output was normal, the boost circuit output 6V voltage was normal, the ESP01S power control circuit worked normally, and the St7567 screen lit up normally.
### On April 2, the outer shell was made. The ready-made waterproof box was used directly. Holes were made in the corresponding positions. The screen was separated from the main circuit board and connected through cables. The main control board, screen, and battery box were all placed on the top cover, and only the water pump motor was placed on the bottom cover.
### On April 4, the program was gradually improved. ADC acquisition, INA226 current acquisition, RTC real-time clock acquisition, serial port 1 and serial port 2 data receiving and sending, button control, buzzer control, SPI mode driving 1.7-inch screen and other functions have been realized. ESP01S uses Arduino to write programs. At first, a lighting server was used, but the program took up too much space, so we later switched to Shell IoT. Remote control is temporarily realized, and other platforms will be switched later. During this period, I encountered a problem. After ESP01S was powered on, it could not connect to the server and kept restarting. I thought it was caused by insufficient power supply. It was not possible to power the module with flying wires. Later, I found that CW32 could send data to the ESP01S serial port only after the module successfully connected to the server. In the CW32 initialization program, the ESP01S power supply was delayed for 5 seconds before continuing to execute the following program, and then it was normal. In addition, there is a problem in the circuit design. The built-in RTC of the microcontroller is used, and the IO of PC13 cannot be used normally. This was not considered at the beginning. Currently, flying wires are used to connect PF07 and LED.
### On April 8, when testing the networked watering function, it was found that after starting the motor, the battery voltage drop was relatively large. ESP01S directly powered off and restarted, but CW32 had no problem at all and still worked normally. It did not work to increase the capacity capacitor and reduce the voltage output from the boost circuit to power ESP01S alone. Finally, it was decided to change the plan, which resulted in the second version below.
### On April 9, the second version used LM2596S-ADJ DC-DC buck chip and AMS1117-3.3 LDO buck chip to power the entire system, cancelled the 8M external crystal oscillator, adjusted the functions of individual IO ports, and changed the IO port for controlling LED to PF07. Re-draw the board and place an order on the same day. This time, choose the green board, and the others are the default.
### On April 13, I received the new board (Jialichuang is really fast, it only took 2 days from ordering to delivery, I give them a thumbs up here.), welded the new board overnight, and transplanted the MCU, INA226, and MOS chips on the original V1.0 board in accordance with the principle of no waste. After welding, I tested each pin one by one and found no problem, then slightly modified the program and re-burned it, turned on the power, and tested it to control the water pump motor to open and close in the network state, and there was no power-off restart phenomenon.
### On April 15, a solar charging interface was added to connect to the 2-string 18650 lithium battery protection board to charge the 18650 lithium battery.
### On April 16, a soil moisture sensor was connected to realize automatic watering according to humidity changes. In the program, the watering mode was changed to three modes: 0 manual watering, 1 timed automatic watering, and 2 automatic watering when humidity is below the threshold.
### Next step: continue to improve the program.
## Actual photos (the appearance is ugly, sorry)
### V1.0 version
! [2081694071.jpg]
#### The soldering iron and heating plate were used to solder the board, and the soldering is ugly.
![1659654267.jpg]
#### The display screen was removed from the POS machine in Lao Wang's shop. It is a 1.7-inch black and white screen with a resolution of 12864. It has an SPI interface and is compatible with ST7567. A screen base plate is specially made for easy installation on the shell. It is connected to the main control board using a flat cable and an 8P PH2.0 plug.
![1831879244.jpg]
#### The first version has a 5V charging function, and the shell has a charging port.
### V2.0 version
![328564738.jpg]
#### This is the second version, which uses DC-DC step-down mode to power the entire system. Most of the components, including the main control chip, are moved from the 1.0 board. Although it is a bit laborious, at least it is not wasted. After all, the 1.0 version of the board is useless.
![516754250.jpg]
#### The screen, circuit board and battery are all placed on the upper cover of the waterproof box, and only a water pump is placed on the bottom cover. This water pump is a water pump for a tea machine bought on Taobao. The merchant's parameter is 6V power supply.
![351737742.jpg]
#### The screen display content can be switched by pressing the buttons. The first page is time, watering mode, water pump status, and battery voltage. The second page displays some specific parameters. The third page is the setting page. You need to press and hold the rightmost button to enter. Short press is to switch the display content. On the setting page, you can adjust specific parameters through the first and second buttons (that is, plus and minus), such as changing the watering mode, watering time, watering duration and other parameters.
![1837380421.jpg]
#### This finished junction box is very suitable for DIY. The only disadvantage is that it is ugly. As the saying goes, "It's not unusable."
![1092447916.jpg]
#### My skills are limited and time is tight. I took the previous code from Shell IoT and modified it. With their applet, I can quickly realize network control.
![1837380421.jpg]
#### To fix this water pump, you need to unscrew the three screws on the top and insert them through the three holes in the shell to fix it. For safety reasons, hot melt glue is also applied under the motor and the bottom cover to prevent the water pump from being pushed in when the water pipe is connected outside.
![988215926.jpg]
#### The inner diameter of the water pipe I bought online was wrong. I bought a size larger. In the end, I used the pipe on the atomizer. I also bought the wrong water pipe diverter. I would have bought a 4-port one. The water pressure of the 6-port one is too low, and the water flows very slowly at the pipe mouth.
![IMG_20230415_205616.jpg]
#### A charging port is added to the shell, with an input voltage of DC 8.4~9V, which can connect to a 10V solar panel. The positive pole of the charging port is connected to a 2-string 7.4V lithium battery protection board through a diode. This lithium battery protection board can both charge and discharge the 2-string lithium battery.
![IMG_20230415_205700.jpg]
#### The three interfaces on the side are used to expand other modules, including temperature and humidity sensors and soil moisture sensors.
![IMG_20230415_205425.jpg]
#### 2 lithium battery protection boards are glued to the battery box with 3M tape.
![IMG_20230415_212517.jpg]
#### It is recommended to use a capacitive soil moisture sensor, which is sensitive and not easily corroded.
![QQ图片20230418111315.jpg]
## At present, most of the basic functions have been realized, but the function of online calibration time has not been realized, and there are still some BUGs in the program that have not been solved. The program has been open sourced, and the level of a novice is limited. It is not well written. Please advise!