Go Down

Topic: Optocoupler issues (Read 692 times) previous topic - next topic

gizmodigi

Hi!

I'm currently working on a robotics application with Arduino Uno. The idea would be to control the robotic arm with the arduino so I need to measure the position of every axis. I'm using potentiometers on most of the axis but on one I need to use an optocoupler  because of mechanical reasons. So i had an optocoupler lying around and I made a disc with holes for it. The problem is that I want to count the number of rising edges emitted by the optocoupler anytime during the programs run. I'm using interrupt on pin number 0 for the signal, but that is terribly bouncy. I tried to eliminate the bouncing with a resistor and a cap but without any results. I also tried to eliminate it in software by only looking signals witch have at least 1.5sec delay between them. It gave better results but still there are issues...
Code: [Select]

int interruptpin = 0;

int LEDpin = 13;
int number = 0;

unsigned long int_time = 0;
unsigned long last_int_time = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(LEDpin, OUTPUT);
  attachInterrupt(interruptpin, count, RISING);
}

void count()
{

  int_time = millis();
 
  if (int_time - last_int_time>1500)
    number++;
  last_int_time = int_time;
}

void loop()
{
  digitalWrite(LEDpin,HIGH);
  delay(150);
  digitalWrite(LEDpin,LOW);
  delay(150);
  Serial.print("Number of int. = ");
  Serial.print(number);
  Serial.print("\n");
}


If anyone has some ideas it would be greatly appreciated! :)
Thanks

Shpaget

Don't you want to update last_int_time only when you actually do number++?
It appears to me that current code will never increment number if interrupt is sent faster than 1500 ms, because you are updating last_int_time regardless of (int_time - last_int_time>1500).
Adding { } in the if statement would update last_int_time only if if returns true.
Code: [Select]
void count()
{

  int_time = millis();
 
  if (int_time - last_int_time>1500)
    { 
       number++;
       last_int_time = int_time;
     }
}

gizmodigi

Hey!
Sadly it makes no difference, tried that one also...
But thanks!

Shpaget

So, what exactly are "issues"?

retrolefty

Can you show a picture of your 'optocoupler' and it's installation and possibly a link to a datasheet for it. I suspect you are not getting a clean switching signal from your setup but without further information that is just a gut guess.

Lefty


holmes4

Quote
I'm using interrupt on pin number 0


As pin 0 is used for serial I/O this is not a good idea. Try pin D2 or D3 the external interrupt pins.

Mark

gizmodigi

Thanks guys for the help!

The datasheets link is the following:
http://www.ia.omron.com/data_pdf/data_sheet/ee-sx91_dsheet_csm485.pdf
It's the EE-SX912R model.

The issue is that the interrupt routine is called way to many times, even if there is nothing inside it.(I tried inserting a credit card or paper, and removing it but sometimes it counts sometimes it doesn't, and when nothing in the middle it just starts counting randomly...i left it covered unattended so it wont get in interference with some other light source but still the same problem.
I tried to make it work without interrupts(in the loop function, only by reading it's signal with a 10ms delay between them)  and it worked great-without bouncing- , but that's not the solution for my problem...
Thanks

gizmodigi


Quote
I'm using interrupt on pin number 0


As pin 0 is used for serial I/O this is not a good idea. Try pin D2 or D3 the external interrupt pins.

Mark


Thanks, I will try that tomorrow!
Attila

holmes4

Quote
The issue is that the interrupt routine is called way to many times, even if there is nothing inside it.(I tried inserting a credit card or paper, and removing it but sometimes it counts sometimes it doesn't, and when nothing in the middle it just starts counting randomly...i left it covered unattended so it wont get in interference with some other light source but still the same problem.


Yes, your use of serial will cause this on pin 0.

Mark

retrolefty

Another possible issue is that the output signal from the opto appears to be of the 'open collector' type requiring an external pull-up resistor wired to +5vdc. Are you using such? It could even be as simple as enabling the internal pull-up for the input pin you finally end up using.

Lefty

gizmodigi


Quote
The issue is that the interrupt routine is called way to many times, even if there is nothing inside it.(I tried inserting a credit card or paper, and removing it but sometimes it counts sometimes it doesn't, and when nothing in the middle it just starts counting randomly...i left it covered unattended so it wont get in interference with some other light source but still the same problem.


Yes, your use of serial will cause this on pin 0.

Mark

I tryed it and it's not working at all on pin D2 and D3 if i'm using interrupts,but if i use the same hw setup and use a simple digitalRead it is working fine on them...

gizmodigi



Quote
The issue is that the interrupt routine is called way to many times, even if there is nothing inside it.(I tried inserting a credit card or paper, and removing it but sometimes it counts sometimes it doesn't, and when nothing in the middle it just starts counting randomly...i left it covered unattended so it wont get in interference with some other light source but still the same problem.


Yes, your use of serial will cause this on pin 0.

Mark

I tryed it and it's not working at all on pin D2 and D3 if i'm using interrupts,but if i use the same hw setup and use a simple digitalRead it is working fine on them...

Problem solved. I found the problem, just had to change number from simply be "int" to be "volatile int" so after each reference it needs to be reloaded, and also using D2.
Thanks for the help guys!
Cheers.

Go Up