the current of DC motor with Monster moto shield

Hi
I'm using the monster moto shield VNH2SP30 to control a 12V DC motor but, the current pin shows a constant value (16)!

my code is:

#define BRAKE 0
#define CW    1
#define CCW   2
#define CS_THRESHOLD 15   // Definition of safety current (Check: "1.3 Monster Shield Example").

//MOTOR 1
#define MOTOR_A1_PIN 7
#define MOTOR_B1_PIN 8

//MOTOR 2
#define MOTOR_A2_PIN 4
#define MOTOR_B2_PIN 9

#define PWM_MOTOR_1 5
#define PWM_MOTOR_2 6

#define CURRENT_SEN_1 A2
#define CURRENT_SEN_2 A3

#define EN_PIN_1 A0
#define EN_PIN_2 A1

#define MOTOR_1 0
#define MOTOR_2 1

short usSpeed = 150;  //default motor speed
unsigned short usMotor_Status = BRAKE;
 
void setup()                         
{
  pinMode(MOTOR_A1_PIN, OUTPUT);
  pinMode(MOTOR_B1_PIN, OUTPUT);

  pinMode(MOTOR_A2_PIN, OUTPUT);
  pinMode(MOTOR_B2_PIN, OUTPUT);

  pinMode(PWM_MOTOR_1, OUTPUT);
  pinMode(PWM_MOTOR_2, OUTPUT);

  pinMode(CURRENT_SEN_1, OUTPUT);
  pinMode(CURRENT_SEN_2, OUTPUT);  

  pinMode(EN_PIN_1, OUTPUT);
  pinMode(EN_PIN_2, OUTPUT);

  Serial.begin(9600);              // Initiates the serial to do the monitoring 
  Serial.println("Begin motor control");
  Serial.println(); //Print function list for user selection
  Serial.println("Enter number for control option:");
  Serial.println("1. STOP");
  Serial.println("2. FORWARD");
  Serial.println("3. REVERSE");
  Serial.println("4. READ CURRENT");
  Serial.println("+. INCREASE SPEED");
  Serial.println("-. DECREASE SPEED");
  Serial.println();

}

void loop() 
{
  char user_input;   

  
  
  while(Serial.available())
  {
    user_input = Serial.read(); //Read user input and trigger appropriate function
    digitalWrite(EN_PIN_1, HIGH);
    digitalWrite(EN_PIN_2, HIGH); 
     
    if (user_input =='1')
    {
       Stop();
    }
    else if(user_input =='2')
    {
      Forward();
    }
    else if(user_input =='3')
    {
      Reverse();
    }
    else if(user_input=='4')
    {
      Serial.println(A2);
    }
    else if(user_input =='+')
    {
      IncreaseSpeed();
    }
    else if(user_input =='-')
    {
      DecreaseSpeed();
    }
    else
    {
      Serial.println("Invalid option entered.");
    }
      
  }
}

void Stop()
{
  Serial.println("Stop");
  usMotor_Status = BRAKE;
  motorGo(MOTOR_1, usMotor_Status, 0);
  motorGo(MOTOR_2, usMotor_Status, 0);
}

void Forward()
{
  Serial.println("Forward");
  usMotor_Status = CW;
  motorGo(MOTOR_1, usMotor_Status, usSpeed);
  motorGo(MOTOR_2, usMotor_Status, usSpeed);
}

void Reverse()
{
  Serial.println("Reverse");
  usMotor_Status = CCW;
  motorGo(MOTOR_1, usMotor_Status, usSpeed);
  motorGo(MOTOR_2, usMotor_Status, usSpeed);
}

void IncreaseSpeed()
{
  usSpeed = usSpeed + 10;
  if(usSpeed > 255)
  {
    usSpeed = 255;  
  }
  
  Serial.print("Speed +: ");
  Serial.println(usSpeed);

  motorGo(MOTOR_1, usMotor_Status, usSpeed);
  motorGo(MOTOR_2, usMotor_Status, usSpeed);  
}

void DecreaseSpeed()
{
  usSpeed = usSpeed - 10;
  if(usSpeed < 0)
  {
    usSpeed = 0;  
  }
  
  Serial.print("Speed -: ");
  Serial.println(usSpeed);

  motorGo(MOTOR_1, usMotor_Status, usSpeed);
  motorGo(MOTOR_2, usMotor_Status, usSpeed);  
}

void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)         //Function that controls the variables: motor(0 ou 1), direction (cw ou ccw) e pwm (entra 0 e 255);
{
  if(motor == MOTOR_1)
  {
    if(direct == CW)
    {
      digitalWrite(MOTOR_A1_PIN, LOW); 
      digitalWrite(MOTOR_B1_PIN, HIGH);
    }
    else if(direct == CCW)
    {
      digitalWrite(MOTOR_A1_PIN, HIGH);
      digitalWrite(MOTOR_B1_PIN, LOW);      
    }
    else
    {
      digitalWrite(MOTOR_A1_PIN, LOW);
      digitalWrite(MOTOR_B1_PIN, LOW);            
    }
    
    analogWrite(PWM_MOTOR_1, pwm); 
  }
  else if(motor == MOTOR_2)
  {
    if(direct == CW)
    {
      digitalWrite(MOTOR_A2_PIN, LOW);
      digitalWrite(MOTOR_B2_PIN, HIGH);
    }
    else if(direct == CCW)
    {
      digitalWrite(MOTOR_A2_PIN, HIGH);
      digitalWrite(MOTOR_B2_PIN, LOW);      
    }
    else
    {
      digitalWrite(MOTOR_A2_PIN, LOW);
      digitalWrite(MOTOR_B2_PIN, LOW);            
    }
    
    analogWrite(PWM_MOTOR_2, pwm);
  }
}
  pinMode(CURRENT_SEN_1, OUTPUT);

