Go Down

Topic: Servo as a pointer but using a non linear scale, help mapping (Read 202 times) previous topic - next topic


So Im new to the forum, and new to arduino.  Great another newb...yeah...While I design and build full automotove chassis harnesses, this is all new to me and coding is not my strong point.  But I digress...

I have thermometer project, analog using a small servo as the needle pointer.  The scale is non linear for the temp reading and I cannot change that due to the nature of the project.  Repurposing a 100 year old volt meter.  Te scale starts at 0 and goes to 175, but 100 is in the middle.  The scale ramps up quick from 0 to 80 within the first 20% of needle movement, then the rest of the scale goes from 80-175 with quite a bit of resolution.  See the attached pic for the scale.

So used the code on adafruit for an analog servo thermometer, and I mostly understand how it works, and it does work, but it seems most basic servo codes give you a range you can set, but that maps in a linear fashion to the temperature map via the code.  So you can set 0 deg on the servo to 0 deg F and 180 deg on the servo to 120 deg F, for example of course.

I want to be able to more precisley map the temp to points.  20 deg servo to 40F, 23 deg to 45F and so on to be somewhat accurate with the scale provided in the project.  Thoughts on how I can do this?  Is the servo even the right way to go?  Would a moving coil movement be more appropriate?  In that case I would still have to set some kind of non linear mapping for this project as well.

Sorry for the long post, if someone can point me in the direction for code, or a vid I can watch that would be sweel, thanks!


Servo is fine. What you need to do is derive a formula which connects the temperature reading input (0-1023) with the required servo angle. If you have that then it can certainly be coded. In the worst case you could do it as a lookup table but that shouldn't really be necessary.

Effectively you are looking to produce a customised map() function which is non-linear. Your particular scale looks to be condensed at both ends, widening towards the centre but it's too early in the morning here for me to guess at a formula.



Collect some more data by experiment telling you how many degrees correspond to what temperature.

With that data in mind, take a look at MultiMap.

Alternatively, use the experimental results and derive a formula - Excel's solver or similar can do the calc for you.


Oof, so very over my head.  Guess I gotta do a bunch of reading and try to understand how the multi map function works.  This seems to be exactly what I need to do.  I'll start by making a map of degrees vs temp then go from there.


I'll start by making a map of degrees vs temp
Post a table of degrees versus temperature and we can find a function to fit it.


Will do, gonna be a few days though, I left all the parts at work lol.


Ok attached a PDF since I couldn't attach an XLS with the servo position in relation to the scale, or temp readings...So what so we think?


That curve is well fit with a 4th order polynomial, but there would be many other possibilities.

If T=temperature (float variable), then the servo angle A:

Code: [Select]
float A = 140.94 -0.25543*T + 0.011764*T*T - 0.00024458*T*T*T + 1.0574E-6*T*T*T*T;


So that formula pretty much plots the temp value to the service position if I understand correctly?

I should be able to take the code I have then and use this in it's place I'm guessing instead of the map function


The line of code IS a mapping function, which calculates the servo angle from the temperature.

If you have a problem with the code, post it (using code tags) and tell us what goes wrong.

Go Up