Sub topic 3
Variables and Data Types
Assume you can open, verify, and upload an Arduino sketch, and that you understand pins, voltage, and LEDs. This chapter focuses on how a sketch stores values and how the selected data type affects those values.
What a variable is
A variable is a named storage location in memory. Its value can change while the program runs. A declaration gives the variable a type and a name:
int sensorValue;This declares sensorValue as an int. You can assign a value later:
sensorValue = analogRead(A0);You can also initialize a variable when you declare it:
int sensorValue = 512;Initialization is usually safer because the variable starts with a known value. An uninitialized local variable can contain an unpredictable value.
Common Arduino data types
A data type determines which values a variable can represent and how much memory it uses.
| Type | Typical Uno size | Values or purpose |
|---|---|---|
| `bool` | 1 byte | `true` or `false` |
| `byte` | 1 byte | 0 through 255 |
| `int` | 2 bytes | -32,768 through 32,767 |
| `unsigned int` | 2 bytes | 0 through 65,535 |
| `long` | 4 bytes | Larger signed whole numbers |
| `float` | 4 bytes | Numbers with a fractional part |
| `char` | 1 byte | One character, such as `'A'` |
The exact size of some types depends on the board. An int is 16 bits on an Arduino Uno but commonly 32 bits on larger boards. When the range matters across boards, use a clearly sized type such as int32_t or uint32_t.
Use unsigned only when negative values are impossible. For example, elapsed milliseconds cannot be negative, so Arduino programs often use unsigned long.
Choosing a type
Use a whole-number type for counts, pin numbers, and raw readings. Use float when a fractional result is important.
const byte sensorPin = A0;
const byte sampleCount = 10;
void setup() {
Serial.begin(9600);
}
void loop() {
int rawValue = analogRead(sensorPin);
float voltage = rawValue * (5.0 / 1023.0);
Serial.print("Raw value: ");
Serial.println(rawValue);
Serial.print("Voltage: ");
Serial.println(voltage, 2);
delay(1000);
}Here, sensorPin and sampleCount need only small nonnegative values, so byte is suitable. rawValue is an integer because analogRead() returns a whole-number reading. voltage uses float because the calculated voltage can include a fraction.
Conversion and overflow
When an expression combines different types, Arduino C++ converts values to a compatible type. You can also request a conversion explicitly:
int total = 7;
int count = 2;
float average = (float)total / count;Without the conversion, integer division produces 3, not 3.5.
Overflow occurs when a result is outside a type's range:
byte count = 255;
count++;After this increment, count wraps around to 0. Choose a wider type when a value may exceed its range.
Constants and readable names
A constant is a named value that should not change. Use const for pin numbers, limits, and calibration values:
const byte ledPin = LED_BUILTIN;
const int threshold = 700;The compiler reports an error if code tries to assign a new value to either constant. Constants make code easier to change because the value has one clearly named definition.
Exercise
What type and declaration would you choose for a value that stores the number of button presses and may reach 50,000 on an Arduino Uno?
Show answer
Use unsigned int presses = 0; because the value is never negative and an Uno unsigned int can store values through 65,535.