(Solved) Fluctuating Voltage Measuring Across 12v Dc Motor

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 :slight_smile:

The advice is to study up on all you can find on using PWM to control your motor speed. Look especially at what kind of a signal is PWM. Then look at the only instrument that can properly measure the voltage of a PWM signal. It is not a DVM. Nor is it an Arduino.

Paul

That divider will put 4.9V pulses on the analog input pin when running one direction and 7V when running the opposite direction and probably ruin the input pin if not the whole Arduino. You cannot read PWM pulses with the ADC, you will only see 0V sometimes and 5V other times, depending on when you sample the pulse train.

Paul_KD7HB:
The advice is to study up on all you can find on using PWM to control your motor speed. Look especially at what kind of a signal is PWM. Then look at the only instrument that can properly measure the voltage of a PWM signal. It is not a DVM. Nor is it an Arduino.

Paul

Thanks for the advice, now i learn something new about reading PWM Signal :slight_smile:

JCA79B:
That divider will put 4.9V pulses on the analog input pin when running one direction and 7V when running the opposite direction and probably ruin the input pin if not the whole Arduino. You cannot read PWM pulses with the ADC, you will only see 0V sometimes and 5V other times, depending on when you sample the pulse train.

okay thanks, so rather than using ADC script like "temp = (analogvalue * 5) / 1023.0;" , maybe i should try something like The pulseIn() Function ?

Somehow, i tried to measure it using multimeter and it gives a stable 6.6v or 6.68v

Your multimeter should be OK, as it reads out the average voltage over some interval.

jremington:
Your multimeter should be OK, as it reads out the average voltage over some interval.

Yes, the multimeter works really fine when i tested to 12v power adaptor or brandnew 1.5v battery :slight_smile:

any other suggestion maybe ? i'm so confused right now. Should i use another component to measure it, or change the pin connection and script or any other else maybe ?

Why would you want to measure this? You control the PWM, you know the motor supply voltage.

What you might want to measure is the motor back-EMF in zero-current conditions, which means turning
off one-side of the H-bridge, grounding the other, waiting a little and then measuring the motor voltage -
that will give a useful value directly proportional to the motor speed. However you have to interrupt driving the motor to do this.

So what are you actually trying to achieve, I sense a bit of an xyproblem here.

MarkT:
Why would you want to measure this? You control the PWM, you know the motor supply voltage.

Maybe i sounded silly but how do i know my voltage :confused: , the voltage reading of my circuit was fluctuate like sometimes 12v go back to 1v and then up again 6v. So idont know the exact voltage of my drawing if i only see it from serial monitor of arduino. If i measure it with multitester yes sure it gives the stable value.
Some say i can't read pwm to analog pin arduino, so how do i read the output of my voltage divider then ? :confused:

Well, if you insist on trying, you need to "clock" or "time" the reading of the voltage pulse with the PWM pulse. Determine when the pulse goes "high" and then start the read.

Paul

Noir97:
Maybe i sounded silly but how do i know my voltage :confused: , the voltage reading of my circuit was fluctuate like sometimes 12v go back to 1v and then up again 6v. So idont know the exact voltage of my drawing if i only see it from serial monitor of arduino. If i measure it with multitester yes sure it gives the stable value.
Some say i can't read pwm to analog pin arduino, so how do i read the output of my voltage divider then ? :confused:

The voltage of your supply you know I hope!

The duty cycle of the PWM is set by you with analogWrite.

So you know everything.

Dear all, thanks for the suggestion, by the way the fluctuation was gone by using bypass capacitor, 100microF parallel with 100nanoF in analogin of the voltage divider. :slight_smile: