Counting Pulses

tonyengineer

New Member
Hi All
I have an application using a 08M where I am counting the input from two photo interrupters. I have a disc on a motor (max 95 rpm) with 120 holes on one circle and 1 on the other i.e. this counts each revolution.

I am trying to say to my motor move so many holes (3 degrees each) and then do something else, either pause or reverse or change speed.

Neither Pulsin nor Count seems to be suitable, any ideas.

Thanks Tony
 

hippy

Ex-Staff (retired)
At 120 holes and 95 rpm you get 190 max per second, that's 10ms between holes. That should be long enough to poll the presence of a hole ...

#Picaxe 08M
#Terminal 9600
SetFreq M8
Do
Do : Loop Until pinX = 0
Do : Loop Until pinX = 1
w0 = w0 + 1
SerTxd( #w0, CR, LF)
Loop

Adjust pinX to be the pin your sensor input is on, turn the wheel and you should see a count increase as each hole passes through.
 

westaust55

Moderator
95 rpm = 1.58 rev / sec ==> 0.631 sec per rev
with 120 holes ==> 0.631 / 120 = 5.2ms per hole
assuming 50% mark to space ratio that is 2.6ms per hole.

at 4MHx PICAXE clock speed the PULSIN resolution is 10us so you can easily see each hole.

Based upon the above, 2.6ms will give a count of approx 263 which exceeds the max value of a byte variable but if using a word variable as per the manual it should work.
 

MartinM57

Moderator
...I have a disc on a motor (max 95 rpm) ...I am trying to say to my motor move so many holes (3 degrees each) and then do something else...

Neither Pulsin nor Count seems to be suitable, any ideas.
I can't understand the problem that needs solving at the moment.

Moving a motor say 5 holes (15 degrees) sounds like a stepper motor. 95rpm sounds like an ordinary (rotating) motor

So is it a homeade stepper, or something else, and what are you trying to do more precisely please...
 

tonyengineer

New Member
Thank you Hippy, it worked as you predicted, my input is pin3 for the 120 circle.

What I am now trying to do and failing miserably is to reset the counter to zero each time the single slot goes low on pin4, to determine my position on the disc.

Thanks Tony
 

hippy

Ex-Staff (retired)
#Picaxe 08M
#Terminal 9600
SetFreq M8
Do
Do
If pin4 = 0 Then : w0 = 0 : End If
Loop Until pinX = 0
Do
If pin4 = 0 Then : w0 = 0 : End If
Loop Until pinX = 1
w0 = w0 + 1
SerTxd( #w0, CR, LF)
Loop

You probably only need one of those IF PIN4. You might start running into time problems but there are ways out of that, and it should give you something to start with.
 

tonyengineer

New Member
Hi Martin,

It's a 12v window motor which I would like to attach to an arm or a carriage via a chain drive and thus know the position of the arm or carriage. A poor mans stepper motor, but I'm powering it until I receive a signal to stop.

Tony
 

tonyengineer

New Member
Hi Hippy, you were correct I didn't need the second If pin4... and it all works fine at about 25 rpm but misses at 95 rpm.

Thanks Tony
 

hippy

Ex-Staff (retired)
You might need to go to interrupts but maybe not. You might not have to flag the synch hole every time so you could just move to that as part of initialisation then count the other holes to determine where you are. Or more likely count down holes until you need to stop. For example ...

Code:
SetFreq M8
Gosub MoveToSynch
Do
  Gosub Spin30Holes
  Pause 4000
Loop

MoveToSynch:
  Motor On
  Do : Loop Until pin4 = 0
  Motor Off
  Return

Spin30Holes:
  w0 = 30
  Motor On
  Do
    Do : Loop Until pinX = 0
    Do : Loop Until pinX = 1
    w0 = w0 - 1
  Loop Until w0 = 0
  Motor Off
  Return
 

boriz

Senior Member
You sure about the photo interrupter circuit? It could (theoretically) be missing pulses because it’s reacting slowly.
 

Dippy

Moderator
Boriz may be onto something.

You haven't told us the size of the holes have you?
That may be significant.

You haven't told us what type of photo-interrupter?
Something nice and fast or home-made LED-LDRs?
If it's a proper one, provide us with a link to a Data Sheet.

Basically you have 2 parts to this problem.

1. Is the PICAXE+Code fast enough?
2. Is the photo-interrupter circuit giving out a nice 'pulse' that can be read by the PICAXE+Code.

To sort Q1, you could use another PICAXE and set the PWM to simulate the pulses.
This also has the advantage of being able to set the duty to simulate the hole sizes too.
(UNless you have a nice Function Generator?).

To sort out Q2 you really need an osillyscope.

I assume the disc isn't really thick and you're getting reflections off the insides of the holes? You'd see that on a 'scope.

Can you post the schematic of your photo-interruptor circuit?
I have seen examples in the past where unsuitable connections/components have 'ruined' the performance of good devices.
 

tonyengineer

New Member
Hi all

The disc is 1.6mm aluminium, the holes are 2mm diameter, with a clean de-bur each side, on a 142mm circle (about 3.7mm cts) and made on a dividing head. The interrupter is a ZD1901 from Jaycar-Australia, powered from 3 AA batteries. I've checked the signal on my CRO and repositioned the interrupter, I was getting a fairly spiky signal, it’s now closer to a square wave, I'll re check and come back. I am using PWM to slow the motor.

I’m not sure how to post a schematic diagram; the ones I’ve seen seem to turn into ASCII text symbols.

Thank you all again for your help, I’m learning rapidly.

Thanks Tony
 

westaust55

Moderator
To upload your schematic, one way is to:

1. press shift - PrintScrn
2. go into Paint and press Ctrl-V to paste into Paint
3. saveas a jpeg file
4. upload into your post. Use the paperclip symbol at the top of the post window which allows you to add up to 2 files to a post.
 

Dippy

Moderator
I can't find data on that photo-interrupter, so I'm in guessing-games mode.

The ones I have looked at have a large range of response times.
And the response times vary with the load resistor.

Can you increase that 68R value? You're over-doing the current a bit based on a few that I have looked at.
But as you haven't provided a product Data Sheet I can't be sure.

Time to:
A) Experiment the load Res.
B) See if ambient light is upsetting things.
C) Dig out the 'scope.

