ESP32-Based Wi-Fi Controlled Robotic Vehicle with Ultrasonic Sensors and DC Motors
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This document provides a detailed overview of a circuit that includes an ESP32 microcontroller, two HC-SR04 ultrasonic sensors, an L298N DC motor driver, two DC motors, and a 5V battery. The ESP32 is programmed to control the motors based on the distance measured by the ultrasonic sensors. The circuit also includes additional sensors for temperature, gas, voltage, and vibration.
Component List
ESP32
- Description: A powerful microcontroller with built-in WiFi and Bluetooth capabilities.
- Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, VIN, 3V3, D15, D2, D4, RX2, TX2, D5, D18, D19, D21, RX0, TX0, D22, D23, BOOT
HC-SR04 Ultrasonic Sensor
- Description: A sensor used to measure distance by using ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND
L298N DC Motor Driver
- Description: A dual H-Bridge motor driver that allows control of two DC motors.
- Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
DC Motor
- Description: A simple DC motor.
- Pins: pin 1, pin 2
5V Battery
- Description: A power source providing 5V.
- Pins: positive, negative
Wiring Details
ESP32
- D32: Connected to ENB of L298N DC motor driver
- D33: Connected to IN4 of L298N DC motor driver
- D25: Connected to IN3 of L298N DC motor driver
- D26: Connected to IN2 of L298N DC motor driver
- D27: Connected to IN1 of L298N DC motor driver
- D14: Connected to ENA of L298N DC motor driver
- GND: Connected to GND of L298N DC motor driver
- 3V3: Connected to VCC of both HC-SR04 ultrasonic sensors
- GND: Connected to GND of both HC-SR04 ultrasonic sensors
- D15: Connected to ECHO of the second HC-SR04 ultrasonic sensor
- D2: Connected to TRIG of the second HC-SR04 ultrasonic sensor
- D4: Connected to TRIG of the first HC-SR04 ultrasonic sensor
- D5: Connected to ECHO of the first HC-SR04 ultrasonic sensor
HC-SR04 Ultrasonic Sensor (First)
- VCC: Connected to 3V3 of ESP32
- TRIG: Connected to D4 of ESP32
- ECHO: Connected to D5 of ESP32
- GND: Connected to GND of ESP32
HC-SR04 Ultrasonic Sensor (Second)
- VCC: Connected to 3V3 of ESP32
- TRIG: Connected to D2 of ESP32
- ECHO: Connected to D15 of ESP32
- GND: Connected to GND of ESP32
L298N DC Motor Driver
- ENB: Connected to D32 of ESP32
- IN4: Connected to D33 of ESP32
- IN3: Connected to D25 of ESP32
- IN2: Connected to D26 of ESP32
- IN1: Connected to D27 of ESP32
- ENA: Connected to D14 of ESP32
- GND: Connected to GND of ESP32
- 5V: Connected to positive of 5V battery
- OUT1: Connected to pin 2 of the second DC motor
- OUT2: Connected to pin 1 of the second DC motor
- OUT3: Connected to pin 2 of the first DC motor
- OUT4: Connected to pin 1 of the first DC motor
DC Motor (First)
- pin 1: Connected to OUT4 of L298N DC motor driver
- pin 2: Connected to OUT3 of L298N DC motor driver
DC Motor (Second)
- pin 1: Connected to OUT2 of L298N DC motor driver
- pin 2: Connected to OUT1 of L298N DC motor driver
5V Battery
- positive: Connected to 5V of L298N DC motor driver
- negative: Not connected in the provided net list
Code Documentation
#include <HardwareSerial.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/timers.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHTesp.h>
HardwareSerial loraSerial(2);
// Pin untuk sensor ultrasonik
const int trigPinLeft = 4; // Sensor kiri
const int echoPinLeft = 5;
const int trigPinFront = 2; // Sensor depan
const int echoPinFront = 15;
// Pin untuk motor driver L298N
const int in1 = 27; // Motor A IN1
const int in2 = 26; // Motor A IN2
const int in3 = 25; // Motor B IN3
const int in4 = 33; // Motor B IN4
const int enA = 14; // Enable pin untuk Motor A (PWM)
const int enB = 32; // Enable pin untuk Motor B (PWM)
// Variabel PID
float Kp = 1.5;
float Ki = 0.03;
float Kd = 12.5;
float previousErrorLeft = 0;
float integralLeft = 0;
float setPointLeft = 17;
float previousErrorFront = 0;
float integralFront = 0;
float setPointFront = 40;
long durationLeft, durationFront;
int distanceLeft, distanceFront;
int speedA = 220; // Kecepatan Motor A
int speedB = 220; // Kecepatan Motor B
// Variabel sensor lainnya
#define DHTPIN 18
#define GAS_SENSOR_PIN 13
#define VOLTAGE_SENSOR_PIN 34
#define VIBRATION_SENSOR_PIN 12
DHTesp dht;
// WiFi & Server Information
const char* ssid = "DTEO-VOKASI";
const char* password = "TEO123456";
const char* serverName = "http://10.17.36.187/mobile_robot/kirimdata.php";
// Variabel tambahan
unsigned long previousMillis = 0;
const long turnDelay = 290;
const unsigned long interval = 1000; // Interval pengiriman data (ms)
const long forwardDelay = 2000;
float V_BEBAN;
int VOLTAGE_SENSOR_RAW;
float RATIO = 5.920; // Sesuaikan rasio tegangan
const float GETARAN_TO_G = 0.1; // Sesuaikan nilai konversi sesuai sensor, contoh 0.001 g per microsecond
// Fungsi sensor
int getDistance(int trigPin, int echoPin);
float computePIDLeft(float input);
void stopMotors();
void turnRight();
void turnLeftAndMoveForward();
void kirimDataLoRaTask(TimerHandle_t xTimer);
void kirimDataKeServer();
void baca_sensor();
void setup() {
Serial.begin(115200);
loraSerial.begin(9600, SERIAL_8N1, 16, 17);
// Inisialisasi pin
pinMode(trigPinLeft, OUTPUT);
pinMode(echoPinLeft, INPUT);
pinMode(trigPinFront, OUTPUT);
pinMode(echoPinFront, INPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
// Inisialisasi DHT
dht.setup(DHTPIN, DHTesp::DHT22);
pinMode(VIBRATION_SENSOR_PIN, INPUT);
// Setup WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(10);
Serial.println("Mencoba menghubungkan ke WiFi...");
}
Serial.println("Terhubung ke WiFi");
// Membuat task untuk gerakan robot
xTaskCreate(gerakRobotTask, "GerakRobot", 4096, NULL, 3, NULL);
// Membuat task untuk mengirim data sensor ke server
x