Variable Block

The Variable block lets you read or write a Variable in your program. You can also create a new Variable and name it.

A Variable is a location in the memory of the EV3 Brick that can store a data value. You can write to a Variable to store a data value. Later in the program, you can read from the Variable to access the stored value.
Each variable has a Type and a Name. The different Types are Numeric, Logic, Text, Numeric Array, and Logic Array. You can choose the Name of the variable, which is used to identify the variable.

See Data Types for more information on the different Types for a Variable.

The value of a variable can be changed while a program is running. Each time you write to a variable, any previous value is erased and replaced with the new value. For example, you could use a variable named “Max Light” to keep track of the highest light intensity that your robot has measured from the Colour Sensor so far. Each time the robot detects a higher value, it could write the new value into the “Max Light” variable.
Adding a New Variable


Mode Selector
Variable Selector
Value Input

To add a new variable to your project:
1.Insert or select a Variable block.
2.Use the Mode Selector to choose Write mode.
3.Choose the variable type (Numeric, Logic, Text, Numeric Array, or Logic Array).
4.Click the Block Text Field on the top of the block and select “Add Variable”. The New Variable dialogue box will appear, as shown below.
5.In the New Variable dialogue box, enter a name for your variable and click OK. A variable name can be a single letter, a word, several words, or any sequence of letters and numbers.
Tips and Tricks
Choosing a short variable name will make it easier to see the whole variable name when you use it in your program.
Choose a name that will help you remember what the variable means, and how it is different from other variables in your program.
Once you add a variable, it will be available for use in any of the programs in your project.
6.After you have added the variable, you can use the Variable block in Write mode to enter an initial Value for the variable.
Writing to a Variable


Mode Selector
Variable Name
Input
Write
The Write mode lets you choose a variable that you have already added to your program (see Adding a New Variable) and store a value in it.
To write to a variable:
1.Use the Mode Selector to choose the Write mode.
2.Choose the Type of the variable.
3.Click the block text field to display the popup menu.
4.Select Name of the variable that you want to use.
Tips and Tricks
You must choose the variable Type from the Mode Selector before choosing the variable Name from the popup menu. The popup menu will only list the variables that match the selected Type.
5.You can now store a value into the variable using the Value input. You can either enter the value directly in the Value input, or you can use a Data Wire.
Tips and Tricks
You can write to a variable as many times as you want, but only the last value is kept. Writing a variable erases the previous value of the variable and replaces it with the new value.
Reading from a Variable


Mode Selector
Variable Name
Output
Read
The Read mode lets you choose a variable that you have already added to your program (see Adding a New Variable) and get the value stored in it.
To read from a variable:
1.Use the Mode Selector to choose the Read mode and the Type of the variable.
2. Click the text field at the top of the block to display the popup menu, and select the Name of the variable that you want to use.
Tips and Tricks
You must choose the variable Type from the Mode Selector before choosing the variable Name from the popup menu. The popup menu will only list the variables that match the selected Type.
3.You can now get the value of the variable from the Value output, and use this in your program with a Data Wire.
Tips and Tricks
If you read from a variable that has never had a value written to it, the result will be 0 for a Numeric variable, False for a Logic variable, empty text for a Text variable, and an empty array for a Numeric Array or Logic Array variable.
Examples Using Variables
Example 1: Store a Value and Use it Later
This program uses a Variable block in Write mode to store a data value from the Colour Sensor in the variable named “Light”, and then it uses a Variable block in Read mode to get the value and use it later in the program.

The program is designed to make a robot drive out and back. On the way back the robot will stop over a colour that is at least as dark as the colour it started over.
Tips and Tricks
The program above could also be done using a long Data Wire instead of a Variable, but for long programs, using Variables to store values can make the program easier to work with.
Example 2: Count Touch Sensor Presses
This program counts the number of times that a touch sensor is pressed and displays the count on the EV3 Display. It uses a Numeric variable named “Presses” to keep track of the number of times that the touch sensor has been pressed.

A Variable block in Write mode sets the initial value of “Presses” to 0 at the beginning of the program. Inside the loop, the program adds 1 to the value stored in the variable each time the touch sensor is bumped. This is done by getting the current value with a Variable block in Read mode, using a Mathematics block and Data Wires to add 1, and writing the result back into the variable with a Variable block in Write mode. Finally, another Variable block in Read mode is used to get the current value and display it.
Inputs and Outputs
In Write mode, the Variable block has a single input. You can enter the input value directly into the block. Alternatively, the value can be supplied by a Data Wire from the output of another Programming Block.
InputTypeNotes
Value (Depends on the block mode)Value to store into the variable
In Read mode, the Variable block has a single output. To use the output, use a Data Wire to connect it to another Programming Block.
OutputTypeNotes
Value (Depends on the block mode)Value of the selected variable
Variable
Quick links