Data types and variables

We have already used variables several times in the previous two lessons, even though we have not yet examined them in detail.

Arduino for beginners · Lesson 3

Difficulty: Beginner

Created: February 2021

Technically reviewed: August 2026

Reference platform: Arduino Uno family / general Arduino API

Prerequisites: Lesson 2 — Digital and analogue inputs and outputs

Variables allow us to give names to the data used in a program. This means that we do not need to write numbers or other values directly everywhere, and it also makes the program’s operation easier to understand.

const int buttonPin = 2;
int buttonState = digitalRead(buttonPin);

In this lesson, we will learn:

  • what a variable is;
  • what a data type is;
  • how to create and modify variables;
  • what const is used for;
  • what the scope of a variable means;
  • which basic data types we encounter in Arduino programs;
  • why the size of a type may differ between Arduino boards.

Required equipment

  • Arduino board;
  • USB data cable;
  • computer running Arduino IDE 2.

The examples use the board’s built-in LED, so no additional electronic components are required.

What is a variable?

A variable is named data that a program can store and use. If the variable is not const, its value can be changed while the program is running.

For example:

int blinkDelay = 1000;

In this line:

  • int is the data type;
  • blinkDelay is the variable name;
  • 1000 is its initial value.

Later, we can refer to the stored value by the variable’s name:

delay(blinkDelay);

This is easier to read than writing the number 1000 directly at many different points in the program.

The declaration int blinkDelay = 1000; divided into its data type, variable name and initial value, together with the relationship between blinkDelay and the stored value 1000.
The three basic parts of a variable: its data type, name and initial value.

What is a data type?

A data type determines:

  • the kind of data being stored;
  • which operations can be performed on it;
  • which values it can represent;
  • how much memory it uses on the particular platform.

Different types may be suitable for storing, for example:

  • a true/false state;
  • a character;
  • an integer;
  • a non-negative integer;
  • a decimal fraction.

Choosing the appropriate type helps make a program understandable and reliable.

Declaration, initialisation and assignment

These three concepts are similar, but they are not the same.

Declaration

When declaring a variable, we specify its type and name:

int blinkDelay;

This creates the blinkDelay variable, but we have not yet given it an initial value of our choosing.

Do not use a local variable until it has been given a value.

Initialisation

During initialisation, we give the variable an initial value when it is created:

int blinkDelay = 1000;

For beginners, it is preferable to use this form because it clearly shows the value with which the variable starts.

Assignment

We can change the value of an existing variable later:

blinkDelay = 250;

Here, we do not write the int type again because we are not creating a new variable. We are changing the value of the existing blinkDelay variable.

Variables in a familiar program

We can make the Blink program easier to read by using variables:

const int ledPin = LED_BUILTIN;
int blinkDelay = 1000;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  digitalWrite(ledPin, HIGH);
  delay(blinkDelay);

  digitalWrite(ledPin, LOW);
  delay(blinkDelay);
}

ledPin indicates the pin on which the LED is located. blinkDelay stores the delay time in milliseconds.

If we want the LED to blink faster, we only need to change one value:

int blinkDelay = 250;

Variable names

A good variable name briefly indicates what the variable stores.

Good examples:

int blinkDelay = 1000;
int sensorValue = 0;
bool buttonPressed = false;

Less descriptive names:

int x = 1000;
int a = 0;
bool b = false;

Short names may be appropriate in small calculations, but it is generally worth choosing descriptive names for the physical components and states in an Arduino program.

A variable name:

  • may contain letters, numbers and underscores;
  • cannot begin with a number;
  • cannot be a reserved C++ keyword;
  • is case-sensitive.

Therefore, the following two names are not the same:

sensorValue
SensorValue

camelCase notation is common in Arduino examples:

buttonState
potentiometerValue
previousMillis

const: when a value must not change

If we do not want to change the value associated with a name while the program is running, we can indicate this with the const keyword:

const int buttonPin = 2;

This is particularly useful for pin numbers:

const int ledPin = LED_BUILTIN;
const int buttonPin = 2;
const int potentiometerPin = A0;

The advantages of using const are that it:

  • makes our intention clear;
  • prevents accidental modification;
  • provides a C++ name with a type and scope;
  • allows the compiler to help detect incorrect assignments.

The following line would therefore cause a compilation error:

const int buttonPin = 2;

buttonPin = 3;

This is a good thing: the compiler indicates that we are trying to change a value that we declared constant.

const does not mean that every number must be stored as a constant. We use it when we genuinely do not intend to change the value associated with the given name.

Basic data types

bool

A bool can store two logical values:

true
false

For example:

bool ledIsOn = false;
bool buttonPressed = true;

It can be used to represent states such as:

  • on or off;
  • true or false;
  • succeeded or failed;
  • pressed or not pressed.

In older Arduino code, we may also encounter the name boolean. This is an Arduino compatibility alias, but the standard C++ bool is recommended in new code.

