Hooking up a Omron E6A2-CW3 Rotary encoder with Picaxe 18X

Gavinn

Member
Hi Freddy,

I used this code with another rotary encoder which was similar but I think it was 'pulsing' too fast when rotating for the PICAXE to capture and I therefore gave up with it. Anyhow give my sample code a try which might help you get started. Depending on which physical input pins you use your value for PINS might be different to what is in the code, have a look at what the debug output of PINS gives you then comment it out.

Code:
setfreq m8	; Switch to 8 MHz operations - to improve reliability?

'A small program to test rotary encoding.
'Using 2 bit Grey code.
'BIN   DEC
'00  = 0
'01  = 2	
'11  = 6
'10  = 4

symbol new = pins	'Define the b2 register as new [value].
symbol old = b2		'Define the b3 register as old [value].

b1 = 180 ' used for testing (180 deg)

start: 'This code checks is there is any changes in the encoder, if not it just loops.

'debug b1
old=new	'If we set old and new to be the same so if new changes we do something...
new=pins	'Set the value of new to the value of the input pins.
debug pins
if new = old then goto start

pause 75

if old = 22 then goto increment 'check your own PINS value
if old = 6 then goto decrement 'check your own PINS value

nextup:
pause 10
if new = 6 then goto increment

nextdown:
pause 10
if new = 22 then goto decrement
goto start


increment:
inc b1
goto tx

decrement:
dec b1
goto tx

