Wireless fluid level meter







1.     Introduction.. 1

2.     Description.. 1

3.     Usage. 1

4.     Schematic diagram... 2

5.     Explanation of the schematic diagram... 2

6.     Building the sensor. 4

7.     Adjusting the transmitter/measuring module. 4

8.     The transmitter/measuring module PCB: 2 versions. 4

9.     Testing.. 5

10.       RF data transmission/reception.. 7

11.       Software for the measuring/transmitter module. 7

12.       The receiver module. 8

13.       The receiver module’s schematic diagram... 8

14.       The receiver PCB.. 9

15.       Software for the receiver module. 9



1.                   Introduction


This circuit is based around the PIC 16F687 microcontroller. This chip is actually a small computer contained in a single chip, including RAM memory,  EEPROM, I/O ports, CPU and so on. When you buy this chip, it comes empty with no program on it. You have to compile the source code and download the resulting  machine code into it, using a PC and a small programmer attached to the parallel port of the PC and the chip. To get yourself familiar with this stuff, I suggest you first read this link: Getting started with microcontrollers.


Back to top


2.                   Description


This device measures the content of a tank filled with some kind of liquid, using 8 ac electrodes.

The device can be used on its own, or in combination with a “in-house” receiver unit (using wireless data communication).

This project demonstrates a number of aspects of the PIC16F687 microcontroller, like: ultra low power wake up, serial communications, nanowatt technology etc.

The measuring of fluid level is done with a resolution varying from 1/8 (almost empty) to 8/8 (full).

The device can be used for different purposes. For example: measuring the content of a rain water tank, of a water tank in a motor home or trailer, and so on.


Back to top


3.                   Usage


Place the sensor wires in the tank. After putting the batteries, you should see the content on the bar-graph display (between 1/8 and 8/8). After about a minute the display will switch itself off. You can switch back on again at any time by shortly pressing the button. To switch back off, press and hold the button for more than 1 second.


Single button operation: press the button to activate the display. Press and hold the button for more than 1 second to put the display back off.


Zero-indication: if the tank is completely empty, the below LED will flash to show the tank is empty.


Once an hour the display will put itself on for a few seconds, while this is happening the device will also transmit the measured value using RF (433.92 MHz) (optionally).


The transmitter/measuring module operates on batteries for about one year.

Replacement of batteries: use 2 lithium button cells size 2032. These batteries have a capacity of about 220 mAh.


The transmitter/measuring module can be used by itself or in combination with an optional receiver module (wich operates on 230 VAC). This receiver module can be placed in a radius of 30 meter around the transmitter/measuring module.


Back to top


4.                   Schematic diagram






List of components


Link to Eagle circuit


Link to Eagle circuit, SMD


Back to top


5.                   Explanation of the schematic diagram


(Refer to the schematic diagram).


Power supply


Diode D2 is for protection against reverse polarity of the batteries and also for lowering down the supply voltage somewhat. Two lithium batteries are supposed to deliver 6 volts, but in reality this will be about 6.2 volts with new batteries, the voltage drop over the diode will bring this voltage down to about 5.7 volts which is acceptable for the PIC and the other chips. So we don’t need to use a voltage regulator IC (which would consume more power).



Measuring liquids


Because measuring fluids with the aid of DC current will lead to electrolytic effects and corrosion problems, in this device we will use a measuring current consisting of an AC block wave with a frequency of about 10 kHz.

The circuit’s main purpose is measuring the level in a tank filled with rain water, but of course you can use it for other fluids too. The circuit uses 8 measuring points that are mounted inside the tank at certain places. By carefully choosing the positions of the electrodes, inaccuracy can be minimized.

