BlueCore Wireless Sensor & Control Station — Arduino Mega, UNO and NRF24L01

by vandenbrande in Circuits > Arduino

28 Views, 0 Favorites, 0 Comments

BlueCore Wireless Sensor & Control Station — Arduino Mega, UNO and NRF24L01

Remote Node Schematic.png
Arduino Wireless Sensor & Relay Control Station | NRF24L01 Mega + UNO | BlueCore Tech

Hello world! Today i made a wireless sensor and control station with an Arduino Mega 2560, an Arduino UNO and two NRF24L01 radios!


The Mega is the controller: a 16x2 LCD keypad shield lets me view temperature, humidity, soil moisture, tank level and motion, and configure four remote relays. The UNO reads the sensors and runs the relay automation locally. Each relay can be manual, soil-controlled, water-level-controlled, temperature-controlled or humidity-controlled.


The two boards exchange commands and status. If the radio link disappears, manual outputs switch OFF after the timeout, while automatic channels continue using the configuration already received by the UNO. This is a maker project with a specific communication failsafe, not a complete unattended pump or safety controller.


I tested the final sketch pair on the actual hardware. A reliable 3.3V connection and explicitly setting radio.setPayloadSize(32) on BOTH boards were important in getting this build to stay ONLINE. The supplied firmware and wiring diagrams are the tested project files.


By Danny van den Brande — BlueCore Tech.

Supplies

CONTROLLER

• 1 Arduino Mega 2560

• 1 compatible 16x2 LCD keypad shield

• 1 NRF24L01-compatible radio

• 1 passive buzzer or compatible passive buzzer module


REMOTE NODE

• 1 Arduino UNO

• 1 NRF24L01-compatible radio

• 1 active-LOW 4-channel relay module

• 1 DHT22 temperature/humidity sensor

• 1 PIR motion sensor

• 1 analog soil-moisture sensor/module

• 1 HC-SR04 ultrasonic sensor


BUILD MATERIALS

USB cables, jumper wires, a suitable 5V supply for the connected hardware, reliable regulated 3.3V power for the radios, and a breadboard or other power-distribution method. Use low-voltage test loads. A bare DHT22 needs its data pull-up; many three-pin DHT modules include one.


RADIO OPTIONS

Two small PCB-antenna modules, a small module plus a PA+LNA external-antenna module, or two PA+LNA modules. My development build used a mixed pair. For external-antenna modules use suitable 2.4GHz antennas. A dedicated 3.3V regulator/adapter is especially useful for PA+LNA radios.


SOFTWARE

Arduino IDE, RF24 by TMRh20, LiquidCrystal, Adafruit DHT sensor library and its Adafruit Unified Sensor dependency. SPI comes with the Arduino AVR board support.

How the Two Boards Work

The Mega sends a command containing the mode, manual state and two thresholds for each relay. The UNO replies with temperature, humidity, soil RAW and percentage, water distance and percentage, motion, relay output states and uptime.


The UNO makes the automatic decisions locally. For example, once a soil-controlled relay has received its thresholds, the UNO can keep controlling it without the Mega. The LCD is the interface, not the place where the sensor automation runs.


Returned relay states are the UNO’s software output states. There is no contact or load-current feedback, so the display does not prove that a pump is physically running. When the link is offline, displayed readings can be old.

Choose and Power the NRF24 Radios

Small PCB-antenna radios and larger PA+LNA radios can communicate with each other when their radio settings match. You do not need identical module shapes. Start with the supplied 1Mbps / minimum-power settings.


Connect the radio module VCC to 3.3V, never directly to 5V. A regulator adapter may accept 5V at its own input, but check that adapter’s markings: its output to the radio must be 3.3V. Keep radio power and SPI wiring short and secure. Each board and its attached modules need a common ground; the two wireless stations do not need a ground wire between them.


My PA+LNA module originally had an unreliable physical 3.3V connection where the LCD shield covered the header. Making that connection solid solved one source of instability. An extension header or proper regulator adapter is preferable to an improvised loose contact. Shield modification is not a requirement.


Allow enough supply current for the radio and relay board. Do not assume an Arduino’s 3.3V output can supply every PA+LNA module, particularly at increased RF power.

Wire the Mega Controller

Controller Schematic.png

