Python Forum

Full Version: Mapping a range
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I've an analog sensor which generates a voltage from 0 to 3.3. It's connected to a digital converter (ADC) which outputs a range from 0 to 4095 (bits).

In C+ I use
output_value = map(pin_value, 4095, 0, 0, 100);
which returns values from 0 to 100.

How is it done with Python? I tried with statements like if x >= n < xx but it's too cumbersome.

I appreciate some help.
TIA
volts = pin_value * 3.3 / 4095
scalar =pin_value / 4095
percent = pin_value * 100 / 4095
(Jun-12-2021, 07:46 PM)deanhystad Wrote: [ -> ]percent = pin_value * 100 / 4095

Thank you. If I understood it right, this' the way to calculate the convertion but it's not returning what it should.

Let's say I've 1.5V on the pin_value, the percentage should be about 50%

def map(pin_value):
    volts = pin_value * 3.3 / 4095
    scalar =pin_value / 4095
    percent = pin_value * 100 / 4095
    return percent

print(map(1.5))
You mention 3 different ranges (0-3.3V, 0-4096bits, 0-100percent), but this algorithm will only map from one to a second. Which mapping do you want? I'm assuming you don't read volts at all but just get the bits from the ADC? I'd expect you'd want to map ADC to percent.

Your last code has both 3.3 and 4096 and 100. I'm not sure exactly what you're trying to convert from or to.

>>> def maprange( a, b, s):
...     (a1, a2), (b1, b2) = a, b
...     return  b1 + ((s - a1) * (b2 - b1) / (a2 - a1))
...
>>> maprange((0, 4096), (0, 100), 2048) #bits to percent
50.0
>>> maprange((0, 3.3), (0, 100), 1.5) #Volts to percent
45.45454545454546
Code is from the Rosetta Code project.
What is the range of the ADC? Is it 0 to 3.3V, -3.3 to 3.3V. Is it a 5 volt ADC? Is there a programmable gain? My assumption was a 12 bit ADC with an input range 0..3.3V and gain of 1.

I'm sorry I used pin-value. You don't know the pin value, you know the ADC value. If you have 1.5 volts at the pin the ADC should be reading around 1861 (1.5/3.3 * 4095). You could convert this back to volts = 1861 * 3.3/4095 = 1.4997V
or to percent = 1861 * 100 / 4096 = 45.4%

If this is not near the reading you are seeing it means there is a range or gain error. If you can get readings at two different voltages you can compute the conversion without knowing anything about the ADC.

scaling = (reading1 - reading2) / (voltage1 - voltage2)

Is this close to 4095 / 3.3?
(Jun-12-2021, 10:33 PM)deanhystad Wrote: [ -> ]What is the range of the ADC? Is it 0 to 3.3V, -3.3 to 3.3V. Is it a 5 volt ADC? Is there a programmable gain? My assumption was a 12 bit ADC with an input range 0..3.3V and gain of 1.
Exactly!

Quote:I'm sorry I used pin-value. You don't know the pin value, you know the ADC value. If you have 1.5 volts at the pin the ADC should be reading around 1861 (1.5/3.3 * 4095). You could convert this back to volts = 1861 * 3.3/4095 = 1.4997V
or to percent = 1861 * 100 / 4096 = 45.4%
That's was what I was looking for. Thank you much!!

Yes, a voltage range of 0 to 3.3 enters into a microcontroller and with micropython I get a readout of 0 to 4095.