Introduction: Building a Capacitive Liquid Sensor

     A capacitive liquid sponsor relies on the fact the the capacitance or charge between 2 metal plates will change (in this case increase) depending on what material is between them.
     This allows us to create a level sensor that is safe for use with any liquid, this one will be used in a buggy with gasoline (petrol).

One plate is hooked to ground. The other connects to pin 23. There is a 820K ohm resistor from pin 22 to 23. The sensor works by charging the capacitor (the water bottle) and measuring how long it takes to drain through the resistor.

Step 1: Parts

1. A solder-less bread board is strictly not needed but make it a lot easier, especially if you plan to add other stuff later.
2. Arduino, I'm using an Arduino mega but a standard one should have just enough pins.
3. LCD character display.
4. Some odds and ends including some wire and a 1MΩ resistor.
5. A computer, you know, that thing your using to read my instructable with.
6. Patience.

Step 2: Connecting the LCD and Letting Your Creation Talk to the World.

Like every step in this instructable there are many ways to do this. I will show you my favorite.

    Your lcd has 16 throe hole solder pads so the first thing is to attach some pins. If your patent then I recommend purchasing a header like this http://www.sparkfun.com/commerce/product_info.php?products_id=117. But if you want to get done as fast as possible (like me) then you can use wire.
    Simple cut 16 pieces of wire at about 1/2" (13mm (longer is okay)). Then solder them to the board.

Step 3: Connecting the LCD Continued.

Sins I'm using special characters I will be connecting all the wires.

Pin 1 Ground
Pin 2 +5 Volt
Pin 3 Contrast adjust
Pin 4 RS
Pin 5 R/W Goes to Ground
Pin 6-14 Data
Pin 15 Back-light Power
Pin 16 Back-light Ground

Step 4: Data Lines

Now you need to connect the Arduino to the lcd.
It doesn't mater what pins you use, but I recommend following the schematic.


Step 5: Power MaHaHaHa

The usb port on you computer has enough power to run the Arduino and led back-light so just connect the ground and power rails on you bread board to the power out on the Arduino board.


Step 6: Make Capacitive Sensor

For testing i used aluminum foil and a plastic water bottle, It will work with any container so long as it isn't metal.

You can use any type of wire but any non shielded lines will provide poor performance.

You can use any 2 pins, I chose 22 and 23.

Connect one side to ground and the other to a resister and 2 I/O pins.


Step 7: Programming

You need to add 2 library files to make this work
LiquidCrystal.h  http://arduino.cc/en/Tutorial/LiquidCrystal
CapSense.h      http://www.arduino.cc/playground/Main/CapSense

Copy and past this into Arduino 0017 or newer.








//Capacitive Liquid Sensor
//Vadim December 7th 2009
#include
#include

//This is to set the size of the lcd
const int numRows = f=4;
const int numCols = 20;

//This sets the pins for the lcd (RS, Enable, data 0-7)
LiquidCrystal lcd (53, 52, 51, 50, 49, 48,47,46,45,44);
#define Tempin 0x48
#define Tempout 0x49
CapSense   cs_22_23 = CapSense(22,23);


uint8_t block[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};

uint8_t tl[8] = {0x0F,0x08,0x08,0x08,0x08,0x08,0x0F,0x0F};
uint8_t tr[8] = {0x16,0x11,0x11,0x11,0x11,0x11,0x1D,0x15};
uint8_t bl[8] = {0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x1F};
uint8_t br[8] = {0x15,0x15,0x15,0x15,0x15,0x15,0x12,0x18};
void setup() {
  lcd.begin(numRows, numCols);
  lcd.createChar(4, tl);
  lcd.createChar(5, tr);
  lcd.createChar(6, bl);
  lcd.createChar(7, br);
 
  lcd.setCursor(18,0);
  lcd.print(4, BYTE);
  lcd.setCursor(19,0);
  lcd.print(5, BYTE);
  lcd.setCursor(18,1);
  lcd.print(6, BYTE);
  lcd.setCursor(19,1);
  lcd.print(7, BYTE);

  lcd.setCursor(0,2);
  lcd.print("Fuel ");
  lcd.setCursor(0,3);
  lcd.print("E");
}


