PLC Programming Tutorial - Ladder Logic #3: Math and Calculations | Siemens TIA Portal Master PLC math functions and calculate and compute instructions in this hands-on ladder logic tutorial using Siemens TIA Portal. This is Part 3 of my PLC programming series, building on the binary and Boolean fundamentals from Part 1, and Timers and Counters from Part 2.
What you'll learn: Addition (ADD), Subtraction (SUB), Multiplication (MUL), and Division (DIV) Calculate/Compute Instructions (CPT) Modulus (MOD) - integer remainders from division
These instructions are essential building blocks in industrial automation—you'll use them in nearly every PLC program you write. Perfect for beginners learning PLC programming, electricians moving into controls, or experienced techs brushing up on Siemens ladder logic fundamentals. Watch The Full Playlist Here! ▸ https://www.youtube.com/watch?v=hlfSkoxqIxE&list=PLsFyHm8kJsx2R5ABmofwE_aKHvobT9GeE Resources PLC used in this tutorial: https://amzn.to/4p90T8d Support the channel: patreon.com/LeMasterTech
Timestamps 00:00 Introduction 01:10 Addition (ADD), Subtraction (SUB), Division (DIV), and Multiply (MUL) 04:27 Modulus (MOD) PLC Instruction! 06:27 Celsius To Fahrenheit Converter PLC Function 08:47 Calculate (CPT) PLC Instruction Function Block! 12:22 Outro
Hello and welcome back to the Master
Tech. And in today's video, we'll be
continuing our series on the basics of
PLC programming in latter logic
programming language. Today, we'll focus
on basic math operations, including
multiplication, division, addition,
subtraction, and then we'll talk about
the calculate block, which combines many
of these operations together. If you
like the content in this video and
you've been finding these tutorials
helpful, please consider leaving a like
on the video and subscribing to the
channel. It helps me out a ton. Now,
let's get into this tutorial. Okay, so
for today's video, I've just gone ahead
and made some empty integer tags in my
PLC. So, we're going to use integers. We
don't need reals or floats, but the math
that we're doing in today's video, you
could make these uh floats or decimal
values, and they will still work. We're
just going to use integers today to keep
it simple. So, let's go into our ladder
tutorials binary uh block. It doesn't
matter if you still have some of the
binary code from past videos in there.
Today we're going to start from scratch.
Concepts covered in these videos are
applicable to generally every PLC
programming uh platform in the world.
Ladder diagram. These are standard
blocks. Um so today we're going to talk
about when you hit an empty block.
Pretty much every PLC programming
software has some version of all the
basic mathematical operations. They have
an add block. Uh they have a subtract
block. They have a a division block and
a multiplication block. And a cool thing
about TIA, uh most platforms have some
version of this. But if you have a
block, it's very easy to change in
between one and the other. Uh to make
this a little bit more readable, I'll
put add and subtract up top and I'll put
multiply and divide in the bottom. And
just to talk about the uh sort of the
variables that you're seeing on these
blocks, um you're seeing input one and
input two and then an output. The
easiest thing we can do to showcase
these working is use one variable like
my int zero and uh put a hard-coded
value in input two and then uh put a new
integer in the output here and say my
int one. So what this should do is say 1
+ 2 goes into the output. Now let's go
ahead and set all of these up uh at once
because this isn't really the super
interesting part. I'm going to just sort
of jump ahead to where I have my values
added into each block so you don't have
to watch it. Okay. And you don't have to
use the same uh actually it's probably
better if I don't. You don't have to use
the same constant for every block. Just
to be clear, this is just me entering a
number. And you could enter another
variable in here, too. If you're doing
something dynamic scaling or checking
states of sequence steps or anything
else like that, this could also be a
variable. And you see addition and
multiplication are ones where they're
also okay with you adding multiple
operands. So obviously to do addition
you have to add at least two things
together. Same with multiplication. Um
but you could do a long string of
multiple values addition and
multiplication on these blocks. It isn't
there for subtraction and division
because the order matters and they can't
necessarily tell if you want to divide
this by this by this by this or subtract
this by this by this by this in what
order. Um, but addition and
multiplication you can. And in just a
minute, we'll talk about a more
complicated block that does let you
combine uh all of these functions. But
let's go ahead and download to the PLC
just to showcase these four simple
mathematical blocks [snorts]
and we'll go ahead and write some values
uh to these integers and we'll see what
happens to the outputs. Okay. So here we
see my int zero currently set to zero.
If I change it to 50, you see my int uh
one is just 50 + 10, it's 60. If I
change this to 75, uh my int one is 85.
So the output is simply the addition of
1 + 2. Um a little bit different over
here. If you look at the subtract block,
it's 1 minus 2. A little bit different,
but not really. Um and if you do 20 - 8,
it's 12. Okay? So input 1 minus input 2
goes into input 3. Multiplication 32 * 6
is 195. Pretty straightforward. Um you
know 5 * 6 should be 30. So there you
go. Division 0 / 4 0. Uh let's make it
something more interesting than zero
like 26. And you see something
interesting now with TIA portal. If
you're dividing an integer by an integer
and putting it in an integer value, it
will still allow that, but it's going to
give you the nearest integer value. And
so 26 / 4 is obviously 6.5. And if we
went to 27, you're getting 6.75, but you
see it's doing floor division. It's
rounding down. Um, there's one block I
wasn't necessarily going to cover
because it's a little less common here.
Um, but the modulus block is sort of the
fifth math block. And if we took this
input six and we still put four down
here and we put it in a new integer
value, my int 8, let's say, and we
divide or uh download. There we go. You
can see that if you're using integer
values, because booleans or uh sorry,
floats, decimal values use up more
memory. Sometimes you do just want to
use an integer and know what the
remainder is. That's what the mod block
is. Modulus is the remainder of a
division system. So 27 / 4 where you
want the integer value will round down
to six and so will 26 and so will 25.
But what's changing is the remainder. So
you can get the remainder separately
using this modulus block. Okay. So
there's addition, subtraction,
multiplication, division, and uh
modulus. All right. So that's a lot of
simple math pretty fast. one maybe it's
useful maybe not to you. One useful
thing to call out is you can actually
put the same uh input on the left side
and the right side of a math block. So
if you're doing some sort of
transformation on one line of a value
like scaling, you can say like oh okay
the integer that I currently have + 10
and then store it in the same variable
name. There's really no good reason to
do that, but technically it's still
valid code. So, let's look at a real
world scenario really quick where we
combine a couple of these uh to create a
Celsius to Fahrenheit or Fahrenheit to
Celsius converter, right? You can take
you know your initial value times 9 and
you can uh store that in my int one and
then you can take my int one as the
output from there and store it as the
input from two and then say divide by
five put that in my int 2 and then you
can add my int 2 uh to 32. Oops. I
didn't copy it. There we go. And uh that
obviously you can store in my int 3. So
let's go ahead and download that to the
PLC. I'm sure all those intermediates
had values, but they'll get overwritten.
Oh, this is Celsius to Fahrenheit. 75
Celsius is quite hot. 30 Celsius uh is
86 Fahrenheit. Okay, that math checks
out. So we built a Celsius to Fahrenheit
converter. I uh put it in my notes
backwards. Um, let's actually go ahead
real quick and do the same thing. Let's
do Fahrenheit to Celsius. See it in
reverse.
Okay. And I went ahead and spared you
the pain of having to watch me type it
all out. But here we can see Fahrenheit
to Celsius is the same formula but in
reverse. It's taking, you know, some
number, subtracting 32 out, and then uh
multiplying by five, dividing by 10. So
in this case, rather than 30, let's just
see it in reverse. If it's 86 degrees
Fahrenheit, it's 30 Celsius. This makes
sense because we're seeing the same
formula in reverse. So, let's see what
our Fahrenheit to Celsius converter
says. If it's a brisk autumn day and
it's 60°, that's 15 Celsius. And uh you
know, the math should check out because
if we go and make this 15, we should get
okay, 59. But that's actually an
interesting one because you're seeing
the two different types of rounding
going on by using exclusively uh integer
values. because if these were all reals,
they would obviously be to the decimal
precision the exact same number. Um, but
that's actually cool. You're seeing a
symptom of using integers for your math.
But this is still a functioning Celsius
to Fahrenheit and Fahrenheit to Celsius
converter. However, you may be looking
at this and saying, "This looks kind of
stupid. I need six integers to be able
to do this math." um you know I have to
output to an integer value that then
goes and becomes the input of another
block on every one of these blocks and
no you don't that's actually where a far
more useful block for doing multi-step
uh calculations like this comes in and
in seammens it's called calculate in
Allen Bradley Rockwell it's called uh
compute so it you type a CPT in pretty
much every Alen Bradley instruction has
like a threeletter or four-letter um
abbreviation and CPT is the version of
this that you use in Alen Bradley. I
actually think the compute block in
Rockwell is a little bit more intuitive
than the calculate block in Seammens,
but they do functionally the same thing.
And what's cool about this is I can take
that same input, my int zero, which is
going to be our what was this one?
Celsius input and then I can store it to
a new output that'll be our secondary
check. So my int 4 if we do this
calculation right should be the same as
my int3 because we're going to attempt
to multiply by 9, divide by five and add
32 all in this one block. So, we're
going to try to multiply by 9, divide by
five, and then add 32 all to my int zero
by tap doubleclicking this output or
whatever your PLC IDE version of editing
this is. And the output, we reference
those input variables in defining our
output function. So, we just come in
here and we say input 1 times input 2.
So multiply by 9 / input 3 / 5 and then
plus input 4. So add 32. Okay. And uh
I'm quite confident this is going to
work. But let's go ahead and do uh the
reverse version of it too. So you can uh
we can just get both of them knocked out
at the same time. So we'll use that my
int five as the input. And what's the
next unused one? We'll use my int 9 as
the output for this one. And then the
only thing different here is we want to
subtract by 32, multiply by 5, and
divide by 9. I'll just keep them in that
order we want to use them here. And this
function is going to be
a little different. It's going to be
input one minus input 2. Fortunately,
uh, compute blocks and and calculate
blocks respect PEMDAZ. It uh, respects
the order of operations to put it more
clearly. So, we're going to use those
parentheses to say do the subtraction
first and then multiply by input three
and divide by input four. Uh, that was
There we go. I put a space in the wrong
spot. Okay. So, this is saying, you
know, the input minus 32 * 5ide by 9.
Okay. So, if we did our math correctly,
and I'm something of a math wiz. Let's
go ahead and download. So we did uh the
kind of step show each step using each
block uh one at a time and we have 15
converts up to 59 and then we have 60
converts down to 15. We see the same
thing happening here. Um so if we say
Celsius 22°
71° F well let's say if we have 72° F it
converts down to 22 degrees C. Um, so
75, 23, uh, yeah, I guess that saw that
coming. 19 would convert up to 66. Okay,
so this is a pretty solid basic
introduction to math and simple, uh,
mathematical operations in ladder
programming for semens. And so, uh,
we'll probably do a advanced math video
at some point in the future, but those
come up far less frequently than the
ones we covered today. So, um, hopefully
you found this useful. Uh hopefully
you've been enjoying the series. If
there's a specific concept you want to
see covered in the video uh in a video,
be sure to let me know about in the
comments below. As always, thank you for
watching. Uh don't forget to like the
video, subscribe to the channel, and see
you next time. Thanks. Bye.