Pages: [1] 2   Go Down
Author Topic: motor shield current monitoring code  (Read 1766 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Which part of it do you need help with?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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]
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3743
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Get rid of these for a start - the Analog pins are ready to go at startup for reading the ADC:
Code:
  pinMode(A1,INPUT);//motor current monitor B
  pinMode(A0,INPUT);//motor current monitor A
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
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'.

Quote
Code:
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.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

opps understand < not >
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:
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]
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26528
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int limit =.22;
Is the same as writing
Code:
int limit =0;
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26528
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it makes no difference to what the serial monitor writes.
Why should it?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: [1] 2   Go Up
Jump to: