0% found this document useful (0 votes)
7 views7 pages

Arduino Ultrasonic Sensor Control

The document contains an Arduino program that utilizes ultrasonic sensors to measure distances and control a robot's movement based on obstacle detection. It defines functions for measuring distances from three sensors and implements logic for navigating around obstacles by moving forward, backward, or turning. The setup and loop functions manage the initialization of pins and the continuous measurement and response to detected distances.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Arduino Ultrasonic Sensor Control

The document contains an Arduino program that utilizes ultrasonic sensors to measure distances and control a robot's movement based on obstacle detection. It defines functions for measuring distances from three sensors and implements logic for navigating around obstacles by moving forward, backward, or turning. The setup and loop functions manage the initialization of pins and the continuous measurement and response to detected distances.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

int trig1 = A0;

int echo1 = A1;


int trig2 = A2;
int echo2 = A3;
int trig3 = A4;
int echo3 = A5;

int in1 = 4;
int in2 = 5;
int in3 = 6;
int in4 = 7;
int ENA = 3;
int ENB = 11;
int LeftSpeed = 125;
int RightSpeed = 130;
long leftDistance = 0,middleDistance = 0,rightDistance = 0;
long pingTime,distance;
float speedSound = 0.0343;
int DIS = 25;

long leftMeasurement()
{
digitalWrite(trig1,LOW);
delayMicroseconds(2);
digitalWrite(trig1,HIGH);
delayMicroseconds(10);
digitalWrite(trig1,LOW);
pingTime = pulseIn(echo1,HIGH);
distance = (pingTime/2)*speedSound;
return(distance);
}

long middleMeasurement()
{
digitalWrite(trig2,LOW);
delayMicroseconds(2);
digitalWrite(trig2,HIGH);
delayMicroseconds(10);
digitalWrite(trig2,LOW);
pingTime = pulseIn(echo2,HIGH);
distance = (pingTime/2)*speedSound;
return(distance);
}
long rightMeasurement()
{
digitalWrite(trig3,LOW);
delayMicroseconds(2);
digitalWrite(trig3,HIGH);
delayMicroseconds(10);
digitalWrite(trig3,LOW);
pingTime = pulseIn(echo3,HIGH);
distance = (pingTime/2)*speedSound;
return(distance);
}

