24 October 2017

If you have a fuel or water tank that is either metal or colored plastic, you may have run into the same problem I had: How do you know when it’s empty?

Mechanical level gauges are nice, but not always practical or even possible. So, I went on the hunt for an ultrasonic level sensor for my fuel tank.

Alas, what’s available on the market is too expensive, not very reliable, and often goofy in terms of features. So I decided to build my own!

This ultrasound fuel gauge can be built for around $33 using a breadboard, Arduino Uno, and one of 2 different ultrasonic transceiver modules designed for use with Arduino or Raspberry Pi boards.

First, a lovely video intro to the project:

How exactly does it work?

It’s pretty simple, actually.

First, the schematic:

The ultrasonic transducer has some electronics onboard. You supply 5V DC, and there are two other pins: TRIG, and ECHO.

So, the Arduino simply has to do this:

  1. Set the TRIG pin high (5V) for at least 10 μs (microseconds)
  2. The ultrasound board then records a timestamp, and sends out a 40Khz sound pulse
  3. The ultrasound wave travels down to the liquid in the tank, and reflects off of it
  4. The ultrasound wave then travels back up to the transducer
  5. The ultrasound board then receives the reflected wave, and records another “end” timestamp
  6. Finally, the ultrasound board sets its ECHO pin high for the number of microseconds it took for the ultrasound wave to travel down and back again

The result on the ECHO pin can be converted quite simply as: 58 μs/cm

So, if the ECHO pin was high for 5800 μs (5.8 ms), then we know that the distance between the sensor and the liquid level in the tank is:

5800μs / 58μs/cm = 100cm = 1m

Note that this is the level of “empty space” in the tank, like so:

Actually, it’s a bit more complicated than that…

In my code, there are two levels you must set:

  • tankEmptyDepth = The level at which you want the sensor to show 0%
  • tankFullDepth = The level at which you want the sensor to show 100%

The reason for tankEmptyDepth is obvious from the diagram above, but tankFullDepth might be a bit of a mystery.

If you look at the image above, you’ll notice that when the tank is full, the liquid level will still be below the sensor – maybe by about 15cm. So, tankFullDepth allows you to specify this distance between the sensor and the liquid when the tank is full.

Long story short, if you do the math, you’ll discover that with tankFullDepth incorporated into the calculation, you get a more accurate measurement!

Note also that you can set the tankEmptyDepth to less than your actual measurement (I used a heavy nut on a string). The idea here is that if you subtract, say, 10cm off this value, then the level measurement will show 0% when you still have 10cm of liquid left in your tank… sort of like an “emergency reserve”.

This is all re-explained in greater detail in the commented source code, which you can download below!

Other bits and bobs

As I mentioned in the video above, you have your choice of any “Arduino-standard” ultrasonic module. If you want maximum accuracy but without the waterproofness, you can just use the very common HC-SR04 that comes in many Arduino starter kits.

Also keep in mind that if you use the JSN-SR04T as I did, your “tank full accuracy” will probably be very bad. This ultrasonic sensor module is “blind” below 25cm.

But I’m guessing that like me, you don’t much care about the level when the tank is full, since the whole reason to have a fuel gauge is to know when the tank is nearing empty! 😉

In my particular case, my fuel tank is 163cm deep, and the sensor will be about 15cm away from the fuel after the tank is filled. Since the JSN module is blind below 25cm, I will need to use 10cm of fuel before I get an accurate reading.

It’s a small price to pay IMO for the waterproof and easier-to-mount sensor!

The Goods

You can get the parts needed here:

From Banggood.com:

Or you can get the whole shebang more quickly (and more expensively) on Amazon:

Elegoo UNO Project Super Starter Kit with Tutorial for Arduino (with HC-SR04 transducer)

Once you’re done building, you’ll need some code to make it go!

You can either visit my GitHub repository, or download the .INO directly.

That reminds me…

This was my first project using an Arduino, so I kept it rather simple.

There is a bit of a learning curve to figure out how to connect the Arduino to your puter, how to flash code, etc.

The following are some super-handy resources to get you going quickly if you’re also an Arduino Newbie:

Like I said in my video, it looks daunting at first, but if you’ve ever done ANY programming at all, you will quickly find it’s far easier than you think!

Have fun!

DIY Ultrasonic Fuel Gauge / Level Sensor
Tagged on:             

Leave a Reply

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