Lesson 2 - Understanding int Data Type
Learn how the int data type stores whole numbers and helps your ESP32 keep track of values like counters, sensor readings, and delays.
Progress indicator
Lesson 2 of 4
Learning Objectives
- Understand that int stores whole numbers (no decimal point).
- Use int variables for counters, delays, and simple sensor values.
- Read and update an int value line by line in Arduino code.
- Explain how int helps an ESP32 remember changing values while a program runs.
Concept Explanation
What is int in Arduino?
In Arduino and ESP32 code, int is a data type used to store whole numbers such as 0, 5, 100, or -3. It does not store decimal values like 3.14.
Why is int useful in ESP32 projects?
ESP32 programs often need whole-number values for things like LED pin numbers, delay times, counters, and sensor readings. Using int makes these values easy to store and update.
Real example: delay time
int blinkDelay = 1000; stores 1000 milliseconds so the same value can be reused in multiple delay() calls.
Real example: counter
int blinkCount = 0; can count how many times the LED has blinked by adding 1 each loop.
Code Example
This example uses int for a delay value and a blink counter.
int blinkDelay = 1000;
int blinkCount = 0;
void setup() {
pinMode(2, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH);
delay(blinkDelay);
digitalWrite(2, LOW);
delay(blinkDelay);
blinkCount = blinkCount + 1;
}Code Explanation (Step-by-Step)
- int blinkDelay = 1000; creates a whole-number variable that stores the LED wait time in milliseconds.
- int blinkCount = 0; creates a counter that starts at zero.
- pinMode(2, OUTPUT); prepares pin 2 so it can control an LED.
- digitalWrite and delay use blinkDelay so the same timing value can be reused without typing 1000 again.
- blinkCount = blinkCount + 1; increases the counter by one after each blink cycle.
Practice Task
Change blinkDelay from 1000 to 300 and explain how the LED behavior changes. Then change blinkCount so it starts at 10.
Try it now
Open the simulator workspace and practice changing integer values such as blinkDelay and blinkCount.