Sub topic 5
Constants with `const` and `#define`
Assume you can open, verify, and upload an Arduino sketch, and that you understand variables, data types, and basic digital input and output. This chapter focuses on values that should not change while a program runs.
Why use constants?
A constant is a named value that your program must not change. Constants make code easier to read and safer to update.
Compare these two lines:
digitalWrite(13, HIGH);
delay(500);The numbers do not explain themselves. A named constant gives each value a purpose:
const byte ledPin = 13;
const unsigned long blinkTime = 500;If the LED moves to pin 8, you change one declaration instead of searching through the whole sketch.
`const`: a typed constant
Use const before a variable declaration to prevent later assignment. The declaration includes a data type, so the compiler can check how the value is used.
const byte ledPin = LED_BUILTIN;
const unsigned long blinkTime = 500;
const int sensorLimit = 700;Here, byte is suitable for a small pin number, unsigned long is suitable for time values returned by millis(), and int is suitable for a typical analog reading.
The compiler rejects an attempt to change one of these values:
const byte ledPin = 13;
// ledPin = 8; // Error: a const value cannot be assignedA const value still has a type and follows normal C++ scope rules. This makes it the usual choice for new Arduino code.
`#define`: a preprocessor macro
#define is handled before the compiler sees the program. It replaces every matching name with its text.
#define SENSOR_LIMIT 700
#define LED_PIN LED_BUILTINThe preprocessor changes this:
if (reading > SENSOR_LIMIT) {
digitalWrite(LED_PIN, HIGH);
}Into code equivalent to this:
if (reading > 700) {
digitalWrite(LED_BUILTIN, HIGH);
}A macro is not a variable. It has no data type, does not follow normal scope rules, and cannot be inspected like an ordinary variable. It also does not use an equals sign or a semicolon.
| Feature | `const` | `#define` |
|---|---|---|
| Has a data type | Yes | No |
| Checked by the compiler | Yes | No |
| Follows scope rules | Yes | No |
| Can be used for text replacement | No | Yes |
| Best default for numeric values | Yes | Usually no |
A complete example
This sketch uses const for pin and timing values. It also includes a #define to show a common legacy style.
#define SENSOR_LIMIT 700
const byte ledPin = LED_BUILTIN;
const byte sensorPin = A0;
const unsigned long reportInterval = 500;
unsigned long previousReport = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int reading = analogRead(sensorPin);
if (reading > SENSOR_LIMIT) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
unsigned long now = millis();
if (now - previousReport >= reportInterval) {
previousReport = now;
Serial.print("Reading: ");
Serial.println(reading);
}
}The const declarations document the type and purpose of each value. SENSOR_LIMIT works, but const int sensorLimit = 700; would usually be clearer in a new sketch.
Common errors and choices
Do not put a semicolon after a #define:
#define SENSOR_LIMIT 700This is wrong:
#define SENSOR_LIMIT 700;The semicolon becomes part of the replacement text and can create confusing compiler errors.
Use const for pins, limits, delays, and configuration values. Use #define when you specifically need preprocessor behavior, such as conditional compilation:
#define DEBUG_MODE
#ifdef DEBUG_MODE
Serial.println("Debugging is enabled");
#endifA macro can also be undefined with #undef, while a const value cannot be changed or removed during compilation.
Exercise
Which declaration is the better default for an analog threshold of 650, and why: #define LIMIT 650 or const int limit = 650;?
Show answer
const int limit = 650; is the better default because it has a type, follows scope rules, and is checked by the compiler.