Variables & Constants

Lesson 11 - Understanding word Data Type

Learn how Arduino word stores non-negative 16-bit values, how it compares with unsigned int, and when it is useful for compact positive data.

Progress indicator

Lesson 11 of 11

Learning Objectives

  • Understand that word stores non-negative whole numbers only.
  • Learn word syntax, declaration, initialization, range, and memory size basics.
  • Compare word with unsigned int in beginner-friendly language.
  • Recognize overflow and wrap-around behavior in word counters.
  • Use word for compact positive values when the range is known and safe.

Concept Explanation

What is word

word is an Arduino type used for non-negative whole numbers. It is commonly treated as a 16-bit unsigned value (0 to 65535).

You can think of word as a beginner-friendly name for a small positive integer type often used for counts, readings, and compact values.

word Syntax

Basic syntax:

word variableName;
word reading;
word pwmValue = 255;
word sampleCount = 1200;

Declaring word Variables

Declaring means creating a variable and choosing word as the type so it can store non-negative integer values.

word pulseCount;
word sensorReading;

Initializing word

Initializing means giving the variable a starting value when it is created.

word count = 0;
word maxValue = 4095;

Because word is non-negative, do not use it for values that may go below zero.

word Range

A common beginner model for word is 0 to 65535 (16-bit unsigned range).

This makes it useful for positive counters and readings that fit inside that range.

word Memory Size

word typically uses 2 bytes (16 bits) in Arduino-style examples.

This can help save memory compared with larger integer types, but only if the range is safe.

Why word Stores Only Positive Values

word is an unsigned type, so all of its bits are used for non-negative values. It has no sign (minus) representation.

When to Use word

  • Sensor readings that are always non-negative
  • Small/medium counters that never go below zero
  • PWM-related or mapped values in positive ranges
  • Compact positive values where 16-bit range is enough

word vs unsigned int (Comparison)

Featurewordunsigned int
Signed?No (unsigned)No (unsigned)
Arduino readabilityCommon Arduino alias styleC/C++ style explicit type
Range/sizeOften 16-bit (Arduino-style)Platform-dependent

In many Arduino lessons, word is used as a readable unsigned 16-bit-style value. unsigned int may vary more by platform.

Example Code

This example uses word for positive readings and counters.

word sensorValue = 1023;
word maxSensorValue = 4095;
word sampleCount = 0;

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

  word scaledValue = sensorValue / 4;
  sampleCount = sampleCount + 1;

  Serial.println("word values updated");
}

void loop() {
  // Lesson focus is word variables in setup()
}

Example Code Explanation

  1. word sensorValue = 1023; stores a positive reading.
  2. word maxSensorValue = 4095; stores a positive maximum value.
  3. word sampleCount = 0; creates a non-negative counter.
  4. word scaledValue = sensorValue / 4; calculates a smaller positive value.
  5. sampleCount = sampleCount + 1; increases the counter.
  6. Serial.println(...) prints a message after the values update.

Real-life example

A simple sensor sampling routine may count samples and store scaled values that are always non-negative.

Overflow and Wrap-Around

If a word value grows beyond its maximum, it can wrap around to a low value (often 0) because it is unsigned.

word count = 65535;
count = count + 1;  // Wraps to 0

This behavior is normal for unsigned types and must be considered in counter logic.

Common Mistakes with word

  • Using word for values that may need to go negative.
  • Ignoring wrap-around in long-running counters.
  • Assuming word always matches unsigned int behavior on every platform without checking.
  • Choosing word before checking if the maximum value can exceed 65535.

Best Practices for word

  • Use word only for non-negative values that fit safely in its range.
  • Use descriptive names like sensorValue, sampleCount, and scaledValue.
  • Plan for overflow if a counter may run for a long time.
  • Check board documentation when exact type size/range matters.

Try it now

Open the simulator workspace and observe variable updates while stepping through examples.

Run in Simulator