Disconnect power before wiring. Fit the LCD keypad shield to the Mega and use the supplied Controller Schematic as the visual reference.


NRF24 → MEGA

CE → D48

CSN → D49

MISO → D50

MOSI → D51

SCK → D52

VCC → 3.3V

GND → GND

IRQ → not connected


CE controls radio operation; CSN selects the radio for SPI transfers. MISO, MOSI and SCK are the Mega’s hardware SPI signals. Do not use the UNO’s D11–D13 pin numbers on the Mega.


PASSIVE BUZZER

Signal → D22

Ground → GND

For the three-pin module shown, follow its marked power/ground/signal connections in the diagram. A bare two-pin piezo uses the signal and ground connections.


LCD KEYPAD SHIELD

RS D8; EN D9; LCD data D4–D7; keypad A0.

The firmware uses LiquidCrystal lcd(8, 9, 4, 5, 6, 7). Adjust the shield contrast potentiometer if the LCD lights but text is difficult to see.

Wire the UNO Remote Node

Remote Node Schematic.png

Use the supplied Remote Node Schematic with the power disconnected. Check module labels rather than assuming every breakout has the same pin order.


NRF24 → UNO

CE D7; CSN D8; MOSI D11; MISO D12; SCK D13.

VCC 3.3V; GND GND; IRQ unconnected.


RELAY BOARD → UNO

IN1 D2; IN2 D3; IN3 D4; IN4 D5.

VCC 5V; GND GND, for the module arrangement shown.


DHT22 → UNO

DATA D6; VCC 5V; GND GND.


HC-SR04 → UNO

TRIG D10; ECHO A0; VCC 5V; GND GND.

A0 is used as a digital input here.


PIR → UNO

OUT A1; VCC 5V; GND GND.

A1 is also used as a digital input.


SOIL SENSOR → UNO

Analog output A2; VCC 5V; GND GND.

Use the analog output, not a module’s comparator/digital output.


Distribute 5V and ground through secure connections. Keep the 3.3V radio rail separate from the 5V supply rail.

Relay Polarity, Startup and Test Loads

The tested board is active LOW. The UNO sketch has RELAY_ACTIVE_LOW set to true: LOW activates a relay, HIGH releases it. For a genuinely active-HIGH board, change that setting to false.


The sketch writes the OFF level before configuring each output pin. All four channels start in MANUAL/OFF. This does not guarantee the behaviour of every relay board during reset or before firmware starts, so check startup with harmless test loads.


The IN pins control the relay coils through the relay module. Loads connect through the separate COM/NO/NC contacts. Start with a suitable low-voltage lamp or other safe test load and observe the module indicators/clicks. Keep pumps disconnected while learning the menu because changes are transmitted immediately.


The diagrams cover the low-voltage electronics. Mains switching needs a properly designed enclosure, insulation, fusing and qualified electrical work; it is not part of this beginner test.

Install the Libraries and Open the Sketches

In Arduino IDE Library Manager install RF24 by TMRh20 and the Adafruit DHT sensor library, including Adafruit Unified Sensor when offered. Install LiquidCrystal if it is missing. Select Arduino AVR Boards support for the UNO and Mega.


The Mega uses SPI, RF24 and LiquidCrystal. The UNO uses SPI, RF24 and DHT.


Use the two supplied final sketches as a pair. Their filenames contain the development hardware names; the Mega filename’s PA_LNA suffix does not mean a small compatible radio is excluded. Keep each .ino inside a folder with the same base name when opening it in Arduino IDE.


Do not substitute an older radio test sketch or change packet layouts, addresses or timing while establishing the first working link.


Library references:

https://nrf24.github.io/RF24/

https://github.com/adafruit/DHT-sensor-library

Upload the UNO and Mega Firmware

Start with the relay load circuits disconnected.


1. Open the supplied UNO remote-node sketch.

2. Select Arduino UNO and its actual COM port.

3. Upload, then open Serial Monitor at 115200 baud.

4. Look for BLUECORE REMOTE NODE, NRF24 detected. and Remote node ready.

5. Open the supplied Mega controller sketch.

6. Select Arduino Mega or Mega 2560 and its COM port, then upload.

7. Power both boards and wait for BLUECORE ONLINE on the Mega.