void setup()
{
[Link](9600);
pinMode(trig1,OUTPUT);
pinMode(trig2,OUTPUT);
pinMode(trig3,OUTPUT);
pinMode(echo1,INPUT);
pinMode(echo2,INPUT);
pinMode(echo3,INPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
moveStop();
}

void loop()
{
leftDistance = leftMeasurement();
delay(10);
middleDistance = middleMeasurement();
delay(10);
rightDistance = rightMeasurement();
delay(10);
[Link]("leftDistance = ");
[Link](leftDistance);
[Link]("cm /");
[Link]("middleDistance = ");
[Link](middleDistance);
[Link]("cm /");
[Link]("rightDistance = ");
[Link](rightDistance);
[Link]("cm");

// Obstacle on left side


if(leftDistance < DIS && middleDistance > DIS && rightDistance > DIS)
{
turnRight();
delay(25); // turn lightly right
}
// Obstacle on front
else if(leftDistance > DIS && middleDistance < DIS && rightDistance >
DIS)
{
moveStop();
delay(400);
if(leftDistance > rightDistance)
{
turnLeft();
delay(400); // turn 90 degree left
}
if(leftDistance < rightDistance)
{
turnRight();
delay(400); // turn 90 degree right
}
}
// Obstacle on right side
else if(leftDistance > DIS && middleDistance > DIS && rightDistance <
DIS)
{
turnLeft();
delay(25); //turn lightly left
}
// Obstacle on front & left side
else if(leftDistance < DIS && middleDistance < DIS && rightDistance >
DIS)
{
moveStop();
delay(400);
turnRight();
delay(400); // turnRight 90 degree
}
// Obstacle on front & right side
else if(leftDistance > DIS && middleDistance < DIS && rightDistance <
DIS)
{
moveStop();
delay(400);
turnLeft();
delay(400); // turnLeft 90 degree
}
// Obstacle on left & right side
else if(leftDistance < DIS && middleDistance > DIS && rightDistance <
DIS)
{
if(leftDistance > rightDistance)
{
turnLeft();
delay(50); // shift left
}
if(leftDistance < rightDistance)
{
turnRight();
delay(50); // shift right
}
}
// Obstacle on all 3 side
else if(leftDistance < DIS && middleDistance < DIS && rightDistance <
DIS)
{
moveStop();
delay(1000);
moveBackward();
delay(1000);
turnLeft();
delay(800); //turn 180 degrees
}
else
{
moveForward();
}
}

void moveForward()
{
[Link]("Move Forward");
analogWrite(ENA,LeftSpeed);
analogWrite(ENB,RightSpeed);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}

void moveBackward()
{
[Link]("Move Backward");
analogWrite(ENA,LeftSpeed);
analogWrite(ENB,RightSpeed);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}

void turnRight()
{
[Link]("Turn Right");
analogWrite(ENA,LeftSpeed);
analogWrite(ENB,RightSpeed);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}

void turnLeft()
{
[Link]("Turn Left");
analogWrite(ENA,LeftSpeed);
analogWrite(ENB,RightSpeed);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}

void moveStop()
{
[Link]("Move Stop");
analogWrite(ENA,LOW);
analogWrite(ENB,LOW);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
const int trigPin = 6;

const int echoPin = 5;

void setup() {

[Link](115200);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

[Link]("Khoi dong cam bien sieu am...");

void loop() {

long duration;

int distance;

// Gửi xung trigger

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Đo độ rộng xung echo

duration = pulseIn(echoPin, HIGH, 30000); // timeout 30ms


// Tính khoảng cách (cm)

distance = duration * 0.034 / 2;

// In ra Serial

[Link]("Khoang cach: ");

[Link](distance);

[Link](" cm");

delay(500);

Common questions

Powered by AI

The setup function in an Arduino script initializes the serial communication and sets the modes for various pins required for input and output operations. It ensures all connected components, such as motors and sensors, are ready for deployment by setting these pins correctly. The loop function contains the main logic for the program and continuously executes its code to maintain operations like measuring distances and making navigation decisions based on sensor data. For example, it repeatedly reads the distance from sensors and makes a decision to move the robot forward, backward, stop, or turn based on the sensed surroundings.

The turnRight and turnLeft functions are implemented by controlling the direction and speed of the motors using specific GPIO settings, which allow the robot to pivot around its wheels. For turnRight, the left motor moves forward while the right motor moves backward, and vice versa for turnLeft. This steering mechanism is effective for obstacle avoidance as it enables direction adjustments in narrow spaces by only using a small turn radius. However, the effectiveness is limited by response speed and the physical turning capabilities of the robot hardware, which rely on precise motor control.

Serial communication in this Arduino setup assists in debugging and monitoring by providing a continuous stream of data output about the robot's interactions and sensor readings to the computer. Serial.print statements are used throughout the code to output the measured distances from the sensors and the current action taken by the robot (e.g., moving forward, turning left/right), allowing users to interpret real-time data on the robot's decision-making and navigation path, facilitating the identification of unexpected behavior and troubleshooting such issues.

The moveForward function controls the robot to move forward by setting the ENA and ENB pins to the speed values of LeftSpeed and RightSpeed respectively, and sets the direction pins in1, in2, in3, and in4 such that both motors rotate forward. The moveStop function halts the robot's movement by setting the ENA and ENB pins to LOW, effectively ceasing power delivery to the motor channels, and turning all direction pins to LOW, which stops the motor rotation entirely.

The logical structure that ensures the robot stops when obstacles are detected on all sides is a conditional check in the loop function, comparing leftDistance, middleDistance, and rightDistance against the DIS threshold. If all three are less than DIS, moveStop and moveBackward functions are executed sequentially, followed by a turnLeft, allowing the robot to reverse and rotate to find an open path. This structure is necessary to prevent the robot from entering potential deadlock or collision scenarios, enabling it to retreat safely when surrounded.

The decision-making process in the robot's navigation when faced with obstacles is structured hierarchically using a series of conditionals. The code prioritizes actions based on the distances reported by the left, middle, and right sensors. It first checks for clear pathways by evaluating obstacles on the left, front, and right individually, and then combinations like front and left, front and right, or all sides. The conditions are prioritized to handle immediate threats with specific actions like stops, turns, or reverses before resorting to forward movement in clear conditions. The approach seeks efficiency by resolving conflicts via sensor readings aimed at minimal detouring while maintaining safety.

The specific motor speeds LeftSpeed and RightSpeed, set to 125 and 130 respectively, are used to ensure forward movement and turning actions compensate for possible motor differences in torque. This slight speed difference helps the robot move in a straight line. However, adjustments may be necessary based on the physical characteristics of the environment or mechanical wear. Improvements could include dynamic speed control that adjusts power based on feedback from additional sensors or a PID control loop for more precise navigation and alignment with designated paths.

The DIS variable acts as a threshold distance to determine whether an obstacle is close enough to warrant a change in the robot's movement path. The value of DIS is set to 25 cm, which is used to compare with measured distances from the sensors to decide the next actions, such as stopping, turning, or continuing forward. If any measured distance is less than DIS, it indicates that the robot is approaching an obstacle, prompting programmed actions to avoid collisions.

Timeout implementation in the pulseIn function impacts sensor readings by preventing indefinite blocking of the program if an echo pulse is not received within a specified time. This timeout ensures that the program can proceed if an object is too far or the echo is absorbed, rather than endlessly waiting. The timeout value (30000 microseconds in the given code) must be tuned to balance sensitivity and responsiveness, allowing the program to resume operation promptly in cases of anomalous readings while ensuring reliable distance measurements under normal conditions.

The primary function of the trig and echo pins in the Arduino ultrasound sensor setup is to initiate and receive the ultrasound signal, respectively. The trig pin sends out a short pulse when activated, which causes the ultrasound sensor to emit a sound wave. The echo pin captures the duration until the wave returns after bouncing off an object, which is then used to calculate the distance of the object from the sensor.

You might also like