Conversion

Lesson 62: Using float() Conversion

Learn how float() conversion creates decimal-capable values from integers and expressions for accurate sensor and math operations.

Progress indicator

Lesson 62 of 62

Learning Objectives

  • Understand what float() conversion does in Arduino.
  • Learn float() syntax and why casting changes math behavior.
  • Differentiate float() conversion from declaring a float variable.
  • Use float() in division and scaling calculations for sensor values.
  • Avoid common precision and mixed-type mistakes with float conversions.
  • Apply float() safely in real voltage and normalization workflows.

Concept Explanation

What is float() Conversion

float() converts a value into float type so decimal parts can be preserved.

This is important when integer math would otherwise cut off fractions.

float() Syntax

float result = float(value);
float ratio = float(a) / float(b);

How float() Works

  1. Evaluate source value or expression term.
  2. Convert it to float representation.
  3. Subsequent math can keep decimal precision.
  4. Result is stored in float variable.

Important: conversion order matters. Casting before division keeps decimal results, but casting after integer division cannot recover lost fractional part.

Type Casting Concept

Casting forces a value into a chosen type explicitly.

int a = 3;
int b = 2;
float x = float(a) / float(b); // 1.5

Without casting, a / b would be integer division and become 1.

Correct pattern: cast at least one operand before division. Safer for beginners: cast both.

float() vs float Variable

  • float value; declares a variable type.
  • float(expr) converts expression result to float.
  • Combined use: float ratio = float(a) / float(b);

Left side defines storage type, right side controls how expression is evaluated.

When to Use float()

  • Sensor normalization (0.0 to 1.0 range).
  • Voltage/current/temperature formulas with decimals.
  • Any division where fractional result matters.
  • Calibration formulas where small decimal changes affect output behavior.

Example Code

This sketch uses float() conversion to keep ADC normalization and voltage math accurate.

const int LED_PIN = 2;

int adcRaw = 2048;
int maxAdc = 4095;
int resistorTop = 10000;
int resistorBottom = 2200;

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

void loop() {
  float normalized = float(adcRaw) / float(maxAdc);
  float voltage = normalized * 3.3;
  float dividerRatio = float(resistorTop + resistorBottom) / float(resistorBottom);
  float estimatedInputVoltage = voltage * dividerRatio;

  if (voltage > 1.65) {
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }

  Serial.print("normalized=");
  Serial.print(normalized, 4);
  Serial.print(", voltage=");
  Serial.print(voltage, 3);
  Serial.print(", estimatedInputVoltage=");
  Serial.println(estimatedInputVoltage, 3);

  delay(900);
}

Example Code Explanation

  1. float(adcRaw) / float(maxAdc) keeps fractional normalized value.
  2. voltage multiplies normalized value by 3.3V reference.
  3. dividerRatio is computed using float conversion to avoid truncation.
  4. estimatedInputVoltage scales measured voltage through divider ratio.
  5. LED threshold uses decimal voltage check for cleaner control.
  6. Serial prints with decimal precision to verify conversion behavior.
  7. This workflow mirrors real embedded measurement pipelines: raw ADC -> normalize -> physical unit.

What Happens Inside

  1. Input integers are converted to float for math safety.
  2. Division runs in floating-point mode, preserving decimal part.
  3. Decimal results propagate through later calculations.
  4. Converted values drive both LED logic and Serial output.
Key idea: cast before division when you need fractional results.
ExpressionResultWhy
3 / 21Integer division truncates fraction.
float(3) / 21.5One operand is float, expression becomes float math.
float(3 / 2)1.0Division already truncated before cast.

Common Mistakes with float()

  • Casting after integer division (too late to recover decimals).
  • Mixing int and float math without checking conversion order.
  • Assuming float results are exact for every decimal value.
  • Printing too few decimals and misreading calculation accuracy.
  • Casting the final result only (float(a / b)) instead of operands.

Best Practices for float()

  • Cast operands before division when fractions matter.
  • Keep critical formulas consistently float-based.
  • Use Serial precision (e.g., 3-4 decimals) during calibration.
  • Document expected numeric range and tolerance in comments/docs.
  • Cross-check key formulas with known manual examples during debugging.

Try it now

Open the simulator workspace and inspect float() conversion math step-by-step.

Run in Simulator