void loop() {
  long fuel;
  lcd.createChar(2, block);
 
  long start = millis();
  fuel = cs_22_23.capSenseRaw(200);
 
 
  //Temratue makes a bit of a difrence so let it run for 5 min before tuning.
  //Adjust this number so that the output is as close to zero as posible.
  fuel = fuel - 7200;
  //Then fill up the conataner
  //Un-comment and adjust this so that the output, when the container is full,
  //is as close to 100 as possible.
  //fuel = fuel / 93;
 
  lcd.setCursor(0,0);
  lcd.print("      ");
  lcd.setCursor(0,0);
  lcd.print(fuel);
 
  if (fuel >= 6) {
    lcd.setCursor(1,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(1,3);
    lcd.print(" ");
  }
  if (fuel >= 12) {
    lcd.setCursor(2,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(2,3);
    lcd.print(" ");
  }
  if (fuel >= 17) {
    lcd.setCursor(3,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(3,3);
    lcd.print(" ");
  }
  if (fuel >= 23) {
    lcd.setCursor(4,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(4,3);
    lcd.print(" ");
  }
  if (fuel >= 28) {
    lcd.setCursor(5,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(5,3);
    lcd.print(" ");
  }
  if (fuel >= 34) {
    lcd.setCursor(6,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(6,3);
    lcd.print(" ");
  }
  if (fuel >= 39) {
    lcd.setCursor(7,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(7,3);
    lcd.print(" ");
  }
  if (fuel >= 44) {
    lcd.setCursor(8,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(8,3);
    lcd.print(" ");
  }
  if (fuel >= 50) {
    lcd.setCursor(9,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(9,3);
    lcd.print(" ");
  }
  if (fuel >= 55) {
    lcd.setCursor(10,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(10,3);
    lcd.print(" ");
  }
  if (fuel >= 60) {
    lcd.setCursor(11,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(11,3);
    lcd.print(" ");
  }
  if (fuel >= 64) {
    lcd.setCursor(12,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(12,3);
    lcd.print(" ");
  }
  if (fuel >= 69) {
    lcd.setCursor(13,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(13,3);
    lcd.print(" ");
  }
  if (fuel >= 74) {
    lcd.setCursor(14,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(14,3);
    lcd.print(" ");
  }
  if (fuel >= 78) {
    lcd.setCursor(15,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(15,3);
    lcd.print(" ");
  }
  if (fuel >= 83) {
    lcd.setCursor(16,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(16,3);
    lcd.print(" ");
  }
  if (fuel >= 87) {
    lcd.setCursor(17,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(17,3);
    lcd.print(" ");
  }
  if (fuel >= 92) {
    lcd.setCursor(18,3);
    lcd.print(2, BYTE);
  } else {
    lcd.setCursor(18,3);
    lcd.print(" ");
  }
  if (fuel >= 96) {
    lcd.setCursor(19,3);
    lcd.print("F");
  } else {
    lcd.setCursor(19,3);
    lcd.print(" ");
  }
 
  delay (50);
}

Step 8: Stuff

This is perfect for measuring volatile liquids, even works inside a propane tank.
Have fun.



Any and all information is for educational purposes only and I can not be held responsible if you blow yourself up.

Comments

author
smartwatch made it! (author)2016-12-11

Can a clear circuit be drawn to explain the connection for the capacitive sensor to the arduino. I have tried multiple configurations and i always get 0 as my fuel.

My steps:

1. attach 2 foil and thus 2wires to opposite sides of the bottle.

2. connect one wire to gnd.

3. Connect the other wire to a point in the breadboard

4. add a 10k ohm resistor from the point at (3) to another point.

5. connect (3) to pin 10

6. connect (4) to pin 13(I'm using Arduino Uno and it does not have pin 22, 23)

Please advice. Thanks

20161211_194257.jpg
author
PhilipC89 (author)smartwatch2017-06-07

hi smartwatch, I know you tried to build this 6 months ago, but I just built this, it kind of works, not very accurate though.

0 (empty) and 100 (full) works, but everything in between is not accurate

anyways, here's how I connected it:

1-3 same as you.

4. connect (3) to pin 13 and one end of resistor (i used 1M ohm, i've discovered that higher resistance makes it more sensitive, so your 10k ohm might not provide enough sensitivity to register any capacitance)

5. connect other end of resistor to another point on breadboard.

6. connect (5) to pin 12

change the code in the sketch where he has the numbers 22 & 23 to 12 & 13.

author
smartwatch (author)2016-12-13

Sorry didn't make it. Typo below. Need help

author
Abhi909 (author)2016-01-31

great project

can i place your project on my website.

i'm working on a website which is related to electrical projects.

i also mention your name, link and other info.

plz reply

author
Nateowami (author)Abhi9092016-06-05

@Abhi909 The answer is yes, and you don't really have to ask unless you want "special permission." At the top right it says it's licensed Creative Commons Attribution-NonCommercial-ShareAlike 2.5 Generic (CC BY-NC-SA 2.5). If you click it you can view the details: http://creativecommons.org/licenses/by-nc-sa/2.5/ As long as you follow the license rules you're fine.

author
aeysanity (author)2016-01-11

Nice work sir! I have few questions:

1. Did the aluminum foil is putted in the 2 sides of the bottle?

2. If 2 sides of foil is attached, where I can tap the wire on arduino specially the ground of the foils? and the data.

3. Can I replace the LCD for simple LED with 3 water levels?

4. Is this applicable for Glass Bottle?

Thanks in advance! Happy Developing!

author
mjbedford (author)2013-12-03

Agree, interesting project. I just tried it and it works pretty good! However, I have some comments/thoughts and was wondering if anybody could provide some feedback.
1. Since temperature could change the result, I was wondering if a temperature sensor was implemented, could this keep it calibrated? I assume the liquid temperature would be most important. I was thinking a temperature sensor attached to the water bottle at the lowest point.
2. The instructions mentioned that shielded cable would work better. Would you put the shield to the ground piece of foil and the center wire to the other foil (sense)? I did notice that the results we jumpy and that my hand would throw it off. I wonder if the shielded cable would help the result be more consistent and prevent the noise (from my hand).
3. I see in the image of the water bottle that the wire is soldered to the foil and then wrapped around the bottle a few times. It also appears you twisted the lead back. Are these important steps as well? Would they help with the noise? Would they be required if you are using shielded wire?

I also saw another writeup on this method and it mentions using an insulator over the foil and then another layer all the way around that is a ground plane.

Thanks!

author
VadimS (author)mjbedford2013-12-04

1: The best you can hope for is +- 5%, it's not vary accurate, and impurities in the water will affect the result far more then temperature.
2: Two conductor shielded cable. The shield should not be used as a conductor.
3: It's not wrapped around the bottle? The cable I used at the time was a twisted pair from an Ethernet cable.
4: An insulator and extra layer of foil would act as a shield

A far better option is a float attached to a potentiometer. I only used this because I could not put anything inside the tank.

author
MateusA2 (author)VadimS2015-05-30

3. What do you mean by saying: "It's not wrapped around the bottle?". It's must be wrapped around the bottle or not?
Thanks !

author
FathinL (author)MateusA22015-09-08

its half on one side, and half on the other side i thinkl

author
rodneyA1 (author)2015-08-23

hi sir.i admire your project..im making a fuel level sensor.. please help me for my project.. can this work to the fuel tank?

author
marion12 (author)2015-08-17

sir help us our project pls send us clearer circuit configuration

author
nthumma (author)2015-06-30

can we use arduino uno

author
RussM2 (author)2015-05-12

Oh, I must get this code to work! This project is fantastic and I need it in one of my larger projects. Sadly, I am still very much a nube and there are apparently obsolete keywords and syntax in this sketch that are kicking my butt. Any help you could offer that would allow me to get this running on an Uno with Arduino 1.6.3 would be most appreciated.

Thanks!

Here's the error log:

Arduino: 1.6.3 (Windows 8.1), Board: "Arduino Uno"

CapLiquidMeter.ino:7:21: error: 'f' was not declared in this scope

CapLiquidMeter.ino:14:2: error: 'CapSense' does not name a type

CapLiquidMeter.ino: In function 'void setup()':

CapLiquidMeter.ino:31:18: error: expected primary-expression before ')' token

CapLiquidMeter.ino:33:18: error: expected primary-expression before ')' token

CapLiquidMeter.ino:35:18: error: expected primary-expression before ')' token

CapLiquidMeter.ino:37:18: error: expected primary-expression before ')' token

CapLiquidMeter.ino: In function 'void loop()':

CapLiquidMeter.ino:51:11: error: 'cs_22_23' was not declared in this scope

CapLiquidMeter.ino:90:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:97:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:104:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:111:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:118:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:125:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:132:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:139:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:146:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:153:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:160:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:167:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:174:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:181:19: error: 'BYTE' was not declared in this scope

CapLiquidMeter.ino:188:19: error: 'BYTE' was not declared in this scope

Error compiling.

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

author
zoomx (author)RussM22015-05-24

I was able to compile without errors but not tested it.

Get the Capacitive Sensor library here

https://github.com/PaulStoffregen/CapacitiveSensor

and install it. Maybe you can istall using the library manager that will download it for you.

Change the include in

#include <LiquidCrystal.h>

#include <CapacitiveSensor.h>

Since the library has a new name you must change every reference to the old library in a reference of the new library.

change

CapSense cs_22_23 = CapSense(22,23);

in

CapacitiveSensor cs_22_23 = CapacitiveSensor(22, 23);

change

fuel = cs_22_23.capSenseRaw(200);

in

fuel = cs_22_23.capacitiveSensorRaw(200);

Change every line with

lcd.print(x, BYTE);

where x can be 1,2,3 or 4

with

lcd.write(x);

That's all! Good Luck!

author
Mjtrinihobby (author)2015-05-06

now this is a REAL project. I love it.

author
mckoffly (author)2015-04-26

Hey, is it possible to realise a capacitive pressure sensor on an easy way with arduino? Grettings from Germany

author
HarshalS1 (author)2015-03-22

Hi !

Could you please explain in detail, how to make the capacitor. I didnt got the exact idea behind it.

author
Akshay18352 (author)2014-12-24

hi i am trying to build a fuel sensor based on the above schematic using an arduino UNO. the sensor works fine with water but it does no work with petrol(gasoline). i think this is because petrol has a lesser dielectric constant compared to water. presently i am winding the foil on the opposite sides of the tank. please suggest me a method to increase the sensitivity of the sensor.

i cannot do anything from inside the tank so please suggest alternatives which can be implemented externally

THANKS :)

author
victor.leroy1 (author)2014-11-25

Hi !

You said this project could be done with a standard Arduino board but i am wondering whether an Arduino Uno would serve the purpose, as it does not have send/receive pins. What do you think ?

author
newton.l.barbosa (author)2014-08-21

how the resistor and the ground are connect to the arduino?

author
syedzeyad (author)2014-08-12

what is the material used in capacitence

author
gustavo_bf (author)2014-04-01

Hi. First of all, congratulations for your experiment.

I just did not understand what ''fuel = fuel - 7200'' and the ''fuel = fuel/93'' do.

Thank you!

author
VadimS (author)gustavo_bf2014-04-06

7200 is the capcitence without liquide. It's you're zero adjust.

93 is the adjustment for a full tank.

author
jimsims (author)2014-03-20

Excellent structable. I am interested in measuring and logging the tide
level in a saltwater canal. Do you think your concept would work for that? I would use stainless steel for the probe. I had planned to use pressure differential but I think I like the capacitance idea better.

author
VadimS (author)jimsims2014-03-21

A ss tube and ss rod in the middle would work, but you'll need to insulate it somehow. A thick player of polyurethane (it's the clear used with automotive paint) might do.

author
jimsims (author)VadimS2014-03-21

Thanks so much. I was thinking more like coating with the rubber liquid that is used for tool handles. Would I have to coat only the rod or both the rod and the tube?

author
VadimS (author)jimsims2014-03-23

Only the rod, the tube would be grounded. That setup would be a lot more
linear then what I built. so calibration would probably be easier.
Just need a reading with it above the water, and one all the way in.

author
match0927 (author)2013-11-21

It is an interesting project. Just be wondering what is the capacitance value in your measurement? A few pF or tens of pF? I will be having a similar project to go.

You mentioned “The sensor works by charging the capacitor (the water bottle) and measuring how long it takes to drain through the resistor.” Can you be a bit more specific on the process?

Thanks.

author
29guitarman (author)2011-03-31

hey yeah, how did you connect the wires aswell? can you explain further on how you set the bottle out?
Cheers

author
VadimS (author)29guitarman2011-03-31

2 pieces of aluminum foil on aether side. One wire soldered to each.

One acts as a ground, the other sens.
doesn't matter witch way around you set it up.

The sheets should go from the bottom to top of the container.

It's a bit of a pain to solder aluminum foil but can be done. otherwise anything conductive.

author
SolidRaven (author)VadimS2013-10-14

Sorry to barge in so late after the initial post but there is an easy trick to this problem in fact! In electronics we love using aluminium tape to connect pieces of a frame to each other for EMC and grounding reasons. But copper tape also exists, and it's as easy to solder as regular wire. So you just slap a piece of copper tape on the aluminium and solder your wire to that. Saves you a lot of headaches.

author
pelrun (author)2009-12-07

Sweet 'ible, but can you elaborate more on exactly how the bottle is wired up? It's rather brief and ambiguous at the moment...

author
VadimS (author)pelrun2009-12-07

I just got lazy. I intend to add more after I test it on a real gas tank (Tonight or Thursday night).

author
bill.schuller (author)VadimS2012-02-08

How'd it go now that Thursday night was a year and a half ago?

author
VadimS (author)VadimS2009-12-07

I answered your specific question, hope it helps.

author
lw119 (author)2010-11-29

This is pretty clever. I think I am going to try something similar.

I have pet cockatiels that love to backwash in their water dish. It doesn't take long for the water to get kinda nasty. I wonder if something like this could be used to alert me when their water needs a change out.

Have you ever noticed a significant reaction to the electrolyte getting contaminated? Any tips you can offer would be much appreciated.

Thanks for the inspiration.

author
VadimS (author)lw1192010-11-29

Hmm, never tried. I don't think it would be able to tell the difference between a change in water lvl and stuff in the water.

Using an infrared led on one side and a receiver on the other could be used to detect when impuritys where in the water.

author
lw119 (author)VadimS2010-11-29

I have a float that maintains the water level so I don't think that would be an issue.

I considered the infrared idea but they like to bathe in the water too, so that might trip it. Plus, they like to break things, so I was hoping some submerged concentric metals tubes would be beak proof.

I guess it's a trivial problem but a fun challenge.

Thanks for the reply.

author
Hunter601 (author)2010-10-14

Hmm.. I'm trying to figure out if this would be the right way to measure the qty of Heating oil left in a 1000 Liter plastic tank, which is about 160 cm high and the other dimensions about 80 cm each.
I haven't noticed (or have overlooked) any max practical size for your setup but I assume it might not work out for my requirements, right?

Could anyone point me in the right direction? Maybe sink a pressure sensor to the bottom of the tank and read out the static pressure, using an Arduino?

author
VadimS (author)Hunter6012010-10-14

The accuracy decreases when you move the plates away from each-other.

Your best bet would be to use a couple metal pipes. one inside the other making shire they don't touch.

Then use this setup to measure the capacitance between them.


Another option is a simple float inside a tube, with a small magnet attached to it. Then put some read switches along the length of the pipe.

author
Hunter601 (author)VadimS2010-10-14

Ok, concentric tubes makes good sense. Thanks for the tip.

author
TheBestJohn (author)2010-05-25

 2 questions and a comment.

1: so the two aluminum sheets sit outside the container? one on either side of it?

2: would there not be a risk of the capacitor discharging and creating a spark?

Comment: NO METAL CONTAINERS!? I guess a keg level sensor is out of the picture lol.

author
VadimS (author)TheBestJohn2010-07-16

Kind of lait, but anyway. The plates only run at 5v so a spark is extremely unlikely. You can build one to go in a keg using a metal rod and a metal pipe but you would have to modify the keg and re-weld it when you're done (not so easy on aluminum. Yes the sheets sit on the outside. Thay can be put on the inside but need to be electrically isolated from the licqwid.

author
linkthewise (author)2010-05-07

 Hey I one question did you cut the aTinFoil in two pieces and you put them in the sides of the conteiner?. 

author
MomentumV (author)2010-02-02

how well did this work with petrol instead of water?

author
VadimS (author)MomentumV2010-02-17

We broke a few things on the buggy so it hasn't bean installed yet. But I did test it with gasoline (petrol) and it works, it's accuracy is down a bit at about 10%, with water it was within 5%.

author
makkan (author)2010-01-28

Just what I have been looking for to measure the coffee level in the office coffee machine.

author
bullzebub (author)2009-12-09

how are you going to handle the odd shapes of a gas tank?
it would be nice to have someting that shows a realistic approximation on whats left in the tank.... not like most car systems

author
VadimS (author)bullzebub2009-12-09

It was built for a specific tank from a 10 hp brigs attached to a buggy.

About This Instructable

194,724views

162favorites

License:

Bio: I tinker with electronics and have half an electric bike that I never seem to get finished.
More by VadimS:High power flash.Using a Wii Remote to control WMP on a PCBuilding a Capacitive Liquid Sensor
Add instructable to: