Hi guys,
I'm making a robot and I'm having a problem with his drive.
I'm using a H-Bridge which is getting the power from a 9V battery to drive the motor. Since i want the robot to be able to determine how far he traveled I need to make a dependency on the battery voltage. Because when the battery discharges the motor rotates slower at the same pwm rate.
And now to the problem,
i need to be able to measure the voltage during the operation of the robot. I'm using a voltage divider circuit to split the battery voltage in half so that it is within the boundaries of 5V. When I measure only the battery, everything works fine, but the problem occurs when I connect it to the H-Bridge. I include the wiring diagram and the code that i use for the H-Bridge
wiring:
sketch:
//this sketch is intended to drive the robot for a given amount of time
//and then measure the battery voltage
//and physically measure distance it has driven
//so I can find out the speed it goes at given battery voltage
#include <bateeRobo.h> //my library which contains a function to control the H-Bridge
int i = 0, beg, ende, m1 = 6, m2 = 11, aPin = 0, pwm, time;
void setup() {
Serial.begin(9600);
}
void loop() {
++i;
Serial.println("Enter pwm");
while(Serial.available()==0);
pwm = Serial.parseInt();
Serial.println("Enter time (in ms)");
while(Serial.available()==0);
time = Serial.parseInt();
Serial.print(i);
Serial.print(": ");
calibration(m1, m2, aPin, time, pwm);
}
void calibration(int pin1, int pin2, int measPin, int del, byte pwm) {
int i;
//measurement of the battery voltage before the test run
for (i = 0; i < 10; ++i ) {
beg += analogRead(measPin);
delay(10);
}
beg = beg / 10;
writeHBridge(pwm ,pin1, pin2); //set the H-Bridge pwm to 255, FWD
delay(del);
writeHBridge(0 ,pin1, pin2); //stop the motor
//measurement of the battery voltage after the test run
for (i = 0; i < 10; ++i) {
ende += analogRead(measPin);
delay(10);
}
ende = ende/10;
Serial.print(beg);
Serial.print(" ");
Serial.print(ende);
Serial.print(" pwm: ");
Serial.println(pwm);
}
when the H-Bridge is powered off "writeHBridge(0 ,pin1, pin2)" I'm not able to measure the 1/2 of the battery voltage, the input in the analogPin 0 gives 1023 all the time, but when i disconnect one wire from the motor it measures correct values.(around 900)
then another interesting thing is when the motor is running at full pwm(255) the battery voltage measurement is also possible.
I assume that there are some current flows that i dont know where they originate at that do affect the measurement on the analog pin 0
i'm looking forward to your replies