0% found this document useful (0 votes)
12 views5 pages

Lab 5

The document describes using an ultrasonic sensor connected to an Arduino board to measure the distance to an object while rotating the sensor. It initializes the sensor, takes distance measurements in a loop while increasing the rotation angle, and displays the results. Distance measurements are taken over 360 degrees of rotation and stored in an array.

Uploaded by

Marimer Castelow
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)
12 views5 pages

Lab 5

The document describes using an ultrasonic sensor connected to an Arduino board to measure the distance to an object while rotating the sensor. It initializes the sensor, takes distance measurements in a loop while increasing the rotation angle, and displays the results. Distance measurements are taken over 360 degrees of rotation and stored in an array.

Uploaded by

Marimer Castelow
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/ 5

Marimer Castelow

% Connect to Arduino with ultrasonic sensor on COM3


arduinoObj = arduino('COM4', 'Uno', 'Libraries', 'Ultrasonic');

% Define ultrasonic sensor pins


trigPin = 'D12'; % Connect Trigger to pin 12
echoPin = 'D13'; % Connect Echo to pin 13

% Create ultrasonic sensor object


ultrasonicObj = ultrasonic(arduinoObj, trigPin, echoPin);

% Trigger ultrasonic sensor and measure distance


distance = readDistance(ultrasonicObj);

% Display measured distance


disp(['Measured distance from the ultrasonic sensor: ', num2str(distance), '
cm']);

Measured distance from the ultrasonic sensor: 0.50638 cm

% Actual distance measured with tape measure


actual_distance = 50; % cm

% Compare measured distance with actual distance


if abs(distance - actual_distance) < 1
disp('The measured distance matches the actual distance within
tolerance.');
else
disp('The measured distance does not match the actual distance. Please
troubleshoot.');
end

The measured distance does not match the actual distance. Please troubleshoot.

% Start data acquisition again


% Initialize variables
angle = 0; % Initialize angle of rotation
reported_distances = []; % Initialize array to store reported distances

% Slowly rotate the box


while angle <= 360 % Rotate 360 degrees
% Measure distance
distance = readDistance(ultrasonicObj);

% Store reported distance


reported_distances = [reported_distances distance];

% Display current angle and distance

1
disp(['Angle: ', num2str(angle), ' degrees, Distance: ',
num2str(distance), ' cm']);

% Increment angle
angle = angle + 10; % Increase angle gradually
pause(1); % Pause for stability
end

% Initialize reported_distances array


reported_distances = [];

% Slowly rotate the box


angle = 0;
while angle <= 360 % Rotate 360 degrees
% Measure distance
distance = readDistance(ultrasonicObj);

% Store reported distance


reported_distances = [reported_distances distance];

% Display current angle and distance


disp(['Angle: ', num2str(angle), ' degrees, Distance: ',
num2str(distance), ' cm']);

% Increment angle
angle = angle + 10; % Increase angle gradually
pause(1); % Pause for stability
end

Angle: 0 degrees, Distance: 0.48172 cm


Angle: 10 degrees, Distance: 0.48052 cm
Angle: 20 degrees, Distance: 0.47879 cm
Angle: 30 degrees, Distance: 0.4781 cm
Angle: 40 degrees, Distance: 0.475 cm
Angle: 50 degrees, Distance: 0.46845 cm
Angle: 60 degrees, Distance: 0.46966 cm
Angle: 70 degrees, Distance: 0.46534 cm
Angle: 80 degrees, Distance: 0.46655 cm
Angle: 90 degrees, Distance: 0.46638 cm
Angle: 100 degrees, Distance: 0.46552 cm
Angle: 110 degrees, Distance: 0.46552 cm
Angle: 120 degrees, Distance: 0.46966 cm
Angle: 130 degrees, Distance: 0.465 cm
Angle: 140 degrees, Distance: 0.46586 cm
Angle: 150 degrees, Distance: 0.46845 cm
Angle: 160 degrees, Distance: 0.46483 cm
Angle: 170 degrees, Distance: 0.46586 cm
Angle: 180 degrees, Distance: 0.46586 cm
Angle: 190 degrees, Distance: 0.46483 cm
Angle: 200 degrees, Distance: 0.46914 cm
Angle: 210 degrees, Distance: 0.46638 cm
Angle: 220 degrees, Distance: 0.47069 cm
Angle: 230 degrees, Distance: 0.47069 cm
Angle: 240 degrees, Distance: 0.46552 cm
Angle: 250 degrees, Distance: 0.46966 cm

2
Angle: 260 degrees, Distance: 0.46534 cm
Angle: 270 degrees, Distance: 0.46966 cm
Angle: 280 degrees, Distance: 0.46966 cm
Angle: 290 degrees, Distance: 0.47017 cm
Angle: 300 degrees, Distance: 0.47069 cm
Angle: 310 degrees, Distance: 0.46534 cm
Angle: 320 degrees, Distance: 0.47017 cm
Angle: 330 degrees, Distance: 0.46603 cm
Angle: 340 degrees, Distance: 0.47017 cm
Angle: 350 degrees, Distance: 0.47103 cm
Angle: 360 degrees, Distance: 0.46552 cm

% Generate sample data


time = 0:0.1:10; % Time vector (0 to 10 seconds)
position = sin(time); % Sample position data (sine wave for illustration)

% Plot the data


figure;
plot(time, position, 'b-', 'LineWidth', 2); % Blue solid line
xlabel('Time (s)');
ylabel('Position');
title('Position versus Time Plot');
grid on;

% Given position (position) and time (time) arrays

3
% Assuming position and time are the arrays obtained from your experiment

% Calculate the velocity using gradient


velocity = gradient(position) ./ gradient(time);

% Plot velocity versus time


figure;
plot(time, velocity, 'r-', 'LineWidth', 2); % Red solid line
xlabel('Time (s)');
ylabel('Velocity (m/s)');
title('Velocity versus Time Plot');
grid on;

% Given position (position) and time (time) arrays


% Assuming position and time are the arrays obtained from your experiment

% Calculate the velocity using gradient


velocity = gradient(position) ./ gradient(time);

% Smooth the velocity data


smoothed_velocity = smooth(velocity);

% Plot smoothed velocity versus time


figure;
plot(time, smoothed_velocity, 'b-', 'LineWidth', 2); % Blue solid line
xlabel('Time (s)');

4
ylabel('Smoothed Velocity (m/s)');
title('Smoothed Velocity versus Time Plot');
grid on;

You might also like