pcbbc: #define is a pre-processor directive, essentially a sort of macro. Personally I would suggest beginners steer clear of it. There’s nothing you can do with #define that you can’t do by writing the code out longhand (or by using const int/long/whatever). That last sentence is not accurate. … Answer from bperrybap on forum.arduino.cc
🌐
Arduino
docs.arduino.cc › language-reference › en › variables › variable-scope-qualifiers › const
const | Arduino Documentation
November 12, 2024 - const keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable "read-only". This means that the variable can be used just as any other variable of its type, but its value cannot be changed. You will get a compiler error if you try ...
🌐
Delasign
delasign.com › blog › arduino-system-constants
How to create Constants that can be used across an Arduino project
September 28, 2023 - Create a new header and CPP file and define the constants. To use the constants, include the header file in your script and implement the constant. ... The code for the following tutorial is found on Github through our Open Source Arduino starter project.
Discussions

When to use const int, int, or #define
What is the main difference between these when naming a variable? Is what I am about to say correct? 'const int' will mainly be used when needing to declare a specific value/target to a variable? Such why I would for assigning to my Arduino UNO. 'int' is manly used as a counter, but my main ... More on forum.arduino.cc
🌐 forum.arduino.cc
13
0
July 30, 2020
What is the difference between #define and const int when defining pin names in Arduino IDE?
#define asks the preprocessor to do a literal text-wise copy paste into the code before the compiler even sees it. #defined values have no type, which prevents the compiler from throwing warnings for various code issues. const int theoretically consumes RAM, however compiler optimizations will often annul any RAM usage if the variable is only used in one file and you never take its address (ie int* pointer = &mypin). const int variables are strongly typed, so the compiler can warn you if you do something weird with it. Sometimes the distinction between these is quite important, but in the context of pin designations in a typical Arduino project there's little to no practical difference. More on reddit.com
🌐 r/arduino
37
21
November 29, 2022
Predefined Constants Reference
I'm new to Arduino, and am learning by a sort of osmosis that there are many useful constants pre-defined in the Arduino core. However I have not been able to find a comprehensive listing or reference to them. If I've missed it I'll be very happy for a pointer! More on forum.arduino.cc
🌐 forum.arduino.cc
6
0
January 21, 2013
arduino uno - How do I set a constant in setup() that I can use in loop()? - Arduino Stack Exchange
I am new with coding, and I was wondering how I could run something once and define it as a constant. More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
November 19, 2015
🌐
Arduino Forum
forum.arduino.cc › projects › programming
When to use const int, int, or #define - Programming - Arduino Forum
July 30, 2020 - What is the main difference between these when naming a variable? Is what I am about to say correct? 'const int' will mainly be used when needing to declare a specific value/target to a variable? Such why I would for assigning to my Arduino UNO. 'int' is manly used as a counter, but my main issue is what is the max value for this variable?
🌐
Arduino Getting Started
arduinogetstarted.com › reference › arduino-const
const constant | Arduino Getting Started
3 weeks ago - It is a variable qualifier that modifies the behavior of the variable, making a variable "read-only". This means that the variable can be used just as any other variable of its type, but its value cannot be changed.
🌐
Educative
educative.io › answers › what-are-variables-data-types-and-constants-in-arduino
What are variables, data types, and constants in Arduino?
Constants are like variables, but their value remains constant throughout the program's execution. They are used to represent fixed values that should not be altered during the program's runtime.
Top answer
1 of 6
59
#define asks the preprocessor to do a literal text-wise copy paste into the code before the compiler even sees it. #defined values have no type, which prevents the compiler from throwing warnings for various code issues. const int theoretically consumes RAM, however compiler optimizations will often annul any RAM usage if the variable is only used in one file and you never take its address (ie int* pointer = &mypin). const int variables are strongly typed, so the compiler can warn you if you do something weird with it. Sometimes the distinction between these is quite important, but in the context of pin designations in a typical Arduino project there's little to no practical difference.
2 of 6
21
The other answers cover this fairly well. So this is a more of a "did you know?" type add-on... #defines are actually macros, not just constant definitions. So they are actually quite different to const - but they can also do something similar to const. A macro can be #define'd to accept parameters and the preprocessor (the step in the compilation that deals with all of the # directives) will process them accordingly. For example, did you know that the max "function" (return the larger of two numbers) is defined as: #define max(a,b) ((a)>(b)?(a):(b)) Reference: line 93 in Arduino.h . You will note a whole bunch of other functions declared similarly (e.g. min and abs) So, why would you do that as compared to defining an actual C function for it? Well, as others have mentioned macros (i.e. #defines) aren't typed. That means, that the one declaration works for all possible types and all possible combinations of types and will "do the right thing" for them - including generate a compilation error further down the track (should you do something silly when invoking it). If you were to define max as a function then you would need at least the following following functions: int max(int a, int b) {... double max(int a, double b) { ... double max(double a, int b) {... double max(double a, double b) {... To cater for all of the possible alternatives of comparing just integers and doubles. Additional declarations might be needed for other variants - e.g. for finding the largest char of a pair of chars - what about a char and an int? etc: char max(char a, char b) {... char /* or int? */ max(char a, int b) {... // and so on The function body in each and every one of those functions that I listed above would be exactly the same - specifically: return a > b ? a : b; } i.e. the body of each C function would basically be the same as the macro definition, just without (the critically important) brackets around each of the variables in the macro expression (i.e. (a) > (b) ... as shown in the #define variant). So hopefully you can see that with a single macro (define) definition, I have saved myself from defining all the possible datatype combinations of finding a max - let alone adding on all the code to find all the possible datatype combinations for min, abs... And that is just one way the preprocessor can be used to make life a little easier for us all. As for why do people use const or #define for simple constants since they basically do the same thing? Some other answers might include how old you are and/or who taught you. Back in the dark ages, there was no const keyword. There was only #define. But as others mentioned there is value to using const for the reasons mentioned and more. So, the various committees that define the C language and its evolution agreed to introduce const. So "old timers" might use #define - because "if it aint broke don't mess with it", and that's what they tell the newbies to use. Whereas others might prefer const - because it does have some benefits, can be used in different ways and probably should be used as "best practice" and that is what they have been told to use. IMHO. Hope this makes sense and gives a bit more insight.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › arduino › arduino_variables_and_constants.htm
Arduino - Variables & Constants
Before we start explaining the variable types, a very important subject we need to make sure, you fully understand is called the variable scope. Variables in C programming language, which Arduino uses, have a property called scope.
🌐
Puriphico
puriphico.com › post › constants-variables-in-arduino-programming-part-1
constants: variables in arduino programming (part 1)
June 9, 2021 - This article details the types of constants in Arduino programming • Overview • They are predefined expressions in the Arduino language, used to make the sketch easier to read.
🌐
Arduino Getting Started
arduinogetstarted.com › reference › arduino-constants
constants | Arduino Getting Started
March 13, 2026 - INPUT, INPUT_PULLUP, OUTPUT, HIGH, LOW, LED_BUILTIN, true, false. How to use constants with Arduino. Learn constants example code, reference, definition. Constants are predefined expressions in the Arduino language. What is Arduino constants.
🌐
Arduino Forum
forum.arduino.cc › community › suggestions for the arduino project
Predefined Constants Reference - Suggestions for the Arduino Project - Arduino Forum
January 21, 2013 - I'm new to Arduino, and am learning by a sort of osmosis that there are many useful constants pre-defined in the Arduino core. However I have not been able to find a comprehensive listing or reference to them. If I've mi…
🌐
Mikro blog
mikroblog.net › home › arduino › arduino tutorials › tutorial: arduino variables and constants
Tutorial: Arduino Variables and Constants - Mikro blog -
April 13, 2025 - Constants: Fixed values that do not change throughout the program’s execution. Used to store whole numbers. Range: -32,768 to 32,767 (16-bit on Arduino).
Top answer
1 of 2
6

