Operators

Lesson 32: Using * Multiplication Arithmetic Operator

Learn how * multiplies values, works with different data types, and how it differs from *= in Arduino code.

Progress indicator

Lesson 32 of 57

Learning Objectives

  • Understand what the multiplication operator (*) does.
  • Use * with variables, constants, and expressions.
  • Use multiplication with int and float data types.
  • Understand * vs *= and when each is useful.
  • Avoid overflow, precision loss, and common multiplication mistakes.

Concept Explanation

What is the Multiplication Operator (*)

The * operator multiplies two values and returns one result.

It is used in Arduino for scaling values, repeated timing calculations, and gain math.

Multiplication Operator Syntax

result = a * b;
scaled = sensor * 2;
total = value * count;

How * Works

  1. Read the left operand.
  2. Read the right operand.
  3. Multiply both operands.
  4. Store the result into a variable or use it immediately.

Multiplication result type depends on operand types. For example, int * float results in a float value.

Multiplying Variables and Constants

You can multiply variable * variable, variable * constant, or constant * variable.

int pwm = 50;
int scale = 3;
int output = pwm * scale;

* with Different Data Types

  • int * int gives int (if within range).
  • long * int usually gives long.
  • float * int gives float.
  • Large products can overflow if destination type is too small.
ExpressionOutput TypeExample Result
int * intint200 * 3 = 600
long * intlong5000L * 4 = 20000
float * floatfloat1.1 * 2.5 = 2.75

* vs *= (Comparison)

  • a = a * b; and a *= b; perform the same update.
  • *= is shorter when repeatedly scaling one variable.
  • Use * for fresh expressions, *= for in-place scaling.

When to Use *

  • Scaling sensor values
  • Converting units with a factor
  • Computing total time (singleCycle * cycleCount)
  • Applying gain/amplification values

Overflow in Multiplication

Multiplication can exceed type limits faster than addition/subtraction. Use larger types when multiplying bigger values.

int x = 20000;
int y = 3;
int z = x * y; // may overflow on many boards

Example Code

This sketch uses multiplication to compute blink timing, voltage gain, and total cycle time.

int ledPin = 2;
int baseDelay = 200;
int multiplier = 3;
int blinkDelay = 0;
int cycleCount = 4;
int totalBlinkTime = 0;
float voltage = 1.1;
float gain = 2.5;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  blinkDelay = baseDelay * multiplier;
  float amplifiedVoltage = voltage * gain;
  totalBlinkTime = blinkDelay * cycleCount;

  digitalWrite(ledPin, HIGH);
  delay(blinkDelay);
  digitalWrite(ledPin, LOW);
  delay(blinkDelay);

  Serial.print("blinkDelay = ");
  Serial.println(blinkDelay);
  Serial.print("amplifiedVoltage = ");
  Serial.println(amplifiedVoltage);
  Serial.print("totalBlinkTime = ");
  Serial.println(totalBlinkTime);
}

Example Code Explanation

  1. baseDelay and multiplier define one scaled delay value.
  2. blinkDelay = baseDelay * multiplier sets delay from 200 to 600 ms.
  3. voltage * gain demonstrates float multiplication with decimal precision.
  4. totalBlinkTime = blinkDelay * cycleCount calculates total cycle duration.
  5. LED uses the multiplied delay for visible ON/OFF timing.
  6. Serial prints confirm multiplied values for easy debugging.

Real-life analogy

Think of bus fare: if one ride costs 20 and you ride 5 times, total cost is 20 * 5. Multiplication scales one unit by how many times it repeats.

What Happens Inside

  1. Compiler emits multiplication instructions for operands.
  2. CPU reads both operands into registers.
  3. Multiply unit computes product in binary.
  4. Result is written to destination variable memory.
  5. Overflow can occur if result is larger than variable range.
  6. Next lines execute using updated multiplied values.

Common Mistakes with *

  • Using small integer types and getting overflow in large products.
  • Expecting decimal precision while storing result in int.
  • Confusing * with pointer syntax from C/C++ contexts.
  • Writing x =* 2 instead of x *= 2.
  • Using very large constants without checking final range.

Best Practices for *

  • Choose data types that safely hold multiplied results.
  • Use *= for repeated scaling updates.
  • Use explicit float values (like 2.0) when decimal precision matters.
  • Print intermediate results in Serial while debugging.
  • Use long/unsigned long when products can grow quickly.

Practice Task

  1. Change multiplier from 3 to 4 and observe blinkDelay.
  2. Add int warmupCycles = 2; then compute int warmupTime = blinkDelay * warmupCycles;.
  3. Create float adjustedVoltage = amplifiedVoltage * 0.9; and print it.
  4. Replace one multiplication with *= and compare readability.

Try it now

Open the simulator workspace and inspect multiplied values line by line.

Run in Simulator