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

Task 7

Uploaded by

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

Task 7

Uploaded by

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

Student Name: Leonardo Diaz Martinez

Lab Section: 077L

UNIVERSITY OF PUERTO RICO


MAYAGÜEZ CAMPUS – COLLEGE OF ENGINEERING
MECHANICAL ENGINEERING DEPARTMENT

MECHATRONICS LABORATORY
INME4235

TASK: CLOSED LOOP POSITION CONTROL USING ANALOG


DISTANCE SENSOR AND ARDUINO
Objective: The objectives of this experiment are to build a simple robot using a DC motor, an analog
distance sensor and a simple mechanism; to calibrate the distance sensor; to build an H-bridge to
control the direction of the DC motor; and to build the necessary electronic hardware and program the
necessary software including a closed looped feedback control in the microcontroller to control the
robotic system.

Materials:
To carry out this task the following are provided in the laboratory:

 DC Power Supply: The DC Power supply will be used to supply voltage to the breadboard for the
circuit.
 Simple Mechanism: to convert rotational motion of the DC motor to linear motion capable of
moving a target towards and away from a target. Rail Platforms located in L-131 are used to
move a plate towards and away from an infrared sensor.
 Analog Distance Sensor: Either an infrared or ultrasonic distance sensor that measures voltage
as a function of distance to a target.
 Arduino: The Arduino will be used as the microcontroller for this experiment.
 Transistors: The transistors will be used as solid-state switches for the H Bridge.
 Resistors: The resistors are elements of electrical circuits that resist and reduce the current flow
in the circuit. They are used at the base of BJT transistors to control the current being supplied
by the Arduino to the transistor base.
 DC Motor: The DC motor will be used to produce motion in your robotic mechanism.

Procedure:
 Students need to bring a metric ruler or a metric tape measurer to the lab. Use the ruler or tape
measurer to define 10 target positions, in near equally spaced intervals, from 0cm to the system
range. The longer rail platforms in the lab can be used from 0 to 100 cm, in which case target
positions would be 10 cm apart. The first four measurements will occur at 0, 3, 5, and 10 cm, to
obtain IR sensor hysteresis measurements.

Calibration of the distance sensor

 Each student will calibrate their own distance sensor. Write an Arduino code to record the
voltage measurements from the distance sensor. Move the target to the minimum position,
measure voltage. Move the target to the next interval position, measure voltage. Perform this
through the range of your system measuring voltages at each point. If the sensor output values
fluctuate, take multiple measurements per distance and use the average of these values.
 (10 points) Copy/paste code from Arduino to your Task document. Explain code (either line by
line using comments or by blocks of code that carry out some function). DO NOT use
screenshots, rather copy/paste text.

void setup() {

// put your setup code here, to run once:

Serial.begin(9600); //Set data rate in the Serial Monitor to 9600 bits per second (baud)

void loop() {

// put your main code here, to run repeatedly:

int sensorValue = analogRead(A0); //Reads IR sensor values in Analog pin

float voltage = sensorValue*(5.0/1023.0); //Converts the values from the sensor to voltage values

Serial.println(voltage); //prints Voltage output

delay(1000); //Delays loop for 1 second


 (20 points) Fill TABLE 1 with your data.

Table 1: Sensor calibration


N Distance (cm) Voltage (V)
1 0 (0 in) 1.55
2 3 (1.181 in) 1.71
3 5 (1.968 in) 2.79

4 10 (3.937 in) 3.03

5 20 (7.874 in) 1.44

6 30 (11.811 in) 0.78

7 40 (15.748 in) 0.74

8 50 (19.685 in) 0.42

9 60 (23.622 in) 0.48

10 70 (27.559 in) 0.43

11 80 (31.496 in) 0.41

 (10 points) Using spreadsheet software, graph these values in a Voltage vs. Distance plot,
o Obtain a trendline and develop a best-fit equation relating voltage as a function of
distance.
3.5

2.5

All values
Voltage [V] 2
f(x) = 2.41208562166836 exp( − 0.262703690634685 x ) Not consider-
1.5 R² = 0.808588133313927 ing x = 0, 3, 5
cm
1 Exponential
(Not consider-
0.5 ing x = 0, 3, 5
cm)
0
-1 1 3 5 7 9 11 13
Distance [cm]

Distance-voltage trendline equation: V =2.412 x e−0.263

o NOTE: Make sure the relationship is valid only for the working range of the system
(starting at approximately 10cm for the IR sensor).
 NOTE: At least one distance sensor must be calibrated on the platform. The other sensors can
be calibrated off-platform.

H-bridge circuit and DC motor control

 (30 points) Each student will construct their own H-bridge circuit, connect a DC motor, and
change DC motor polarity.
o Use an external power supply according to the DC motor requirements.
o Connect the Arduino and power supply grounds to a common ground on the
breadboard.
o Use 4 TIP120 NPN transistors or 2 TIP120 NPN and 2 TIP125 PNP, depending on how
you want to do this.
o Connect a 1 KΩ resistance on each of the transistor base pins.
o Connect the DC motor to the H-Bridge.
o Include in your task a picture taken using a cellphone of the H-Bridge.
 (20 points) Write a program to change the DC motor polarity as a function of measured
distance.
o The code includes commands to measure distances and digitalWrite() functions to open
and close transistors on the H-Bridge.
o Use the calibration voltage-distance equation acquired previously in your code.
o Copy/paste code from Arduino to your Task document. Explain code (either line by line
using comments or by blocks of code that carry out some function). DO NOT use
screenshots, rather copy/paste text.

int pin1 = 10; //1st terminal connected to pin 10

int pin2 = 11; //2nd terminal connected to pin 11


void setup() {

// put your setup code here, to run once:

pinMode(pin1, OUTPUT); //1st terminal pin as output pin

pinMode(pin2, OUTPUT); //2nd terminal pin as output pin

void loop() {

// put your main code here, to run repeatedly:

digitalWrite(pin1, HIGH); //Sending 5V to 1st terminal

digitalWrite(pin2, LOW); //Sending 0V to 2nd terminal

delay(3000);

digitalWrite(pin1, LOW); // Sending 0V to 1st terminal

digitalWrite(pin2, HIGH); //Sending 5V to 2nd terminal

delay(3000);

 (10 points) Include in your task a picture taken using a cellphone of the motor shaft rotating in
different directions, include in the picture the serial monitor showing which pins are ON for each
rotation scenario. Use a piece of tape on the DC motor shaft to better visualize shaft rotation.
Make sure to include pictures and a schematic (using smartdraw, diagrams.net, iCircuit, or
similar) in group lab report.

Rail platform – for group report only

 Students will choose from their H-bridges and software codes to implement on the rail platform.
 Adapt the previous program to move the rail platform target forward and backwards to a
desired input distance.
 Run your code to move the plate to a list of desired distances, see TABLE 2.
o Using TABLE 2, write the actual distance values of the plate location.
o Find the error percentage between the real and desired distances.
Table 1: Sensor calibration
N Distance (cm) Voltage (V)
1 0 (0 in) 1.55
2 3 (1.181 in) 1.71
3 5 (1.968 in) 2.79

4 10 (3.937 in) 3.03

5 20 (7.874 in) 1.44

6 30 (11.811 in) 0.78

7 40 (15.748 in) 0.74

8 50 (19.685 in) 0.42

9 60 (23.622 in) 0.48

10 70 (27.559 in) 0.43

11 80 (31.496 in) 0.41

Table 2: Desired versus actual distances on rain platform


Experimental
N Input Distance (cm) % error
Distance (cm)
1
2
3

10

You might also like