This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Feedback pulse measurement from DC motor

Hi guys the following is an Arduino code:

volatile long pulses = 0;
unsigned long timeO = 0;
unsigned long timeF = 0;
float encoderSpeed = 0;

void setup()
{
Serial.begin(9600);
pinMode(7, OUTPUT); // stdby
pinMode(4, OUTPUT); //ain1
pinMode(5, OUTPUT); //ain2
pinMode(6, OUTPUT); //pwmA

pinMode(11, OUTPUT); //bin2
pinMode(10, OUTPUT); //bin1
pinMode(9, OUTPUT); //pwmB


digitalWrite(7, LOW); // stdby LOW

pinMode(2, INPUT_PULLUP); //Pulse A
pinMode(3, INPUT); //Pulse B
attachInterrupt(digitalPinToInterrupt(2), detectPulses, RISING);
}

void loop()
{
int input = Serial.parseInt();

int select = input / 10000; //// switch motors
int val = input % 10000; //// notrmal parameter

int speedm = val % 1000;
int state = val / 1000;

if (select == 1);
{
if (state == 1) //clockwise
{
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
analogWrite(6, speedm); ///PWM
digitalWrite(7, HIGH); //stdby
}

if (state == 2) //clockwise
{
digitalWrite(4, HIGH);d
digitalWrite(5, LOW);
analogWrite(6, speedm); ///PWM
digitalWrite(7, HIGH); //stdby
}
}

///////////////motor 2///////////////

if (select == 2);
{
if (state == 3) //clockwise
{
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
analogWrite(9, speedm); ///PWM
digitalWrite(7, HIGH); //stdby
}

if (state == 4) //clockwise
{
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
analogWrite(9, speedm); ///PWM
digitalWrite(7, HIGH); //stdby
}
}

timeF = micros(); // Geting the final time to get the speed
encoderSpeed = ((float)pulses) / ((float)(timeF - timeO) / (6000.0));
Serial.print("Speed: ");
Serial.print(encoderSpeed); // Printing speed value
Serial.println(" rev/min");
pulses = 0; // Initialize pulses to count again
timeO = micros(); // Initialize initial time again
}

void detectPulses()
{
if (digitalRead(3) == LOW)
{ //Confirm if we are turning on CW direction.
pulses++; // Adding pulses
}
else
{ //Confirm if we are turning on CCW direction.
pulses--; // Substraing pulses
}
}

In here,

We are taking the rising edge pulse to measure the rotation per minute of the DC motor and to detect whether the DC motor rotates clockwise or anti-clockwise. I am using the NRF52840 to replace arduino here. So , How can I detect rising edge clock with NRF. It would be very useful if I can get examples. The encoders inside give a feedback (I think they are hall sensors).

Related