Interrupt - Signal w/o interrupt INPUT

Hi there,
i am using an Und Rev3. Currently I am in a training using interrupts, following this project:

Klick hier @brainy-bits.com

It's about using a speed IR sensor as interrupt for DC velocity measurement.

Problem:
Installation of IR-Sensor OUT on Port 2 (= port 0 for Interrupt input): even without DC speed i get a count con variable counter, that slightly changes between 3-6 each refreshment.

using Port 3 (= port 1 for Interrupt input) in programm: even without a connected cable i get a count con variable counter, that is constant at 24,5 (*20=490 -> frequency of regular PWM) each refreshment.

So first of all:

  • Why?! what do I wrong that counter gets just increased by real IR-signal?

Thanks a lot

Please post the code you are using right here on the forum, so all can see it.
Paul

#include <TimerOne.h>
unsigned int counter = 0;

const int a1_speed = 3;  // Motor Pin A - analog -> speed
const int a1_direction = 12;  // Motor Pin A - digital -> direction


const int CW  = HIGH;
const int CCW = LOW;


void docount()  // counts from the speed sensor
{
  counter++;  // increase +1 the counter value
}

void timerIsr()
{
  Timer1.detachInterrupt();  //stop the timer
  Serial.print("Motor Speed: "); 
  
  int rotation = (counter/20);  // divide by number of holes in Disc
  Serial.print(rotation,DEC);  
  
  Serial.println(" Rotation per seconds"); 
  
  counter=0;  //  reset counter to zero
  Timer1.attachInterrupt( timerIsr );  //enable the timer
}

void setup()
{
  Serial.begin(9600);
  
  pinMode(a1_speed, OUTPUT);
  pinMode(a1_direction, OUTPUT);
  pinMode(MotorBrakePinA, OUTPUT);

  Timer1.initialize(1000000); // set timer for 1sec
  attachInterrupt(0, docount, RISING);  // increase counter when speed sensor pin goes High
  Timer1.attachInterrupt(timerIsr); // enable the timer
 
  }

void loop()
{
  analogWrite(a1_speed, 80);  // set speed of motor
  digitalWrite(a1_direction, CW);  // set rotation of motor to Clockwise
}

using Port 3 (= port 1 for Interrupt input) in programm: even without a connected cable i get a count con variable counter, that is constant at 24,5 (*20=490 -> frequency of regular PWM) each refreshment.

If I'm not mistaken, you are already using pin 3 to send a PWM signal to the motor controller.

I would try to use a pulldown resistor from pin 2 to ground. The guide you linked has an amazon link to a lm393 based speed sensor, but it has four pins and the schematic in the guide only shows 3 wires connected to it, so maybe double check the pins you're using too.

It just occurred to me that i may have misunderstood your issue though. Using pin 2, Are you getting a reading that makes sense but it changes 3-6 each time? Or are you getting a reading that is only 3-6 counts per second?

Hey Isaiah935,
thanks for your help so far! Please find below my statements. Lets start with your question:

no it not makes sense for me.
There seems to be a "background noise" / offset regardless of whether something is plugged into pin 2 or not. Connecting and using an IR sensor increases this value as i would suggest - But probably with a slightly offset.

I got confused here due to the fact that I'm using an Arduino Motor Shield Rev3 as well and just the A Pins are physically connected to the motor, not PIN 3. So it was free.
But anyhow, your comment makes sense here, whether I do not know how the speed / voltage is controlled then at the DC... but this is a different question I could search by my own.

Connections are ok.
Question to your proposal proposal using a pull down resistor: Don't I have some kind of pull down resistor if I don't have anything connected to PIN 2? --> Resistance goes to infinity, that would be my guess.

And another question, that could help here: Do I need to use other PINS for Interrupts by using the Motor Shield Rev3, mounted on UNO Rev3?

Thanks in advance! Best regards, snipes04

Attaching a resistor from pin 2 to the ground pin should eliminate any stray voltage and noise on pin 2 that could be triggering the interrupt.

In your code you set a1_speed as an output on digital pin 3.

const int a1_speed = 3;  // Motor Pin A - analog -> speed
...
pinMode(a1_speed, OUTPUT);

I believe this uses digital pin 3. I could be wrong though.

I do not know the answer to your last question about which pins to use with the motor shield, but I would expect that they would be the same on the motor shield as they are on the Arduino.

Hi Isaiah935,

so applied a resistor pin 2 to ground without usage of IR-Sensor (just to rate the plain effect of this change), it's getting a bit better. But there are still irritations.

astonishingly the following experiences have been made:

  • having additionally the IR sensor connected to pin 2 (w/o measuring the rotating DC) : problem gets even worse

  • a faster running of the engine 80 -> 255 (analogWrite(a1_speed, 255); // set speed of motor influences interrupts --> to higher value ( ...even with no IR-Sensor installed, use pull down resistor)

  • adding a load to the motor influences interrupts --> to higher value ( ...even with no IR-Sensor installed, use pull down resistor)

In another forum I have read that the volatile voltage change (incl. voltage peaks) leads to interferences in whole board network. Unfortunately it is a forum with German language (https://www.mikrocontroller.net/topic/413399. Maybe this is another hint?
I tried it out with 10µF and 100µF... without any big optimization...

BR, snipes04

Unfortunately, I don't really know of any other possible solutions. Good luck with your project, sorry i couldn't be of more help.

This will give misleading results. Serial.print() must not be used in an Interrupt service routine.