motor shield current monitoring code

Can anyone help with the code that will monitor pin A0 and A1 on the Arduino motor shield R3. I want to stop the motors once the A0 and A1 pins detect a set parameter for overcurrent i.e. motor stall?

Which part of it do you need help with?

I can't understand how i get the board to read the current the motor is pulling and then incorporate that in an if statement. Here's my code efforts to date. At the moment the value that prints is always 16 regardless of how much load i apply to the motor.

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 limit=A0

void setup () {
  // int sensorValue=analogRead(A1);
  pinMode(A1,INPUT);//motor current monitor B
  pinMode(A0,INPUT);//motor current monitor A
  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
  
//  int Current = 0; //Value of current
  Serial.begin(9600);
  
   
  }
  void loop () {
 
  int sensorValue=analogRead(A1);
  
 int limit =1; //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);
}
 // digitalWrite (Bbrake,LOW);
 // delay(5000);
  digitalWrite (motorB,LOW);
  digitalWrite (Bbrake,LOW); 
  delay(3000);
  digitalWrite (Bbrake,HIGH);
  delay (1000);
//  Serial.println(sensorValue);
}/code]

Get rid of these for a start - the Analog pins are ready to go at startup for reading the ADC:

  pinMode(A1,INPUT);//motor current monitor B
  pinMode(A0,INPUT);//motor current monitor A
int limit =1; //Value that triggers motor to shutoff once it has reached endpoint

That's a very low limit, just under 5mV on the current sense pin.You'll need a much higher limit than that. What current limit do you want, and what value current sense resistor are you using? Use Ohm's Law to work out the resulting voltage across the current sense resistor, then you can work out the correct value for 'limit'.

if (sensorValue<limit)

{
    digitalWrite (Bbrake,HIGH);
 
  delay(5000);
}

Is this code meant to switch the motor to braking mode if the current is over the limit? If so, you need to use > instead of < in the if-condition.

I set it at 1 to see if it worked. it did and it would work up to 16 but it made no difference to the value when i load up the motor. it stays at 16. my motor stalls at 0.5amps. i'm not using any resistors in the circuit, i'm using the Arduino motor shield, should I being using resistors somewhere in the circuit? I've got the motor connected to the outputs for the motor and a seperate 6v supply on the board nothing else is wired in. I'll look up the >instead of< code.

This help is much appreciated, thank you.

Colin

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.