0% found this document useful (0 votes)
2 views6 pages

codeDC MOTOR R2SA

This document contains code for controlling a DC motor using an Arduino UNO, BTS7960 driver, and a 5V encoder. The motor operates at 24V with an encoder providing 2000 pulses per revolution, and the voltage can be adjusted via the Serial Monitor. The code includes setup for motor control, reading encoder pulses, and calculating RPM based on input voltage and pulse count.

Uploaded by

nghiale.201104
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

codeDC MOTOR R2SA

This document contains code for controlling a DC motor using an Arduino UNO, BTS7960 driver, and a 5V encoder. The motor operates at 24V with an encoder providing 2000 pulses per revolution, and the voltage can be adjusted via the Serial Monitor. The code includes setup for motor control, reading encoder pulses, and calculating RPM based on input voltage and pulse count.

Uploaded by

nghiale.201104
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//

==============================================
=======

// DC MOTOR R2SA + BTS7960 + ARDUINO UNO + ENCODER 5V

// Nguon motor: 24V

// Encoder: 2000 xung/vong

// Dieu chinh dien ap bang Serial Monitor

// Motor quay 1 chieu

//
==============================================
=======

// Chan dieu khien BTS7960

const byte RPWM = 5; // PWM quay thuan

const byte LPWM = 6; // PWM quay nguoc, trong code nay khong dung

const byte R_EN = 7; // Enable ben phai

const byte L_EN = 8; // Enable ben trai

// Chan encoder

const byte ENC_A = 2; // Pha A encoder - chan interrupt

const byte ENC_B = 3; // Pha B encoder

// Thong so motor va encoder

const float MOTOR_SUPPLY_VOLTAGE = 24.0; // Nguon cap motor 24V

const int PPR = 2000; // Encoder 2000 xung/vong

// Bien encoder

volatile long pulseCount = 0;


volatile long totalPulse = 0;

// Bien dieu khien

float inputVoltage = 0.0;

int pwmValue = 0;

// Bien tinh toc do

unsigned long lastTime = 0;

const unsigned long sampleTime = 1000; // ms

void setup() {

[Link](9600);

// Khai bao chan BTS7960

pinMode(RPWM, OUTPUT);

pinMode(LPWM, OUTPUT);

pinMode(R_EN, OUTPUT);

pinMode(L_EN, OUTPUT);

// Bat BTS7960

digitalWrite(R_EN, HIGH);

digitalWrite(L_EN, HIGH);

// Ban dau motor dung

analogWrite(RPWM, 0);

analogWrite(LPWM, 0);
// Encoder 5V

pinMode(ENC_A, INPUT_PULLUP);

pinMode(ENC_B, INPUT_PULLUP);

// Doc xung pha A

attachInterrupt(digitalPinToInterrupt(ENC_A), readEncoder, RISING);

[Link]("=====================================
=");

[Link](" DC MOTOR R2SA + BTS7960 + ENCODER");

[Link](" Nguon motor: 24V");

[Link](" Encoder: 2000 xung/vong, 5V");

[Link](" Dieu chinh dien ap bang Serial Monitor");

[Link]("=====================================
=");

[Link]("Nhap dien ap tu 0 den 24V roi nhan Enter");

[Link]("Vi du: 6, 12, 18, 24, 0");

[Link]("--------------------------------------");

void loop() {

// Doc dien ap nhap tu Serial Monitor

if ([Link]() > 0) {

String data = [Link]('\n');

[Link]();
if ([Link]() > 0) {

inputVoltage = [Link]();

// Gioi han dien ap 0 den 24V

if (inputVoltage < 0) {

inputVoltage = 0;

if (inputVoltage > MOTOR_SUPPLY_VOLTAGE) {

inputVoltage = MOTOR_SUPPLY_VOLTAGE;

// Quy doi dien ap sang PWM

pwmValue = (inputVoltage / MOTOR_SUPPLY_VOLTAGE) * 255.0;

pwmValue = constrain(pwmValue, 0, 255);

// Dieu khien motor quay 1 chieu

analogWrite(RPWM, pwmValue);

analogWrite(LPWM, 0);

[Link]("Da dat dien ap mong muon: ");

[Link](inputVoltage, 2);

[Link](" V");

[Link](" | PWM = ");

[Link](pwmValue);

}
}

// Tinh toc do moi sampleTime

unsigned long currentTime = millis();

if (currentTime - lastTime >= sampleTime) {

noInterrupts();

long pulses = pulseCount;

pulseCount = 0;

long total = totalPulse;

interrupts();

// Tinh RPM

// RPM = so_xung / PPR / thoi_gian_phut

float rpm = (pulses * 60000.0) / (PPR * sampleTime);

[Link]("U dat: ");

[Link](inputVoltage, 2);

[Link](" V");

[Link](" | PWM: ");

[Link](pwmValue);

[Link](" | Xung trong ");

[Link](sampleTime);

[Link]("ms: ");

[Link](pulses);
[Link](" | Tong xung: ");

[Link](total);

[Link](" | Toc do: ");

[Link](rpm, 1);

[Link](" RPM");

lastTime = currentTime;

// Ham ngat doc encoder

void readEncoder() {

pulseCount++;

totalPulse++;

You might also like