The microcontroller (IC1) acts as the circuit’s main steering element. Port RC6 (output) is a software oscillator of about 10 kHz. This output pin delivers the measuring current, which is applied to voltage divider R3/R4. By means of the de-multiplexer IC2 en capacitors C5..C12 this measuring current is also applied on the electrodes connected with sub-d connector X1. This happens because the de-multiplexer is controlled by a BCD value (0..7) provided by the microcontroller on pins <RB6:RA1:RA2>, resulting in an alternate scanning of the electrodes.

When the electrode being scanned is dry, the measuring voltage is of the same level as provided. When the electrode is under water, an extra load is put on the voltage divider R3/R4 resulting in a somewhat lower voltage. By means of a simple demodulator (D1/C1/R5) the progress of the ac voltage will be converted to some kind of a envelope voltage which is guided to comparator IC3.

The output of comparator IC3 is then guided back into the microcontroller (RB4), a “high” level meaning the electrode is dry, and a “low” level meaning the electrode is under water.

A side effect of the demodulator is caused by time constant C1/R5, holding the measured value of each electrode until somewhat after the next switchover. This would mean that the next electrode is measured as “under water” while in fact it might be “dry”. We avoid this by waiting half a time block (in terms of RB6).

The measured value is showed via leds LD1..LD8, which are controlled in a multiplexing way from the microcontroller.





We choose an external 20 MHz crystal as the oscillator because the built-in (max. 8 MHz) oscillator is not fast enough to produce a software frequency of 10 kHz. We need to produce this frequency ourselves in order to have full control during each period (so we cannot use the PIC’s PWM module).

When using a 20 MHz crystal it is better to choose the configuration option _HS_OSC in the MPLAB source code.



The choice of the microcontroller


The use of the PIC16F687 as a microcontroller seems like a good choice because this controller embeds some important features which we really need for this project: nanowatt technology, serial communications, …


I almost always use ICSP programming (in-circuit serial programming) for programming my PICs. You have to realise that this PIC device uses pins RA0 and RA1 for programming and not RB6..RB7 like is the case with most other PICs.


For more information have a look at the PIC16687 datasheet.


Thanks to Fr. Tom McGahee’s I managed to get the serial communications up and running, because he has an excellent website regarding this subject (notice to click on PICUART.ZIP). I had to make a few changes to his example PICUART source code because of some specific matters on the PIC16F687.



Ultra low power consumption thanks to periodic wakeup.


The circuit is designed to last at least one year on two lithium button cells. Therefore an on/off switch is obsolete. This is made possible because of the PIC16F687’s built-in so-called Ultra Low Power Wake Up (ULPWU) module. The microcontroller goes into active mode once an hour during 1.5 seconds, enough to do the measuring task and send this on RF. After that, the processor will go back to sleep for one hour. During sleep the current consumption is less than 1 µA. During active periods it is about 10 – 20 mA.


The above is implemented like this: a 680 nF capacitor (C13) is connected to pin RA0 (the ULPWU pin). Just before going to sleep the software will do these steps:

-          power down the rest of the circuit (i.e. the other chips, the RF module etc.) by clearing pin RC7;

-          charge the capacitor (this is done by setting RA0 as an output, set it high and wait 5 ms);

-          set RA0 as a digital input again;

-          enable the Interrupt-on-change;

-          enable the ULPWU module


After this the controller is put into sleep. During sleep, the current consumption is less than 1 µA. During sleep, the ULPWU module will slowly discharge the capacitor. After 15 seconds the voltage over the capacitor will be dropped below a certain level such that the controller (thanks to the Interrupt-on-change feature) automatically will wake up. Because 15 secs is too short for our goal we maintain a counter (sleepCounter) and go back asleep immediately. Only after cycling 240 times we reach the “one-hour” milestone and we decide to do the “once-every-hour-work”: measuring the fluid and send it on RF. This sending is done a few times (using a counter <clock:clockH:clockHH>) to be sure that the receiver has a good reception. After that we go back to sleep and we are back to square one.

Each 15 seconds awake-cycle is also used to shortly (5 ms) flash LED8 so the user can clearly see the device is working.


