Menu
CoddyTech

Scaling to Percent

Part of the Analog and Sequences section of Coddy's PLC Programming journey. Lesson 4 of 17.

Raw numbers are hard to read: nobody knows what 19660 means at a glance. Scaling turns the raw value into units people use, here percent:

Percent = Level × 100 / 32767

Multiply first, then divide. When PLC integers divide, they drop the fraction, so Level / 32767 is 0 until the tank is full, and 0 × 100 is still 0.

The product needs room. At full, Level × 100 is 3,276,700, and an INT holds at most 32767. It would overflow: wrap around to a wrong number while the PLC keeps running. A DINT holds up to about 2 billion. A math box works in the type of its inputs, so first MOVE Level into a DINT tag, then multiply and divide in DINT.

This rung has no contact, so it runs every scan and Percent always follows the level.

challenge icon

Challenge

Medium
  • create three DINT tags: Raw, Scaled and Percent
  • add a rung: MOVE Level into Raw, MUL Raw by 100 into Scaled, DIV Scaled by 32767 into Percent
  • fill to 60 %: in the fill rung, compare Percent instead of Level

Try it yourself

{"v":1,"lang":"ld","tags":[],"rungs":[{"els":[{"t":"par","branches":[[{"t":"no","tag":"Start"}],[{"t":"no","tag":"Fill"}]]},{"t":"no","tag":"Stop"},{"t":"cmp","op":"LT","a":"Level","b":16384},{"t":"coil","tag":"Fill"}]}]}

All lessons in Analog and Sequences

Practice on your own: PLC playground