volatile

Description

volatile
is a keyword known as a variable qualifier, it is usually used before the datatype of a variable, to modify the way in which the compiler and subsequent program treat the variable.

Declaring a variable

volatile
is a directive to the compiler. The compiler is software which translates your C/C++ code into the machine code, which are the real instructions for the Atmega chip in the Arduino board.

Specifically, it directs the compiler to load the variable from RAM and not from a storage register, which is a temporary memory location where program variables are stored and manipulated. Under certain conditions, the value for a variable stored in registers can be inaccurate.

A variable should be declared

volatile
whenever its value can be changed by something beyond the control of the code section in which it appears, such as a concurrently executing thread. In the Arduino board, the only place that this is likely to occur is in sections of code associated with interrupts, called an interrupt service routine.

int or long volatiles

If the

volatile
variable is bigger than a byte (e.g. a 16 bit int or a 32 bit long), then the microcontroller can not read it in one step, because it is an 8 bit microcontroller. This means that while your main code section (e.g. your loop) reads the first 8 bits of the variable, the interrupt might already change the second 8 bits. This will produce random values for the variable.

Remedy: While the variable is read, interrupts need to be disabled, so they can’t mess with the bits, while they are read. There are several ways to do this:

  1. noInterrupts
  2. Use the ATOMIC_BLOCK macro. Atomic operations are single MCU operations - the smallest possible unit.

Example Code

The

volatile
modifier ensures that changes to the
changed
variable are immediately visible in
loop()
. Without the
volatile
modifier, the
changed
variable may be loaded into a register when entering the function and would not be updated anymore until the function ends.

1// Flashes the LED for 1 s if the input has changed
2 // in the previous second.
3
4 volatile byte changed = 0;
5
6 void setup() {
7 pinMode(LED_BUILTIN, OUTPUT);
8 attachInterrupt(digitalPinToInterrupt(2), toggle, CHANGE);
9 }
10
11 void loop() {
12 if (changed == 1) {
13 // toggle() has been called from interrupts!
14
15 // Reset changed to 0
16 changed = 0;
17
18 // Blink LED for 200 ms
19 digitalWrite(LED_BUILTIN, HIGH);
20 delay(200);
21 digitalWrite(LED_BUILTIN, LOW);
22 }
23 }
24
25 void toggle() {
26 changed = 1;
27 }

To access a variable with size greater than the microcontroller’s 8-bit data bus, use the

ATOMIC_BLOCK
macro. The macro ensures that the variable is read in an atomic operation, i.e. its contents cannot be altered while it is being read.

1#include <util/atomic.h> // this library includes the ATOMIC_BLOCK macro.
2 volatile int input_from_interrupt;
3
4 // Somewhere in the code, e.g. inside loop()
5 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
6 // code with interrupts blocked (consecutive atomic operations will not get interrupted)
7 int result = input_from_interrupt;
8 }

See also

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.