Good luck.
 

BCJKiwi

Senior Member
With a recent similar application found that resistor values are critical to get a good square wave (470R on LED and 16k pulldown on transistor).

Found the switched type of interruptor much more succesful than the ratiometric type - used Rohm RPR 220 photoreflector.

Also found it necessary to sense on a schmitt trigger port to ensure hysteresis and clean switching points. CRO was very helpful in balancing the resistor value to the switching points of the PICAXE port.
 

MPep

Senior Member
Also of ocourse, be wary of Fluorescent lighting tubes. All manner of problems are associated with photo detectors and the presence of Fluoros.
My 2c worth. :)
 

BCJKiwi

Senior Member
Following from my earlier post,
before using the Rohm RPR-220, had used a ZD1901 in the circuit with a 470R between +5V and LED, and a 300K between output and 0V (output on 0V side, Not +5V side of output).
 
Last edited:

Dippy

Moderator
Aha.
Spec looks good.

Try reducing your load res value, say, 470R to 1k0. I would also increase your LED res. - say 150R to 220R.

Repeat experiment with little ambient light, esp as pointed out by MPep no fluoros including Energy Saving bulbs.

If that doesn't work then you have some options.

1. 'scope it. I'm afraid when you get into this kind of electronics you REALLY need test equipment.
2. Simplify your code so that it 'follows' the sensor. Dump the IFs and Do/Whiles. Just get output to follow input and connect a 150R+LED to an output. Whizz your wheel and waggle your eyes and see if the LED is flashing quickly.

If your LED is flashing quickly this means your proper detection code is too slow.
If it isn't then either your sensor o/p is not adequate or PICAXE too slow.

HOWEVER, with a scope you can determine which bit (sensor or code) is at fault in 30 seconds.
It will also tell you if the pulse shape is good.
Remeber, the pulse has to go cleanly below PIC Input Low spec and above PIC Input High spec. - see Microchip Data Sheet for the PIC you are using.
Different PIC input pins have different specifications TTL and ST - check these in the Data Sheet too.
I can't remember which PICAXE you are using.
(When you get to a 'certain stage' in hobbying then its time to get a 'scope. It WILL save hours waiting for replies from Forum and/or hair loss)


Final test without a scope.
Connect the sensor output to a (bright) LED. Whiz your wheel. Do you see definite flashes from LED?

If the above isn't possible, then sorry, I've run out of ideas.
 

MPep

Senior Member
Also, the idea of light being reflected off the sides of the holes is something else to keep in mind. Try spray-painting the disc black, Matt preferred.
 

MartinM57

Moderator
@Dippy - Tony has a scope
...I've checked the signal on my CRO and repositioned the interrupter, I was getting a fairly spiky signal, it’s now closer to a square wave, I'll re check and come back....
Let's hope the scope of the problem is quickly identified;) (otherwise this'll degrade to an oscilloscope pun-tastic thread)
 

tonyengineer

New Member
I think we've solved it!

Hi all
I've attached the trace from the CRO, it's not perfect, but it looks fairly defined. I would appreciate your opinions.
  1. I tried using pin2 as it had Schmitt input, that made very little difference and I need pin2 for the "Pwmout".
  2. I tried it in semi darkness, including switching off the computer screen, again very little difference.
  3. I commented out "SerTxd(...." and the counting seemed to be consistent. I am aware "debug" drags down the execution and obviously "SerTxd" does so also, but to a lesser degree.
  4. I think painting the disc black would worth while.
I'll let you know the outcome.

Thanks Tony
 

Attachments

MPep

Senior Member
Hi Tony,

How are you getting on??
Got some good pulses coming through now?

Or have you now finished the whole project?

MPep.
 
Top