Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mapping a range
#1
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
Reply
#2
volts = pin_value * 3.3 / 4095
scalar =pin_value / 4095
percent = pin_value * 100 / 4095
Reply
#3
(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))
Reply
#4
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.
Reply
#5
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?
ebolisa likes this post
Reply
#6
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mapping a value to an interval JazonKuer 12 1,842 Mar-17-2023, 07:59 PM
Last Post: Gribouillis
  access is denied error 5 for network drive mapping ? ahmedbarbary 2 1,731 Aug-17-2022, 10:09 PM
Last Post: ahmedbarbary
  matplotlib x axis range goes over the set range Pedroski55 5 3,110 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  mapping joystick axis to specific 'channels' in python DashOrion 1 2,552 Jul-07-2020, 04:26 PM
Last Post: DashOrion
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 6,877 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  mapping-camera-coordinates-to-a-2d-floor-plan fauveboyxuuki 0 2,493 Dec-10-2019, 10:34 PM
Last Post: fauveboyxuuki
  Maze Mapping with Image Processing furkankuyumcu 0 2,159 Dec-16-2018, 02:45 PM
Last Post: furkankuyumcu
  Python Idea Assist.: BerryIMU & Raspberry Pi 3 Audio Mapping StephLeber 0 2,261 Dec-09-2018, 10:43 AM
Last Post: StephLeber
  Python Inbuilt library for XAML to HTML5 mapping adityamandlekar 1 2,339 Jun-07-2018, 08:05 AM
Last Post: Larz60+
  Mapping Fields zak308 0 2,467 Jan-09-2018, 10:02 PM
Last Post: zak308

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020