Introduction: Automatic Room Light -- for Beginners

This project provides Automatic Room Light that turns on when someone enters and turns off when they leave.

If the light being controlled is a 5V USB power light this project is suitable for beginners and only requires two solder connections.

The project has the following features;-

– Person Presents detector using mmWave FMCW

– A tuning app to adjust the mmWave parameters and check the operation

– Light turns on when person enters and turns off shortly after everyone leaves

– BLE remote controlled light switch. Either USB 5V light OR 110V/240V light switches retrofitted with BLE control.

– Light sensor so the light is not turned on during the day

– Simple plug in component construction. Minimal soldering

– Maintains manual override.

This project builds on previous BLE controlled light switches projects and adds a mmWave presents detector, light sensor, and BLE controller to turn the light on and off.

The previous BLE controlled switch projects are:-

Very Simple BLE Power Switch - Suitable for Beginners, if not switching mains power (shown above)

BLE LED Light Switch 2023 – Retrofit

BLE High Power Light Switch 2023 – Retrofit

Each of these allows you turn lights on and off via BLE control. This project add the presents detector to provide that control.

This project is also online at Automatic Room Light - for beginners

Supplies

Prices as at June 2024, excluding shipping

1 x FireBeetle 2 ESP32-E IoT Microcontroller with Header SKU:DFR0654-F US$9.90

1 x Gravity: IO Shield for FireBeetle 2 (ESP32-E/M0) SKU:DFR0762 US$4.90

1 x Gravity: mmWave C4001 24GHz Human Presence Detection Sensor (12 Meters, I2C & UART) SKU:SEN0610 US$12.90

1 x Gravity: I2C VEML7700 Ambient Light Sensor (0~120Klx) SKU:SEN0228 US$9.90

1 x Plastic Project Box Enclosure Waterproof Clear Cover - 6.61x4.72x 2.17 inch SKU:FIT0723 US$9.90

1 x White Nylon Screw and Mounting Kit -M2 SKU:FIT0666 US$9.90

1 x Set of 10 M3 * 6 clear nylon screws and nuts SKU:FIT0195 US$1.50

1 x Set of 10 M3 * 10 Nylon Standoff SKU:FIT0066 US$1.50

1 x pfodApp General Purpose Android app US$12.99

1 x pfodDesigner menu designer and Arduino code generator for pfodApp FREE

1 x BLE controlled switch from one of the previous projects Very Simple BLE Power Switch, BLE LED Light Switch, BLE High Power Light Switch

The Very Simple BLE Power Switch is the easiest to build.

Arduino IDE and the ESP32 V2.0.17 add on installed via the IDE board manager. This project used ESP32 V2.0.17. Other versions will their own set of features/bugs.

PersonalDetection_Light_OnOff.zip

libraries.zip – The source code also needs the following libraries :- SafeString V4.1.31+, pfodParser V3.63.1+, DFRobot_VEML7700 V1.0.0 and DFRobot_C4001 V1.0.0. The libraries.zip contains those files. Unzip it to your Sketch directory.

Step 1: Construction

The construction is trivial. Just plug the mmWave and light sensor into the IO shield I2C pins. Align the wire colours with the pin colours. Then plug the IO shield onto the FireBeetle.

NOTE: Before powering up set the switches on the back of the mmWave to I2Cand address 0x2A

Mount the boards on the plastic front cover of the enclosure.

Programming

Install the Arduino IDE and the ESP32 V2.0.17 add on and the libraries from libraries.zip. Unzip to your Arduino Sketch directory to update the existing libraries subdirectory.

Unzip PersonalDetection_Light_OnOff.zip to you Arduino sketch directory.

Set the board to FireBeetle 2 ESP32-E (note carefully as there are other similarly named boards). See also the Dfrobot wiki

Set the Partition Scheme to “Huge APP (3MB No OTA/1MB SPIFFS)”

At the top of the PersonalDetection_Light_OnOff.ino file, set your network's SSID and Password and choose an unused IP address in your network. You can use the Fing app to see what IP are being used on you network.

As delivered the sketch has DEBUG and PFOD_PARSER and NO_LIGHT enabled. The two other settings at the top of the PersonalDetection_Light_OnOff.ino file are:-

static unsigned long LIGHT_OFF_DELAY_ms = 20000; // 20sec   or 2ul * 60 * 1000; // 2mins
static float LUX_LOW_LEVEL = 15000;

