Hi there. Recently i'm doing simple project and my goal is to measure the voltage across dc motor (from motor out motor driver). In this project i'm using pwm to control my 12v dc motor. To measure the voltage i'm using voltage divider, there are 6.8k ohm as r1 and 4.7k ohm as r2. But, i'm facing some problem with my devices here especially the voltage divider. The measurement of my voltage divider is so fluctuating.
At first i'm assuming that my wiring was the trouble maker and I tried to simulate and resolve it in proteus but still i have no idea what's going on.
My component for this project are 12v dc motor, Arduino Uno, L298n Motor Driver, and voltage divider circuit. For further description there's an attachment of my wiring and below here is my code.
const byte pin_a = 2; //for encoder pulse A
const byte pin_b = 3; //for encoder pulse B
const byte pin_fwd = 4; //for H-bridge: run motor forward
const byte pin_bwd = 5; //for H-bridge: run motor backward
const byte pin_pwm = 6; //for H-bridge: motor speed
int encoder = 0;
int m_direction = 0;
int sv_speed = 150; //this value is 0~255
double pv_speed;
int timer1_counter; //for timer
//==========================================================
float input_volt = 0;
float temp = 0;
float r1 = 6800; //r1 value
float r2 = 4700; //r2 value
void setup() {
pinMode(pin_a, INPUT_PULLUP);
pinMode(pin_b, INPUT_PULLUP);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(pin_fwd, OUTPUT);
pinMode(pin_bwd, OUTPUT);
pinMode(pin_pwm, OUTPUT);
attachInterrupt(digitalPinToInterrupt(pin_a), detect_a, RISING);
// start serial port at 9600 bps:
Serial.begin(9600);
//--------------------------timer setup
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
timer1_counter = 34286; // preload timer 65536-16MHz/256/2Hz
TCNT1 = timer1_counter; // preload timer
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enable all interrupts
//--------------------------timer setup
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() {
int analogvalue = analogRead(A0);
temp = (analogvalue * 5) / 1023.0;
input_volt = (temp / (r2 / (r1 + r2)));
if (input_volt < 0.1)
{
input_volt = 0.0;
}
digitalWrite(pin_fwd, 0); //run motor backward
digitalWrite(pin_bwd, 1); //run motor backward
analogWrite(pin_pwm, sv_speed); //set motor speed
Serial.print("kecepatan motor (rpm) adalah = ");
Serial.print(pv_speed); //Print speed value to Computer
Serial.print(" // ");
Serial.print("tegangan sebesar (v) = ");
Serial.println(input_volt);
delay(500);
//}
}
void detect_a() {
encoder += 1;
m_direction = digitalRead(pin_b);
}
ISR(TIMER1_OVF_vect) // interrupt service routine - tick every 0.5sec
{
TCNT1 = timer1_counter; // set timer
pv_speed = 60 * (encoder / 200.0) / 0.34;
encoder = 0;
}
so, from the code above, i set the pwm to 150, and it gives the the result some what like this (from serial monitor)
RPM//Voltage
0 1.16
79.41 9.89
128.82 12.23
134.12 6.73
133.24 12.23
132.35 8.04
133.24 12.23
134.12 6.72
134.12 12.23
The speed maybe fluctuate too but not too much, meanwhile in the voltage, there are so many ups and downs. Somehow, i tried to measure it using multimeter and it gives a stable 6.6v or 6.68v. Maybe some advice will be helpful. Thanks a lot