# Creating a library for DF Robot - Water Level Transmitter

• Hi there,
I am Master's Student currently working on remote monitoring with IoT.

I bought an Industrial water level sensor - DF robot Throw-in Type Water Level Transmitter coupled with an Analog Current to Voltage Converter.

Find the Product Wiki below:

Let me start off with saying I am very new to this field and to micropython. I have scoured the internet for a library for this sensor but I can't seem to find one.

I understand that this sensor would use both the ADC and I2C functions on my pycom board but I am struggling to integrate these two function in order to develop a new library for this sensor. I am also aware that developing an entire library for this sensor is far above where my level of python coding is at in this moment. I was hoping that someone could help me with this as I have somewhat hit a brick wall. Any example code I could use as a guide or any pointer on how to begin writing the library would help so much.

Below is sample code given in the product wiki but as you will see, this code is in C/C++ language.

``````/***********************************************************
DFRobot Gravity: Analog Current to Voltage Converter(For 4~20mA Application)
SKU:SEN0262

All above must be included in any redistribution
****************************************************/

#define ANALOG_PIN A1
#define RANGE 5000 // Depth measuring range 5000mm (for water)
#define CURRENT_INIT 4.00 // Current @ 0mm (uint: mA)
#define DENSITY_WATER 1  // Pure water density normalized to 1
#define DENSITY_GASOLINE 0.74  // Gasoline density
#define PRINT_INTERVAL 1000

int16_t dataVoltage;
float dataCurrent, depth; //unit:mA
unsigned long timepoint_measure;

void setup()
{
Serial.begin(9600);
pinMode(ANALOG_PIN, INPUT);
timepoint_measure = millis();
}

void loop()
{
if (millis() - timepoint_measure > PRINT_INTERVAL) {
timepoint_measure = millis();

dataCurrent = dataVoltage / 120.0; //Sense Resistor:120ohm
depth = (dataCurrent - CURRENT_INIT) * (RANGE/ DENSITY_WATER / 16.0); //Calculate depth from current readings

if (depth < 0) depth = 0.0;

//Serial print results
Serial.print("depth:");
Serial.print(depth);
Serial.println("mm");
}
}```
``````

Your help would be greatly appreciated.

Best wishes
Roberto

• I understand that this sensor would use both the ADC and I2C

Look into docs how to use ADC:

do not have this sensor then you must test it self. Below code can contain errors but should be good for start:

``````import machine
from time import sleep_ms

ANALOG_PIN = 'P16'
RANGE = 5000 # Depth measuring range 5000mm (for water)
CURRENT_INIT=4.00 # Current @ 0mm (uint: mA)
DENSITY_WATER=1  # Pure water density normalized to 1
DENSITY_GASOLINE = 0.74  # Gasoline density
PRINT_INTERVAL = 1000

while True:
dataVoltage = apin()

dataCurrent = dataVoltage / 120.0 # Sense Resistor:120ohm
depth = (dataCurrent - CURRENT_INIT) * (RANGE/ DENSITY_WATER / 16.0) #Calculate depth from current readings

if (depth < 0):
depth = 0.0;

#Serial print results
print('depth:', depth, 'mm')

sleep_ms(PRINT_INTERVAL)
``````

Above your sensor is connected to pin P16 - you can choose different one.
look also at attenuation - i choose ADC.ATTN_11DB but maybe different should be used.
Remember also that all pins work on 3V3. Excceeding it can damage the board.