You can't, and there's other things wrong with your code.

int sensorValue1 = analogRead(A0);  
int sensorValue2 = analogRead(A1);  

Calling functions from the global scope is an incredibly bad idea. Those are run before the board has been properly initialized, and the results you may get from it are not guaranteed in any way, shape, or form. Instead you must call functions like analogRead() from within another function, like setup(), or loop().

As for const - a const is just that - a constant value that can never change. You can only assign a value to it at the moment of creation.

And that means that you can only use it within the scope it was defined within. As you can't do that in the global scope (see above) then you cannot use a constant.

So just use a normal global variable. It will only change when you tell it to change. Using a const only makes sense when you have a numeric literal that you want to refer to by name. Using const doesn't protect the variable from being changed at run-time by memory leaks, etc, it just protects it from attempts to modify it at compile time - and if possible replace it with the numeric literal that it refers to.

So in summary just use:

int sensorValue1;
int sensorValue2;
int p1;
int t1;

void setup() {
    sensorValue1 = analogRead(A0);
    sensorValue2 = analogRead(A1);
    p1 = (1.09512*sensorValue2-22.105)*133.3225;
    t1 = (-0.08126*sensorValue1+65.6597)*274.15;
}
... etc ...

HOWEVER: Your program methodology is also completely flawed. You read the analog values once, and do calculations with them once, then repeatedly display the same values over and over again.

You must perform a new analogRead() of your sensors every iteration of loop() in order to update the values in your variables.

2 of 2
1

You can define Constants using the "const" keyword. Ex:

    const float pi = 3.14;
    const int foo = 12;

https://www.arduino.cc/en/Reference/Const

