Scaling an Analogue input

parky

Member
Join Date
Oct 2004
Location
Midlands
Posts
5,645
There have been a number of posts asking about scaling so I thought this may help.

The formula is:
Out := ((Variable_In - ZeroIn) / (SpanIn - ZeroIn)) * (SpanOut - ZeroOut) + ZeroOut.

The Variable_IN is the raw value i.e. it could be 0-32767 or -32767 to +32767 or even 200 to 2000
or whatever the raw range is
The ZeroIn & SpanIn are the minimum & maximum of the input range as depicted by the analogue card.
SpanIn & SpanOut are the min & max of the scaled output.
Examples:
Raw in 0 to 2000
ZeroIn = 0
Span in = 2000
ZeroOut = -50
SpanOut = +150
See logic version in pic.
Note: you will need to convert integers to reals for the input variable and if required the output if needed to send it to an analogue out card.
This applies to the Zero & spans as well although these could be fixed values.
This will work in both directions doing the math in integer would not produce accurate results.

Scale_2.png
 
I have a pinned Excel document just for this scaling calc. I just type the raw/scaled values I need and get the result. I find it super handy
 
Wow, I just read some of that posted link by mylespetro, What I did seems so much simpler in description people love to complicate things. Only managed to read a few otherwise this post would be dated 1 Jan 2020.
đź“š
 
You should have a check that the result of of the SpanIn - ZeroIn block is not zero. If it is, you will get a divide by zero error and likely crash the program.
 
Wow, I just read some of that posted link by mylespetro, What I did seems so much simpler in description people love to complicate things. Only managed to read a few otherwise this post would be dated 1 Jan 2020.
đź“š


Yes, it's an old post but I think it's actually kind of a math lesson on what y=mx+b means in a practical sense. It gets the actual practical stuff out of the way at the start using the SCL instruction, although usually nowadays you would probably use a SCP (scale with parameters) instruction.
 
I think it's actually kind of a math lesson on what y=mx+b means in a practical sense

exactly ... and yes, it's so long that I had to break it up into two sections - because some folks had their web connections timing out before they were able to download it all in one chunk ...

and even though I'm retired now, I still get calls and emails once in awhile from people who want a full PDF version - so I keep a copy stashed away on my website ...

and as mylespetro correctly pointed out - the purpose for writing it wasn't to simply get the scaling "done" - it was more to explain the underlying math so that the basic ideas could be applied to MANY different platforms ... most of my students were maintenance technicians who weren't trying to "write" a program ... their jobs were more concerned with troubleshooting existing systems - and figuring out where the analog values were coming from - and what those values represented ... this "graphical" material helped a lot in meeting those needs ...

party on ...
 
Last edited:
Just remember before you use anything with the divide to error check so you don't divide by zero. That will fault all AB PLCs.
I know the AB SCP instruction does that fault checking first and also limits the outputs to the min/max scale values.
 
Just remember before you use anything with the divide to error check so you don't divide by zero. That will fault all AB PLCs.
I know the AB SCP instruction does that fault checking first and also limits the outputs to the min/max scale values.


Divide by Zero does NOT Major Fault A-B PLCs. It is a Minor Fault, flagged up as an Arithmetic Overflow.
 
This was done on FX3U and no matter what the values it does not fault.
For example if Zero in is a minus value and value in is either pos or neg it does not fault I have tried as many combinations as I could think of without faulting.
Indeed I think on an earlier version I checked for divide by zero and forced the calculation to be set so that if the difference was not equal to zero then it enabled the divide, however this was not required on the FX. but does apply to many other PLC's including the Q range.
 
Last edited:
It is unlikely that you would set the Zero & span in to produce 0 this would not make sense, however the picture shown only shows the math as a block of code, in any scaling block you would put limits in place to stop erroneous data entry.
 
For those that use RSLogix and are familiar with their SCP (Scale With Parameters) instruction, I've created one similarly to be used in Codesys or possibly some other platform that allows creation of your own functions and function blocks. I always appreciated the SCP instruction in RSLogix as it was so easy to use and there was no writing of code. Of course the math behind it is easy once you know the formula, but having to code that math every time you need to scale an analog signal is a pain. The SCP instruction in RSLogix made it easy as you just drop a SCP instruction in your code, pass it a few required parameters, and you're done. If you don't have a handy SCP instruction in the platform you're using and you have to code it in LD, then holy cow is that going to be cumbersome coding the math in LD every time you have to scale an analog!!

