0% found this document useful (0 votes)
6 views

arduino-motor-control

The document contains code for controlling an L298N motor driver and an ultrasonic sensor using an Arduino. It defines pin configurations, motor control functions, and a loop that measures distance to avoid obstacles by stopping, moving backward, or turning. The code also includes constants for motor speeds and distance thresholds for obstacle detection.

Uploaded by

matey0228
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

arduino-motor-control

The document contains code for controlling an L298N motor driver and an ultrasonic sensor using an Arduino. It defines pin configurations, motor control functions, and a loop that measures distance to avoid obstacles by stopping, moving backward, or turning. The code also includes constants for motor speeds and distance thresholds for obstacle detection.

Uploaded by

matey0228
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

// Pin definitions for L298N Motor Driver

const int ENA = 10; // Enable pin for Motor A


const int IN1 = 9; // Input 1 for Motor A
const int IN2 = 8; // Input 2 for Motor A
const int ENB = 5; // Enable pin for Motor B
const int IN3 = 7; // Input 1 for Motor B
const int IN4 = 6; // Input 2 for Motor B

// Pin definitions for Ultrasonic Sensor


const int TRIG_PIN = 12;
const int ECHO_PIN = 11;

// Constants
const int MOTOR_SPEED = 200; // Motor speed (0-255)
const int TURNING_SPEED = 150; // Speed while turning
const int MIN_DISTANCE = 20; // Minimum distance in cm before changing
direction
const int STOP_DISTANCE = 10; // Distance at which to stop completely

void setup() {
// Configure motor control pins
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);

// Configure ultrasonic sensor pins


pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);

Serial.begin(9600); // Initialize serial communication


}

// Function to measure distance using ultrasonic sensor


int getDistance() {
// Clear the trigger pin
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);

// Send 10μs pulse


digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

// Read the echo pin, convert time to distance in cm


long duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}

// Motor control functions


void moveForward() {
analogWrite(ENA, MOTOR_SPEED);
analogWrite(ENB, MOTOR_SPEED);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}

void moveBackward() {
analogWrite(ENA, MOTOR_SPEED);
analogWrite(ENB, MOTOR_SPEED);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}

void turnRight() {
analogWrite(ENA, TURNING_SPEED);
analogWrite(ENB, TURNING_SPEED);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}

void turnLeft() {
analogWrite(ENA, TURNING_SPEED);
analogWrite(ENB, TURNING_SPEED);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}

void stopMotors() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

void loop() {
int distance = getDistance();
Serial.print("Distance: ");
Serial.println(distance);

if (distance <= STOP_DISTANCE) {


// Too close to obstacle - stop
stopMotors();
delay(500);
moveBackward();
delay(1000);
turnRight();
delay(800);
}
else if (distance <= MIN_DISTANCE) {
// Approaching obstacle - turn to avoid
if (random(2) == 0) { // Randomly choose left or right
turnLeft();
} else {
turnRight();
}
delay(500);
}
else {
// Clear path ahead - move forward
moveForward();
}

delay(100); // Small delay to stabilize readings


}

You might also like