Circuit Documentation
Summary
This document provides a detailed overview of a circuit designed to control a set of DC motors using an ESP32 microcontroller and an L298N motor driver. The circuit is powered by a 9V battery and includes four motor and wheel assemblies. The ESP32 is programmed to control the motors based on serial input commands.
Component List
ESP32 38 PINS
- Description: A versatile microcontroller with Wi-Fi and Bluetooth capabilities.
- Pins: GND, 23, 22/SCL, 1, 2, 21/SDA, 19, 18, 5, 17/TX2, 16/RX2, 4, 0, 15, D1, D0, CLK, 3V3, EN, VP, VN, 34, 35, 32, 33, 25, 26, 27, 14, 12, 13, D2, D3, 11, 5V/VIN
9V battery
- Description: A standard 9V battery used to power the circuit.
- Pins: +, -
L298N DC motor driver
- Description: A dual H-bridge motor driver that allows control of the speed and direction 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
Motor and wheels (4 units)
- Description: DC motors with attached wheels for movement.
- Pins: vcc, GND
Wiring Details
ESP32 38 PINS
GND is connected to:
- GND of L298N DC motor driver
-
- of 9V battery
23 is connected to:
- ENA of L298N DC motor driver
22/SCL is connected to:
- ENB of L298N DC motor driver
19 is connected to:
- IN4 of L298N DC motor driver
18 is connected to:
- IN3 of L298N DC motor driver
17/TX2 is connected to:
- IN2 of L298N DC motor driver
16/RX2 is connected to:
- IN1 of L298N DC motor driver
5V/VIN is connected to:
- 5V of L298N DC motor driver
9V battery
+ is connected to:
- 12V of L298N DC motor driver
- is connected to:
- GND of ESP32 38 PINS
- GND of L298N DC motor driver
L298N DC motor driver
OUT1 is connected to:
- vcc of Motor and wheels (2 units)
OUT2 is connected to:
- GND of Motor and wheels (2 units)
OUT3 is connected to:
- vcc of Motor and wheels (2 units)
OUT4 is connected to:
- GND of Motor and wheels (2 units)
Motor and wheels (4 units)
vcc of Motor and wheels (2 units) is connected to:
- OUT1 of L298N DC motor driver
GND of Motor and wheels (2 units) is connected to:
- OUT2 of L298N DC motor driver
vcc of Motor and wheels (2 units) is connected to:
- OUT3 of L298N DC motor driver
GND of Motor and wheels (2 units) is connected to:
- OUT4 of L298N DC motor driver
Documented Code
ESP32 38 PINS Code
#include <Arduino.h>
#include <driver/ledc.h> // Include the Arduino core for ESP32
// Right motor
int enableRightMotor = 22;
int rightMotorPin1 = 16;
int rightMotorPin2 = 17;
// Left motor
int enableLeftMotor = 23;
int leftMotorPin1 = 18;
int leftMotorPin2 = 19;
#define MAX_MOTOR_SPEED 255
const int PWMFreq = 1000; /* 1 KHz */
const int PWMResolution = 8;
const int rightMotorPWMSpeedChannel = 4;
const int leftMotorPWMSpeedChannel = 5;
void rotateMotor(int rightMotorSpeed, int leftMotorSpeed)
{
if (rightMotorSpeed < 0)
{
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, HIGH);
}
else if (rightMotorSpeed > 0)
{
digitalWrite(rightMotorPin1, HIGH);
digitalWrite(rightMotorPin2, LOW);
}
else
{
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, LOW);
}
if (leftMotorSpeed < 0)
{
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, HIGH);
}
else if (leftMotorSpeed > 0)
{
digitalWrite(leftMotorPin1, HIGH);
digitalWrite(leftMotorPin2, LOW);
}
else
{
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, LOW);
}
ledcWrite(rightMotorPWMSpeedChannel, abs(rightMotorSpeed));
ledcWrite(leftMotorPWMSpeedChannel, abs(leftMotorSpeed));
}
void setUpPinModes()
{
pinMode(enableRightMotor, OUTPUT);
pinMode(rightMotorPin1, OUTPUT);
pinMode(rightMotorPin2, OUTPUT);
pinMode(enableLeftMotor, OUTPUT);
pinMode(leftMotorPin1, OUTPUT);
pinMode(leftMotorPin2, OUTPUT);
rotateMotor(0, 0);
}
void setup() {
setUpPinModes();
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
char input = Serial.read();
switch (input) {
case 'w':
rotateMotor(MAX_MOTOR_SPEED / 2, MAX_MOTOR_SPEED / 2);
break;
case 's':
rotateMotor(-MAX_MOTOR_SPEED / 2, -MAX_MOTOR_SPEED / 2);
break;
case 'a':
rotateMotor(-MAX_MOTOR_SPEED / 2, MAX_MOTOR_SPEED / 2);
break;
case 'd':
rotateMotor(MAX_MOTOR_SPEED / 2, -MAX_MOTOR_SPEED / 2);
break;
case 'x':
rotateMotor(0, 0);
break;
}
}
}
Motor and wheels Code
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
This concludes the documentation for the circuit.
