Handling relais measurements with Modbus

I recently had to measure the number of products passing a light emitting sensor. I got this Photo Electric switch E3JK-DS30M1 which has a relais to indicate if something is reflecting the infrared light it emits or not.

The schema is pretty simple:

You just attach 24V DC to the BLUE (-) and BROWN (+) wires. Then the sensor device behaves like a relais where the WHITE and BLACK wires resemble the ‘normally open’ state. The WHITE and GREY wires are used for the ‘normally closed’ state.

I tested the relais with my multimeter (set to Ohm). The relais was truly indicating objects passing by the beam of the photo electric switch.

The measuring distance can differ. My device has a range from 5 to 40 centimeters (it is adjustable with a potentiometer). But white and shiny objects reflect the beam better than dark objects. And do not hold the sensor directly into the sun, you get a lot of false readings!

I used an Advantech Wise 4012E IO module to measure the state of the relais. Let’s check out the settings.

Disclaimer: These sensors and devices are used as an example. You are free to pick other devices, I am not asked to promote these specific devices in any way.

The Wise 4012e is advertized as a device to practice with IoT. Therefor the device is powered by 5V over USB.

Warning: Do not power it with anything else than the 5V USB connection. Do not try to expose the Wise module to more than 10V on the inputs. Read the specs first.

I attached the WHITE ‘Relais normal’ wire and BLACK ‘normally open’ wire to the first (0) Digital In port (and its ground) of the Wise Module.


Normally, when you read the value of a relais, you get the state as an integer, zero or one. This is fine if you can attach events (push) to the changes.

But we will be using Modbus as the protocol to read changes. And using Modbus, we pull the current value at a certain interval. So if your Modbus timing interval is smaller than the relais change timing interval you are still fine.

But once the time intervals are not aligned (eg. five times per second an object passes by but you only measure once a second), you can have false negatives (you will miss objects passing the sensor).

Luckily, the Wise module has an useful mode for this, the counter:

Every time a product passes the sensor, a counter is increased:

Here you can see I placed fifty times an object in front of the sensor. Fifty times the light emitted by the sensor, reflected into the sensor back.

Now it’s simple to read changes. The counter value will always increase if a reflection is detected. There is still some room for false positives: if one object reflects the beam multiple times. But this has less impact in the overall performance of our solution.

Note: You can also experiment with the ‘Normally’ closed port of the sensor. You need some reflective material within the distance of the beam. You read the number of times the beam is ‘broken’ by products passing by.


Now we are ready to read the value using Modbus.

First, look up the right modbus address of the counter in the manual provided by Advantech:

As you can see, we are interested in Register 40017.

Then I installed a free modbus tool, CAS Modbus scanner on Windows 10 to read Modbus values:

I had to add a Task with the IP address of my Wise module and the right starting register (40017):

I added holding register 40018 too which shows the possible counter value of the other Digital In of our Wise module (the value should be zero, not used).

Once I read the register value, I see the correct counter value appear:

This scanner app does not support timed reads on registers. But every time we refresh the page, we read the latest counter value.

Modbus on IoT Edge

From here, it’s easy to connect our sensor, using the IoT Edge Modbus module to the Azure cloud. You could even add an extra custom module in between which turns the counter changes into separate IoT telemetry messages, just to fill in the gaps. From there your flow is ‘event-driven’.


We have seen how easy it is to transform a unreliable, pull driven, discrete relais measurements into a more reliable event driven solution using the counter feature of this Wise module.