๐Ÿ“˜ Basic Motor Control Programming


๐ŸŽฏ Lesson Objective

In this lesson, students will learn:

  • How to control DC motors using ESP32
  • How to write movement logic (Forward, Backward, Left, Right, Stop)
  • How digital signals control motor direction
  • How to test robot movement without WiFi

By the end of this lesson, students will be able to program and control the robot manually using ESP32.


๐Ÿง  Concept Introduction

Before adding WiFi or mobile control, we must first understand how to control motors directly using programming.

The ESP32 sends HIGH (1) and LOW (0) signals to the motor driver, which controls motor direction.


โš™๏ธ Motor Control Logic

Each motor is controlled using 2 input pins:

Motor Action Logic
Forward IN1 = HIGH, IN2 = LOW
Backward IN1 = LOW, IN2 = HIGH
Stop IN1 = LOW, IN2 = LOW

๐Ÿ‘‰ Same logic applies to the second motor.


๐Ÿ”Œ Pin Setup (Recap)

  • IN1 โ†’ 26
  • IN2 โ†’ 27
  • IN3 โ†’ 14
  • IN4 โ†’ 12

๐Ÿ’ป ESP32 Motor Control Code

int IN1 = 26;
int IN2 = 27;
int IN3 = 14;
int IN4 = 12;

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
}

// Movement Functions

void forward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void backward() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void left() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void right() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void stopRobot() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

void loop() {
  forward();
  delay(2000);

  left();
  delay(2000);

  right();
  delay(2000);

  backward();
  delay(2000);

  stopRobot();
  delay(2000);
}

๐Ÿ” Code Explanation

  • pinMode() โ†’ Sets pins as output
  • digitalWrite() โ†’ Sends HIGH/LOW signals
  • Functions โ†’ Used for modular programming
  • delay() โ†’ Controls timing of movements

๐Ÿงช Practical Activity

  1. Upload the code to ESP32
  2. Power ON the robot
  3. Observe movement sequence:
    • Forward โ†’ Left โ†’ Right โ†’ Backward โ†’ Stop

๐Ÿง  Key Learning Concepts

  • Digital Output Control
  • Motor Direction Logic
  • Function-Based Programming
  • Sequential Execution

โš ๏ธ Common Issues & Solutions

Problem Solution
Motors not moving Check wiring
Only one motor working Check connections
Wrong direction Swap motor wires
No response Check power supply

๐Ÿ“ Lesson Summary

In this lesson, students learned how to control motors using ESP32 by sending digital signals. They also understood how to organize code using functions and tested robot movement without WiFi.


๐Ÿ“Œ Practice Task

  • Modify delay values and observe changes
  • Create your own movement sequence
  • Write a function for diagonal movement