If the radio cannot initialize, the sketch stops with an error. Fix power and SPI wiring before testing the sensors. Successful NRF detection confirms local radio communication over SPI; it does not by itself confirm the wireless link.

The Tested Radio Settings and 32-Byte Fix

Both final sketches explicitly call radio.setPayloadSize(32) after radio.begin() succeeds. Keep that line on BOTH boards.


TESTED SETTINGS

Channel: 76

Data rate: RF24_1MBPS

Transmit power: RF24_PA_MIN

Payload setting: 32 bytes

Mega command destination / UNO receive address: BC001

UNO status destination / Mega receive address: BC002

Packet signature: 0xB24C

Retries: setRetries(5, 15)


In my development testing, the larger relay-configuration version showed OFFLINE/REMOTE LOST even though the UNO detected its radio. Explicitly setting the payload size on both boards and uploading both final sketches resolved that behaviour. This is the tested fix for this build, not a claim that every RF24 project needs a change from the library defaults.


The packet structures use __attribute__((packed)) and static_assert checks against the 32-byte limit. Leave both definitions consistent. The signature is a format check, not encryption or authentication.

First Link and Sensor Checks

On the UNO Serial Monitor, valid received commands produce lines beginning Packet followed by a number and sensor/relay values. On the Mega, HOME should show BLUECORE ONLINE and SYSTEM should show REMOTE ONLINE.


Next check each sensor page: warm the DHT22 gently, vary the soil sensor condition, move a flat target in front of the HC-SR04, and allow the PIR to settle before testing motion. Confirm the displayed values react.


The Mega initiates communication approximately every 500ms. The UNO normally listens for a command, then waits 3ms before sending its status reply. The Mega response window is about 80ms. Keep these tested timings while bringing up the project.


The SYSTEM packet count is the Mega’s request counter. A rising count alone is not proof of reception: look for ONLINE plus valid UNO packet activity.

Navigate the LCD Keypad Menu

Outside the relay editor:

UP or RIGHT → next page

DOWN or LEFT → previous page

SELECT → action for the current page


Pages: HOME, TEMP, SOIL, WATER, MOTION, RELAY 1, RELAY 2, RELAY 3, RELAY 4 and SYSTEM.


HOME shows link status plus temperature/humidity. SOIL includes the RAW ADC value for calibration. WATER includes distance in centimetres and the calculated percentage. MOTION shows PIR state and whether the alarm is armed. Relay pages show the mode and last reported output state.


The keypad is read through a resistor ladder on A0. Thresholds are RIGHT <60, UP <200, DOWN <400, LEFT <600 and SELECT <850. A shield with different resistor values may need keypad calibration.


Press and release buttons promptly: the current sketch waits for release, so holding a button can delay radio updates and eventually trigger the remote link timeout.

Switch a Relay Manually

Navigate to RELAY 1 and press SELECT. In the mode editor, LEFT/RIGHT cycles modes. Choose MANUAL and press SELECT.


LEFT switches the manual setting OFF; RIGHT switches it ON. Observe the physical relay indicator and low-voltage test load. SELECT exits the editor. Repeat for all four channels.


Settings changes are transmitted while editing; SELECT advances or exits the editor, rather than being a delayed commit button. The relay page shows the output state returned by the UNO when communication is healthy.


Each relay is independent. The labels remain Relay 1–4 so you can decide what each output controls.

Automatic Modes and Hysteresis

Every relay supports MANUAL, AUTO SOIL, AUTO TEMP, AUTO HUM and AUTO WATER. Select a mode with LEFT/RIGHT, then use SELECT to advance through the ON and OFF thresholds. LEFT decreases and RIGHT increases a threshold. SELECT after the final threshold exits.


DEFAULTS WHEN SELECTING A MODE

SOIL: ON at or below 30%; OFF at or above 45%.

WATER: ON at or below 20%; OFF at or above 80%.

TEMP: ON at or above 30°C; OFF at or below 27°C.

HUMIDITY: ON at or above 70%; OFF at or below 60%.


Between the thresholds, the relay keeps its existing state. This hysteresis avoids repeated switching near one boundary. Soil and water are low-trigger modes; temperature and humidity are high-trigger modes. The editor maintains the corresponding threshold ordering.