The actual memory size of a bool is implementation-dependent. In a beginner program, its meaning is generally more important than its exact size.

char

A char can store a single character:

char command = 'A';
char separator = ',';

A character is written between single quotation marks:

'A'

The following, however, is no longer a single character:

"Arduino"

This is a character string delimited by double quotation marks. We will cover character strings in detail in Lesson 9.

Although char can also be used as a numeric type, whether a plain char is signed or unsigned may depend on the platform. If we specifically want to handle numeric byte data, it is worth selecting a type that clearly expresses this.

byte and unsigned char

Arduino’s byte type can store an 8-bit unsigned value:

byte brightness = 200;

Its typical range is:

0–255

unsigned char is a standard C++ type and, on common Arduino platforms, is also an 8-bit unsigned value:

unsigned char rawValue = 200;

byte can be easy to read when we are genuinely handling one byte of data. unsigned char, however, is a standard C++ type. Neither needs to be selected automatically instead of the other in every situation.

In more advanced, portable code, we will often encounter the exact-width type uint8_t:

uint8_t brightness = 200;

We do not yet need to use it regularly in this lesson.

int

int is one of the most frequently used integer types:

int sensorValue = 0;
int blinkDelay = 1000;
int temperature = -5;

Important: the size of an int is not the same on every Arduino board.

  • On the Uno R3 and other classic AVR boards, it is typically 16 bits.
  • On the Uno R4 and many modern 32-bit boards, it is typically 32 bits.

This means that the same int variable can handle a larger range of values on a modern board. Therefore, we do not learn as a general rule that an int is always between −32,768 and 32,767.

For simple pin numbers, counters and measurement results, int is often a convenient choice, provided that the value definitely fits within the range of the particular platform.

unsigned int

An unsigned int stores only non-negative integers:

unsigned int eventCount = 0;

Because it does not need to represent negative values, it can represent a larger positive range than a signed int using the same amount of storage.

Like the size of int, its exact range is platform-dependent:

  • 16-bit unsigned int: 0–65,535;
  • 32-bit unsigned int: 0–4,294,967,295.

Do not use an unsigned type merely because a value is not expected to be negative. Calculations and possible conversions must also be considered when selecting a type. We will examine these in more detail later.

short

short is a signed integer type:

short smallNumber = 100;

C++ guarantees a size of at least 16 bits. In the current Uno R3 and Uno R4 environments, it is typically 16 bits.

It is less frequently needed in beginner Arduino programs. If we are storing a general integer value, int is usually the simpler choice.

long

long can store larger signed integer values:

long distance = 300000L;

The L suffix indicates that we want to treat the integer literal as a long:

300000L

In the Uno R3 and Uno R4 environments, long is typically 32 bits, although the C++ language generally guarantees only that it is at least 32 bits.

unsigned long

An unsigned long stores larger non-negative integer values:

unsigned long elapsedTime = 0;

We often encounter it for time values in Arduino programs:

unsigned long previousMillis = 0;

This is because the return type of millis() is unsigned long.

We will learn about non-blocking timing based on millis() in a later lesson. For now, it is enough to remember that unsigned long is an important Arduino type for timestamps.

If we want to specify a numeric constant explicitly as an unsigned long, we can use the UL suffix:

const unsigned long interval = 1000UL;

word

We may encounter word in older Arduino examples:

word value = 25000;

This is a type name provided by Arduino, not a separate standard C++ fundamental type.

It is not worth learning as a general rule that word is always 16 bits. Its meaning may depend on the type definition in the particular Arduino core.

If a program requires an unsigned integer that is exactly 16 bits wide, the following type may be clearer:

uint16_t value = 25000;

We can examine fixed-width types in more detail later.

float

A float stores floating-point numbers:

float voltage = 3.3;
float temperature = 21.75;

Floating-point types have finite precision. Many decimal fractions cannot be represented in them with complete accuracy.

For example, the mathematical result of a calculation might be 0.3, while the computer’s internal representation may produce a value that differs from this by a very small amount.

Therefore, when comparing measured values, we often do not check for exact equality. Instead, we check whether the difference between the two values is sufficiently small. A detailed discussion of this belongs in later material.

In the Uno R3 and Uno R4 environments, float is typically 32 bits, with approximately 6–7 significant decimal digits.

double

double is also a floating-point type:

double measurement = 68.665;

Its size and precision are platform-dependent.

  • In the classic AVR-based Uno R3 environment, double has traditionally been the same size as float.
  • On the Uno R4 and in many newer 32-bit environments, double is typically a higher-precision, 64-bit type.

Therefore, we cannot assume that double provides greater precision than float on every Arduino board.

In beginner programs, float is generally sufficient for simple measured values. For high-precision calculations, however, the properties of the particular platform must also be checked.

Checking the size of a type