The theoritical current consumption in one year (=8760 hours) is calculated as follows:

One-hour cycle:

15 mA x (1.5 x (1 h)/ 3600) x 8760 = 54 mAh per year for periodic transmitting of data

Standby time:

0.001 mAh x 8760 h = 8.8 mAh per year for the sleep mode

15 seconds lightpulse on LED8:

      8 mA x 0.005 (1 h / 3600) x 240 x 8760 + 23.4 mAh per year for the blink-LED8 mode.


So the total is about 86 mAh per year (a lithium-button-cell size 2032 can deliver about 220 mAh so this should be no problem).



(Information about the ULPWU module was taken from Microchip’s application note 879).


Back to top


6.                   Building the sensor


Fabricating a suitable sensor is a simple thing to do. Take some flatcable consisting of 9 wires and cut off each wire at the desired length. The ground has to be the longest because ground is coming on the bottom of the tank, or at least below the wire for sensor 1. Strip all 9 ends of the wires about a few millimetres. Next put the flatcable in a piece of PVC tube and fix it.


The other side of the flatcable must be soldered on a 9-pin female sub-d connector (ground=pin9, wire 1 =pin1, wire 2=pin2 and so on). This connector is then plugged onto the male 9-pin (pcb mounted) sub-d connector on the pcb.


Below is a very simple sensor (for the moment, without the PVC tube) that I used for testing in a glass of water.



Back to top


7.                   Adjusting the transmitter/measuring module


The circuit must be adjusted once. Put the sensor in a glass of water and make sure that only the ground wire and wires 1..4 are under water. Now adjust P1 so that exactly 4 LEDs are lit.


Back to top


8.                   The transmitter/measuring module PCB: 2 versions


There are 2 versions of the pcb: a through-hole version and a smd version.


Through-hole versio,


Doublesided, suitable for ICSP programming.


PDF file showing top side, bottom side, both sides and components setup: Print-layout


Link to Eagle pcb


Prototype of the complete PCB:


The prototype PCB



On the upper left you can see the 4051 IC with the RF Transmitter module TWS-434 on top (the right header can be used to put in a wire aeral). Below somewhat right in the middle sits the PIC16F687. Left of it the 20 MHz crystal. At the right, the 8 LEDs. To to left of them, the pushbutton.

As you can see this is only the first version of the PCB containing a few design errors, hence the patch wires.



SMD version


Doublesided, suitable for ICSP programming.


PDF file with topside, bottomside, both sides and components setup: PCB-layout


Link to eagle PCB, SMD


Back to top


9.                   Testing


To discover errors when building the device you might use a scope and do some measuring.

The below picture shows what you should expect in a correctly built device, when using a 2-channel scope connected to pins RB6 (pin 11 on the PIC) and opamp-out (pin 6 on the CA3130). The green signal is from RB6, the erd signal is from the CA3130 under the condition that sensor wires 1 and 2 are under water. Time division of the scope is set at 1 ms/div. The more wires that are under water, the wider the red signal’s low-time will be. In the below picture the width of the red signal’s low-time is about 2 green pulses (a green low and a green high). Like mentioned before, the low time of the red signal will expand a little further in time than expected (this is normal) and then it will rise again.

The green signal shows the LSB (least significant bit) of the BCD value for selection of the sensorwire. The other bits of the BCD are not shown here.




Two other interesting signals to have a look at are the ac voltage on one sensor and the oscillator. Remove the sensor from the water and connect the scope’s channel one to the anode of D1. Connect the scope’s channel 2 to pin 1 of the sub-d connector on the PCB. The scope will now show an ac voltage (the green signal) because of capacitor C5 in between. This AC voltage has 8 periods on sensor one during 1/8 of the time, because the 4051 IC will divide (demultiplex) the oscillator frequency among the 8 sensor wires. The continue oscillator frequency on the anode of D1 (the red signal) is DC voltage because it didn’t pass any capacitor.