🌐
Compile N Run
compilenrun.com › arduino tutorial › arduino basics › arduino constants
Arduino Constants | Compile N Run
Constants are fixed values that don't change during the execution of your Arduino program. They play a crucial role in making your code more readable, maintainable, and efficient.
🌐
YouTube
youtube.com › watch
Arduino Tutorials: How to Use Constants In Arduino! - YouTube
In Arduino tutorials and Arduino projects, we frequently use constants alongside variables.Constant variables, as the name suggests, are variables whose valu...
Published   April 9, 2024
🌐
Arduino
docs.arduino.cc › language-reference › en › variables › constants › highLow
HIGH | LOW | Arduino Documentation
May 15, 2024 - Floating Point ConstantsHIGH | LOWINPUT | INPUT_PULLUP | OUTPUTInteger ConstantsLED_BUILTINtrue | false
🌐
GitHub
github.com › arduino › reference-en › issues › 956
Document the ARDUINO_* constants · Issue #956 · arduino/reference-en
December 15, 2023 - The official Arduino cores define a set of constants that can be used to write more portable sketches and code by identifying the architecture and board being used. We have architecture-specific constants such as: ARDUINO_ARCH_AVR ARDUIN...
Author   arduino
🌐
Moreware Blog
moreware.org › home › arduino ide: variables, constants and macros #2
Arduino IDE: variables, constants and macros #2 - Moreware Blog
May 25, 2020 - The use of constants ensures that the value assigned before the execution of the code is kept unchanged throughout the execution of the program, avoiding unwanted changes.
Top answer
1 of 4
25

It's important to note that const int does not behave identically in C and in C++, so in fact several of the objections against it that have been alluded to in the original question and in Peter Bloomfields's extensive answer are not valid:

  • In C++, const int constants are compile time values and can be used to set array limits, as case labels, etc.
  • const int constants do not necessarily occupy any storage. Unless you take their address or declare them extern, they will generally just have a compile time existence.

However, for integer constants, it might often be preferable to use a (named or anonymous) enum. I often like this because:

  • It's backward compatible with C.
  • It's nearly as type safe as const int (every bit as type safe in C++11).
  • It provides a natural way of grouping related constants.
  • You can even use them for some amount of namespace control.

So in an idiomatic C++ program, there is no reason whatsoever to use #define to define an integer constant. Even if you want to remain C compatible (because of technical requirements, because you're kickin' it old school, or because people you work with prefer it that way), you can still use enum and should do so, rather than use #define.

2 of 4
7

EDIT: microtherion gives an excellent answer which corrects some of my points here, particularly about memory usage.


As you've identified, there are certain situations where you're forced to use a #define, because the compiler won't allow a const variable. Similarly, in some situations you're forced to use variables, such as when you need an array of values (i.e. you can't have an array of #define).

However, there are many other situations where there isn't necessarily a single 'correct' answer. Here are some guidelines which I would follow:

Type safety
From a general programming point-of-view, const variables are usually preferable (where possible). The main reason for that is type-safety.

A #define (preprocessor macro) directly copies the literal value into each location in code, making every usage independent. This can hypothetically result in ambiguities, because the type may end up being resolved differently depending on how/where it's used.

A const variable is only ever one type, which is determined by its declaration, and resolved during initialisation. It will often require an explicit cast before it will behave differently (although there are various situations where it can safely be implicitly type-promoted). At the very least, the compiler can (if configured correctly) emit a more reliable warning when a type issue occurs.

A possible workaround for this is to include an explicit cast or a type-suffix within a #define. For example:

#define THE_ANSWER (int8_t)42
#define NOT_QUITE_PI 3.14f

That approach can potentially cause syntax problems in some cases though, depending on how it's used.

Memory usage
Unlike general purpose computing, memory is obviously at a premium when dealing with something like an Arduino. Using a const variable vs. a #define can affect where the data is stored in memory, which may force you to use one or the other.

  • const variables will (usually) be stored in SRAM, along with all other variables.
  • Literal values used in #define will often be stored in program space (Flash memory), alongside the sketch itself.

(Note that there are various things which can affect exactly how and where something is stored, such as compiler configuration and optimisation.)

SRAM and Flash have different limitations (e.g. 2 KB and 32 KB respectively for the Uno). For some applications, it's quite easy to run out of SRAM, so it can be helpful to shift some things into Flash. The reverse is also possible, although probably less common.

PROGMEM
It's possible to get the benefits of type-safety while also storing the data in program space (Flash). This is done using the PROGMEM keyword. It doesn't work for all types, but it's commonly used for arrays of integers or strings.

The general form given in the documentation is as follows:

dataType variableName[] PROGMEM = {dataInt0, dataInt1, dataInt3...}; 

String tables are a bit more complicated, but the documentation has full details.