Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Mega 2560 Based Multi-Mode Robot with Bluetooth Control and Obstacle Avoidance

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 Based Multi-Mode Robot with Bluetooth Control and Obstacle Avoidance

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

7-Segment Display (Common Anode)

18650 Li-Ion Batteries

L298N DC Motor Driver

DC Motors with Encoders

MPU6050

IR Sensors

HC-06 Bluetooth Module

HC-SR04 Ultrasonic Sensor

Resistors (200 Ohms)

Rocker Switch

Wiring Details

Arduino Mega 2560

7-Segment Display (Common Anode)

L298N DC Motor Driver

DC Motors with Encoders

MPU6050

IR Sensors

HC-06 Bluetooth Module

HC-SR04 Ultrasonic Sensor

Resistors (200 Ohms)

Rocker Switch

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