So, here is the SCP instruction from RSLogix, to be used in Codesys or possibly some other platform that allows creation of your own instruction blocks via functions or function blocks. Included are images for use of it in both a ST and LD program.

The code for the function "F_SCP". Included is an "Offset" if you ever may need it. Also, it checks for "divide by 0" and kicks out an error and error message if there is ever an instance of that.
1M49m_javkgfyb0Te2P2jSS9HwHw5d8A6vNEPqjeW_j17TSmBXelQT6mAthXXeZQd-gQ4fH8KV6hv_Z9xjhThlwxpXktmQ6hAxIFkDe9Q10EwLOwZqnNNSA2NJCcg4DpAoiL1gAMRo43l0Ti6s2uBeEYlOXrk5yGLcXbAgMj5gCcnNYs6CDfcDAE4dBG89cqYW7eEIAnzFACHT9Unn6VvrsWkik7SynpSczzeQ00d87wA4kLJPmAWWfiJvi9anhqzE7c-ll46zOIGk-E1eY7Ajh1fm4FiIqBE6ymioXSBfLb2XTYty5uI9YSDF-3_AkEO-eGJyP0y4gGbOGfaVQfCDutGH64Kc4p2QpxLF9jsOQF7tDn6uiJo9xBpDY_SS2WqbwuiJRMDfIvv4mc8kf6I2_92VZ9qaFOb1U2A5yyri1sV96MHuvw8G2roYbqZrG6awemqyikkbaUljD3twC54mip_i9_DL2QZjcczwxEq5NnkZlFt4ESTjXwVyqB9U9fP9mzFYaYXPQlJZKvdNxC-b6rvUN_6mcIV0gaNwhkMuiaiKT-Fty_kPed0_dkn9VMPTgBVMbgcReJXu9QMLouXp18troOS3Qg5FC5kplxvHPmy5fxhUfHkpqX_wh4LmauHUT65fOEdonsfdn-qcxaOfq00IZN7o8B0rLHfxssax-lfJ3wbzM2H5Xni0MvKQagbI0-H9CqsXBUoPLg4W2kO4W2szN5us4ufFRZvqtgvQIt2w=w1473-h859-no


Here is the function being used a couple different ways in a ST program:
3OO1XfdGxy8VWbQTVyq-Y59zkKzLVE61ycE56GMvlJjXlDk0yvSb7avHZJrnlki1VhrATIBDOSt82CMzgLwRYt8zeo68CRbZ4ojFuOt_XMy5eYV68H5Ybr6IwzZ-mZ8ehn_jgdVhk_wRRxOS3A09wewnylQG4_LWzdTTbomDBGX4vMMLf_LQp5KG--SYIyzaDtUHFrxEF05SvoLs8HyTH9L_GM1EIERxLgpVQUEbJPzhBd8dSKAa9wI4339VHtJfXT163XNvSE3GFaXi4GyMKRV4LZNVx78T4GGxHnUmSgq6mMbpskXYQcpC8JOek7rVWehOVajW92n6GXG-SbXrfB4LrPzw1uh48AFVSsC8aUt8fh6sRNfTCwaIaLUUdKh5WcxUITxwDFYSEFdTg9aIH_iIrEFG0r3_9iRHpIfZxUj1MHziQA869txP7GRubetMDdY3wNqATpKOy_fIxPjCVsxTuq8s9oSFUBe6akYhVszreCHPfCnSfvJLrdeMU89zP5YBPE2aygVmIg-JfzpzktVMTgGUihRrGD9pq-O_H5gIxrKmmCG3wGVqTCNHSJTsSfdj0uB5teu-dtTONYQBCdx1dcHSJ4cUvTZ2xosV3Rh3GB56fgiUffFjCE49T4vPTPnze3tucPVA0ihz8gPndKRsDxCfRxSPg0zRkWAXlT9JcmdoQ-Wy=w1476-h766-no


