Circuit Documentation
Summary
This circuit is designed to control two DC motors using an Arduino UNO and an H-Bridge motor driver. The motors are powered by a 2x 18650 battery pack. The Arduino UNO is programmed to control the direction and speed of the motors based on the input from an IR receiver.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
DC Motor (Motor A)
- Description: A simple DC motor.
- Pins: pin 1, pin 2
DC Motor (Motor B)
- Description: A simple DC motor.
- Pins: pin 1, pin 2
H-Bridge Motor Driver (Puente H carrito)
- Description: A motor driver used to control the direction and speed of the motors.
- Pins: OUT1, OUT2, OUT3, OUT4, 12V, GND, 5V, ENA, IN1, IN2, IN3, IN4, ENB, 5VEN
2x 18650 Battery Pack
- Description: A battery pack consisting of two 18650 batteries.
- Pins: vcc, gnd
Wiring Details
Arduino UNO
- GND is connected to GND of the 2x 18650 Battery Pack and GND of the H-Bridge Motor Driver.
- 5V is connected to 5V of the H-Bridge Motor Driver.
- D9 is connected to ENA of the H-Bridge Motor Driver.
- D8 is connected to IN1 of the H-Bridge Motor Driver.
- D7 is connected to IN2 of the H-Bridge Motor Driver.
- D6 is connected to IN3 of the H-Bridge Motor Driver.
- D5 is connected to IN4 of the H-Bridge Motor Driver.
- D3 is connected to ENB of the H-Bridge Motor Driver.
DC Motor (Motor A)
- pin 2 is connected to OUT1 of the H-Bridge Motor Driver.
- pin 1 is connected to OUT2 of the H-Bridge Motor Driver.
DC Motor (Motor B)
- pin 2 is connected to OUT3 of the H-Bridge Motor Driver.
- pin 1 is connected to OUT4 of the H-Bridge Motor Driver.
H-Bridge Motor Driver (Puente H carrito)
- 12V is connected to vcc of the 2x 18650 Battery Pack.
- GND is connected to GND of the Arduino UNO and GND of the 2x 18650 Battery Pack.
- 5V is connected to 5V of the Arduino UNO.
- ENA is connected to D9 of the Arduino UNO.
- IN1 is connected to D8 of the Arduino UNO.
- IN2 is connected to D7 of the Arduino UNO.
- IN3 is connected to D6 of the Arduino UNO.
- IN4 is connected to D5 of the Arduino UNO.
- ENB is connected to D3 of the Arduino UNO.
2x 18650 Battery Pack
- vcc is connected to 12V of the H-Bridge Motor Driver.
- gnd is connected to GND of the Arduino UNO and GND of the H-Bridge Motor Driver.
Code Documentation
#include <IRremote.hpp>
const int IR_RECEIVE_PIN = 2;
// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;
// Motor B connections
int enB = 3;
int in3 = 6;
int in4 = 5;
void setup() {
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the IR receiver
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Turn off motors - Initial state
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop() {
directionControl();
delay(1000);
speedControl();
delay(1000);
}
// This function lets you control spinning direction of motors
void directionControl() {
// Set motors to maximum speed
// For PWM maximum possible values are 0 to 100
analogWrite(enA, 100);
analogWrite(enB, 100);
// Turn on motor A & B
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(2000);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
// Turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
// This function lets you control speed of the motors
void speedControl() {
// Accelerate from zero to maximum speed
for (int i = 0; i < 101; i++) {
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
// Decelerate from maximum speed to zero
for (int i = 100; i >= 0; --i) {
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
// Now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
This code initializes the IR receiver and sets up the motor control pins as outputs. The loop function calls two functions: directionControl and speedControl. The directionControl function controls the direction of the motors, while the speedControl function controls the speed of the motors.
