Professional Documents
Culture Documents
1. INTRODUCTION
In lab assignment 1, we have learned how to use Arduino to acquire various types of the
signal from a sensor such as a digital photosensor, potentiometers, incremental rotary encoder.
In this lab assignment, we will turn our attention to actuator control, which is an important part
of an automatic system.
The goal of this lab is to learn how to control a DC servo motor using the Arduino
microcontroller Mega 2560 board (shown in Figure 1) and the L298P Motor Shield driver
(shown in Figure 2). We take a look at some basic techniques for controlling DC motors. We
will use the reading from a rotary encoder attached to the back of the motor as our feedback
signal. We will learn how to design a controller for the motor, and drive the motor to a set-
point.
2. LAB DEVICES
2.1 Arduino Mega 2560 Board
2.5 Screwdriver
Used to screwing and unscrewing screws on the Control System Kit 02.
3. PROCEDURE
3.1 Set up your Arduino Programming Environment
You should set up the Arduino programming environment and Encoder Library on your PC.
You can review the guideline in Lab assignment 1 to install the software.
3.2 Wiring diagram and electrical circuit
Device Name Control System Kit 02 Arduino Mega
+5V GND 8 2 3 A0 A1 A2
Driver Shield
OUT A B
GND (8) (2) (3)
KP KD
(A0) (A2)
KI
(A1)
Figure 7. Wiring diagram between Mega 2560 board, a motor shield and the Kit 02
M+ M+
12V
12V M-
M-
GND GND
int pwm_value = 0;
void setup() {
pinMode(DIR1, OUTPUT);
pinMode(DIR2, OUTPUT);
}
void loop() {
pwm_value = 100; // PWM value
digitalWrite(DIR1,HIGH); // change the direction of DC motor
analogWrite(SPEED1, pwm_value); //change the speed of DC motor
}
long motorPos = 0;
void setup() {
Serial.begin(9600); // setup serial
myEncoder.write(0); // reset encoder counter to 0
}
void loop() {
motorPos = myEncoder.read(); // read pulses of encoder
Serial.print("Pulses of encoder: "); // show counter on screen
Serial.println(motorPos);
}
3.5 Potentiometer
The Arduino microcontroller can read analog inputs between 0 and 5 V and generate
corresponding integer values. This is accomplished using the analogRead() function. It takes
about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is
about 10,000 times a second. We can use this function to get the analog value of a potentiometer
with the example below:
void setup() {
Serial.begin(9600); // setup serial
}
void loop() {
ADvalue = analogRead(POT1); // read AD value
Serial.print("Value of Potentiometer 1: "); // show value on screen
Serial.println(ADvalue);
}
void setup() {
Serial.begin(9600); // setup serial
pinMode(PHOTO_SENSOR, INPUT); // set the digital pin 8 as input
}
void loop() {
if (digitalRead(PHOTO_SENSOR)== LOW)
{
Serial.print("There is a obstacles ");
}
else
{
Serial.print("There is nothing ");
}
}
START
Position control
NO
START
Speed control
NO
4. REFERENCE
[4] howtomechatronics.com, How Rotary Encoder Works and How To Use It with
Arduino, 2016. Available: https://howtomechatronics.com/tutorials/arduino/rotary-
encoder-works-use-arduino/. [Accessed: 1- May- 2018].
REPORT
Date:………………… Group:……………… Duration: 5 class hours
Full Name Student ID Signature Grade
Task 2. Typing and run the example in section 3.5 Potentiometer and answers the
question below (1 point)
Potentiometer to ADC has a minimum ADC value: adc _ max .............
1024
Potentiometer to ADC has a maximum ADC value: adc _ min .............
0
The resolution of AD convert of Arduino 2560 is n .........
10 bits
Task 3. Wire program to read signal of A and B channel of the encoder, determine the
resolution of the encoder (1 point)
800
Number of pulse per round of encoder : res _ encoder ............. (ppr)
Determine the equation to calculate the speed of motor if the Arduino receives pulses
in 100 milliseconds. 600ppm
speed ........................................................................................
600n/800 (rpm)
speed ........................................................................................
600n/800 x 360/60 (degree/s)
Task 4. Typing and run the example in section 3.3 Method control of DC motor (1 point)
Can you run the code in section successful? Yes No
Modify your code to add a value 30 to a pwm_value variable every 1 second. If the to
pwm_value > 255, it will be set to 0. (Hint: use For loop and delay function)
0% 25% 50% 75% 100%
Task 5. Use your code in task 4 then modify it to reverse the direction of rotation of motor
as having an obstacle in front of the photoelectric sensor? (1 point)
Task 6. Write a program to change the speed of motor depend on the value ADC of the
potentiometer. As we turn the potentiometer from minimum value to maximum value,
why is the speed of motor changed many times? Please fix this problem? (1 point)
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
Task 7. Write a program to control the motor to the angle = 900 (1.5 point)
0% 25% 50% 75% 100%
Task 8. Write a program to control the motor rotate to angle 900 if we put an obstacle in
front of the photoelectric sensor; otherwise, the motor will return to 00 (1.5 points)
0% 25% 50% 75% 100%
Task 9. Write a program to control the speed of motor = 500 rpm (1.5 point)
0% 25% 50% 75% 100%