motor shield current monitoring code

opps understand < not >

I can't get the serial monitor to show any value that relates to the current draw from the motor. when i test pin A1 with my multimeter it registers a voltage change appropriate to the values listed in the spec 2amps equivalent to 3.3V, i'm getting about .4v. What do i need to change on the code to read the change in voltage at pin A1 as i load up the motor?

 void loop () {
 // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  //int sensorValue=analogRead(A1);
  float voltage = sensorValue * (5.0 / 255);
 int limit =.22; //Value that triggers motor to shutoff once it has reached endpoint
  analogWrite  (motorspdB,255);
  digitalWrite (motorB,HIGH);
  digitalWrite (Bbrake,LOW);
  delay(2000);
  Serial.println(voltage);
  if (sensorValue>limit)
  {
    digitalWrite (Bbrake,HIGH);
  
  delay(5000);
}/code]
int limit =.22;

Is the same as writing

int limit =0;

I've looked at the Motor Shield rev 3 schematic at http://arduino.cc/en/uploads/Main/arduino_MotorShield_Rev3-schematic.pdf and it shows a 0.15 ohm current sense resistor on each channel. So if that's the board you are using, with 0.5A flowing you will get 0.075V at the analog input. That corresponds to an analogRead() result of 15.

but why doesn't the serial monitor change it's value if i load up the motor. Surely if the motor has load on it at the point the program reads the serial print command it will display the change from the value it would read if the motor had no load?

Because limit is 0, and even with no load the current is probably enough to give a current sense reading greater than 0.

Just tried value 10 and 20 and it makes no difference to what the serial monitor writes.

it makes no difference to what the serial monitor writes.

Why should it?

And what exactly does the serial monitor write? Preferably, get it to write the raw value returned by analogRead.

Just tried this and I get 16 as the value.

 void loop () {
 // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  //int sensorValue=analogRead(A1);
//  float voltage = sensorValue * (5.0 / 255);
 int limit =10; //Value that triggers motor to shutoff once it has reached endpoint
  analogWrite  (motorspdB,255);
  digitalWrite (motorB,HIGH);
  digitalWrite (Bbrake,LOW);
  delay(2000);
  Serial.println(sensorValue);
  if (sensorValue>limit)
  {
    digitalWrite (Bbrake,HIGH);
    delay(5000);
}/code]

Think i just worked it out. Sensor value =analogRead needed to be before serial print

  analogWrite  (motorspdB,255);
  digitalWrite (motorB,HIGH);
  digitalWrite (Bbrake,LOW);
  delay(2000);
   sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  delay(2000);
  if (sensorValue>limit)
  {
    digitalWrite (Bbrake,HIGH);
    delay(5000);/code]

Is the motor actually turning? What I would expect fro that code is:

  1. Motor is initially stationary. So your initial sensor reading is 0.

  2. You make the analogWrite call to apply power. As the motor is stationary, the current quickly rises to the stall current of 0.5A.

  3. You wait 2 seconds and then print the initial sensor value (0).

  4. The sensor value for 0.5A is around 15, so the next time you enter loop() and read the sensor, this exceeds your limit.

  5. After another 2 seconds, you print the sensor value (15) and then turn the motor off for 5 seconds.

  6. The cycle repeats.

Is that what you are seeing? (OK you are getting 16 instead of 15, but that's near enough).

Yep got it running well now. Here's the code. I've got the motor running back and forth until i apply a high load near the stall value and above the limit i set and the motor pauses for 7000`ms. Cracking. Thanks Gys, thanks DC. I'll see if i can build this into an RC signal now to switch on and off.

int Bbrake=8;//brake signal for motor B
int Abrake=9;//brake signal for motor A
int motorspdB=11;//PWM speed signal for motor B
int motorspdA=3;//PWM speed signal for motor A
int motorA=12;
int motorB=13;
int sensorPinB = A1;    // select the input pin for the current
int sensorValue = 0;  // variable to store the value coming from the sensor
//int limit=A0

void setup () {
  pinMode (13, OUTPUT);//motor B enable and direction HIGH or LOW
  pinMode (12, OUTPUT);//motor A enable and direction HIGH or LOW
  pinMode (8, OUTPUT);//Brake motor B
  pinMode (9, OUTPUT);//Brake motor A
  Serial.begin(9600);//start serial print monitor

  }
  void loop () 
  {
  int limit =110; //Value that triggers motor to shutoff once it has reached endpoint
  analogWrite  (motorspdB,255);
  digitalWrite (motorB,HIGH);
  digitalWrite (Bbrake,LOW);
  delay(1000);
  sensorValue = analogRead(sensorPinB);
  Serial.println(sensorValue);
  delay(1000);
  if (sensorValue>limit)
  {
  digitalWrite (Bbrake,HIGH);
  delay(500);
  analogWrite  (motorspdB,0);
  digitalWrite (Bbrake,LOW);
  delay(7000);
  }
  digitalWrite (motorB,LOW);
  digitalWrite (Bbrake,LOW); 
  delay(1000);
}/code]

I want to stop the motors once the A0 and A1 pins detect a set parameter for overcurrent i.e. motor stall?

I see.

  delay(1000);
  delay(1000);
  delay(500);
  delay(7000);
  delay(1000);

You've got anywhere between 3 seconds and 10.5 seconds between when the motor stalls and the time you do anything about it. Be sure to give those motors a kiss (goodbye) now and then.

Will do Paul Thnx.

Was just surfing the net and came across your thread!!! Quickly signed up for this forum and will follow this thread. I think that what you are doing is exactly what I am trying to do. I want to be able to hook up a pair of brushed dc motors to work some retractable landing gear that I am designing. The motors need to stop (and hopefully reverse a little) when they reach the mechanical stops. I assume this will cause a rise in the current when the motor is stalled. The Arduino Uno V3 board says it is current sensing, so I would hope this could be used to signal the motors to stop.

Am I correct in assuming that this is what your thread is about. I also want to use an RC signal to control the motors. So I would need to be able to convert the RC signal to hi/lo analog I guess. I have less than ZERO experience with programming and Arduino, but I'm willing to try to learn. Please let me know how your code is working. I will order an Arduino Uno and a motor controller ASAP XD XD XD

Thanks...

Ron

You will find plenty of examples showing how to read a servo pwm signal to get the corresponding servo position.

Rather than stalling the motor out and trying to detect that, it would be much better to put a limit switch in to tell you when the gear is fully up, and another to tell you when it is fully down. It's simple to do in electrical terms and software terms, and would give you a much more robust solution which is also kinder on your retract mechanism.

I have considered using limit switches, but the miniature scale of what I am doing would make this a little difficult. I suppose I could use some sort of linear scale to detect the position of the retract mechanism as it moves along the worm drive. On the other hand, motors are fairly inexpensive and hopefully will last a little while. I will however investigate your suggestion. I am so new to this that I don't yet understand all of what the Arduino is capable of. Have taken the first step and ordered a couple of Unos and a motor controller. :~

Thanks

sorry for not getting back on the forum for a while. yes have made great progress with this and have got the U/C to work well the gear doors open, gear lowers and stop at the end of travel with the stall current of the motor being read by the Arduino, then the gear doors close. Works well. Have used a component to convert the rc signal to a digital 5v or 0v. This has now proved unreliable so now trying to get the arduino to read the pwm. have found some great motors for the job.