Something wrong here?

And fix the analogRead() so it uses this name. That might make the error more obvious next time.

The other thing you will find is the current sense pin follows the PWM signal. It is either on or off and analogRead() essentially gets one of those at random. Add a 0.1uF capacitor in parallel with the pin or increase your PWM frequency. Then it will show average current.

1 Like

Whoops, setting those pins as outputs may have fried them.

MarkT:
Whoops, setting those pins as outputs may have fried them.

you mean that I fried them and they don't work

koronus:
you mean that I fried them and they don't work

and there is no chance to use these pins?

Unlikely. But test the pins without the shield. Grab AnalogReadSerial off the examples menu and a spare piece of wire to test 0V, 3.3V and 5V.

MorganS:
Unlikely. But test the pins without the shield. Grab AnalogReadSerial off the examples menu and a spare piece of wire to test 0V, 3.3V and 5V.

thanks, I did and fortunately it works!

I removed OUTPUT definition for those pins but when I want to serial print the analogRead it shows a constant value yet! actually, it does not sense the current

koronus:
I removed OUTPUT definition for those pins but when I want to serial print the analogRead it shows a constant value yet! actually, it does not sense the current

does it have a special reason such as the shield? or it is because of coding?

It sounds like something is wrong. Is there anything else connected? Another shield? Make a really really simple sketch which drives the motor at some constant level and reads out the current feedback. Maybe don't use a real motor or don't use full voltage. You don't want to burn anything while just testing.

MorganS:
It sounds like something is wrong. Is there anything else connected? Another shield? Make a really really simple sketch which drives the motor at some constant level and reads out the current feedback. Maybe don't use a real motor or don't use full voltage. You don't want to burn anything while just testing.

first of all thanks for your answer, what you mean about using the real motor and full voltage, about the full voltage I think when I use PWM the motor gets special voltage and special current and it does not depend on the full voltage I mean if the maximum voltage is 6V the maximum current is X and for the other value of the voltage, for example, 3V the current must be Y and the problem is here for any value of the voltage the current sense pin just shows 0!

I mean don't use a voltage which will burn out your motor if you accidentelly analogWrite(255). If it's a large motor that makes a lot of noise then it is going to be difficult to work with it on your workbench. So use a small motor or just a resistor of an appropriate wattage.

MorganS:
I mean don't use a voltage which will burn out your motor if you accidentelly analogWrite(255). If it's a large motor that makes a lot of noise then it is going to be difficult to work with it on your workbench. So use a small motor or just a resistor of an appropriate wattage.

ok thanks, maybe I should use the external current sense module like ACS712 because I couldn't find the mistake and the reason for this problem, so is it possible to use this module with the monster moto shield?

Yes of course. Just make sure you use an analog filter appropriate to the PWM frequency you are using.

Here's an example for the VNH2SP30...
Current Sense Filter.jpg

The 10K and 1uF capacitor are the filter. This could probably be used directly on the ACS712 output.

Current Sense Filter.jpg

MorganS:
Yes of course. Just make sure you use an analog filter appropriate to the PWM frequency you are using.

Here's an example for the VNH2SP30...
Current Sense Filter.jpg

The 10K and 1uF capacitor are the filter. This could probably be used directly on the ACS712 output.

thanks

is this wiring correct?

https://ufile.io/lfawu![](https://ufile.io/lfawu)

Please look for the sticky post which teaches you how to attach images.

I looked at the picture. You connected the output of the VNH to the output of the ACS. That won't work.

MorganS:
Please look for the sticky post which teaches you how to attach images.

I looked at the picture. You connected the output of the VNH to the output of the ACS. That won't work.

ok thanks, I pasted the image URL between the but it didn't show the image, about the circuit I must connect the output of ACS to one of the analog pins of the Arduino board? and the other question is which the voltage of the capacitor is suitable? and actually what is the difference between 1microf 50V and 1microf 12V?

In my image, remove the VNH2SP30 and its 1.5K resistor. Put the ACS output there. The Arduino analog input connects to the net I labelled CS1.

All capacitors have a voltage rating. For the same capacitance, a higher voltage is a physically bigger capacitor. A bigger capacitors is more expensive. But for tiny capacitors under 1uF, anything smaller than 50V is too small to pick up with tweezers, so you don't normally see those small ones in lower voltages.

Always over-rate your capacitor voltage. Operating a 12V capacitor at 12V will shorten its life and any surge, such as merely plugging your device into power, will destroy the capacitor, start a fire and burn your house down. Even 16V is not enough on most 12V circuits, since they can be up to 14.1V when charging the battery.

MorganS:
In my image, remove the VNH2SP30 and its 1.5K resistor. Put the ACS output there. The Arduino analog input connects to the net I labelled CS1.

All capacitors have a voltage rating. For the same capacitance, a higher voltage is a physically bigger capacitor. A bigger capacitors is more expensive. But for tiny capacitors under 1uF, anything smaller than 50V is too small to pick up with tweezers, so you don't normally see those small ones in lower voltages.

Always over-rate your capacitor voltage. Operating a 12V capacitor at 12V will shorten its life and any surge, such as merely plugging your device into power, will destroy the capacitor, start a fire and burn your house down. Even 16V is not enough on most 12V circuits, since they can be up to 14.1V when charging the battery.

if you mean physicly removing(remove the VNH2SP30 from the Arduino) so how I could control the dc motor ? actually, I want to sense the current of the dc motor in different voltages so I need the VNH2SP30 to control the dc motor and the ACS to measure the current.Infact I want to connect both of them to the Arduino board