You are on page 1of 13

BÀI THỰC HÀNH SỐ 2

HP: LẬP TRÌNH NHÚNG CƠ SỞ

NỘI DUNG: LẬP TRÌNH VỚI MOTOR DC, SERVO, MOTOR STEP VÀ ỨNG DỤNG

Yêu cầu: Nộp báo cáo (tương tự như bài 1)


Bai1: Motor DC:
Tham khảo:
https://www.electronicwings.com/sensors-modules/dc-motor
https://www.electronicwings.com/arduino/dc-motor-interfacing-with-arduino-uno
- Tóm tắt kiến thức về Motor DC
- Phương pháp điều khiển Motor DC bằng điều chế độ rộng xung (PWM)
- Tóm tắt một số hàm Arduino lập trình với Motor DC
a) Cho mạch như hình vẽ H1
- Tìm hiểu mạch cầu và IC L293D
- Vẽ sơ đồ proteus thực hiện mạch sau (hình H2):

Motor DC

L293D

Button
pot

Hình H1
Phân tích code chương trình:
const int POT_input = A1; /* assign ADC Channel */
bool d1 = HIGH;

bool d2 = LOW;

void setup() {

pinMode(4, OUTPUT); /* Motor control pin 1 */

pinMode(7, OUTPUT); /* Motor control pin 2 */

pinMode(3, OUTPUT); /* PWM pin for Speed Control */

pinMode(2, INPUT_PULLUP); /* Interrupt pin for direction control */

attachInterrupt(digitalPinToInterrupt(2), motor, FALLING); /* Interrupt on falling edge on pin 2 */

void loop() {

int pwm_adc;

pwm_adc = analogRead(POT_input); /* Input from Potentiometer for speed control */

digitalWrite(4,d1);

digitalWrite(7,d2);

analogWrite(3, pwm_adc / 4);

void motor(){

d1 = !d1;

d2 = !d2;

_delay_ms(200);

}
Hình H2
b) Thực hiện mạch như hinh H3

Hình H3
Code:
int motorPin = 9;// Initialize pin9 to drive dc motor
void setup() {
pinMode(motorPin, OUTPUT);//define motor pin as output
Serial.begin(9600);
while (! Serial);
Serial.println("Speed 0 to 255");
}
void loop() {
if (Serial.available()) {
int speed = Serial.parseInt(); // Parse the speed value to write on analog pin A0
if (speed >= 0 && speed <= 255) {
analogWrite(motorPin, speed);
Serial.println(speed);
}
}
}
Bài 2: Motor Servo
- Tóm tắt kiến thức motor servo
- Thư viện servo của Arduino và các hàm thông dụng
- Các phương pháp điều khiển motor servo với Arduino
a) Thực hiện mô phỏng điều khiển motor servo bằng proteus (hình H1)
#include<Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 1; pos -= 1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

Hình H1
b) Mô phỏng mạch (hình H2) với proteus
Tham khảo:
https://www.electronicwings.com/sensors-modules/servo-motor
Code chương trình:
#include <Servo.h>
Servo myservo; /* create servo object to control a servo */
int potpin = 1; /* analog pin used to connect the potentiometer */
int val; /* variable to read the value from the analog pin */
void setup() {
Serial.begin(9600);
myservo.attach(9); /* attaches the servo on pin 9 to the servo object */
}
void loop() {
val = analogRead(potpin); /* reads the value of the potentiometer (value
between 0 and 1023) */
Serial.print("Analog Value : ");
Serial.print(val);
Serial.print("\n");
/* scale it to use it with the servo (value between 0 and 180) */
val = map(val, 0, 1023, 0, 180);
Serial.print("Mapped Value : ");
Serial.print(val);
Serial.print("\n\n");
myservo.write(val); /* sets the servo position according to the scaled
value */
delay(1000); /* waits for the servo to get there */
}
Motor-pwmservo

c) Vẽ và thực hiện mạch mô phỏng như hình H3:

Hình H3
Code:
#include <Servo.h>
Servo myservo;
int degree90 = 8;
int degree45 = 9;
int degree0 = 10;
int degree_45 = 11;
int degree_90 = 12;
void setup()
{
myservo.attach(4);
pinMode(degree90, INPUT_PULLUP);
pinMode(degree45, INPUT_PULLUP);
pinMode(degree0, INPUT_PULLUP);
pinMode(degree_45, INPUT_PULLUP);
pinMode(degree_90, INPUT_PULLUP);
}
void loop()
{
if(digitalRead(degree90) == LOW)
{
myservo.write(180);
}
if(digitalRead(degree45) == LOW)
{
myservo.write(117);
}
if(digitalRead(degree0) == LOW)
{
myservo.write(93);
}
if(digitalRead(degree_45) == LOW)
{
myservo.write(68);
}

if(digitalRead(degree_90) == LOW)
{
myservo.write(3);
}
}
Bài 3: Motor step
- Tóm tắt kiến thức motor step
- Các phương pháp điều khiển motor step
- Chip UL2003
- Thực hiện mô phỏng mạch hình H1 với proteus
Tham khảo:
https://www.electronicwings.com/sensors-modules/stepper-motor

Hình H1
Code chương trình:
void setup() {
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop() {
/* Rotation in one direction */
for(int i = 0; i<12; i++)
{
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
delay(100);
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
delay(100);
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
delay(100);
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
delay(100);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
delay(100);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
delay(100);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
delay(100);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
delay(100);
}
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
delay(100);
/* Rotation in opposite direction */
for(int j = 0; j<12; j++)
{
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
delay(100);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
delay(100);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
delay(100);
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
delay(100);
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
delay(100);
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
delay(100);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
delay(100);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
delay(100);
}
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
delay(100);
}

You might also like