The LIGHT_OFF_DELAY_ms sets how long the light stays ON after no presents is detected. For initial testing 20sec is a good start.

The LUX_LOW_LEVEL is the light level above which the system will NOT turn on the light when presents is detected. That is the light will only turn on if there is low light in the room. Once the light level exceeds the LUX_LOW_LEVEL, the turn on is disabled. This prevents the light automatically turning on during the day. The initial very high setting of 15000 is for testing. You will set it to suit your situation later. If you find you current light level is higher than 15000, just increase this value so that presents detected will turn the light on.

The NO_LIGHT setting skips trying to connect to the BLE controlled light. For tuning the mmWave presents detector you don't actually need to have it connected to a BLE controlled light. BUT to actually control the light you need set the connection parameters for the BLE controlled light. These three settings specify the address of the BLE controlled light and it turn On and turn Off command. You need to edit the deviceAddress to match your BLE device and the commands to match your device commands.

static char deviceAddress[] = "F5:FA:14:1C:85:B7";  // either upper or lower case OK here
static char turnOnCmd[] = "{A`1}"; // the Very Simple BLE Power Switch uses slightly different menu items that needs the `1
static char turnOffCmd[] = "{B`1}";

The Very Simple BLE Power Switch , BLE LED Light Switch and BLE High Power Light Switch are designed to be operated from pfodApp. See those projects for the details. You can check what commands are sent to turn the light on/off by looking at the pfodApp Debug View.

Compile and upload the PersonalDetection_Light_OnOff sketch. Check the Arduino Serial Monitor to see what is happening.

Step 2: MmWave Tuning

Install the Android pfodApp and set up a new WiFi connection or the IP you assigned to your board. The port is 4989 for pfodDevices. See the pfodAppForAndroidGettingStarted.pdf for how to create a new connection.

Opening the pfodApp and connecting to the mmWave shows this menu. The sketch contains the complete menu code. pfodApp just displays it. This menu, and its sub-menu was designed using the free pfodDesigner app which was then used to generate the Arduino code for this ESP32 board.

This front screen shows the current state of the light, the current lux measurement, whether or not presents is detected and two buttons. The Settings button opens the settings sub-menu and the Toggle Light button turns the light state on/off. The light will only turn ON if presents is detected and the lux level is less than the LUX_LOW_LEVEL set in the sketch.

When no presents is detected, a timer starts to turn the light off. The turn off delay is set by LIGHT_OFF_DELAY_ms

If presents is detected again before the light goes off, then the timer is reset. NOTE: Once the light is ON the light level is ignored. Any presents detected will keep the light on.

mmWave Settings Sub-menu

The Setting button opens the mmWave Settings sub-menu. Again this menu was designed and the Arduino code generated using the free pfodDesigner app.

There are a number of tuning parameters that control the operation of the C4001 mmWave presents detector. The most critical ones are shown on this mmWave Settings sub-menu. Use this menu to adjust the distances and sensitivities to detect your presents when you enter the room and to detect your NON presents when you leave.

Make sure to walk around the rest of the house with the app and check that the mmWave does not detect your presents. In the installation photo shown above the mmWave was mounted in the centre of the ceiling. However in this position, anyone walking above it in the upper room also triggered presents detection. The fix in this case was to remount the project lower down on a wall pointing at the door. In that position it could be tuned for no false triggers while still turning the light on as soon as any one entered.

Once you have determined the settings for your installation, update the coded settings in the processDetection.cpp file.

// values from testing with the sketch and pfodApp
int minDist = 258; // name the variable for 'Min Distance'
int maxDist = 1200; // name the variable for 'Max Distance'
int triggerDist = 535; // name the variable for 'Trigger Distance'
int triggerSens = 9; // name the variable for 'Trigger Sensitivity'
int keepSens = 3; // name the variable for 'Keep Sensitivity'

For the LOW_LIGHT_LEVEL setting in PersonalDetection_Light_OnOff.ino, take a note of the lux levels at dusk or with the curtains drawn to determine the level to set below which you want the light to automatically turn on. Note: the lux measurement depends on where the project is mounted so choose a mounting spot first to give the required mmWave presents detection and then look at the lux levels.

Conclusion

This project is a very simple person presents detector which turns a light on automatically when anyone enters the room. The project also includes a light level sensor so that the light in not turned when there is already enough light in the room. The project has just four (4) boards which plug together. No soldering required. It connect to the light via a BLE connection so no wires are needed between the detector and the light.