0

I am currently working on a Turbidity meter that returns a value of the voltage from 0-5.0 (this number will change depending on how turbid the water so a lower voltage reading indicates a more turbid water).

What I am trying to do is take the voltage reading that I get and convert it to a reading that expresses the turbidity in the water (so a voltage reading of 4.8 would equal 0 to a voltage reading of 1.2 would equal 4000).

I have written some code using MicroLogic and I know that in there there is a box that looks at the incoming reading and will scale the outgoing reading between a Min and Max number that you put in (an example in MicroLogic is that I get a 4-20mA signal in and it will scale the output to mean a water level in a tank based on that 4mA = 0ft and 20mA = 12ft)

Is there a scaling code for python, or how to I go about doing this? Thanks!

SlntStlkr
  • 3
  • 3
  • I am afraid that there is nothing ready for this in Python, but this is also very easy to implement. – norok2 Apr 03 '20 at 19:03
  • @Kokosbrood suggests you to look at https://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio – Gareth Ma Apr 03 '20 at 19:13
  • I don't use Python, but I think it is always good to have a generic rescaling function handy. See for example this answer on SO: [Convert a number range to another range, maintaining ratio](https://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio). That question is also for Python, and looks very similar I think to that you try to achieve? – Kokosbrood Apr 03 '20 at 19:03
  • Thanks all. I am a wastewater treatment operator by trade, and my boss said "you like working with computers, so this should be a good project for you" and handed me a Raspberry Pi and a Turbidity meter probe (like from a washing machine). I have no knowledge in Python (my knowledge is limited to MicroLogic and some basic ladder codes), so in the last week I have had to learn to: (1) program the Pi, (2) make a LED Diode turn on off, (3) figure out a DA/AD board, (4) get the board to read the turbidity meter, (5) export my results to a spreadsheet. All this in about 6 days. – SlntStlkr Apr 04 '20 at 17:49

1 Answers1

1

You could do something like this:

def turbidity(voltage):
    return 4000 - (voltage-1.2)/(4.8-1.2)*4000

print(turbidity(1.2), turbidity(2.4), turbidity(4.8))

Which would print out:

4000.0 2666.6666666666665 0.0

If you want integers instead, add an int() call like so:

4000 - int((voltage-1.2)/(4.8-1.2)*4000)

Good luck with your turbidity!

Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
  • 1
    This worked perfectly for what I needed. I was able to code the word "voltage" to be voltage that the program is getting every time it updates, and was able to get it to record it in the data file it is writing each day. Thanks! – SlntStlkr Apr 05 '20 at 15:53