Program ESC 2019
Program ESC 2019
#include <LiquidCrystal_I2C.h>
int16_t rpm;
// 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;
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)
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() {
lcd.backlight();
// Timer2 configuration
TCCR2A = 0;
ADCSRA = 0x84; // Enable ADC module with 16 division factor (ADC clock = 1MHz)
bldc_step = PINB & 7; // Read hall effect sensors status (PINB: read from PORTB which is
arduino pins 8..13)
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:"));
lcd.setCursor(4,0);
lcd.setCursor(4,0);
lcd.setCursor(5,1);
lcd.print(spd); // dutycycle
lcd.setCursor(14,pos);
lcd.setCursor(14,pos);
lcd.print(senarus);
lcd.setCursor(posx,posy);
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;
byte mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
int16_t AcsValue=0;
float Voltage = 0;
float Amps = 0;
return Amps;
tegangan = (tegangan*5/1023.0);
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);
teks();
rpm = senrpm();
hasil1(rpm,dtc);
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.println (rpm);
Serial.println (tegangan);
Serial.println (tegangan);
hasil3(tegangan,4,3);
Serial.println (tegangan);
hasil3(tegangan,14,0);
//delay(400);