Unable to sense current with Arduino Motor Shield

Hi!

I'm working on a project where I want to sense the current of a DC-motor to know when it has reached it's end points.
I'm using the Aduino Motor Shield, reading the value on A0. The problem is that this value is unchanged when the motor is stopped, running or running with high load. It always reads the same, 850 when using channel A and 1018 when using channel B.
I don't know if I have a broken shield or if there's some other problem.
Any help would be greatly appriciated.

// Motor A
const int dirA = 12;
const int pwmA = 3;
const int brakeA = 9;
const int currentA = A0;

// Buttons
const int leftBtn = 7;
const int rightBtn = 4;

// Globals
int currentLow = 2000;
int currentHigh = 0;

void setup() {
  // put your setup code here, to run once:
  
  pinMode(dirA, OUTPUT);
  pinMode(pwmA, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(leftBtn, INPUT_PULLUP);
  pinMode(rightBtn, INPUT_PULLUP);
  
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:

  if (!digitalRead(leftBtn)) {
    digitalWrite(brakeA, LOW);
    digitalWrite(dirA, HIGH);
    digitalWrite(pwmA, HIGH);
  } else if (!digitalRead(rightBtn)) {
    digitalWrite(brakeA, LOW);
    digitalWrite(dirA, LOW);
    digitalWrite(pwmA, HIGH);
  } else {
    digitalWrite(brakeA, HIGH);
    delay(100);
    digitalWrite(brakeA, LOW);
    digitalWrite(pwmA, LOW);
    digitalWrite(dirA, LOW);
  }

  int current = analogRead(currentA);
  if (current < currentLow) {
    currentLow = current;
  }
  if (current > currentHigh) {
    currentHigh = current;
  }
  Serial.print(current);
  Serial.print(' ');
  Serial.print(currentLow);
  Serial.print(' ');
  Serial.println(currentHigh);
}

Hi,
Welcome to the forum.

We need to know the type of shield you have, there are many different Arduino Motor Shields unfortunately.

Can you post a link to specs/data or where you bought it please?

A picture of your shield will help.

Thanks.. Tom.. :slight_smile:

Thanx!

This is the shield I'm using: Arduino Motor Shield Rev3 — Arduino Official Store

Hi,

first of all analogRead returns int between 0 and 1023 (A/D converter has 2^10 resolution).

So If you want to get value of current you need to recalculate it.

Motor shield has resolution for current sensing 3.3V/2A (if flowing current through motor reach 2A you get 3.3V on A0).

For value of current (mA) you can code it as below:

float current;
current = analogRead(currentA) * (2000 / 1023);

I hope it should help you.

Change:

float current;
current = analogRead(currentA) * (2000 / 1023);

to:

float current = analogRead(currentA) * (2000.0 / 1024.0) ;

2000 / 1024 = 1 in C++, not 1.953125, as integer arithmetic always truncates.

Thanx for your help. Turns out it was a faulty shield. Bought another one and now I actually get a higher value when the motor is under load.