MyMotionSensor – a MySensors based sensor for motion and ambient light level

Posted by

I needed a couple of motions sensors to automate lighting in hallways and other rooms. Since the lights should only turn on when it is dark, the system also needs to know the ambient light level in the location where the motion was detected — so I added a simple brightness sensor to each node.

This is part of my home automation setup, for more details see the previous posts.

The finished motion sensor node, next to a 1 Euro coin.


I had a few key requirements for the system that these sensor nodes are part of:

  • Lights turn on automatically, e.g. in a hallway, when someone walks through the hallway
  • Lights turn off automatically after a configurable time, e.g. 1 min.
  • Lights turn on automatically only if the location is dark, i.e. at night or on very overcast days

In addition, there are a few nonfunctional requirements for the sensor node itself:

  • Low cost, less than 10€ per node
  • Unobtrusive appearance when placed on the floor or on furniture
  • The node must be battery powered, and a battery should last at least a year
  • The node needs to integrate with my existing home automation infrastructure, obviously, which means it talks to either Wifi or the MySensors 2.4 GHz RF gateway.


The source code and schematics are available on Github. I didn’t bother to develop a PCB, just used a small prototype board, and an Arduino Pro Mini clone. I chose the smaller HC-SR505 PIR sensor rather than the larger HC-SR501 sensor used in the MySensors motion sensor example.

Battery life

I have several of these operating for more than a year now. The sensors run well over 18 months on a pair of AA batteries. Here is is a plot of battery voltage over a year, for all MyMotionSensor nodes reporting to my openHAB setup.

The sharp voltage increase on the green, red and orange trace is on the days when I put in fresh batteries.

Over time, I made some software changes to optimize power consumption. The light brown and dark brown traces, labeled EZ and EZ-L, are two sensors in the same room, so they should see the same amount of traffic, i.e.. be triggered the same number times per day. Yet, the battery voltage on “dark brown” drops must faster than on “light brown”. “Light brown” is a newer software version.

Power saving tricks

I followed the recommendations for battery powered sensors from the MySensors homepage, including

  • use a 3.3V Arduino Pro Mini clone, instead of a 5V model
  • remove the power LED and voltage regulator from the module
  • power the device directly from battery

In addition,

  • the node sleeps most of the time, only waking up when a pin change interrupt occurs, or every 30min to check if brightness or battery level need to be reported. Simple polling of the motion sensor output is also performed every 8s, “during sleep”, using my MySnooze library
  • the processor fuses are programmed for internal oscillator 8 MHz, so firmware downloads can be done quickly, at 57.6 kBaud. However, at startup, the firmware configures the processor for 1 MHz clock, to save power while awake.
  • debug messages to the serial port are suppressed when the module is not actually connected to a terminal or FTDI USB-to-serial converter. This is to allow the processor to return to sleep as quickly as possible, not having to wait until a debug message has been sent.
  • the brightness sensor is only powered as long as a measurement is ongoing.

openHAB integration

First, I define a group for all the MyMotionSensor nodes that should turn on the hallway light

/* /etc/openhab2/items/1groups.items */
Group gMoFL     "Group: motion triggering FL light"

Then I define an item for each motion sensor. The sensor communicates with a MySensors MQTT gateway, which publishes messages with a topic of my/+/stat/NodeID/SensorID/1/0/Type and a payload corresponding to the sensor value. For example, the sensor node with NodeID 114 is placed in the hallway, it reports motion as SensorID 21 and brightness level in percent as SensorID 61. You will notice that I still use the MQTT v1 binding.

/* /etc/openhab2/items/mysensors.items */
Switch FL_Motion       "[%s]"       (gMoFL) 
Number FL_Lux          "[%.0f%%]"   

The sensor reports values of 1 and 0 for motion detected and detection ended, which are transformed to the ON and OFF values expected by openHAB through a simple mapping:

/* /etc/openhab2/transform/ */

A simple rule turns on the hallway light when the sensor detects motion, but not in the night (10pm to 8am), and only if the hallway is dark:

/* /etc/openhab2/rules/motion.rules */
rule "any motion triggering hallway L light"
    Member of gMoFL changed to ON 
    // turn on light for a short time
    if (FL_Lux.state < 20) {
        if (now.getHourOfDay() >= 8 && now.getHourOfDay() < 22) {

Lessons learned

The PIR sensor appears to be sensitive to power supply fluctuations. In the first prototype, it would often re-trigger immediately after its output returned to low (inactive), presumably because sending a message via the NRF24L01+ module would draw current from the battery, and lower supply voltage. Therefore, the software ignores transitions of the PIR sensor output for a few seconds after going to sleep.


When I developed this, a few years ago, I wasn’t aware of any low-cost alternative. Today, one could also use an IKEA Trådfri Motion Sensor, which is claimed to run for 2 years on a pair of CR2032 button cells. However, one would need a separate ambient light sensor.

Mission accomplished?

So, did I meet my stated objectives and requirements?

  • Low cost, less than 10€ per node? — Yes, total cost of materials is € 6.30 !
  • Unobtrusive appearance when placed on the floor? — Well, beauty is in the eye of the beholder, but the small, whitish project box from Aliexpress is good enough in my eye.
  • The node must be battery powered and a battery should last at least a year? — Yes, typically over 18 months.
  • The node needs to integrate with existing home automation infrastructure? — Yes, obviously, and I’m glad I went for MySensors 2.4 GHz RF instead of Wifi, probably couldn’t have achieved the battery life otherwise.

Leave a Reply