Pages: 1 [2]   Go Down
Author Topic: motor shield current monitoring code  (Read 1562 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

Just tried this and I get 16 as the value.

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

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

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

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

United Kingdom
Offline Offline
Tesla Member
***
Karma: 223
Posts: 6593
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

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

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.

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Will do Paul Thnx.
Logged

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

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 smiley-lol smiley-lol smiley-lol

Thanks...

Ron
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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. smiley-confuse

Thanks
Logged

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

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

Pages: 1 [2]   Go Up
Jump to: