Introduction to Data Types in C/C++ for Arduino Programming

Understanding data types is crucial when working with C/C++ and Arduino. Data types define the type of data a variable can store and how much memory it occupies. In this tutorial, we will explore various data types, their syntax, and examples to help you understand how to use them efficiently in Arduino programming.

1. Integer Types

Basic Integer Types

Data Type

Description

Size (bytes)

Range

byte

8-bit unsigned integer

1

0 to 255

int

Standard integer

2 (Uno), 4 (Due)

-32,768 to 32,767 (2-byte)

long

Larger integer

4

-2,147,483,648 to 2,147,483,647

short

Smaller integer

2

-32,768 to 32,767

unsigned int

Positive-only integer

2 (Uno)

0 to 65,535

unsigned long

Larger positive-only integer

4

0 to 4,294,967,295

long long

Very large integer

8

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

unsigned long long

Very large positive integer

8

0 to 18,446,744,073,709,551,615

Example:

int age = 25;

long distance = 1234567890L;

unsigned int height = 180U;

byte ledPin = 13;

Fixed-Width Integer Types (from <stdint.h>)

Data Type

Description

Size (bytes)

Range

int8_t

8-bit signed integer

1

-128 to 127

uint8_t

8-bit unsigned integer

1

0 to 255

int16_t

16-bit signed integer

2

-32,768 to 32,767

uint16_t

16-bit unsigned integer

2

0 to 65,535

int32_t

32-bit signed integer

4

-2,147,483,648 to 2,147,483,647

uint32_t

32-bit unsigned integer

4

0 to 4,294,967,295

int64_t

64-bit signed integer

8

Very large range

uint64_t

64-bit unsigned integer

8

Very large range

Example:

int8_t smallNumber = -128;

uint8_t smallPositiveNumber = 255;

int16_t mediumNumber = 32767;

uint32_t largeNumber = 4294967295U;

2. Floating-Point Types

Data Type

Description

Size (bytes)

Precision

float

Single-precision floating point

4

~6-7 digits

double

Double-precision floating point

4 (Uno), 8 (Due)

~15 digits

Example:

float temperature = 36.6;

double pi = 3.141592653589793;

3. Character Types

Data Type

Description

Size (bytes)

char

Stores a single character

1

wchar_t

Stores wide characters (Unicode)

2 or 4

Example:

char initial = 'A';

wchar_t wideChar = L'あ';

4. Boolean Type (C++ Only)

Data Type

Description

Size (bytes)

Values

bool

Boolean type (true/false)

1

true, false

Example:

bool isPassed = true;

5. Other Useful Types

Data Type

Description

Size (bytes)

size_t

Represents size in bytes

Depends on architecture

void

Represents no type (used in functions)

0

word

16-bit unsigned integer

2

Example:

size_t length = sizeof(int);

word sensorValue = 1023;

6. Examples in Arduino Sketch

void setup() {

  Serial.begin(9600);

  // Integer types

  int age = 25;

  long distance = 1234567890L;

  unsigned int height = 180U;

  byte ledPin = 13;

  word sensorValue = 1023;

  // Floating point types

  float temperature = 36.6;

  double pi = 3.141592653589793;

  // Character type

  char initial = 'A';

  // Boolean type

  bool isPassed = true;

  // Fixed-width integer types

  int8_t smallNumber = -128;

  uint8_t smallPositiveNumber = 255;

  int16_t mediumNumber = 32767;

  uint32_t largeNumber = 4294967295U;

  int64_t veryLargeNumber = 9223372036854775807LL;

  // Demonstrating type conversion

  float floatValue = 10.75;

  int convertedValue = (int)floatValue; // Explicit conversion

  // Memory considerations

  Serial.print("Size of int: ");

  Serial.println(sizeof(int));

  Serial.print("Size of float: ");

  Serial.println(sizeof(float));

  Serial.print("Size of double: ");

  Serial.println(sizeof(double));

  // Print values to the Serial Monitor

  Serial.println(age);

  Serial.println(distance);

  Serial.println(height);

  Serial.println(temperature);

  Serial.println(pi);

  Serial.println(initial);

  Serial.println(isPassed);

  Serial.println(smallNumber);

  Serial.println(largeNumber);

  Serial.println(veryLargeNumber);

  Serial.println(convertedValue);

}

void loop() {

  // Empty loop

}

void setup() {

  Serial.begin(9600);

  // Integer types

  int age = 25;

  long distance = 1234567890L;

  unsigned int height = 180U;

  byte ledPin = 13;

  word sensorValue = 1023;

  // Floating point types

  float temperature = 36.6;

  double pi = 3.141592653589793;

  // Character type

  char initial = 'A';

  // Boolean type

  bool isPassed = true;

  // Print values to the Serial Monitor

  Serial.println(age);

  Serial.println(distance);

  Serial.println(height);

  Serial.println(temperature);

  Serial.println(pi);

  Serial.println(initial);

  Serial.println(isPassed);

}

void loop() {

  // Empty loop

}

Explanation of the Code

Conclusion

Understanding data types is essential for writing efficient Arduino programs. Each data type has been chosen based on its suitability for storing specific values while optimizing memory usage. For instance, int is used for general-purpose whole numbers, whereas unsigned int is used for values that will never be negative, saving an extra bit. Floating-point types like float and double are employed when precision is required.

Real-World Applications

By understanding and selecting the appropriate data type, you can improve memory efficiency, processing speed, and overall reliability of your Arduino projects. data types is essential for writing efficient Arduino programs. This tutorial covered integer, floating-point, character, and boolean types, along with their usage in Arduino. Experiment with different data types and observe how they behave in your projects!