100% found this document useful (1 vote)
34 views

Program ESC 2019

This Arduino code controls a brushless DC motor and displays sensor data from the motor on an LCD screen. It uses hall effect sensors to commutate the motor, takes current and voltage readings from sensors, calculates RPM from a tachometer signal, and displays the results on an I2C LCD screen in real-time including RPM, duty cycle, currents, and voltages.

Uploaded by

Aris
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
34 views

Program ESC 2019

This Arduino code controls a brushless DC motor and displays sensor data from the motor on an LCD screen. It uses hall effect sensors to commutate the motor, takes current and voltage readings from sensors, calculates RPM from a tachometer signal, and displays the results on an I2C LCD screen in real-time including RPM, duty cycle, currents, and voltages.

Uploaded by

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

#include <Wire.

h> // Comes with Arduino IDE

#include <LiquidCrystal_I2C.h>

int16_t rpm;

uint8_t bldc_step, pwm_pin = 2, s=0;

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Atur LCD I2C address

// Arduino Mega 2560 sensored Motor Nema 17 BLDC (brushless DC) motor control code

ISR (PCINT0_vect){

if(pwm_pin >240){

bldc_step = 0;

s=1;

}else if(pwm_pin <240 && s==1){

s=0;

pwm_pin=0;

} else{

bldc_step = PINB & 7;// Read and save hall effect sensors status (PINB: read from PORTB which is
arduino pins 8..13)

bldc_move(); // Move the BLDC motor

void bldc_move(){ // BLDC motor move function according to hall effect sensors status

switch(bldc_step){

case 1:

PORTA = 0;

delay(pwm_pin);
PORTA = 0x48;

//pwm_pin;

break;

case 2:

PORTA = 0;

delay(pwm_pin);

PORTA = 0x24;

delay(pwm_pin);

// pwm_pin = 4;

// pwm_pin;

break;

case 3:

PORTA = 0;

delay(pwm_pin);

PORTA = 0x60;

// pwm_pin = 4;

delay(pwm_pin);

break;

case 4:

PORTA = 0;

delay(pwm_pin);

PORTA = 0x90;

// pwm_pin = 6;

delay(pwm_pin);

break;

case 5:

PORTA = 0;

delay(pwm_pin);

PORTA = 0x18;

delay(pwm_pin);
break;

case 6:

PORTA = 0;

delay(pwm_pin);

PORTA = 0x84;

break;

default:

PORTA = 0;

break;

void setup() {

Serial.begin(9600); // matikan untuk kecepatan max

lcd.begin(20,4); // inisialisasi LCD dengan 20 karakter dan 4 baris

lcd.backlight();

DDRA |= 0xFC; // Configure pins 2, 3, 4, 5, 6 and 7 as outputs

PCICR = 1; // Enable pin change interrupt for pins 8 to 13

PCMSK0 = 7; // Enable pin change interrupt for pins 8, 9 and 10

// Timer2 configuration

TCCR2A = 0;

TCCR2B = 2; // Timer2 prescaler = 1/8 (Timer2 clock = 2MHz)

TCNT2 = 0; // Timer2 preload value = 0

// ADC module configurationx

ADMUX = 0x60; // Configure ADC module and select channel 7

ADCSRA = 0x84; // Enable ADC module with 16 division factor (ADC clock = 1MHz)

// BLDC motor first move

bldc_step = PINB & 7; // Read hall effect sensors status (PINB: read from PORTB which is
arduino pins 8..13)

bldc_move(); // Move the BLDC motor (first move)


lcd.clear();

void teks(){

lcd.setCursor(0,0);

lcd.print(F("Rpm:"));

lcd.setCursor(0,1);

lcd.print(F("DTC:"));

lcd.setCursor(8,1);

lcd.print(F("%"));

lcd.setCursor(0,2);

lcd.print(F("V1 :"));

lcd.setCursor(0,3);

lcd.print(F("V2 :"));

lcd.setCursor(10,0);

lcd.print(F("|V3:"));

lcd.setCursor(10,1);

lcd.print(F("|A1:"));

lcd.setCursor(10,2);

lcd.print(F("|A2:"));

lcd.setCursor(10,3);

lcd.print(F("|A3:"));

void hasil1(uint32_t rpm, uint8_t spd){

// hasil tampilan data

lcd.setCursor(4,0);

lcd.print(F(" ")); // menghapus teks

lcd.setCursor(4,0);

lcd.print(rpm); //rpm speed


lcd.setCursor(4,1);

lcd.print(F(" ")); // menghapus teks

lcd.setCursor(5,1);

lcd.print(spd); // dutycycle

void hasil2(float senarus,byte pos){

lcd.setCursor(14,pos);

lcd.print(F(" ")); // menghapus teks

lcd.setCursor(14,pos);

lcd.print(senarus);

void hasil3(float senteg,byte posx, byte posy){

lcd.setCursor(posx,posy);

lcd.print(F(" ")); // menghapus teks

lcd.setCursor(posx,posy);

lcd.print(senteg);

uint16_t senrpm(){

uint16_t Rpm,a;

a = analogRead(A1);

Rpm = a*(5.0*240/247);

return Rpm;

float sensorarus(int16_t RawValue){

byte mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module

int32_t ACSoffset = 2531


,Samples=0;

int16_t AcsValue=0;

float Voltage = 0;

float Amps = 0;

for (int8_t x = 0; x < 40; x++){ //Get 20 samples

AcsValue = analogRead(RawValue); //Read current sensor values

Samples = Samples + AcsValue; //Add samples together

delay (3); // let ADC settle before next sample 3ms

Samples = (Samples / 40);

Voltage = (Samples / 1023.0) * 5000; // Gets you mV

Amps = ((Voltage - ACSoffset) / mVperAmp);

return Amps;

float sensortegangan(float tegangan,float selisih){

//tegangan = round(tegangan*5/1023.0); //tegangan keluaran sensor

tegangan = (tegangan*5/1023.0);

//kalibrasi saat 9v input = 1.83v output

tegangan = tegangan-selisih;//kalibrasi nilai sensor, hasil input sensor sama dengan output sensor

if(tegangan <0){

return 0;

}else{

return tegangan;

}
}

void loop() {

int8_t dtc;

pwm_pin = analogRead(A7);

dtc = map(pwm_pin, 0,255 ,0,100);

dtc = 100 - dtc;

teks();

rpm = senrpm();

hasil1(rpm,dtc);

Serial.print(F("DUTY CYCLE : "));

Serial.println (dtc);

float arus=0,tegangan = 0;

arus = sensorarus(A3);

Serial.print(F("Arus 1 : "));

Serial.println (arus);

hasil2(arus,1);

arus = sensorarus(A4);

Serial.print(F("Arus 2 : "));

Serial.println (arus);

hasil2(arus,2);

arus = sensorarus(A5 );

Serial.print(F("Arus 3 : "));

Serial.println (arus);

hasil2(arus,3);
Serial.print(F("RPM : "));

Serial.println(rpm);

Serial.print(F("Tegangan Tacho : "));

Serial.println (rpm);

tegangan = sensortegangan(analogRead(A8),0); // nilai sensor - selisih

Serial.print(F("Sensor Tegangan 1 : "));

Serial.println (tegangan);

hasil3(tegangan,4,2); //nilai dari sensor, posisi LCD X, Posisi LCD Y

tegangan = sensortegangan(analogRead(A9), 0);

Serial.print(F("Sensor Tegangan 2 : "));

Serial.println (tegangan);

hasil3(tegangan,4,3);

tegangan = sensortegangan(analogRead(A10), 0);

Serial.print(F("Sensor Tegangan 3 : "));

Serial.println (tegangan);

hasil3(tegangan,14,0);

//delay(400);

You might also like