Arduino Variables and Data Types | applied electronics engineering


Arduino Variables and Data Types

By Applied Electronics - Monday, February 27, 2017 No Comments
Arduino is a 8 bit ATmega328P microcontroller based hardware development board. It is primarily programmed using custom C++ programming language. Programming Arduino is basically programming the ATmega328P microcontroller using custom C++ programming language. So when you want to write program you need to know how to declare/define variables and to define variables you need to know what data types are supported by Arduino micrcontroller.

What is variable? How to declare variables?

Variable is a place holder for value in the microcontroller memory. When you declare a variable, the name is associated with a value and stored in the memory.

Variables can be simply defined using the following general syntax

<data type> <variable name> = <value>

For example,

int LED = 10;

declares a variable LED of data type int which means integer and assign the variable LED to 10. 10 is internally known to the microcontroller as the 10th pin.

The <data type> is the data type of the variable and it can be one of the following
  • void
This is used in the function declaration to indicate that the function is not going to return any value.

For example,

void setup() {
          // some statements
  • boolean
Variables defined with the data type boolean can only hold one of two values, true or false.

For example,

boolean ledState = false;
  • byte
This is used to store an 8-bit unsigned number, which is basically any number from 0 to 255.

For example,

byte b = 0xFF;
  • char
This data type stores a character value and occupies 1 byte of memory. When providing a value to char data types, character literals are declared with single quotes.

For example,

char myCharacater = 'P';
  • int
This is data type used to declare integer variables. It stores 16-bit (Arduino Uno) or 32-bit (Arduino Due) numbers and it is one of the primary number storage data types for the Arduino
Arduino also supports short and long integer variables. The short is used for 16 bit data type and long is used for 32 bit data type.
short LED =  13;
long SpeedOfLight =  186000L;

These are used for numbers with decimal points. For example 3.14 is a floating point number.

float pi = 3.14;
  • array
 An array stores a collection of variables that is accessible by an index number. If you are familiar with arrays in C/C++, it will be easier for you to get started, as the Arduino language uses the same C/C++ arrays. The following are some of the methods to initialize an array.

For example,

int A[] = {1, 2, 3, 4, 5};
int B[5] = { 32, 55, 72, 75};
char C[10] = "hello!";

An array can be accessed using an index number (where the index starts from number 0). For example,

A[0] == 1
C[2] == 'e'

See other tutorials on Arduino, How to send PWM signal from Simulink to Arduino and Send pulses from Simulink to the Arduino PWM pins.

No Comment to " Arduino Variables and Data Types "