TX:
Sertxd(#b1,13,10)	'Transmit the new value of b1 over the serial line.
'debug b1
goto start
 

boriz

Senior Member
This works for me:
Code:
#picaxe 14m
do
	do
		bit2=bit0:bit3=bit1
		bit0=pin0:bit1=pin1		
	loop until bit0<>bit2
	if b0=11 or b0=4 then
		inc b1
	elseif b0=1 or b0=14 then
		dec b1
	endif
loop
 
Thanks to Gavin and boriz.
Emm...because Picaxe is all new to me, may I know which pins of the encoder should be hooked to the pin on the Picaxe for Gavin's and Boriz code?


Omron Encoder wire colour:

Brown: 5v~12v
Blue: 0V
Black: out A ---------> pin ? in picaxe?
White: out B ---------> Pin ? in Picaxe?

NB. is there anyways to upload pictures or insert picture in the reply?

thanks

Freddy
 

boriz

Senior Member
My rotary encoder only has 3 terminals and is hacked from a PC mouse. In my example. The input’s are pin0 and pin1 on a 14m. Each has a 4k7 pulldown to 0v, and each is connected to one of the encoder outputs (probably A and B for yours). The third terminal of my encoder is connected to +5v. The pulldowns ensure that when A/B is open circuit, it reads logic LOW. (otherwise, logic high).

Dunno why your has 4 terminals. Prolly just for versatility. You’ll need to experiment. But using my code as-is requires pin0 and pin1 to alternate HIGH and LOW in a standard quadrature pattern.
 
Here is a much clearer illustration of what I'm intended to do. In this image I drew a encoder from the far left then a Picaxe18x and a schematic diagram of a 3179 stepper driver and the motor coils is also illustrated on the right of the schematic diagram.

So if I can get the Picaxe 18x to read the encoder signals and translate into Step and Dir which then will be much easier to hook up with the stepper board.

Hope anyone out there could help on this project,

Cheers,

Freddy
 

Attachments

boriz

Senior Member
“make the Stepper motor turn accordingly to the encoder's direction”

Errr.. Not quite sure what you mean. If you only need direction info, then why an encoder? Are you trying to move the stepper one step when the encoder turns one click? Ten steps for ten clicks etc. Same direction as encoder?

If so, here’s some slightly modded code for one step-per-click:
Code:
#picaxe 14m
do
	do
		bit2=bit0:bit3=bit1
		bit0=pin0:bit1=pin1		
	loop until bit0<>bit2
	if b0=11 or b0=4 then
		gosub right_step
	elseif b0=1 or b0=14 then
		gosub left_step
	endif
loop

right_step:
..stepper stuff here..
Return

left_step:
..stepper stuff here..
return
The ‘stepper stuff’ is something I have never tried, and it depends on what sort of motor and how you are driving it etc.. So I can’t advise you there. I’m sure someone else here has experience driving steppers with Picaxe.

BTW. I can’t see your last attachment for some reason.
 
“make the Stepper motor turn accordingly to the encoder's direction”

Errr.. Not quite sure what you mean. If you only need direction info, then why an encoder? Are you trying to move the stepper one step when the encoder turns one click? Ten steps for ten clicks etc. Same direction as encoder?

If so, here’s some slightly modded code for one step-per-click:
Code:
#picaxe 14m
do
	do
		bit2=bit0:bit3=bit1
		bit0=pin0:bit1=pin1		
	loop until bit0<>bit2
	if b0=11 or b0=4 then
		gosub right_step
	elseif b0=1 or b0=14 then
		gosub left_step
	endif
loop

right_step:
..stepper stuff here..
Return

left_step:
..stepper stuff here..
return
The ‘stepper stuff’ is something I have never tried, and it depends on what sort of motor and how you are driving it etc.. So I can’t advise you there. I’m sure someone else here has experience driving steppers with Picaxe.

BTW. I can’t see your last attachment for some reason.
Hi Boriz,
If you goto this link: http://uk.youtube.com/watch?v=nvmdrLybZXw
I want to do the same thing as shown on that clip but using a stepper motor.
I dont know what happend to the picture I uploaded, the picture size = 600 x 297 mm, created in AdobeIllustrator, converted to jpg file.

Here is a code I found, is a Unipolar Stepper Motor Driver using Picaxe:

symbol singlestep = 4
symbol halfstep = 8
symbol delay = 100

symbol stepcount = b0
symbol counter = b1

output 1
output 2
output 4

loop:

for counter = 1 to 20
for stepcount = 1 to singlestep
lookup stepcount, (0,$01, $02, $04, $10), pins
pause delay
next stepcount
next counter

pause 2000

for counter = 1 to 20
for stepcount = 1 to singlestep
lookup stepcount,(0,$01, $10, $04, $02), pins
pause delay
next stepcount
next counter

pause 2000

for counter = 1 to 20
for stepcount = 1 to singlestep
lookup stepcount , (0,$03, $06, $14, $11), pins
pause delay
next stepcount
next counter

pause 2000

for counter = 1 to 20
for stepcount = 1 to singlestep
lookup stepcount , (0,$03, $11, $14, $06), pins
pause delay
next stepcount
next counter

pause delay

next stepcount

next counter

pause 2000

for counter = 1 to 20
for stepcount = 1 to halfstep
lookup stepcount , (0,$01, $03, $02, $06, $04, $14, $10, $11), pins
pause delay
next stepcount
next counter

pause 2000

for counter = 1 to 20
for stepcount = 1 to singlestep
lookup stepcount , (0,$01, $11, $10, $14, $04, $06, $02, $03), pins
pause delay
next stepcount
next counter

pause 2000

goto loop


I run this code and I got the following error:

output 1
Error: 'Output' command not suported in this mode.

I'm using a picaxe 18x.

freddy
 

eclectic

Moderator
Freddy. Several points:

Your picture STILL doesn't work. Please re-check your Adobe settings.

However, I've borrowed Dippy's crystal ball.

Your program appears to come from David Lincoln's book,
“Programming and customising..........
Pages 238 – 240.

BUT, some things have got mangled in transliteration.

“Loop” is a keyword. Change to "Looper"
“Output 1” , is a 08/08M command See Manual 2, page 106.

AND, several lines have been added.

I've tried to sort out the others, but, NO promises that it'll work!
See asterisks / ***.
Code:
symbol singlestep = 4
symbol halfstep = 8
symbol delay = 100

symbol stepcount = b0
symbol counter = b1

'output 1 ***
'output 2  ***
'output 4  ***

loopER: '*****

for counter = 1 to 20
for stepcount = 1 to singlestep
lookup stepcount, (0,$01, $02, $04, $10), pins
pause delay
next stepcount
next counter

pause 2000

for counter = 1 to 20
for stepcount = 1 to singlestep
lookup stepcount,(0,$01, $10, $04, $02), pins
pause delay
next stepcount
next counter

pause 2000

for counter = 1 to 20
for stepcount = 1 to singlestep
lookup stepcount , (0,$03, $06, $14, $11), pins
pause delay
next stepcount
next counter

pause 2000

for counter = 1 to 20
for stepcount = 1 to singlestep
lookup stepcount , (0,$03, $11, $14, $06), pins
pause delay
next stepcount
next counter

pause delay

'next stepcount ****

'next counter  ****

pause 2000

for counter = 1 to 20
for stepcount = 1 to halfstep
lookup stepcount , (0,$01, $03, $02, $06, $04, $14, $10, $11), pins
pause delay
next stepcount
next counter

pause 2000

for counter = 1 to 20
for stepcount = 1 to singlestep
lookup stepcount , (0,$01, $11, $10, $14, $04, $06, $02, $03), pins
pause delay
next stepcount
next counter

pause 2000

goto loopER ' ****
And, as an aside, this thread shouldn't really be here. :)
This section of the site is for finished posts.

e
 

boriz

Senior Member
Forget that code. Read Picaxe_Manual3.pdf particularly pages 14 and 15.

I have never tried using steppers with Picaxe, but what you require should be easy. Everything you need to know is in the manual and in my code example above.
 
Thanks again guys.

Electric,

I have tried the code you corrected and it worked, does that mean the code from the book is wrong?, thanks.

Boriz,

Thanks fot the tips. I read the thread from the begining and you mentioned that the encoder you have is from a mouse, the encoder code you provided is for 3 pin encoder, is it anywhere similar to the one in this link:
http://www.bourns.com/components.aspx?cmsphid=7631383|7163299|2829252


ok the code from manual 3 page 14~15:

main: for b3 = 0 to 99 ‘ start a for...next loop
gosub lstep ‘ call left step sub-procedure
next b3 ‘ next loop
for b3 = 0 to 99 ‘ start a for...next loop
gosub rstep ‘ call left step sub-procedure
next b3 ‘ next loop
lstep: let b1 = b1 + 1 ‘ add 1 to variable b1
goto step ‘ goto the lookup table
rstep: let b1 = b1 - 1 ‘ subtract 1 from variable b1
step: let b1 = b1 & 2 ‘ mask lower two bits of b1
lookup b1,(%1010,%1001,%0101,%0110),b2 ‘ lookup code into b2
let pins = b2 ‘ output b2 onto control lines
return

I run this code I got the following error:

Error: Illegal character: ((0xFFFFFFA1))

Hilighted: "main: for b3 = 0 to 99"

Ok, I Think I have managed to upload a decent image after modifying it.


The code for the encoder from Gavin;

setfreq m8 ; Switch to 8 MHz operations - to improve reliability?

'A small program to test rotary encoding.
'Using 2 bit Grey code.
'BIN DEC
'00 = 0
'01 = 2
'11 = 6
'10 = 4

symbol new = pins 'Define the b2 register as new [value].
symbol old = b2 'Define the b3 register as old [value].

b1 = 180 ' used for testing (180 deg)

start: 'This code checks is there is any changes in the encoder, if not it just loops.

'debug b1
old=new 'If we set old and new to be the same so if new changes we do something...
new=pins 'Set the value of new to the value of the input pins.
debug pins
if new = old then goto start

pause 75

if old = 22 then goto increment 'check your own PINS value
if old = 6 then goto decrement 'check your own PINS value

nextup:
pause 10
if new = 6 then goto increment

nextdown:
pause 10
if new = 22 then goto decrement
goto start

I manage to run the above code with no problem, and the only thing I need tto know is which pin of the encoder goes to which pin of the picaxe'spin? and the same question to Boriz encoder code, below.


And here is athe Encoder form Boriz:


#picaxe 14m
do
do
bit2=bit0:bit3=bit1
bit0=pin0:bit1=pin1
loop until bit0<>bit2
if b0=11 or b0=4 then
gosub right_step
elseif b0=1 or b0=14 then
gosub left_step
endif
loop

right_step:
..stepper stuff here..
Return

left_step:
..stepper stuff here..
return


Freddy
 

Attachments

boriz

Senior Member
You copied it wrong. Here’s the working version:

Code:
#picaxe 18x
‘Example from manual to drive a BIPOLAR stepper motor

main:
for b3 = 0 to 99 ‘ start a for...next loop
	gosub lstep ‘ call left step sub-procedure
next b3 ‘ next loop
for b3 = 0 to 99 ‘ start a for...next loop
	gosub rstep ‘ call left step sub-procedure
next b3 ‘ next loop

lstep:
let b1 = b1 + 1 ‘ add 1 to variable b1
goto step2 ‘ goto the lookup table

rstep:
let b1 = b1 - 1 ‘ subtract 1 from variable b1

step2:
let b1 = b1 & %00000011 ‘ mask lower two bits of b1
lookup b1,(%1010,%1001,%0101,%0110),b2 ‘ lookup code into b2
let pins = b2 ‘ output b2 onto control lines
return
Note: Always use the [ code ] tag at the start and the [ /code ] tag at the end of any code you post on the forum. (Without spaces)

So, is it a unipolar or a bipolar stepper motor?
 
You copied it wrong. Here’s the working version:

Code:
#picaxe 18x
‘Example from manual to drive a BIPOLAR stepper motor

main:
for b3 = 0 to 99 ‘ start a for...next loop
	gosub lstep ‘ call left step sub-procedure
next b3 ‘ next loop
for b3 = 0 to 99 ‘ start a for...next loop
	gosub rstep ‘ call left step sub-procedure
next b3 ‘ next loop

lstep:
let b1 = b1 + 1 ‘ add 1 to variable b1
goto step2 ‘ goto the lookup table

rstep:
let b1 = b1 - 1 ‘ subtract 1 from variable b1

step2:
let b1 = b1 & %00000011 ‘ mask lower two bits of b1
lookup b1,(%1010,%1001,%0101,%0110),b2 ‘ lookup code into b2
let pins = b2 ‘ output b2 onto control lines
return
Note: Always use the [ code ] tag at the start and the [ /code ] tag at the end of any code you post on the forum. (Without spaces)

So, is it a unipolar or a bipolar stepper motor?
Hi Boriz,

I copied and paste the code to Picaxe programming editor, clik RUN----->

----- please open attachment---------<<<

Freddy
 

Attachments

boriz

Senior Member
What?

What are you trying to show me?

What is the problem?

(I can see that you edited the program)
 

lbenson

Senior Member
After "next b3" you fall into "lstep", then go to "step2", and then "return". Where do you return to?
 

eclectic

Moderator
@Freddy.

1. Add a "goto main" to your program.

next b3 ‘ next loop

goto main ' add this line

lstep: let b1 = b1 + 1 ‘ add 1 to variable b1

2. Possibly alter your Editor settings?
See the attached pic.

e
 

Attachments

@Freddy.

1. Add a "goto main" to your program.

next b3 ‘ next loop

goto main ' add this line

lstep: let b1 = b1 + 1 ‘ add 1 to variable b1

2. Possibly alter your Editor settings?
See the attached pic.

e

Electric,

this is the code I entered:

#picaxe 18x ‘Example from manual to drive a BIPOLAR stepper motor

main:
for b3 = 0 to 99 ‘start a for...next loop
gosub lstep ‘ call left step sub-procedure
next b3 ‘ next loop
for b3 = 0 to 99 ‘ start a for...next loop
gosub rstep ‘ call left step sub-procedure
next b3 ‘ next loop

goto main

lstep:
let b1 = b1 + 1 ‘ add 1 to variable b1
goto step2 ‘ goto the lookup table

rstep:
let b1 = b1 - 1 ‘ subtract 1 from variable b1

step2:
let b1 = b1 & %00000011 ‘ mask lower two bits of b1
lookup b1,(%1010,%1001,%0101,%0110),b2 ‘ lookup code into b2
let pins = b2 ‘ output b2 onto control lines
return


but when I run this code I get an error :

for b3 = 0 to 99 ‘start a for...next loop

" Error : Illegal Character: ((0xFFFFFFa1))"
 

lbenson

Senior Member
If you have an illegal character, it will probably be invisible. Copy your code from the programming editor into Notepad (presuming you are running Windows). The illegal character(s) may become visible (in which case, delete it or them), or Notepad may clean them up. Then copy back into the programming editior and try again.

Or simply copy from your post above--that compiles without a problem for me.

I note that in your post, the comments show a quote mark slanting from upper left to lower right. When I copy them into the programming editor, they slant the other way, the way they usually do when I type them using the key to the left of the <Enter> key--I wonder if those quotes could be the problem.
 
Thanks Ibenson,
problem solved.

#picaxe 18x
main:
for b3 = 0 to 99
gosub lstep
next b3
for b3 = 0 to 99
gosub rstep
next b3

goto main

lstep:
let b1 = b1 + 1
goto step2

rstep:
let b1 = b1 - 1

step2:
let b1 = b1 & %00000011
lookup b1,(%1010,%1001,%0101,%0110),b2
let pins = b2
return


Sorry for asking so many questions, I'm a beginner.
 

lbenson

Senior Member
No problem asking questions, but do try to keep your code commented so that we may understand what is intended, and even so that you may understand after a period--years, months, weeks, days as the case may be.
 
Hi guys,
Thanks again, I run the code in the editor, and is fine.

As Boriz said:

"My rotary encoder only has 3 terminals and is hacked from a PC mouse. In my example. The input’s are pin0 and pin1 on a 14m. Each has a 4k7 pulldown to 0v, and each is connected to one of the encoder outputs (probably A and B for yours). The third terminal of my encoder is connected to +5v. The pulldowns ensure that when A/B is open circuit, it reads logic LOW. (otherwise, logic high)."

I connected the Pin a & B of the encoder to Picaxe board's pin 1 and pin 0 respectively.
I did a illustration of the wiring hook-ups, please open attachment. Can anyone tell me where should the 3 pins go, circled in red?.
In the illustartion, the picaxe board is eaxctly what I have, meaning I have 2 resistor added to R8 and R7, no crystal added, no resistor at R9.

NB. The file I uploaded is 115.8KB I think it exceeded the max upload size, so if you cannot open the file, please drop down you email address and I'll email to you. If try to reduce to a smaller size the image will distort which is impossible to read.

Freddy
 

Attachments

eclectic

Moderator
@Freddy.
There seems to be some confusion here.
Your post #7 shows a CHI035 high power board.

But, your diagram in post#27 shows a CHI030 board.

Also from post #27,
the stepper connection diagram is taken from M3, p. 15
a Unipolar motor.

But, the code underneath is taken from M3, p17 – Bipolar.


Going back to posts #14 and #21,
What sort of stepper do you own?

Can you post the Serial number printed on your stepper motor?

e
 
Electric,
Thanks for point that out and it is a bit of confusion.

* The Picaxe Board I'm using is a a CHI035 high power board. The illustration shown on post #27 is the wrong board. I'll change it and upload the image again later.

* The Stepper Motor I'm using is a 6 wire stepper ( I uploaded a pdf file of the stepper motor), and I thought the code was for Unipolar connection, i'm wrong. Since the Stepper Driver is a Bi-polar settings, then I'll go for Bi-polar

The Stepper Model is: 17PM-K103-16. Page 5 on PDF.

I also have two 8 wire stepper motor from Mclennan, and astronsyn.


Freddy
 

Attachments

eclectic

Moderator
Electric,
Thanks for point that out and it is a bit of confusion.

* The Picaxe Board I'm using is a a CHI035 high power board. The illustration shown on post #27 is the wrong board. I'll change it and upload the image again later.

* The Stepper Motor I'm using is a 6 wire stepper ( I uploaded a pdf file of the stepper motor), and I thought the code was for Unipolar connection, i'm wrong. Since the Stepper Driver is a Bi-polar settings, then I'll go for Bi-polar

The Stepper Model is: 17PM-K103-16. Page 5 on PDF.

I also have two 8 wire stepper motor from Mclennan, and astronsyn.


Freddy
There is a K103 stepper on page 7 of the PDF.
It is UNIpolar.

Please provide correct and consistent information,
otherwise it is very difficult (or impossible) to give any relevant help.

One more time.
1. What sort of stepper do you own?
2. Which project board?
Please provide a photograph and a description of which
resistors are connected / removed.
3. Do you own a 6-pin connector for the stepper?

e
 

Attachments

Hi Electric,
I'm really sorry, I didn't know that motor was a unipolar.

One more time.
1. What sort of stepper do you own?

I own 3 x 17PM-K103-16
1 x Astrosyn Y163
1 x Mclennan 23HSX - 206E

2. Which project board?

1 x Picaxe High Power Board

Please provide a photograph and a description of which
resistors are connected / removed.

3. Do you own a 6-pin connector for the stepper?

For 17PM-K103-16 , I do have the connector header and crims for this motor.

Please open Attachement for photos.

Freddy
 

Attachments

eclectic

Moderator
@ Freddy.
OK. Info received. Thanks.

For a Unipolar stepper, Picaxe manual 3, page 15 for both circuit and program,
is a good place to start.
The circuit works very well with all Picaxe types,
but you will need a separate ULN2003 and some board.

OR, using a "normal-wired" ULN2003, you could use Lincoln P238 top diagram,

BUT, you only need a Picaxe with 2 inputs and 2/4 outputs.

Can you buy a smaller/cheaper project board?
The 14M or 20M should work fine for your application.
(I've just tried a 20M board with the Lincoln prog. Works fine.)

It is possible, if awkward, to use a CHI035,
but you've still got to add the ULN2003 + some board + wires + .....

Just my opinion, but, I would save my CHI035 board for
heavier motor control/lighting etc.

e.
 

Attachments

Hi electric,
I already ordered some ULN2003 ICs from Farnell uk, and I Do Have Picaxe 28 and 40, but I want to use 18x for this project.

Im still trying to figure out where should the pins go for this code, can anyone tell me?.

#picaxe 18x
main:
for b3 = 0 to 99
gosub lstep
next b3
for b3 = 0 to 99
gosub rstep
next b3

goto main

lstep:
let b1 = b1 + 1
goto step2

rstep:
let b1 = b1 - 1

step2:
let b1 = b1 & %00000011
lookup b1,(%1010,%1001,%0101,%0110),b2
let pins = b2
return

Freddy
 

Attachments

eclectic

Moderator
@Freddy.
I quote from post #28
@Freddy.
There seems to be some confusion here.
....
Also from post #27,
the stepper connection diagram is taken from M3, p. 15
a Unipolar motor.

But, the code underneath is taken from M3, p17 &#8211; Bipolar.

e
If you wish to wire a CHI035, then you can use the information
I gave in post #32.
Connections are possible to pins 4, 5, 6 and 7.

I wish you the best of luck in your project.

e
 
Top