In this chapter, we are setting up the main module consisting of an Arduino Mega2560 and an ESP8266 WiFi chip, which are the central hub of the automation and responsible for the following tasks:
Hosting the monitoring and controlling web page for the grow box on the local WiFi
Wirelessly communicating with the other modules, collecting sensor readings, and sending commands
Reporting to Google Sheets via REST API
Reporting to Home Assistant via MQTT
Controlling AC power and measuring power usage
Measuring temperature, humidity, and light intensity
Gbox420 sketch: Gbox420_Mega_Main
Or visit the Gbox420 GitHub page and download the entire project.
If you decide to use a different pin layout from what is shown below, update the Settings.h file, look for variables ending with Pin
A junction box with 12 holes was used to house the components of the Main module. These boxes are very easy to work with, you just need to drill a slightly smaller hole than the cable diameter in the rubber cap covering the holes.
When a module starts up or receives a command a piezoelectric buzzer provides audio feedback. To protect the buzzer from excess current make sure to have a minimum 200Ω resistor between the Arduino port and the + side of the buzzer. If you find the volume too loud use a larger resistor. I took a few sound intensity measurements using different resistance values: 220Ω - 71dB, 330Ω - 69dB, 1kΩ - 59dB, 3kΩ and above are inaudible.
+ pin: Arduino 2 (digital output). Connected through a resistor to limit current and volume: 200Ω(loud) - 1kΩ(quiet)
- pin: Arduino GND (Ground)
Expected test result: Plays the Star Wars theme song
Bypass capacitors or decoupling capacitors are added between each power and ground line to filter out noise from the power supply and make the entire circuit more stable. Here is an excellent video from EEVblog that explains bypass capacitors.
Be mindful that electrolytic capacitors are polarized, meaning they can only be connected one way in a circuit. The negative lead is generally marked on the capacitor with a bar or "-" symbol, connect this to Ground (or the more negative line). Do not mix up the polarity as this will heat up and destroy the capacitor in a few seconds. Ceramic capacitors have no polarity, they can be connected in any direction. The capacitance of both capacitors can be changed based on the power needs, as long they support a minimum of 16V voltage.
DC input 12V
2200 uF - 16V Electrolytic capacitor + pin: +12V DC
2200 uF - 16V Electrolytic capacitor - pin: Ground
0.1 uF - 50V Ceramic capacitor pin 1: +12V DC
0.1 uF - 50V Ceramic capacitor pin 2: Ground
DC step down output 7V
1000 uF - 16V Electrolytic capacitor + pin: DC step down Vo+ output
1000 uF - 16V Electrolytic capacitor - pin: Ground
0.1 uF - 50V Ceramic capacitor pin 1: DC step down Vo+ output
0.1 uF - 50V Ceramic capacitor pin 2: Ground
Arduino 5V
470 uF - 16V Electrolytic capacitor + pin: Arduino 5V
470 uF - 16V Electrolytic capacitor - pin: Ground
0.1 uF - 50V Ceramic capacitor pin 1: Arduino 5V
0.1 uF - 50V Ceramic capacitor pin 2: Ground
Arduino 3.3V
10 uF - 16V Electrolytic capacitor + pin: Arduino 3.3V
10 uF - 16V Electrolytic capacitor - pin: Ground
0.1 uF - 50V Ceramic capacitor pin 1: Arduino 3.3V
0.1 uF - 50V Ceramic capacitor pin 2: Ground
To convert the 12V power input and feed the Arduino and the 8 port Relay module two DC to DC step down power supplies are used. These buck converters are cheap, power-efficient, have a small physical size, and support a wide range of DC input voltages. To adjust the output voltage you will need to have a multimeter tool to monitor the output voltage while changing the adjuster screw. Be careful: The Vin pin does not have a reverse polarity protection diode like the barrel jack, never mix up GND and Vo+ from the power supply!
Arduino: Using a step down-power supply to power the Arduino Mega2560 is optional as it already supports 12V through either the Vin pin or the barrel jack connector. The reason I opted for using the buck converter to provide 7V to the Vin pin is to reduce the heat generated by the Arduino's voltage regulator.
Relay module: The relay module needs 5V to operate. When a single relay is active it uses ~60 mA, when all 8 relays are active the total power consumption is ~480 mA. This exceeds the maximum 200 mA the Arduino Mega2560 can provide on its 5V output pin. This is why we need to use an individual power supply adjusted to 5V and connect it to the JD-VCC pin of the relay module.
Arduino - DC step down power supply
VO+ pin: Arduino Vin. Adjusted to 7 volts.
GND pin: Arduino GND (Ground) + Power input ground (Common ground)
In+: Positive power input (12V)
EN pin: Not connected
Relay module - DC step down power supply
VO+ pin: Relay module JD-VCC. Adjusted to 5 volts.
GND pin: Relay module GND (Ground) + Power input ground (Common ground)
In+: Positive power input (12V)
EN pin: Not connected
DC-DC Buck Converter
Input voltage: DC 4.5 to 24VOutput voltage: Adjustable DC 0.8 to 17VOutput current: Max 3AThe B variant of the Meanwell HLG LED driver`s dimming circuit supports 3 different dimming options:
100 kΩ adjustable resistor
10 V PWM signal
1-10 V DC voltage.
We are going to use PWM dimming, which works by varying the duty cycle of an Arduino-generated PWM signal and modulating the voltage between the LED driver`s DIM+ and DIM- cables with it. As the HLG LED drivers use 10V signals it cannot be directly controlled by the 5V Arduino, an electrically isolated switch called an optocoupler is required to interconnect the two systems using different voltage signals.
LED driver 1
Anode(1) pin: Arduino 11 (digital output). Connected through a 680Ω resistor to limit current.
Cathode(2) pin: Arduino GND (Ground)
Emitter(3) pin: LED driver 1 DIM-
Collector(4) pin: LED driver 1 DIM+
LED driver 2 (Optional)
Anode(1) pin: Arduino 12 (digital output). Connected through a 680Ω resistor to limit current.
Cathode(2) pin: Arduino GND (Ground)
Emitter(3) pin: LED driver 2 DIM-
Collector(4) pin: LED driver 2 DIM+
Expected test result: Increase brightness of Light 1 from 0% to 100% in 20 seconds, waiting 5 seconds, and decreasing the brightness back to 0% in 20 sec. After 5 sec the cycle restarts.
Note 1: The HLG dimming circuit can measure the duty cycle of PWM signals in the 100 Hz - 3 kHz frequency range. The Arduino Mega2560 supports PWM output on ports 2 to 13 with the default frequency of 490Hz, except for ports 4 and 13 where the frequency is 980Hz. Check out this Arduino Forum entry to customize PWM frequency. The Main sketch by default depends on Timer 1 that controls ports 12 and 11 to generate the 490Hz PWM frequency for dimming.
Note 2: Most drivers do not allow dimming to 0%. For example, HLG does not recommend going below 6%. This limit can be adjusted in Settings.h by changing the DimmingLimit in the LightsSettings section.
Note 3: When the Arduino PWM signal is HIGH the collector and emitter on the Output legs are shorted, signaling LOW. This HIGH - LOW difference is called an inverting output, meaning the HLG dimming will measure the inverse of the PWM duty cycle ( Arduino duty cycle: 90% ➞ HLG measures 10% duty cycle, Arduino duty cycle 20% ➞ HLG measures 80% duty cycle). This is compensated in the Arduino sketch.
The LM393 light sensor used in this project has one digital and one analog output. The digital output signals if the screw adjustable light intensity is reached, sending HIGH if light is not detected and LOW if light is detected. The analog output shows how intense the detected light is between 0 and 1023, where a lower value means stronger light. To make more sense the test sketch inverts the readings, making 0 the darkest analog reading and 1023 the brightest.
A0 pin: Arduino A0 (analog input)
D0 pin: Arduino 4 (digital input)
VCC pin: Arduino 5V
GND pin: Arduino GND (Ground)
Expected test result: Analog and digital readings displayed every 2 seconds.
To measure the total power consumption a PZEM-004T V3.0 energy monitor is used. It communicates with Arduino over TTL serial data and measures Voltage, Current, Power Consumption, and Total Energy Usage with a Current Transformer (CT).
Resetting the total power consumption counter: The power meter uses non-volatile flash storage to keep the total power usage even when the power is lost. To clear the counter you need to disconnect the TR and RX lines, hold down the CLR button for 5 seconds, release and press it shortly one more time. Do not forget to disconnect the AC power before doing this procedure, parts of the sensor are exposed to AC power!
DC side
TX pin: Arduino RX2 17 (digital output)
RX pin: Arduino TX2 16 (digital input)
5V pin: Arduino 5V
GND pin: Arduino GND (Ground)
AC side
1 port: AC Neutral(ACN) power line
2 port: AC Live(ACL) power line
3 port: CT ring black wire
4 port: CT ring red wire
There are multiple hardware revisions of the PZEM-004T sensor that can be identified by the model number printed on the circuit board. By default, the Main module uses the V3.0 version, but you can enable V1.0 or V2.0 by uncommenting the lines tagged with //For PZEM004T V1.0 or PZEM004T V2.0 in MainModule_Web.cpp and MainModule_Web.h.
For V1.0 and V2.0 use the following test sketch:
For V3.0 use:
Expected test result: Turns on Light1 and displays power readings every 5 seconds.
PZEM-004T-100A sensor
Hardware Rev V3.0Supported voltage: 80 - 260 VACPower rating: 100A / 22kW, Frequency support: 45 - 65 HzAn 8 channel relay module is used to control AC power to the LED driver 1 and 2, a two-speed internal fan, a two-speed exhaust fan, reservoir air pump and one other devices of your choice. Multi-channel relays tend to use negative logic, where: HIGH = relay OFF, LOW = relay ON. You can find more details of how relays work at lastminuteengineers.com.
NC: Normally connected. When the relay is off or has no power this pin is connected to COM. When the relay is on this pin is not connected.
COM: Common. The incoming power cable connects here, based on the relay's state the power is directed to the NC or NO ports.
NO: Normally Open. When the relay is off or has no power this pin is not connected. When the relay is on this port is connected to COM.
8 port Relay - Long pin group
GND pin: Not connected
In1 pin: Arduino 22 (digital output) - Free
In2 pin: Arduino 23 (digital output) - Reservoir Air pump
In3 pin: Arduino 24 (digital output) - LED driver 2 On/Off (Optional)
In4 pin: Arduino 25 (digital output) - Internal fan On/Off
In5 pin: Arduino 26 (digital output) - Internal fan Low(Default)/High speed
In6 pin: Arduino 27 (digital output) - Exhaust fan On/Off
In7 pin: Arduino 28 (digital output) - Exhaust fan Low(Default)/High speed
In8 pin: Arduino 29 (digital output) - LED driver 1 On/Off
VCC pin: Not connected
8 port Relay - Short pin group
GND pin: 5V from the dedicated power supply
VCC pin: Not connected
JD-VCC pin: Ground from the dedicated power supply
When a single relay is active the module uses ~60 mA, when all 8 relays are active the total power consumption is ~480 mA. This exceeds the maximum 200mA current the Arduino Mega2560 can provide on its 5V output pin. This is why we need to use an individual 5V power supply for the relay module, without it only a limited number of relays(3) can be active at the same time.
Expected test result: Turning all relays on with a 2-second delay until all relays are on. After 10 seconds turn off all relays, then turn on each relay for 2 seconds individually.
8 channel relay
5V power, Supports switching up to 250V - 10A AC, 30V - 10A DC powerTemperature and humidity are monitored using a DHT22 sensor that communicates with the Arduino over a single digital data line.
Specs
5 V power and I/O
2.5 mA max current use
0 to 100 % humidity readings with 2-5% accuracy
-40 to 80 °C / -40 to 176 °F temperature readings with ±0.5° accuracy
0.5 Hz max sampling rate (once every 2 seconds)
DAT pin: Arduino 3 (digital input)
VCC pin: Arduino 5V
GND pin: Arduino GND (Ground)
Expected test result: Temperature and humidity readings are printed to the serial output every 5 seconds.
To relay data between the Main module and all other modules nRF24L01+ wireless transceivers are used. The Main module always acts as a transmitter, all other modules are receivers. When a receiver gets a package from the Main module it sends back a pre-cached Acknowledgement package to report its current status. You can find a great in-depth overview of the wireless module at lastminuteengineers.com.
Specs
Power supply voltage: 1.9V to 3.6V (Use the Arduino's 3.3V pin to power the module, do not connect the VCC pin to 5V! )
I/O pins tolerate 5V logic (Arduino can directly communicate with the module)
Communicates over a 4 pin Serial Peripheral Interface (SPI)
Draws 12 mA during a transmit, 0.026 mA in standby, and 0.0009 mA at power down
Maximum 125 channels
Operates using 2.4 GHz frequency band
Maximum range: 100 meter / 330 feet outdoors with internal antenna, 1000 meter / 3300 feet with external antenna
Supported data rates: 250kbps (Largest range) / 1 Mbps / 2 Mbps (Fastest data transfer speed)
GND pin: Arduino GND (Ground)
VCC pin: Arduino 3.3V
CSN pin: Arduino 49 (digital output)
MISO pin: Arduino 50 (digital input)
MOSI pin: Arduino 51 (digital output)
SCK pin: Arduino 52 (digital output)
CE pin: Arduino 53 (digital output)
Test-WirelessConnection_Mega sketch
This sketch will verify the connectivity between the Arduino and the nRF24L01+ wireless transceiver.
Expected test result: Successful update of the "Data Rate" field
Test-WirelessTransmitter sketch
This sketch will verify the nRF24L01+ wireless transceiver communication between an Arduino Mega as the Transmitter, and an Arduino Nano a Receiver. Requires an Arduino Nano to be set up as a Receiver, check out the Aero module - Wireless transceiver section for more details on setting up the Transmitter.
Expected test result: Getting an Acknowledgement from the receiver when a command message is sent out and displaying the response data: