Micro 131L Lab Experiment 12 13 Servo and Stepper Motor in Arduino Board
Micro 131L Lab Experiment 12 13 Servo and Stepper Motor in Arduino Board
School of Engineering
Grade
SN, FN MI _______________________
Objectives:
Materials:
void setup()
{
init_device();
}
void loop()
{
rotate_cw();
rotate_ccw();
}
void init_device()
{
//attaches the servo on pin 9 to the servo object
MyServo.attach(SERVO_PIN);
//set Servo position to 0 degree
MyServo.write(MIN_ANGLE);
//initialize LCD size
MyServo_LCD.begin(COLS, ROWS);
//clear LCD text display
MyServo_LCD.clear();
//display "MICRO131L Lab 12" at 0,0
MyServo_LCD.setCursor(COL1, ROW1);
MyServo_LCD.print("MICRO131L Lab 12");
//display "Design Apps 12.1" at 0,1
MyServo_LCD.setCursor(COL1, ROW2);
MyServo_LCD.print("Design Apps 12.1");
//wait for 2s
LONG_DEL;
//clear LCD text display
MyServo_LCD.clear();
}
void rotate_cw()
{
//goes from 0 degree to 180 degrees in steps of 1 degree
for(int pos_angle = MIN_ANGLE; pos_angle < MAX_ANGLE; pos_angle++)
{
//tell servo to go to position in variable 'pos_angle'
MyServo.write(pos_angle);
//waits 30ms for the servo to reach the position
SET_DELAY;
//read the current position of Servo motor
display_position(MyServo.read());
}
}
void rotate_ccw()
{
//goes from 180 degrees to 0 degrees
for(int pos_angle = MAX_ANGLE; pos_angle > MIN_ANGLE; pos_angle--)
{
//tell servo to go to position in variable 'pos_angle'
MyServo.write(pos_angle);
//waits 30ms for the servo to reach the position
SET_DELAY;
//read the current position of Servo motor
display_position(MyServo.read());
}
}
Servo digital_Servo;
void setup()
{
//initial external interrupt pins
attachInterrupt(INC_BUT, inc_pos, RISING); //pin 2
attachInterrupt(DEC_BUT, dec_pos, RISING); //pin 3
//attach Servo pin
digital_Servo.attach(SERVO_PIN);
Serial.begin(BAUD_RATE);
Serial.println(“MICRO131L - Microprocessor Systems Lab”);
Serial.println(“Design Application 12.2”);
DISP_DEL;
angle_pos = MIN_POS;
}
void loop() {
//read the current position angle of servo
Serial.print("CURRENT SERVO POSITION: ");
Serial.println(digital_Servo.read(), DEC);
DISP_DEL;
}
void setup()
{
//attaches the servo on pin 9 to the servo object
myServo.attach(SERVO_PIN);
Serial.begin(BAUD_RATE);
Serial.println(“MICRO131L - Microprocessor Systems Lab”);
Serial.println(“Design Application 12.3”);
SET_DELAY;
void loop()
{
//variable to read the value from the analog pin
signed int angle, adc_value;
//reads the value of the potentiometer (value between 0 and 1023)
adc_value = analogRead(POT_PIN);
//scale it to use it with the servo (value between 0 and 180)
angle = map(adc_value, MIN_ADC, MAX_ADC, MIN_ANGLE, MAX_ANGLE);
myServo.write(angle);
SERVO_DEL;
Serial.print("ADC VALUE: ");
Serial.println(adc_value, DEC);
Serial.print("ANGLE POSITION: ");
Serial.println(myServo.read(), DEC);
//sets the servo position according to the scaled value
SET_DELAY; // waits for the servo to get there
}
11. Prepare the all materials that will be used in Figure 13.1.
12. Construct the schematic diagram shown in Figure 13.1 in e-Gizmo
Universal MCU Kit.
13. In Arduino Sketch program, click New and encode or copy the program
listing 13.1 source file.
14. Open the Notepad application, then re-type or copy the header file of
program listing 13.1. Click Save as Type and select All Files.
15. Save the filename as stepper_serial.h, and click Close.
16. Add the header file in your current code by clicking Sketch > Add File.. >
then locate stepper_serial.h and click Open.
17. Save your source code having a filename Design_Apps_13_1.
18. Compile your program. If error exist, fix and re-compile until no error
detected.
19. From the Tools menu, configure the Board (ex. Arduino Uno) and Port
(ex. COM4). Note: If error occur during upload, please check the MCU
module you used, the serial port assignment, or install the USB plug-and-
play driver provided by the manufacturer. Then try to re-upload your code.
20. Observe the functionality of your code by validating the displayed output of
the circuit.
This program will demonstrate how to control stepper motor and display its
rotational position in Serial monitor.
void setup()
{
//set the speed at 60 rpm:
myStepper.setSpeed(60);
//initialize the serial port:
Serial.begin(BUAD_RATE);
Serial.println(“MICRO131L - Microprocessor Systems Lab”);
Serial.println(“Design Application 13.1”);
SET_DEL;
}
void loop()
{
//step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
SET_DEL;
This program will demonstrate how to control stepper motor and display its
rotational position in Serial monitor.
void setup()
{
//set the speed of the motor to 30 RPMs
MyStepper.setSpeed(RPM);
Serial.begin(BAUD_RATE);
}
void loop()
{
//get the sensor value
int step_reading = analogRead(POT_PIN);
//move a number of steps equal to //the change in the
//sensor reading
MyStepper.step(step_reading - previous);
1
VSS
LM016L
2
AREF VDD
3
VEE
13
PB5/SCK
12 4
PB4/MISO RS
RESET 11 5
~PB3/MOSI/OC2A RW
10 6
~ PB2/SS/OC1B E
9
MYSERVO_LCD
~ PB1/OC1A
8 7
PB0/ICP1/CLKO D0
microcontrolandos.blogspot.com
8
D1
1121
7 9
PD7/AIN1 D2
6 10
A0 ~ PD6/AIN0 D3
PC0/ADC0 5 11
A1 ~ PD5/T1 D4
ATMEGA328P-PU
PC1/ADC1 4 12
A2 PD4/T0/XCK D5
PC2/ADC2 3 13
A3 ~ PD3/INT1 D6
ANALOG IN
PC3/ADC3 2 14
A4 PD2/INT0 D7
PC4/ADC4/SDA 1
DIGITAL (~PWM)
A5 TX PD1/TXD
PC5/ADC5/SCL 0
RX PD0/RXD
VCC
DUINO1
ARDUINO UNO R3
+88.8
MYSERVO
Pin configuration of Servo and Stepper Motor
~ PB1/OC1A
1121
8 7
PB0/ICP1/CLKO PD7/AIN1
VCC
6
A0 ~ PD6/AIN0
SERVO_MOTOR
1121
7 PC0/ADC0 5
PD7/AIN1 A1 ~ PD5/T1
ATMEGA328P-PU
PC1/ADC1 4
VCC
6 A2 PD4/T0/XCK
A0 ~ PD6/AIN0 PC2/ADC2 3
PC0/ADC0 5 A3 ~ PD3/INT1
ANALOG IN
PC3/ADC3 2
+88.8
A1 ~ PD5/T1 A4 PD2/INT0
ATMEGA328P-PU
PC1/ADC1 4 PC4/ADC4/SDA 1
PD4/T0/XCK
DIGITAL (~PWM)
A2 A5 TX PD1/TXD
PC2/ADC2 3 PC5/ADC5/SCL 0
A3 ~ PD3/INT1 RX PD0/RXD
ANALOG IN
PC3/ADC3 2
+88.8
A4 PD2/INT0
PC4/ADC4/SDA 1
DIGITAL (~PWM)
A5 TX PD1/TXD
PC5/ADC5/SCL 0
RX PD0/RXD
ARD1
CTS
RTS
TXD
RXD
ARDUINO UNO R3
ARD1
INC_EXT_PB
VCC
CTS
RTS
TXD
RXD
1M/500k
10k
R1
ARDUINO UNO R3
46%
Xmodem, Ymodem, Zmodem
VCC
SERIAL MONITOR
VT52, VT100, ANSI
DEC_EXT_PB
RV1
VCC
SERIAL MONITOR
VT52, VT100, ANSI
microcontrolandos.blogspot.com
13
PB5/SCK
12
1121
7 PB4/MISO
PD7/AIN1 RESET 11
6 ~PB3/MOSI/OC2A
A0 ~ PD6/AIN0 10
PC0/ADC0 5 ~ PB2/SS/OC1B
A1 ~ PD5/T1 9
ATMEGA328P-PU
PC1/ADC1 4 ~ PB1/OC1A
+88.8
A2 PD4/T0/XCK 8
PC2/ADC2 PB0/ICP1/CLKO
microcontrolandos.blogspot.com
3
A3 ~ PD3/INT1
ANALOG IN
PC3/ADC3 2
1121
A4 PD2/INT0 7
PC4/ADC4/SDA 1 PD7/AIN1
DIGITAL (~PWM)
A5 TX PD1/TXD 6
PC5/ADC5/SCL 0 A0 ~ PD6/AIN0
RX PD0/RXD PC0/ADC0 5
STEPPER MOTOR
A1 ~ PD5/T1
ATMEGA328P-PU
PC1/ADC1 4
+88.8
A2 PD4/T0/XCK
PC2/ADC2 3
A3 ~ PD3/INT1
ANALOG IN
PC3/ADC3 2
A4 PD2/INT0
PC4/ADC4/SDA 1
DIGITAL (~PWM)
A5 TX PD1/TXD
PC5/ADC5/SCL 0
RX PD0/RXD
DUINO1
ARDUINO UNO R3
8%
VCC
1k
DUINO1
CTS
RV1
RTS
ARDUINO UNO R3
TXD
CTS
RTS
TXD
RXD
RXD
SERIAL MONITOR
SERIAL MONITOR
Note: Screen shots or captured outputs per program (at least outputs per
program)
DATA ANALYSIS / OBSERVATION
NOTE: Discussion must be based on the data gathered from data results or
observation supported by review of literature with proper reference or author in-
text citation in APA format. Do not copy-paste instead paraphrase it. Data
analysis must be minimum of 2 pages.
QUESTIONS AND ANSWERS
CONCLUSION
Books:
Electronic Books:
Print Journals:
Electronic Journals: