Variables & Constants

Lesson 4 - Understanding unsigned int Data Type

Learn how unsigned int stores only non-negative whole numbers and why it is useful for counters, sizes, and values that never go below zero.

Progress indicator

Lesson 4 of 4

Learning Objectives

  • Understand that unsigned int stores only whole numbers that are 0 or greater.
  • Learn unsigned int syntax, declaration, initialization, range, and memory size basics.
  • Compare int vs unsigned int and signed vs unsigned values in beginner-friendly language.
  • Understand overflow (wrap-around) and common mistakes with unsigned counters.
  • Read a simple Arduino example that increases a counter each loop.
  • Apply best practices when using unsigned int in ESP32/Arduino projects.

Concept Explanation

What is unsigned int

unsigned int is a whole-number data type that stores only non-negative values. That means it can store 0, 1, 50, and other positive values, but not negative values like -1.

In embedded systems, this is useful when a value naturally counts upward, such as button presses, sensor sample counts, menu indexes, or how many times an LED has blinked.

unsigned int Syntax

The basic syntax is:

unsigned int variableName;

You can also declare and assign a value in one line, or declare multiple unsigned int variables for different non-negative values.

unsigned int counter;
unsigned int sampleCount = 10;
unsigned int ledDelayMs = 300;

Declaring unsigned int Variables

Declaring means creating a variable name and choosing its data type. Example:

unsigned int totalBlinks;
unsigned int ledIndex;

Initializing unsigned int

Initializing means giving the variable its first value. Beginners often start counters at 0.

unsigned int totalBlinks = 0;
unsigned int samples = 10;

If you do not initialize a local variable, it may start with an unpredictable value. For beginners, it is a best practice to initialize variables clearly every time.

unsigned int Range

unsigned int stores values from 0 up to a maximum positive value (platform-dependent). On many classic Arduino boards (16-bit unsigned int) the range is 0 to 65535. On ESP32, unsigned int is commonly 32-bit, so the range is much larger.

Beginner rule: always check the board/platform documentation if exact limits matter.

unsigned int Memory Size

Memory size can vary by board/platform. Many Arduino examples teach unsigned int as 2 bytes on AVR boards, while ESP32 often uses 4 bytes for unsigned int.

Key idea: more bytes usually means a larger range of values. Memory size and range are connected.

When to Use unsigned int

unsigned int totalBlinks = 0; is a good choice because blink count increases and should not be negative. It is also useful for indexes, sizes, and counts.

  • Counter values (blinks, button presses, samples)
  • Array indexes (when you know index cannot be negative)
  • Timing values in milliseconds (small ranges)

Why unsigned int Cannot Store Negative Values

Because all available bits are used for non-negative values, there is no sign (minus) representation in unsigned int. That is why it cannot represent values like -1.

If you try to force a negative idea into an unsigned variable (for example subtracting below 0), the result may wrap to a large positive number instead of showing a negative value.

Real-Life Example

Think of a visitor counter at a shop entrance. It counts 0, 1, 2, 3... and should not become negative. That is a good match for unsigned int.

Signed vs Unsigned Numbers

A signed number can be positive or negative. An unsigned number can only be zero or positive.

Signed (example: int)

Can store values like -5, 0, 20.

Unsigned (example: unsigned int)

Can store values like 0, 1, 100, but not -1.

Real-life idea: temperature can be signed (it may go below zero in some places), but a people counter at a door is usually unsigned because you count from 0 upward.

unsigned int vs int (Comparison)

Featureintunsigned int
Negative valuesYesNo
CountersCan workGreat when never below zero
Risk when subtractingCan go negative normallyMay wrap around unexpectedly

Choose int when the value may go below zero. Choose unsigned int when the value is truly non-negative and you want a clearer meaning for the variable.

Code Example

This example uses unsigned int to track how many blink cycles have completed.

unsigned int ledIndex = 0;
unsigned int totalBlinks = 0;

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

void loop() {
  digitalWrite(2, HIGH);
  delay(300);
  digitalWrite(2, LOW);
  delay(300);

  totalBlinks = totalBlinks + 1;
  Serial.println("Blink count updated");
}

Practical Example Explanation (Line by Line)

  1. unsigned int ledIndex = 0; creates a non-negative variable. (This line is included to show unsigned syntax clearly.)
  2. unsigned int totalBlinks = 0; creates the main blink counter. It starts at 0 because no blink cycle has completed yet.
  3. Serial.begin(115200); starts serial output for debugging so we can monitor what the program is doing.
  4. pinMode(2, OUTPUT); prepares the LED pin as output.
  5. digitalWrite(2, HIGH); turns the LED on.
  6. delay(300); waits so the LED on-state is visible.
  7. digitalWrite(2, LOW); turns the LED off.
  8. totalBlinks = totalBlinks + 1; increases the counter after a full blink cycle. This is the main line that demonstrates unsigned int as a counter.
  9. Serial.println(...); prints a message so you can observe the program flow.

Real-life example

Imagine a factory machine counting how many bottles passed a sensor. Every completed bottle increases the count by 1, just like totalBlinks increases after each blink cycle.

Overflow and Wrap-Around

If an unsigned int keeps increasing and reaches its maximum value, the next increase does not become negative. Instead, it wraps back to a small value (often 0). This is called wrap-around.

unsigned int x = 65535;
x = x + 1;  // Wraps to 0 on boards where unsigned int is 16-bit

Why this matters

If you are counting events for a long time, wrap-around may cause confusing results unless you plan for it.

Common Mistakes with unsigned int

  • Using unsigned int for values that may need to go negative later.
  • Subtracting from an unsigned int without checking if it is already 0.
  • Choosing a data type before thinking about the real range of values.
  • Assuming unsigned int has the same size on every board.

Best Practices for unsigned int

  • Use it for counters, sizes, and indexes that should never be negative.
  • Initialize variables clearly (for example, start counters at 0).
  • Be careful with subtraction and check boundaries before decreasing.
  • Choose descriptive names like blinkCounter or sampleCount.
  • Add comments when wrap-around could affect program logic (timers, long-running counters).

Practice Task

  1. Rename totalBlinks to blinkCounter.
  2. Add another unsigned int variable to count how many times the LED turns ON only.
  3. Explain why unsigned int is a better choice than int for this counter example.

Try it now

Open the simulator workspace and practice changing counter values and delays while reading the variable memory panel.

Run in Simulator