Debugging an Arduino plant-watering prototype with relay and pump noise
An Arduino Nano-based automatic plant watering prototype is being tested for a school exhibition. A capacitive soil-moisture sensor feeds A0; a 5 V relay module switches a 12 V 0.6 A pump; the Nano and relay are powered from the USB port of a laptop, while the pump uses a separate 12 V 1 A adapter. The relay module has IN, VCC, GND and a flyback diode on the coil. The sketch reads A0, averages 10 samples, and turns the pump on below 600 and off above 600. During testing, the pump chatters rapidly near the threshold, the serial monitor shows A0 jumping between 300 and 900 whenever the pump runs, and the Nano occasionally resets. The pump wires run parallel to the sensor cable for 2 m. The relay coil draws 70 mA at 5 V; the Nano's 5 V pin is already supplying the sensor. A 12 V 0.6 A pump has a measured stall current of 2.5 A. The team has a 470 microfarad capacitor, a separate 5 V 1 A adapter, a 1N4007 diode, and a 2N2222 transistor.
1. Explain the chattering at the threshold and propose a hysteresis scheme with specific ADC values. Why is averaging 10 samples not enough?
Read the worked answer
The pump is switched by a single threshold: below 600 on, above 600 off. Any noise near 600 causes repeated crossing and relay chatter. Averaging 10 samples reduces random noise but does not remove a slow drift or a supply-correlated jump, and it does not create a stable decision band. Use hysteresis: turn the pump on when the reading falls to 550 or below, and turn it off only when the reading rises to 650 or above. The 100-count band is about 0.49 V with a 5 V, 10-bit ADC, which is wide enough to ignore the observed jumps but still responsive to real soil drying. Implement it as a state variable: if pump is off and reading is less than or equal to 550, set pump on; if pump is on and reading is greater than or equal to 650, set pump off. Add a minimum run time of a few seconds so the pump cannot chatter mechanically even if the sensor is faulty.
2. Diagnose why the Nano resets and why A0 jumps when the pump runs. Propose wiring and power fixes using the available parts.
Read the worked answer
The pump is an inductive load with a stall current of 2.5 A, far above the 1 A adapter rating. Starting current and motor brush noise cause a 12 V rail dip and radiated EMI. The relay module's flyback diode protects only the relay coil, not the pump motor, so the motor's inductive kick is unsuppressed. The 70 mA relay coil and the sensor also load the Nano's 5 V pin from the laptop USB, which can sag. Fixes: power the Nano and relay from the separate 5 V 1 A adapter with a common ground, add the 470 microfarad capacitor across the 5 V rail near the Nano, fit the 1N4007 across the pump terminals with the cathode to +12 V, twist the pump wires and route them away from the sensor cable, and use a 12 V supply rated at least 3 A to cover the 2.5 A stall. Do not share the pump ground return with the sensor ground; join grounds at one point near the supply.
3. Assess the relay and protection ratings. What should be checked before the exhibition, and how would the 2N2222 be used if the relay module were removed?
Read the worked answer
The relay contacts must be rated for the pump's 12 V DC inductive load, not just its headline AC rating. A module marked 10 A 250 V AC may have a much lower DC rating, and the 2.5 A stall current plus inductive kick stresses the contacts. Check the contact rating for 12 V DC and add a fuse, about 3 A, in the 12 V pump line. The 1N4007 across the pump is essential. If the relay module were removed, the 2N2222 could drive the relay coil or a small DC pump only if its ratings are respected: the 2N2222 is a small-signal transistor, not a 2.5 A motor switch. For a 70 mA relay coil, base current through 1 kohm from a 5 V output is about 4.3 mA, which is enough to saturate it. For the pump itself, use a suitably rated MOSFET or a relay; the 2N2222 would overheat. Verify common ground, flyback diode orientation and a current-limited test before connecting the pump.