Skip to content

Arduino Variables and Datatypes

    Arduino Variables

    In programming, values that are stored for further processing are named with a single word called a variable. The value of a variable can change continuously or be changed by the program. The variable name should be chosen in such a way that it is easily readable and understandable within the program. In addition, a variable also has a data type that defines the value range.

    The Variable must be declared before being used. So the data type, the variable name and the value of the variable are set. If no value is specified, the variable value is set to 0.

    datatype Variable_Name = Value;


    int motorSpeed = 255;

    Comprehensible variable names such as motorSpeed or temperatureSensor improve the readability of the program

    Arduino Datatypes

    A data type describes the value range and the possible operations of a variable. Since the data types of the variables require different amounts of memory, you must be aware of exactly which possible values a variable defined can have when creating the program: Is it only a digital state with the values yes and no or can the variable hold an entire text? The data type must be selected accordingly. The most important data types and their value ranges for creating Arduino programs are listed below:

    Data typeValue range    Memory requirement  Explanations/Notes    Examples
    booleantrue/false1 byteValue for Yes/No or On/Off,  boolean machineStatus = true;
    int-32,768 to 32,7672 bytes  Integer values for values without decimal places  int potiValue = 1023;
    unsigned int0 to 4,294,967,295    2 bytesInteger for positive values without decimal places  int tempValue = -5;
    byte0 to 2551 byteLow values (and correspondingly small memory requirements)    byte brightnessLED = 126;
    wordUnsigned 16-bit or 32-bit16-bit on Atmega boards.
    32-bit on Due and Zero
    Be aware of processor usedword value = 1500;
    long-2,147,483,648 to 2,147,483,6474 bytesInteger numbers with big values        long offset = -15362;
    unsigned long0 to 4,294,967,2954 bytesUnsigned Integer numbers with big values  unsigned long secondsPassed = 45379;
    float-3.4028235E+38 to 3.4028235E+384 bytesValues with decimal places  float voltageValue = 3.3;
    double-3.4028235E+38 to 3.4028235E+384 bytes-3.4028235E+38 to 3.4028235E+38double distanceGPS= 722.54;
    char-128 to 127  1 byteValue for storing one character  char charR = ‘R’;
    unsigned char0 to 2551 byteUsually used with numbers having range 0 to 255unsigned char charNumber = 255;
    stringlength depends on definitiondepends on needStrings like texts or several charactersstring brand[ ] = “ROBOSANS”;
    arraysize depends to definitionDepends on array sizestore values in variables with table form like e.g. lists of ports or configuration valuesint portsPWM[ ] = {3,5,6,9,10,11};
    Arduino Datatypes
    error: Content is protected !!