Modbus TCP Temperature Sensor with Arduino and LM335

I used to hate on Arduino a bit, but have recently come around. I was having a conversation with a work colleague along the lines of how super easy it would be nowadays to build a temperature sensor with ethernet connectivity and a modbus interface. Remembering that I had an old Arduino board (with Ethernet) kicking around from some abandoned project years ago, I laid myself a little personal challenge: to see how quickly I could prototype such a device. The answer is: Really, surprisingly quickly!

Gluing together other people’s hard work together made this a trivial exercise, and it was kind of too easy. It felt a bit like cheating. And I think this kind of plug-n-play, lego block engineering has made me lazy – I know someone else has already done the legwork so all my time is spent evaluating the relative merit of one library against another, rather than engineering something tricky from first principles. I guess this is both the blessing and the curse of the Arduino phenomenon.

The original sketch to read q temperature measurement from the LM335 is based on the one found here:

There are a handful of Modbus libraries but I chose to use I also used the runningaverage library found here, in an attempt to tame some of the noise and variability in measurements.

Of the whole exercise, what actually took the longest was debugging the poor performance of the LM335 sensor. In the end, I wisely took the datasheet’s advice and added a 10k potentiometer which just works as a resistive voltage divider into one of the pins of the LM335 which in turn gives a linear offset. The LM335 can then be easily calibrated against another thermometer.Screen Shot 2016-05-15 at 10.28.50 PM

To log, store and historise the data I of course used Mango Automation (and with the server located off-site, I just added a DST-NAT rule to forward TCP traffic on port 502 from my home internet to my Arduino).


The data source configuration in Mango looks like this:

The temperature value is read as a two-byte signed integer. The Mango data point configuration looks like this.

And here’s my actual Arduino sketch:

Previous Post

LibreNMS network monitor with Slack integration and Oxidized config versioning

I used to use Observium for network monitoring. After evaluating a couple of options - Cacti, Zabbix, Nagios - I found Observium, and loved its beautiful, well organised and extremely usable interface. Observium has ... Read more

Next Post

Raspberry Pi internet streaming radio station

My partner has been overseas for a few months and was desperately missing a local radio station. It doesn't have an online stream that she can listen to and so she asked me ... Read more

Leave a Comment