Arduino Mega 2560 Based Multi-Mode Robot with Bluetooth Control and Obstacle Avoidance
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
The circuit in question is designed to control a robotic system with various functionalities including motor control, distance measurement, Bluetooth communication, and display output. The core of the circuit is an Arduino Mega 2560, which interfaces with a range of sensors, actuators, and a display. The circuit includes motor drivers for controlling DC motors with encoders, an MPU6050 for motion tracking, IR sensors for line following, an HC-SR04 ultrasonic sensor for distance measurement, an HC-06 Bluetooth module for wireless communication, and a 7-segment display for user feedback. Power is supplied by 18650 Li-Ion batteries, and the circuit includes resistors for current limiting and a rocker switch for power control.
Component List
Arduino Mega 2560
- Microcontroller board based on the ATmega2560
- Provides a large number of IO pins for interfacing with various components
7-Segment Display (Common Anode)
- Displays numerical information
- Common anode configuration
18650 Li-Ion Batteries
- Provides power to the circuit
- Rechargeable battery cells
L298N DC Motor Driver
- Controls the direction and speed of DC motors
- Capable of driving two motors with H-bridge configuration
DC Motors with Encoders
- Actuators for the robotic system
- Integrated encoders provide feedback on motor position and speed
MPU6050
- Motion tracking device with a 3-axis gyroscope and a 3-axis accelerometer
- Used for balancing and orientation detection
IR Sensors
- Detects reflective surfaces for line following
- Outputs a digital signal
HC-06 Bluetooth Module
- Allows for wireless communication with the Arduino
- Configured for serial communication
HC-SR04 Ultrasonic Sensor
- Measures distance using ultrasonic waves
- Provides input for obstacle avoidance
Resistors (200 Ohms)
- Limits current to protect components
- Used in series with the 7-segment display segments
Rocker Switch
- Controls the power supply to the circuit
- Simple on/off mechanism
Wiring Details
Arduino Mega 2560
5VandGNDpins provide power and ground to other componentsA4 (SDA)andA5 (SCL)are connected to the MPU6050 for I2C communicationD2toD11are used for motor control signals and encoder inputsD18 (TX1)andD19 (RX1)are connected to the HC-06 Bluetooth module for serial communicationD28,D26,D24,D22are connected to resistors which then connect to the 7-segment display segments A, B, C, and D respectivelyD30andD31are connected to the HC-SR04 ultrasonic sensor's TRIG and ECHO pinsD32andD33are connected to the IR sensors' output pins
7-Segment Display (Common Anode)
COMpin connected to Arduino'sGND- Segment pins
AtoGconnected to Arduino through 200 Ohm resistors
L298N DC Motor Driver
IN1,IN2,IN3,IN4connected to Arduino for motor control signalsENA,ENBconnected to Arduino for PWM speed controlOUT1,OUT2,OUT3,OUT4connected to DC motors12VandGNDconnected to the 18650 Li-Ion battery for power
DC Motors with Encoders
Motor +veandMotor -veconnected to motor driver outputsEncoder AandEncoder Bconnected to Arduino for feedback
MPU6050
vccandgndconnected to Arduino's5VandGNDsdaandsclconnected to Arduino'sA4 (SDA)andA5 (SCL)
IR Sensors
vccandgndconnected to Arduino's5VandGNDoutconnected to Arduino's digital pins for signal output
HC-06 Bluetooth Module
VCCandGNDconnected to Arduino's5VandGNDRXDandTXDconnected to Arduino'sD18 (TX1)andD19 (RX1)
HC-SR04 Ultrasonic Sensor
VCCandGNDconnected to Arduino's5VandGNDTRIGandECHOconnected to Arduino'sD30andD31
Resistors (200 Ohms)
- Connected in series with the 7-segment display segments and Arduino digital pins
Rocker Switch
- Connected in-line with the power supply from the 18650 Li-Ion batteries to the Arduino's
GND
Documented Code
#include <Wire.h>
#include <MPU6050.h>
#include <SoftwareSerial.h>
#include <Encoder.h>
#include <SevSeg.h> // Include the SevSeg library
// Pin Definitions
const int motor1Pin1 = 2; // H-Bridge IN1
const int motor1Pin2 = 3; // H-Bridge IN2
const int motor1PWM = 9; // H-Bridge PWM1
const int motor2Pin1 = 4; // H-Bridge IN3
const int motor2Pin2 = 5; // H-Bridge IN4
const int motor2PWM = 10; // H-Bridge PWM2
const int triggerPin = 30; // Ultrasonic Trigger
const int echoPin = 31; // Ultrasonic Echo
const int irLeftPin = 32; // Left IR Sensor
const int irRightPin = 33; // Right IR Sensor
const int buttonPin = 29; // Mode button
// Encoder Pins
const int encoder1PinA = 6; // Encoder Motor A A
const int encoder1PinB = 7; // Encoder Motor A B
const int encoder2PinA = 8; // Encoder Motor B A
const int encoder2PinB = 11; // Encoder Motor B B
// HC-05 Bluetooth
SoftwareSerial Bluetooth(18, 19); // RX, TX
// SevSeg display
SevSeg sevseg; // Create an instance of the display
// Global variables
int currentMode = 0; // 0: Balance, 1: Obstacle Avoidance, 2: Line Following, 3: Bluetooth Control, 4: Dance
bool lastButtonState = LOW;
MPU6050 mpu;
// PID variables for balance
float kp = 2.0, ki = 0.5, kd = 1.0;
float error = 0, prevError = 0, integral = 0, derivative = 0;
float setpoint = 0; // Ideal upright position (0 degrees)
float pidOutput = 0;
// Encoder objects
Encoder encoder1(encoder1PinA, encoder1PinB);
Encoder encoder2(encoder2PinA, encoder2PinB);
// Motor speed variables
int motorSpeed1 = 0, motorSpeed2 = 0;
// Ultrasonic sensor function
long readUltrasonicDistance(int triggerPin, int echoPin) {
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
return pulseIn(echoPin, HIGH) * 0.034 / 2;
}
// PID controller function
float calculatePID(float setpoint, float currentAngle) {
error = setpoint - currentAngle;
integral += error;
derivative = error - prevError;
prevError = error;
return (kp * error) + (ki * integral) + (kd * derivative);
}
// Set motor speed function
void setMotorSpeed(int motor, int speed) {
if (motor == 1) {
if (speed > 0) {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
} else {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
speed = -speed;
}
analogWrite(motor1PWM, speed);
} else if (motor == 2) {
if (speed > 0) {
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
} else {
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
speed = -speed;
}
analogWrite(motor2PWM, speed);
}
}
// Mode switching logic based on Bluetooth input
void switchModes(char command) {
if (command == 'N') { // Next mode
currentMode++;
if (currentMode > 4) currentMode = 0; // Wrap around
} else if (command == 'P') { // Previous mode
currentMode--;
if (currentMode < 0