Heating Oil Level Gauge
April 28, 2013 — 22:26

It seems I can’t just leave the central heating alone. When we got the heating in (after 2 years of zero heating here, cooold) the tank was fitted with a wireless level monitor which measured the level of the tank using an ultrasonic sensor and sent the data back to a base station plugged into a wall socket using its combined battery pack/aerial. This worked fine for a couple of months until the battery pack went dead. A new battery pack worked for a couple of days and it never worked again – exactly the same as the unit fitted to my parents tank , an absolute waste of money. I switched to dipping a stick into the tank which proved far more reliable but slightly inconvenient over the last two years.

A few weeks back i was browsing ebay, as i do, and found some ultrasonic distance sensors for about €4 delivered. (I have no direct link as the auction has expired but searching for SR04 will bring up any amount of similar units.) I had no plan for them immediately but I bought some just to have anyway. It was only after they came that it struck me that a way of remotely monitoring the oil level would be  a nice project.

The units are very simple to use. There is 4 pins: 5v, GND, Trigger and Echo. By applying a 10uS pulse to the trigger pin you activate the sensor to take a distance reading. The sensor replies on the echo pin with a pulse that is as long as the time taken for the sound wave to travel to the distant object and return to the sensor. Knowing the speed of sound we can then calculate the distance.

This is the module as it arrives with its 4 pin header soldered in.

2013-04-27 13.46.02


For the tank gauge i was planning on permanently installing the sensor so i desoldered the header and soldered in some of my 4 core alarm cable, this stuff is great to have around for projects!

2013-04-27 14.12.00

The other end was passed through the side of a project box i had lying around with a knot tied to stop the wire being pulled from the board if someone got a little rough with it once assembled. To install the sensor on the tank i chose to put it on the tank cap, although it leaves it slightly vulnerable when getting the tank filled being able to install it indoors without trying to sit on the tank outside mounting it is a big advantage! With two suitably sized holes drilled the sensor was hotglued into place

2013-04-27 14.26.58

And then the project box hotglued over that. The project box is also secured with two screws – its not going anywhere!

2013-04-27 14.46.30 2013-04-27 14.46.44

And the cap refitted to the tank. The cable is ran across the follows the existing heating pipes (insulated in the background) up into the apartment.

2013-04-28 19.17.24

My intention now is to get the sensor connected the network so i can read the data with my existing heating controller without too much trouble. I am still waiting for my ethernet modules to arrive as i’m not wasting one of my (expensive) W5100 boards on this project so for now I have made something slightly lower tech.

Using code i modified from the Tronixstuff site (http://tronixstuff.wordpress.com/2011/11/28/tutorial-parallax-ping-ultrasonic-sensor/) we can read the distance from the sensor to the oil level at display it via serial. Using my old dip stick I measured it at about 87cm so not bad at all! Also displayed is the level expressed as a percentage. Using my stick I estimated the distances for empty and full and hardcoded them.

int empty = 119;
int full = 19;

The distance is then mapped to give the percentage

 level = map(distance, full, empty, 100, 0);


For the display I am temporarily using a cheapo analogue 5v meter. Its driven by using a PWM pin on the arduino and getting the required output voltage  by mapping the distance variable to the PWM range of 0-255. The above serial printout shows the calculated value for the PWM.

gaugeVoltage = map(distance, full, empty, 255, 0);

2013-04-28 19.35.09


The complete code is as follows. It’s a bit messy as I copied the code from Tronixstuff out of laziness and added to it as I needed to! For the final install the code will need to be integrated with the current code for turning the heating on and off so a single controller will be doing both.

int trig = 8;
int echo = 9;
int gaugePin = 11;
int empty = 119;
int full = 19;
int distance;
int level;
int gaugeVoltage = 127;
unsigned long pulseduration=0;
void setup()
pinMode(trig, OUTPUT);
pinMode(gaugePin, OUTPUT);
pinMode(echo, INPUT);
void measureDistance()

digitalWrite(trig, LOW);

// now send the 10uS pulse out to activate Ping)))
digitalWrite(trig, HIGH);
digitalWrite(trig, LOW);
// finally, measure the length of the incoming pulse
pulseduration=pulseIn(echo, HIGH);
void loop()
// get the raw measurement data from Ping)))

// divide the pulse length by half

//convert to centimetres.
distance = int(pulseduration/29);
level = map(distance, full, empty, 100, 0);
gaugeVoltage = map(distance, full, empty, 255, 0);
// Display on serial monitor
Serial.print("Distance - ");
Serial.print("cm. Level - ");
Serial.print("% ");

  • Colin

    what kind of electronics is involved apart from the SR04 ultrasonic sensor.
    I guess an Arduino (which one) and an Ethernet shield ?

    May 30, 2013 — 12:09
    • chett16

      The code as posted was used on an Uno. I haven’t integrated it with ethernet just yet. I’m waiting to find some time!

      May 30, 2013 — 18:28
  • Hi Chet,

    Just one question; did the environment cause any degradation in performance of the module, considering the ultrasonic sensors of the SR04s are directly exposed to the oil?

    Very curious to know, had been planning to do this for a water tank.

    Best Regards.

    October 29, 2013 — 6:42
    • chett16

      Not so far, who knows how it will last long term though!

      October 29, 2013 — 7:52
  • […] finally, [Chet] saw the oil tank level sensor we featured this week. He built a nearly identical system earlier this year. The oil level sensor works in conjunction with the custom thermostat he built around an Android […]

    December 9, 2013 — 0:17
  • Muhammad Nowfer

    Does water or oil reflect ultrasonic waves? Because Sonars use ultrasonic waves to find distance of the object inside sea, right?
    One more doubt is, are we measuring time the sound takes to come back to Ultra Sonic Sensor? what is the state of echo pin? If it receives a sound wave which it sent, will it go low or high?

    December 10, 2013 — 7:36
  • mark

    Hi ,
    was looking at doing this for a while , i have used your code added a modified bargraph sketch, but being new to programming there are a couple of things i want to do that i am us]nsure of so if you could give me a cuople of pointers would be gratefull
    1 i wish to make the last led (i.e low oil indicator flash and sound a bleeper)
    2 i would like to be able to monitor how fast the level drops . ie burst pipe , theft etc and sound a warning – there has been a few oil thefts local recently
    any starter code for these ideas would be appreciated i am trying bits of code taken from sketches and getting nowhere fast but learning from failures
    my ultimate aim is for a 1.8 grapghic screen and wireless conectivity , which i am currently rrying to port the library from arduino to chipkit
    Thanks in advance

    December 17, 2013 — 12:34
  • Dom

    Nice! was looking at doing something similar myself – hoping to use a battery pack on the sensor, RF transmitter and try to get the unit to go to sleep and only send info every hour or so… its a work in progress!

    March 6, 2014 — 3:20
  • nitin tambe

    what kind of electronics is involved apart from the SR04 ultrasonic sensor.
    I guess an Arduino (which one) and an Ethernet shield

    March 20, 2014 — 6:29
  • fred lemming

    Might be less expensive to use a Raspberry Pi. Integrated ethernet and optional (with a dongle) Wifi in one piece.

    November 4, 2015 — 14:44
  • Leave a Reply

    Your email address will not be published. Required fields are marked *