Mesure DC curent of a motor using an arduino nano

Thread Starter

abouabdelmajid

Joined Aug 20, 2017
13
Hi everyone

i am trying to mesure the curent of a 12v DC motor using an arduino nano. i used this diagram
schéma.png

i used this code

void setup() {
Serial.begin(9600);
}
void loop() {
float average = 0;
for(int i = 0; i < 1000; i++) {
average = average + (.044 * analogRead(A0) -3.78) / 1000;
//5A mode, if 20A or 30A mode, need to modify this formula to
//(.19 * analogRead(A0) -25) for 20A mode and
//(.044 * analogRead(A0) -3.78) for 30A mode
delay(1);
}
Serial.println(average);
}

instead of having the right value 12.92ma (measured by amperemeter) i am havung wrong negative values

Somebody can help me please
 

AlbertHall

Joined Jun 4, 2014
12,663
average = average + (.044 * analogRead(A0) -3.78)/1000
I don't understand where you got these 'magic' numbers from.
Try instead:
average = average + ((5.0 * analogRead(A0) / 1023) - 2.5) / 0.185 /1000
If you are getting negative current readings check that you have the polarity of the current connections correct.
 

ebp

Joined Feb 8, 2018
2,332
You are multiplying your ADC reading by a very small number, then subtracting a constant then dividing by 1000 and assigning that to average - every time you read the ADC.

If the intent is to average 1000 readings:

[EDIT - reworked for more sensible handling of the offset; I would be inclined to make the number of samples a power of two to allow simple shifting to calculate the mean, though this is not a big deal with a processor that is fast and has lots of code space and RAM

void loop() {
[integer of sufficient length] sum = 0; //no point using floating point to accumulate the sum of integers (I'm assuming analogRead returns an integer)

for(int i = 0; i < 1000; i++) {
sum += analogRead(A0)
delay(1);​
}

// You now have the sum of 1000 readings; divide by 1000, subtract the offset and apply the gain factors
// This reduces accumulated error and requires only one set of floating point operations (which really don't need to be floating point).
}

If your intent is a running average, then a different approach is required, usually done by subtracting the value of the oldest sample from the sum (which means you need an array, which will take a lot of RAM for 1000 samples), adding the new ADC value to the sum, putting the new value at the end of the array and updating the pointers to the beginning and end of the array, then doing the gain and mean calc's;
 
Last edited:

Colin55

Joined Aug 27, 2015
519
Before you even start with all this hoop-la, photograph the motor and show us what type it is.
12mA on 12v is very unusual.
Don't expect a 5 amp sensor to do anything at the 12mA mark.
 
Top