Changing modes loads that mode’s defaults. The nominal editor ranges are −20 to 80°C for temperature and 0–100% for percentage modes. Temperature/humidity decisions use integer values in this firmware.

Calibrate Soil Moisture

Open the SOIL page and record the RAW reading in your chosen dry condition. Then measure the wet condition that should represent 100%. Keep the electronics dry and use repeatable sensor placement.


The tested sensor values in the UNO sketch are:

SOIL_DRY_RAW = 0

SOIL_WET_RAW = 650


These are specific to my sensor. This implementation expects the wet RAW endpoint to be greater than the dry endpoint. Do not use equal endpoints. A sensor whose readings fall as moisture rises needs a separately adapted conversion; merely reversing these constants is not supported by the current constrain() call.


The conversion normalizes the reading between those endpoints, squares it, then multiplies by 100. Thus halfway between the endpoints gives about 25%, not 50%. This curve gave a more useful response for my sensor. The result is a calibrated relative indication, not a laboratory soil-water measurement or a depth gauge.


Change your supported calibration values in the UNO USER SETTINGS section and upload the UNO sketch again.

Test AUTO SOIL

Assign a relay to AUTO SOIL, initially with a low-voltage indicator instead of a pump. Start with ON 30% and OFF 45%.


At 30% or lower, an OFF relay switches ON. Increasing moisture to 45% or higher switches it OFF. Between the boundaries it keeps its state.


Try dry and wet conditions several times and check that the chosen thresholds suit your soil and sensor placement. A practical irrigation installation also needs protection against an empty supply tank, stuck sensor and excessive run time. Those protections are not implemented by this sketch.

Calibrate and Test Tank Level

Mount the HC-SR04 above the water, aimed at the surface. It measures the air gap: less distance means a fuller tank. Keep the sensor dry; it is not a submerged probe.


In UNO USER SETTINGS:

TANK_FULL_CM = 10

TANK_EMPTY_CM = 100


Measure your own full and empty distances. Keep the full distance smaller than the empty distance. The tested values map 10cm to 100% and 100cm to 0%, with results constrained to 0–100%. This describes level over the calibrated height, not necessarily volume in an irregular tank.


Before using water, move a flat target through the range. Then set a relay to AUTO WATER: the defaults switch ON at or below 20% and OFF at or above 80%.


IMPORTANT: a missing echo or invalid distance returns 0cm and then 0% in this firmware. AUTO WATER can therefore switch ON when an echo fails. Do not rely on this code alone for unattended filling; an independent high-level cutoff and run-time protection are needed for a real pump installation.

Temperature and Humidity Automation

The DHT22 is read every 2000ms. Temperature and humidity share one sensor but can control different relay channels.


AUTO TEMP is a high-temperature trigger suitable for demonstrating cooling: ON at 30°C, OFF at 27°C. AUTO HUM is a high-humidity trigger: ON at 70%, OFF at 60%. These modes are not low-temperature heating or low-humidity humidifier modes.


For a bench test choose thresholds close to the current room conditions and observe a safe test load. Warm the sensor gently; avoid water on the electronics.


If a DHT read fails, the sketch keeps the previous valid value. Before the first valid measurement the values start at zero. There is no sensor-age alarm or separate sensor-failure shutdown, so a stable displayed number does not prove the sensor is still updating.

PIR Motion Alarm and Silence

The PIR signal is read on UNO A1 and transmitted to the Mega. On the MOTION page, SELECT toggles ARMED/OFF. The controller starts armed.


When a healthy link reports motion while armed, the LCD displays *** MOTION! *** and SELECT=SILENCE. The passive buzzer on D22 sweeps roughly between 650 and 1700Hz.


Press SELECT during an active alarm to silence it. The silence state resets when motion clears, allowing a later motion event to trigger again.


ALARM_HOLD_MS is 10000. Continued reported motion refreshes the hold, so this is not a guaranteed ten-second maximum while movement continues. The buzzer belongs to the Mega; the UNO reports motion but does not drive this local alarm.

Test the Lost-Link Behaviour

With safe test loads, configure one relay MANUAL ON and another automatic channel. Remove power from the Mega while leaving the UNO powered.


