Hello I would like to add current sense on my Arduino algortihm on that pin.
The section of that code was like this but I couldn’t read a good value on that.
I am reading currentRaw value from A0 pin of arduino. I took volt_per_amp 0.95.
But the last value gave me 0.24A on not operating conditon and 0.8A on fully load. But my ossiloscope says its like >3A on fully load.
currentRaw = analogRead(currentPin);
currentVolts = currentRaw *(5.0/1024.0);
currentAmps = currentVolts/volt_per_amp;
I tried to read raw values and write them to console but the values didn’t changes. Does the arduino cannot determine the analog change?
Best regards,
Aykut
Hello, Aykut.
We have a few boards that use the MC33926. Are you using our single driver carrier?
One problem is that your volt_per_amp
value is incorrect. As explained on the product page, the current sense output is approximately 525 mV/A, though this can vary significantly from unit to unit. There might also be some zero-level offset.
Can you post some pictures of your setup, your code, and more details about how you are testing the driver? Also, how are you measuring the motor current; is it an oscilloscope current probe?
- Patrick
Hi Patrick,
I am attaching a diagram for my setup and posting all of my code. In my code I get a PWM value from another device(Pixhawk) and according to the mentioned values the motor stops and starts. But during the working if the ampere value goes higher than 1A the motor has to stop until the next PWM input values.
Best regards,
Aykut Arabacıoğlu
The code :
// This script is used for driving a DC motor, and limiting its current to prevent any damage. For this purpose, MC33926 Motor Driver, and Arduino Nano are used.
byte PWM_PIN = 3; // The pin used to get PWM signal
// The pin names below belong to the pins on the motor driver. The numbers indicate which pin on the Arduino Nano they're connected to.
const int pinM2D2PWM = 5; // The pin used to run the motor (high= run, low=stop)
const int pinM2IN1 = 6; // The pin for direction
const int pinM2IN2 = 9; // The pin for direction
const int pinENABLE = 10; // The pin that enables current flow
const int currentPin = A0; // The pin that receives the value of the current on DC motor
// constants
float volt_per_amp = 0.99; // resolution according to hardware page
// variables
float currentRaw; // the raw analogRead ranging from 0-1023
float currentVolts; // raw reading changed to Volts
float currentAmps; // Voltage reading changed to Amps
float limit = 0.3; // The current limit
int pwm_value; // High time of the PWM signal
bool valM2IN1;
bool valM2IN2;
bool valENABLE;
bool valM2D2PWM;
//------------------------------------------
void setup() {
// put your setup code here, to run once:
pinMode(pinM2D2PWM, OUTPUT);
pinMode(pinM2IN1, OUTPUT);
pinMode(pinM2IN2, OUTPUT);
pinMode(pinENABLE, OUTPUT);
pinMode(currentPin, INPUT_PULLUP);
pinMode(PWM_PIN, INPUT);
Serial.begin(9600); // To activate the serial monitor (9600 indicates the data rate in bits per second)
}
//----------------------------------------
void loop() {
// put your main code here, to run repeatedly:
currentRaw = analogRead(currentPin);
currentVolts = currentRaw *(5.0/1024.0);
currentAmps = currentVolts/volt_per_amp; // The current value on the motor in amperes
//-------------
if(1000<pwm_value && pwm_value<1470) { // If the PWM signal is in between 1100us and 1470 us, motor rotates in2 direction.
valM2D2PWM= HIGH;
valM2IN1= LOW;
valM2IN2= HIGH;
valENABLE= HIGH;
if(currentAmps > limit) { // If current exceeds the limit, motor stops
valENABLE = HIGH;
valM2D2PWM = LOW;
limit=0; // When motor stops, current value drops to zero and motor runs again. In order to prevent this limit is decreased.
}
}
else if(1530<pwm_value && pwm_value<1900) { // If the PWM signal is in between 1530 us and 1900 us, motor rotates in1 direction.
valM2D2PWM= HIGH;
valM2IN1= HIGH;
valM2IN2= LOW;
valENABLE= HIGH;
if(currentAmps > limit) { // If current exceeds the limit, motor stops
valENABLE = HIGH;
valM2D2PWM = LOW;
limit=0; // When motor stops, current value drops to zero and motor runs again. In order to prevent this limit is decreased.
}
}
else { // Motor stops when the PWM high time is not in the desired interval.
valM2D2PWM= LOW;;
valENABLE= HIGH;
limit=0; // When motor stops due to increase in the current, limit becomes zero. If the direction of the motor changes, motor can run again.
}
pwm_value = pulseIn(PWM_PIN, HIGH); // Measures high time of the PWM signal.
digitalWrite(pinENABLE, valENABLE);
digitalWrite(pinM2IN2, valM2IN2);
digitalWrite(pinM2IN1, valM2IN1);
digitalWrite(pinM2D2PWM, valM2D2PWM);
Serial.print("Cur : ");
Serial.println(currentAmps); //Display current value on the serial monitor.
Serial.println(pwm_value); //Display current value on the serial monitor.
delay(100);
}
Also I saw that I’ve changed the values limit and volt_per_amp value but these are for testing. At default they were 1A and 0.95.
I have a current clamp probe for current measurement.
I do not understand where you are getting your values for volt_per_amp
from. As I said before, the sensitivity of the current sense output on our MC33926 carrier is approximately 525 mV/A, so your volt_per_amp
value should be 0.525.
Ultimately though, I am not sure if the built in current sense is going to be accurate enough for what you are trying to do; you may be better off with a separate current sensor in line with your motor. If you really want to try to get this working with just the MC33926 though, I suggest making a simplified test program that prints the raw analog readings and each step of your calculation so you can see exactly what is going on.
- Patrick