Back to top


10.               RF data transmission/reception


Optionally a RF Transmitter module can be mounted on the 5-pin header named “OPTIONAL_RF_TRANSMIT_MODULE”.. As a module I took the TWS-434 RF transmitter module from Rentron (www.rentron.com). This module is specially made for transmission of serial data on 433.92 MHz.

To feed the data to the module we use the USART (universal synchronous/asynchronous receiver/transmitter) module of the PIC microcontroller.


The same company also has the complementary RWS-434 receiver module. This module receives our data and polishes it back to a nice serial block wave that can be directly read bij the PIC’s USART in the receiver.


Back to top


11.               Software for the measuring/transmitter module




The software is heavily interrupt driven. This means that most of the work is done during timer interrupts. The most important task include:

-          generating the oscillator output on RB6

-          generatiing the 3-bit BCD value for the 4051 chip

-          reading back in the result of the opamp on pin RB4 (routine measureOneSensor)

-          multiplexing the LED display (routine LEDmultiplex)

The other tasks are doen in the main loop. This includes reading the pushbutton, delay, serial data transmission, automatically go asleep after a minute and so on.



Software customization:


You might want to recompile the software with some modified definitions (“#define’s”) to slightly change the functionality;


These are the definitions that can easily be changed (for more explanation of them have a look at the source code):


MAINS_POWERED:          if the devices has to operate from mains in stead of batteries (automatic switch off after one minute is than disabled)

REVERSE:                        to upside-down the bargraph LED display

RF_DISABLED:                 to disable the hourly wakeup for data transmission

TESTING:                          for testing purposes, makes the RF data transmission happen every 90 seconds in stead of hourly



ASM file:


Source code


HEX file:


Hex file


Back to top


12.               The receiver module


Features of the receiver module


The receiver module can optionally be used to have a distance readout of the measured tank supply.

The module has the following characteristics:

-          works on mains and will continuously scan if data can be received

-          can be software expanded in the future, for the moment it will only receive the data from the measuring module but in the future it might receive the data of a home built wind speed meter

-          SMD design



Explanation of the receiver’s schematic diagram


The receiver module also contains a PIC16F687 microcontroller as a central element. In addition there are three 7-segment displays. This seems overkill, because one should be enough (measured value is from 0..8). In the future I plan to use this receiver module for other projects like for example a wireless wind speed meter, hence 3 digits.


The three digits are multiplexed from the microcontroller using three transistors Q1..Q3.


The data is received using the RWS-434 RF Receiver module. This module offers its serial data to the PIC on pin 2 (2400 baud). The program is receiving continuously. It might be that you have some other appliances using the same frequency (433.92 MHz) but this is no problem because the measuring module sends an ID (“FLU” for Fluid level meter) that is checked in the receiver software. The received data, if valid, is stored in a variable and multiplexed to the display.


The pushbutton S1 is for changing function i.e. switch between tank meter, another than meter, wind speed meter and so on (not yet implemented).



Power supply


For the power supply you can use almost any wall adapter delivering a voltage (AC or DC) between 8..20 V. This voltage is rectified (using diodes D1..D4) and stabilized using a 7805 voltage regulator. Thanks to the rectifier D1..D4 it is impossible to wrongly connect the wall adapter.





As an oscillator this time we use the PIC’s builtin 8 MHz oscillator.


Back to top


13.               The receiver module’s schematic diagram


(to be tested)


Component list of the receiver module


Link to Eagle circuit


Back to top


14.               The receiver PCB


Double sided, suitable for ICSP programming. SMD only (contains some through-hole components like 7-segment displays and headers).


PDF with top side, bottom side, both sides and components setup: PCB layout


Link to Eagle pcb


Back to top


15.               Software for the receiver module



ASM file:


Source code


HEX file:


Hex file



Back to top





   Home                            Back to Electronic Projects