After approximately three seconds without valid commands, the UNO prints LINK LOST - AUTO CONTINUES / MANUAL OFF. Manual settings are cleared and those outputs switch OFF. Automatic channels continue using their last received mode and thresholds.


Reconnect the Mega and confirm the link recovers. A still-powered Mega can send its current settings again after a temporary radio interruption. If the Mega itself was power-cycled, it starts with its compiled defaults and sends those instead.


The Mega can show OFFLINE immediately after a failed exchange; STATUS_TIMEOUT_MS is also 2500ms. The UNO timeout is 3000ms. This protection concerns radio loss only: it does not detect relay contact failure, stalled pumps or all sensor faults.

Power Loss and Settings Persistence

Configuration is stored in RAM. There is no EEPROM persistence in this version.


After a Mega reset, its four relays return to compiled MANUAL/OFF settings. After a UNO reset, it starts MANUAL/OFF until it receives configuration again. Custom thresholds do not survive a full power cycle.


Keep a written record of your chosen settings. Always check the current modes after resetting, uploading or reconnecting hardware. EEPROM, scheduling and logging are possible future improvements, not included features.

Optional Radio Range Experiments

First establish a stable link with the supplied RF24_1MBPS and RF24_PA_MIN settings.


If both radios support it, you can experiment with RF24_250KBPS on BOTH boards. The lower rate requires the nRF24L01+ variant; do not assume every old non-plus radio supports it. Higher PA settings such as RF24_PA_MAX can increase the supply demands of amplified modules. Use suitable regulated power and avoid beginning a close-range bench test at maximum power.


For PA+LNA modules fit antennas intended for 2.4GHz. Antenna placement, walls, metal, interference and supply quality all affect the result. A mixed pair works, but both directions must be reliable for this command/reply project.


There is no guaranteed range for this build. Test at the intended site and keep the original settings available as a known working starting point.


RF24 API reference: https://nrf24.github.io/RF24/classRF24.html

Troubleshooting

NRF24 ERROR / CHECK RADIO

Check 3.3V, ground, CE/CSN, SPI pins and module seating. Remember the Mega and UNO use different hardware SPI pin numbers.


RADIOS DETECTED, BUT OFFLINE

Confirm both final sketches were uploaded, both contain radio.setPayloadSize(32), channel 76 and 1Mbps match, and BC001/BC002 are used in the correct directions. Check power quality before changing code.


UNO HAS NO PACKET LINES

At 115200 baud, NRF24 detected only proves initialization. Valid command packets must arrive before Packet lines appear. Check the Mega is running and both radios are connected correctly.


INTERMITTENT PA+LNA LINK

Inspect the regulator capacity, supply connections, wire length and antenna. A connection that only works while pressed is a hardware fault to fix.


WRONG SOIL PERCENTAGE

Read RAW values, verify wet > dry for this conversion, and recalibrate. The square curve is intentional.


WRONG WATER PERCENTAGE

Check sensor aim and full/empty distances. 0cm can mean no echo, not an empty tank measurement.


RELAYS INVERTED

Confirm the board’s input polarity and RELAY_ACTIVE_LOW setting.


RADIO DROPS WHILE A BUTTON IS HELD

Release the key. The current Mega button handler waits for key release and pauses its main loop.


SETTINGS DISAPPEAR AFTER RESET

Expected in this version: settings are in RAM.


NUMBERS LOOK FROZEN

Check ONLINE status. Offline readings may be stale; DHT failures also preserve previous readings.

Applications, Lessons Learned and Future Ideas

Possible low-voltage applications include soil-triggered irrigation demonstrations, tank-level indicators, cooling/ventilation control and remote manual switching. Choose the load and its independent protections for the application rather than assuming the same control is safe for every device.


The useful lessons from this build were that detecting a radio is not the same as exchanging packets, solid 3.3V power matters, both packet definitions must agree, and explicit 32-byte payload configuration was part of the tested final solution. Hysteresis and local UNO automation make the controls more useful than simple remote ON/OFF buttons.


Future ideas include EEPROM settings, an enclosure, RTC scheduling, SD logging, sensor-fault handling, pump run-time limits, battery monitoring and additional nodes. None of those are implemented in these downloads.


Start with the tested sketches, confirm each subsystem, and then adapt one documented setting at a time. Thanks for checking out my project!


Danny van den Brande — BlueCore Tech