Variables & Constants

Lesson 8 - Understanding float Data Type

Learn how float stores decimal numbers in Arduino/ESP32 projects and when to use it for sensor values, calculations, and measurements.

Progress indicator

Lesson 8 of 8

Learning Objectives

  • Understand that float stores decimal (fractional) numbers.
  • Learn float syntax, declaration, initialization, range, and memory size basics.
  • Compare float and double in beginner-friendly language.
  • Use float values for measurements, percentages, and sensor calculations.
  • Avoid common mistakes when working with decimal values in Arduino code.
  • Understand that float values are approximate and may not store every decimal exactly.

Concept Explanation

What is float

float is a data type used to store decimal numbers (numbers with a fractional part), such as 3.14, 0.5, or 27.5.

It is commonly used for sensor readings, voltages, temperatures, and percentage calculations.

If your value includes a decimal point and you care about the fractional part, float is usually a better choice than int.

float Syntax

Basic syntax:

float variableName;
float temperature;
float voltage = 3.3;
float ratio = 0.75;

Beginners often use float for sensor values first because many real-world measurements are not whole numbers.

Declaring float Variables

Declaring means creating a variable and choosing float as the type so it can store decimal values.

float humidity;
float sensorVoltage;

Initializing float

Initializing means giving the variable a starting decimal value.

float tempC = 26.5;
float threshold = 0.75;

Adding .0 (like 100.0) can make your decimal math intention clearer for beginners.

Beginner tip

Writing 100.0 instead of 100 helps signal that you want decimal math, not just whole-number thinking.

float Range

float can store a wide range of decimal values (very small and very large), but with limited precision. That means it may not store every decimal exactly.

For example, a calculation may print a value like 0.299999 instead of exactly 0.3. This is normal for floating-point numbers.

float Memory Size

In many Arduino/ESP32 examples, float commonly uses 4 bytes of memory. This gives decimal support with limited precision.

This is usually fine for beginner projects, but it is good to remember that decimal support costs more memory than small integer types.

Why float Stores Decimal Numbers

Unlike int, which stores whole numbers only, float is designed to store values with fractional parts. This is why it is useful for measurements like 27.5 C or 3.30 V.

Internally, the value is stored in a floating-point format, which allows a decimal-like representation but with limited exact precision.

When to Use float

  • Temperature or humidity values with decimals
  • Voltage/current measurements
  • Ratios, percentages, and scaling calculations
  • Math where whole numbers are not enough

If the value is always a whole number (like a pin number or simple counter), use int instead to keep the code simpler.

Real-Life Example

A room temperature sensor may report values like 26.8 C. A battery monitor may read 3.72 V. These are natural examples for float because they include decimal parts.

float vs double (Comparison)

Featurefloatdouble
Main useDecimal values (common beginner use)Higher precision decimal values (platform-dependent)
Memory/precisionUsually less than or equal to doubleMay have more precision (depends on board)
Beginner choiceVery commonLearn after float basics

For beginner Arduino/ESP32 lessons, float is usually enough for basic sensor and measurement examples.

Beginner note

On some boards, double may behave very similarly to float. On others, it may provide more precision. Always check board/platform documentation when precision matters.

Example Code

This example stores decimal measurements and performs simple float calculations.

float voltage = 3.30;
float temperatureC = 27.5;
float offset = 0.5;

void setup() {
  Serial.begin(115200);

  float adjustedTemp = temperatureC + offset;
  float voltagePercent = (voltage / 3.30) * 100.0;

  Serial.println("Float values calculated");
}

void loop() {
  // Lesson focus is decimal values in setup()
}

What this example teaches

  • How to declare and initialize float variables
  • How to add decimal values with float
  • How to compute a percentage using decimal math
  • Why decimal values are useful in measurement-based projects

Example Code Explanation

  1. float voltage = 3.30; stores a decimal voltage value.
  2. float temperatureC = 27.5; stores a decimal temperature.
  3. float offset = 0.5; stores a decimal correction value.
  4. float adjustedTemp = temperatureC + offset; adds decimal values.
  5. float voltagePercent = (voltage / 3.30) * 100.0; uses decimal math to calculate a percentage.
  6. Serial.println(...) is used to print a message after the calculations are complete.

Beginner reading tip

When reading float code, first identify: measured values (like voltage), correction values (like offset), and calculated values (like adjustedTemp or voltagePercent).

Real-life example

A battery monitor may read a voltage like 3.72 V and calculate a battery percentage using float values.

Common Mistakes with float

  • Using int when a value needs decimals (losing the fractional part).
  • Expecting every decimal result to be exact (float precision is limited).
  • Forgetting to use decimal values (like 100.0) in calculations when needed.
  • Using too many floats in memory-limited projects without planning.
  • Comparing float values with exact equality in advanced logic without tolerance checks.

Best Practices for float

  • Use float for measurements and values with decimals.
  • Use clear names like temperatureC, voltage, and humidityPercent.
  • Remember that float values are approximate, not always perfectly exact.
  • For simple whole-number counters and pin numbers, use int instead of float.
  • Keep formulas readable by using intermediate float variables like adjustedTemp.

Practice Task

  1. Change temperatureC to 30.2 and explain how adjustedTemp changes.
  2. Change offset from 0.5 to -0.3 and explain what a negative correction means.
  3. Rename voltagePercent to batteryPercentEstimate for clearer meaning.
  4. Explain why float is a better choice than int for voltage values like 3.30.

Try it now

Open the simulator workspace and observe variables while stepping through examples.

Run in Simulator