The sizeof operator shows how many bytes of storage a type occupies in the current compilation environment.

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

  Serial.print("int: ");
  Serial.println(sizeof(int));

  Serial.print("long: ");
  Serial.println(sizeof(long));

  Serial.print("float: ");
  Serial.println(sizeof(float));

  Serial.print("double: ");
  Serial.println(sizeof(double));
}

void loop()
{
}

The result shown in the Serial Monitor applies to the currently selected board and its compilation environment.

The result of sizeof is expressed in bytes. On common Arduino platforms, one byte is 8 bits, but in this beginner lesson we primarily use it to compare the results from the Uno R3 and Uno R4.

Local variables and scope

Scope determines the part of the program from which a name can be accessed.

A variable created inside a function or code block is a local variable:

void loop()
{
  int sensorValue = analogRead(A0);

  Serial.println(sensorValue);
}

sensorValue can only be accessed from inside the loop() function.

This would not work:

void loop()
{
  int sensorValue = analogRead(A0);
}

void printValue()
{
  Serial.println(sensorValue);  // Not accessible here
}

The advantage of a local variable is that its name exists only where it is actually needed. This reduces the likelihood of accidentally modifying the wrong variable in another part of the program.

Global variables

A variable created outside the functions is a global variable:

int blinkDelay = 1000;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println(blinkDelay);
}

After its declaration, it can be accessed from multiple functions.

Using a global variable may be appropriate, for example, when:

  • multiple functions need to manage the same state;
  • a value must persist between repetitions of loop();
  • we are storing a shared hardware setting.

Even so, it is not advisable to make every variable global automatically. If data is needed only inside one function, it is usually better to create it as a local variable.

The aim is not to avoid global variables completely, but to use the narrowest scope that remains practical.

Integer overflow

Every integer type has a finite range of values. If a number does not fit in the selected type, overflow may occur.

For example, a 16-bit int cannot store 100,000:

int value = 100000;

This may produce an incorrect result on the Uno R3, while the value may still fit on a board that uses a 32-bit int.

This is a good example of why it is not enough to say that “an int has this or that range on Arduino”.

If we need a larger value, select a suitable type:

long value = 100000L;

Overflow of unsigned types wraps around in a defined way. Portable program logic, however, must not rely on signed integer overflow.

Select a type that can safely hold the smallest and largest values expected.

Simple type conversion

When we place a floating-point value in an integer variable, its fractional part is discarded:

float measuredValue = 2.9;
int wholeValue = measuredValue;

In this example, the value of wholeValue will be 2. This is not mathematical rounding; the fractional part is simply discarded.

If we want to round the value:

float measuredValue = 2.9;
int roundedValue = round(measuredValue);

The value of roundedValue will then be 3.

Automatic conversions between different types have additional rules and risks. We will examine these in more detail when we cover operators and calculations.

What about text and arrays?

Arduino programs can also store text and sequences containing multiple elements, but we will not explore them in this lesson.

Later, we will cover the following separately:

  • arrays — Lesson 8;
  • C-style null-terminated character strings — Lesson 9;
  • Arduino String objects — Lesson 10.

For now, it is enough to distinguish between a single character and text:

char letter = 'A';              // A single character
const char text[] = "Arduino";  // Character string

We will examine how the second line works in detail in later lessons.

🧪 Try it!

1. Change the blinking speed

In the familiar Blink program, change:

int blinkDelay = 1000;

for example, to:

int blinkDelay = 250;

Observe how the blinking changes.

2. Use separate on and off times

Create two variables:

int onTime = 200;
int offTime = 800;

Use the value of onTime for the delay after switching the LED on and the value of offTime for the delay after switching it off.

3. Mark values that do not change with const

Examine these two lines:

int ledPin = LED_BUILTIN;
int blinkDelay = 500;

We do not intend to change the value of ledPin, but we may want to change the value of blinkDelay later.

Modify the declarations accordingly.

4. Compare the type sizes

Compile and upload the sizeof example:

  1. with an Uno R3 board selected;
  2. with an Uno R4 board selected, if available.

Write down the sizes of int, long, float and double. For which types can you see a difference?

The program can be compiled with an installed board package even if both boards are not currently connected. Running it and obtaining results in the Serial Monitor naturally requires the selected hardware.

Summary

In this lesson, we learned that:

  • a variable is data accessible by name;
  • a data type determines the nature and representation of the data;
  • declaration, initialisation and assignment are not the same;
  • const prevents the value associated with a name from being changed later;
  • bool, char, byte, int, long, unsigned long, float and double serve different purposes;
  • the actual size of a data type may depend on the board and compiler;
  • the sizes of int and double may differ between the Uno R3 and Uno R4;
  • a local variable can only be accessed within its own scope;
  • we should use a global variable when multiple parts of the program genuinely need it;
  • the selected type must safely cover the expected range of values.