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
- Read the left operand.
- Read the right operand.
- Multiply both operands.
- 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 * intgives int (if within range).long * intusually gives long.float * intgives float.- Large products can overflow if destination type is too small.
| Expression | Output Type | Example Result |
|---|---|---|
int * int | int | 200 * 3 = 600 |
long * int | long | 5000L * 4 = 20000 |
float * float | float | 1.1 * 2.5 = 2.75 |
* vs *= (Comparison)
a = a * b;anda *= 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 boardsExample 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
baseDelayandmultiplierdefine one scaled delay value.blinkDelay = baseDelay * multipliersets delay from 200 to 600 ms.voltage * gaindemonstrates float multiplication with decimal precision.totalBlinkTime = blinkDelay * cycleCountcalculates total cycle duration.- LED uses the multiplied delay for visible ON/OFF timing.
- 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
- Compiler emits multiplication instructions for operands.
- CPU reads both operands into registers.
- Multiply unit computes product in binary.
- Result is written to destination variable memory.
- Overflow can occur if result is larger than variable range.
- 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 =* 2instead ofx *= 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
- Change
multiplierfrom 3 to 4 and observeblinkDelay. - Add
int warmupCycles = 2;then computeint warmupTime = blinkDelay * warmupCycles;. - Create
float adjustedVoltage = amplifiedVoltage * 0.9;and print it. - Replace one multiplication with
*=and compare readability.
Try it now
Open the simulator workspace and inspect multiplied values line by line.