Lesson 3, Topic 1
In Progress

Introduction to Arduino Programming

Lesson Progress
0% Complete

ESP32 Programming with Arduino IDE

Objective: Learn the language of the ESP32 and write your first set of instructions to control hardware.

Presentation:

Esp32_2_Arduino Programming by Infinite Engineers

1. The Core Structure: Setup & Loop

Every Arduino program (called a Sketch) has two essential functions:

  • void setup(): This code runs only once when the ESP32 is powered on or reset. It is used to initialize pins (e.g., telling the ESP32 that an LED is an output).
  • void loop(): This code runs repeatedly in a continuous cycle. This is where the main logic of your project lives.

2. Communicating with the Computer: Serial Print

Since the ESP32 doesn’t have a screen, we use the Serial Monitor on your laptop to see what the brain is thinking.

  • Serial.begin(115200);: Opens the communication line.
  • Serial.println("Hello World!");: Sends text from the ESP32 to your computer screen.

3. Storing Information: Variables & Data Types

Variables are “containers” for storing data values.

  • Data Types:
    • int: For whole numbers (e.g., motor speed).
    • float: For decimal numbers (e.g., temperature).
    • bool: For True/False (e.g., is the button pressed?).
    • char: For single letters.
  • Scope:
    • Global Variables: Declared at the very top; they can be used anywhere in the code.
    • Local Variables: Declared inside a function; they only exist within that specific function.
  • Constants (const): Used for values that never change, like the Pin number where an LED is connected.

4. Decisions and Repetition: Control Structures

  • Control Structures (if, else): These allow the robot to make decisions. Example: “If the soil is dry, turn on the pump”.
  • Loops (for, while): Used to repeat a block of code a specific number of times.
  • Jump Statements (break, continue): Used to exit a loop early or skip a step.

5. Functions & Libraries

  • Functions: Blocks of code that perform a specific task, making your program organized and reusable.
  • Libraries: Pre-written code created by experts that you can “import” to easily use complex hardware. Example: Using a library to read a DHT temperature sensor without writing 100 lines of math.

Hands-on: The “Blink” Logic

Students will write a simple code to make an LED blink using the knowledge above:

C++

const int ledPin = 2; // Constant for the LED pin

void setup() {
  pinMode(ledPin, OUTPUT); // Initialize the pin as an output
  Serial.begin(115200);
}

void loop() {
  digitalWrite(ledPin, HIGH); // Turn LED ON
  Serial.println("LED is ON");
  delay(1000);                // Wait for 1 second
  
  digitalWrite(ledPin, LOW);  // Turn LED OFF
  Serial.println("LED is OFF");
  delay(1000);                // Wait for 1 second
}

Required Materials for this Session

  • ESP32 Board
  • USB Cable
  • Laptop with Arduino IDE installed