I included comments in the SCP function code so that when you begin to type it out in your ST program, it shows the useful description comments next to each "parameter":
bPPHYIX7z7p3RhIO-XgCW66a2_agCip3044TDB2L0hwiaKCXw2Kd_AsqLkZBm5lC6zlysgwm5mE1B6q8G0V-NykmsYpPzxbdOrnKcU1OVApY3T7DbDhwU9YiUSeEIttV2uzU3s_xV1f5eXkJKMoyQPrpaitv0Ww7dzjzL2t4o5qlk669nwG4S7lX9bshrVmgE_7OKUgCOPH5Z2nK7JgcIiuewe5J_hVBj8fm3YLHREL6wXpxG7zZjKFLJ4QLgCSlL2mN-dTolU1cMaNFOifq41T9FvdlbbeTbeR2yiuLkkNRWu5uwauOuDkDNrJw5qHIGaNIfqEkijplihVyQPc1POfN1j9CjPnsQ8evKyQfwRqUPi3SyWYb44sGK3rYMF2HvakTTUTiDW1H-hnWlV08D_EmEb2YP7iOyEQz15Cf-7CYm7JSWRQfUj0kiQpxw3ZCVILmNn8jCZ3EJWHjJ46iK8aiWC1cvh1NqcEwL71509_57862_AAiG3xHpDNiPoHozNYqIXiEogjrdliyUyp7HwiBtHHbH6gdrEs2_4zHv9htGp53CNv9g1FJkaxGaFmzES5dKfYWKNpP0AbN-VQC19SjifWMMmy7O_5xbYPId7YnKY3EQBlUNHk9Crzp-pJ4CdAgVF0xXP9vgvXr3iRVCfPj4gq1aBzROLsBkdzLUcWpnGaEcMW1=w1475-h764-no


Here is the same SCP function being used in a Ladder (LD) program:
FyD6P6ccjjRLaKpKxoIwXaObu5Ou46BWMY_b2kNUI8UdDs9-8HrfQt-jgJRucHytWqr-jpAGFrEWzNVFqve1-VZsrN7gvOrNDeCx1Zb4I9Mq1Ca2qH4fuwANaycch6rR5Pjns3HEMB87RTveHr23tXRKb9cq-PIa8tYLGNvo4kqFXvxylQoJhdUmK_U-wqMGweCAGUEC19JJvwo7-wke-h23aMQgYD-FiYlVKNWkItNAia4irMoySkpL3KRXmVMiTFMMl86SdcOXibsvQDgXosZjrQj35KVQJpkTgg0ahJoctlZp0qprgWhBab_B4QZmcj8nZusYPmAXM7w7NP9SGWQtBTkLgQXazcimLYeazARrTInK3k6gUuUTAue7N5B5hMUvte9qH4simKRYs6x6uiMvvG0ro7KTEzZXuC9rwoNfbKp8uSOVOt2zmhcyFSs15O3IAaadqhirMohQPvEp-KPXRRRRO7wbI25pA5MP9db98L7JNfDSJGPQlpAEnUFpg643uJnlVsPw8u_qD9SvdsV3HEBotT_O_qPc5M_gz-mhtREfiT046qUwxETrnO-IOFdY3oEtc9ud0mlbuL7O7_Jr5YwXz-sg3Q6yCAQlpgkBtOGPbDaA-iMr8zidGj53b06-1Uv95ArFxRSnXxk9usilnJTc5VpfRkqwAvexQJbljnpg7nBc=w1476-h748-no


It should be noted that a function returns a value. The SCP function here has a couple outputs besides the returned calculated value. So, technically, this "should" be written as a Function Block, not a function. With that said, it works fine as a function and I'm using it all over the place in two of our machines without issue.

Happy scaling with the SCP instruction!
 
Last edited:

Similar Topics

Hello guys I am just wondering do I need to set up each channel configuration, including enabling the channel , ranging … if I have already...
Replies
8
Views
1,977
Hi, In a Siemens, when you read an analogue input from a standard modul you read in the range 0-27648 INT value. What is the range in a TSX...
Replies
1
Views
1,638
Hi all you gurus, I've got a CPU942b which is reading an analogue input, and scaling it, but I'm getting odd results. In short, what the code...
Replies
5
Views
3,599
Hi, I have an application with a load cell where I need to log the load cell output, into a small/internal data logger. But I also want the...
Replies
3
Views
1,678
Hello there I am having trouble scaling my analogue input signals. If I use engineering units and use the RAW scale as 4000-20000 everything is...
Replies
10
Views
2,577
Back
Top Bottom