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

3D

This document contains an Arduino code for controlling a motor using an ultrasonic sensor to detect obstacles. The code initializes motor driver, LED, and buzzer pins, and stops the motors if an obstacle is detected within 1 meter while also activating the LED and buzzer. It allows for speed control via a potentiometer and direction change through a button press.

Uploaded by

piyush sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

3D

This document contains an Arduino code for controlling a motor using an ultrasonic sensor to detect obstacles. The code initializes motor driver, LED, and buzzer pins, and stops the motors if an obstacle is detected within 1 meter while also activating the LED and buzzer. It allows for speed control via a potentiometer and direction change through a button press.

Uploaded by

piyush sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <NewPing.

h>

// ultrasonic sensor pins

#define TRIGGER_PIN 10

#define ECHO_PIN 11

#define MAX_DISTANCE 200 // Maximum distance (in cm) to detect obstacles (2 meters)

// motor driver pins

#define ENA 5 // Enable pin for Motor A

#define IN1 6 // Motor A input 1

#define IN2 7 // Motor A input 2

// LED and Buzzer pins

#define LED_PIN 9

#define BUZZER_PIN 8

// Create an Ultrasonic sensor object

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

// Variables for storing speed and direction

int motorSpeed = 0; // Motor speed (0 to 250)

boolean isForward = true; // Direction (true: Forward, false: Reverse)

void setup() {

// Initialize motor driver pins as outputs

pinMode(ENA, OUTPUT);

pinMode(IN1, OUTPUT);

pinMode(IN2, OUTPUT);

// initialize LED and Buzzer pins as outputs

pinMode(LED_PIN, OUTPUT);

pinMode(BUZZER_PIN, OUTPUT);

// motors off

stopMotors();
// turn off the LED and buzzer initially

digitalWrite(LED_PIN, LOW);

digitalWrite(BUZZER_PIN, LOW);

void loop() {

// read the distance from the ultrasonic sensor

unsigned int distance = sonar.ping_cm();

// If an obstacle is detected within 1 meter (100 cm)

if (distance <= 100) {

// Stop the motors

stopMotors();

// Turn on the LED and Buzzer

digitalWrite(LED_PIN, HIGH);

digitalWrite(BUZZER_PIN, HIGH);

// delay to indicate the LED and Buzzer activation for a short duration

delay(500);

// Turn off the LED and Buzzer

digitalWrite(LED_PIN, LOW);

digitalWrite(BUZZER_PIN, LOW);

} else {

// If the obstacle is out of the 2-meter range, turn off the LED and Buzzer

if (distance > 200) {

digitalWrite(LED_PIN, LOW);

digitalWrite(BUZZER_PIN, LOW);

// Read the input to control the speed (from a potentiometer or buttons)

int desiredSpeed = map(analogRead(A0), 0, 1023, 0, 255); // Adjust A0 to the analog input you are using.
// update motor speed

if (desiredSpeed != motorSpeed) {

motorSpeed = desiredSpeed;

analogWrite(ENA, motorSpeed);

// Update motor direction (e.g., by pressing a button)

if (digitalRead(4) == HIGH) { // Connect a button to digital pin 4 for reversing the motor

// toggle direction

isForward = !isForward;

// update motor driver inputs based on direction

if (isForward) {

digitalWrite(IN1, HIGH);

digitalWrite(IN2, LOW);

} else {

digitalWrite(IN1, LOW);

digitalWrite(IN2, HIGH);

delay(100);

//to stop the motors

void stopMotors() {

digitalWrite(IN1, LOW);

digitalWrite(IN2, LOW);

analogWrite(ENA, 0);

You might also like