counting rpm - implausible values with attached DC-Motor

Hello,

this is my first post and I'm new to Arduino and it's environment.
I bought a starter Kit and been working through several excercises.

However, now I'm trying to connect three Projects into one, running a DC-Motor, reading an IR-Speedsensor (LM393 with +/- and D0) and counting rpm.

Each project works fine by itself, but if I put them together in one sketch I get implausible values for the rpm caused by the DC-Motor.

Without the DC-Motor connected everything works just fine and the rpm will be calculated only when the IR-Sensor is triggered. With the DC-Motor connected and running I'll get a "continuous stream" of implausible rpm values.

My question is, is this caused by the program I wrote or is this some sort of "backfiring" influence of the DC-Motor?

Here is the program.

I tried to upload a circuit diagram mad in Fritzing, but it has failed the security check and can not be uploaded.

Thanks in advance!

//DC-Motor
#define motorspeed 6 //declaring PWM-Pin for Motorspeed

//Infrarot Sensor
#define IR_sensor_PIN 2 //declaring PWM-Pin for IR Sensor input

//counting rpm
bool newPulse = false; //set boolean for counting rpm

volatile unsigned long prevPulseMicros = 0;//for time measurement pulses
volatile unsigned long latestPulseMicros = 0;//for time measurement pulses
volatile unsigned long deltaTime = 0;//for time measurement pulses
float rpm = 0;//variable for rpm

void setup() {
  Serial.begin(9600);
  //DC-Motor
  pinMode(motorspeed, OUTPUT);//declaring Pin as output

  //Infrarot Senso
  pinMode(IR_sensor_PIN, INPUT); //declaring IR_sensor_PIN as input

  //counting rpm
  attachInterrupt(digitalPinToInterrupt(IR_sensor_PIN), detectingPulse, FALLING);//declare ISR with FALLING
}

void loop() {
  //DC-Motor
  analogWrite(motorspeed, 128); //set Motospeed between 0-255

  //counting rpm
  if (newPulse == true) {
    deltaTime = latestPulseMicros - prevPulseMicros;//time difference between pulses
    rpm = 60 * pow(10, 6) / deltaTime; //converting time in rpm
    Serial.print("rpm = ");//printing rpm
    Serial.println(rpm);//printing rpm
    prevPulseMicros = latestPulseMicros;//set new time for previous pulse
    newPulse = false;//set pulse switch to false
  }
}

void detectingPulse() {
  latestPulseMicros = micros();//time at each pulse in micros
  newPulse = true;//set pulse switch to true
}

prankenandi:
Without the DC-Motor connected everything works just fine and the rpm will be calculated only when the IR-Sensor is triggered. With the DC-Motor connected and running I'll get a "continuous stream" of implausible rpm values.

I don't understand. If the DC motor is not connected what is producing the RPM?

Do you have a simple program that measures the RPM of the DC motor properly (assuming it is the RPM of the motor you want to measure).

...R

You're trying to print a new value for rpm every time you get an interrupt using a very slow Serial speed. Unless the speed of whatever you're measuring is VERY slow that's not going to work. What sort of values for rpm are you expecting? And what are you reading to generate the interrupts?

You could try changing Serial.begin() to something a lot faster, maybe 115200 which might help a bit. But you really need to print less often, perhaps no more than once a second.

Steve

Thanks for your reply.

"I don't understand. If the DC motor is not connected what is producing the RPM?"

If the DC-Motor is not connected I manually trigger the IR-Sensor.

However, it's probably the DC-Motor causing the implausible values.

If I run the DC-Motor and the counting on seperate Arduinos it works fine.

But when the DC-Motor and the IR-Sensor, even though the programms running on different Arduinos, have the same powersource of the breadboard (that means the power for both comes from one Arduino) I'll get implausible values.

slipstick:
You're trying to print a new value for rpm every time you get an interrupt using a very slow Serial speed. Unless the speed of whatever you're measuring is VERY slow that's not going to work. What sort of values for rpm are you expecting? And what are you reading to generate the interrupts?

You could try changing Serial.begin() to something a lot faster, maybe 115200 which might help a bit. But you really need to print less often, perhaps no more than once a second.

Steve

Hello,
Right now I'm just using the little DC-Motor in the starterkit. Later I would like to read up to 15...20.000 rpm.
To generate the interrupts I've cut a little recess in the driving disc attached to the DC.
So, should I rather accumulate 10 or 20 values and take the mean value or wait e.g. 1 sec after each interrupt before a new counting starts?

Are you getting spurious interrupts from motor noise?

Hi,
Can you please post a circuit diagram showing how you have your hardware connected?
How have you got your DC motor powered?

Thanks.. Tom.. :slight_smile:

TheMemberFormerlyKnownAsAWOL:
Are you getting spurious interrupts from motor noise?

Probably yes.
When the DC Motor and the IR-Sensor have the same powersource (from one Arduino), even though counting and running the DC runs on two different Arduinos, I'll get implausible values, like very quick interrupt.
Doing both on seperate Arduinos completely everything works fine.

TomGeorge:
Hi,
Can you please post a circuit diagram showing how you have your hardware connected?
How have you got your DC motor powered?

Thanks.. Tom.. :slight_smile:

I tried that already, but the forum software is saying my jpg failed the security check.

prankenandi:
I tried that already, but the forum software is saying my jpg failed the security check.

That usually mean the image file contains EXIF data from the camera. Try converting the file to a PNG and posting the PNG file. Or maybe the file is too big. A 640 x 480 pixel image is usually sufficient.

By the way - please don't post a Fritzing diagram because they are much too easy to misunderstand. Just make a simple pencil drawing and post a photo of that.

prankenandi:
If I run the DC-Motor and the counting on seperate Arduinos it works fine.

Please post that program and the wiring diagram that it uses.

...R

prankenandi:
I tried that already, but the forum software is saying my jpg failed the security check.

If you are using Windows OS, load the picture into PAINT then resave it as PNG or JPG but a different name, that should wash the offending imbedded info out.
Using PAINT to resize if needed, is a quick and simple operation.
Tom.... :slight_smile: