IR encoder pyboard V1,1

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
johnbanes
Posts: 10
Joined: Fri May 24, 2019 2:32 am

IR encoder pyboard V1,1

Post by johnbanes » Tue May 28, 2019 1:15 am

I have been googling most of the day attempting to understand the IR encoder and disk with pyboard

https://images.app.goo.gl/aTNkSUFWzMHfjQeR8

The red wire + on the encoder is connected to a 5v power source and black wire - to ground. The disk is freely spinning between the encoder sensors.

output1 on the encoder is connected pin X6 on the pyboard and output2 on the encoder is connected to X7.

I have looked at one encoder library by SpotlightKid and it is beyond my current skill level to implement. I also see the encoder Class by peterhinch, I don't understand how to use it.

Can someone help me with write code that reads this sensor when I turn the encoder wheel? Thank you!

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: IR encoder pyboard V1,1

Post by jimmo » Tue May 28, 2019 3:25 am

Hi,

So there are two parts to this:
- Understanding the signals from the encoder. The general idea here is that every time the disk occludes the encoder, you can see the pin value change. There are two pins so that you can figure out which direction the disk is turning.
- Figuring out how you want to use this information in your program. Some examples:
- Turn the motor a certain number of revolutions. #1
- Drive the motor but count how far you've been. #2
- Turn the motor at a specific speed. #3

I highly recommend writing a simple program that goes something like:

Code: Select all

while True:
  # set the red LED to the value of output1
  # set the green LED to the value of output2
Then turn the disk and watch the pattern that the LEDs make. In one direction the red LED will "lead" the green LED, in the other direction the green will lead the red. (This is called quadrature encoding).

Depending on which type of scenario you want will change a bit how you write your code. For example, if you don't care about detecting forward/backwards (1 & 2 above) because you already know which direction the motor is turning, then you only have to use one of the outputs. (It doesn't matter which).

The main thing with the encoder outputs is you're looking for transitions. i.e. a 0->1 or 1->0 means that something has happened. So your code is going to look something like

Code: Select all

last_state = enc1.value()
...
  if last_state != enc1.value():
    # it's changed (i.e. the motor has done 1/Nth of a turn)
    last_state = enc1.value()
In #1, you can do something really simple like:

Code: Select all

last_state = enc1.value()
n_counts = 10000
# code to turn on motor
while n_counts > 0:
  if last_state != enc1.value():
    n_counts -= 1
    last_state = enc1.value()
# code to turn off motor.
For #2 and #3 you might want to use interrupts. But let us know more about what you're trying to do first.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: IR encoder pyboard V1,1

Post by OutoftheBOTS_ » Tue May 28, 2019 8:05 am

I have used encoders a fair bit for my robotics projects.

There is 2 types of encoders Quadrature encoders (has 2 square wave pules) and single pulse encoders (tachos type encoder). Quadrature encoders can tell direction of rotation dues to the order the 2 pulese are coming but single pulse encoder will get same respose regardless of direction of turning.

I am pretty sure the encoder you have in a single pulse encoder as you can see it only has I sensor that the disk rotates in.

As for spotlight kids code for reading encoder this is the type of software that I used to use but since then I have discovered that STM32 MCUs have a really good hardware encoder on their timers. I have been using them on my projects written in C but I do believe it has been implemented also in Mico-Python

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: IR encoder pyboard V1,1

Post by jimmo » Tue May 28, 2019 8:49 am

OutoftheBOTS_ wrote:
Tue May 28, 2019 8:05 am
I am pretty sure the encoder you have in a single pulse encoder as you can see it only has I sensor that the disk rotates in.
I think you're probably right.
It is possible though there are two IR sensors spaced a little bit apart which would give you quadrature. (And would explain why there's two outputs). Need a better photo or schematic to see.

Try what I suggested with the LEDs and you'll know for sure.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: IR encoder pyboard V1,1

Post by pythoncoder » Tue May 28, 2019 9:14 am

To use my encoder.py copy it to the Pyboard then issue:

Code: Select all

from machine import Pin
import time
import encoder
x6 = Pin('X6', Pin.IN)
x7 = Pin('X7', Pin.IN)
enc = encoder.Encoder(x6, x7, False, 1.0)
while True:
    print(enc.position)
    time.sleep(0.2)
This will work if it's a quadrature encoder. But not otherwise.

The comments made by others are correct, and a Pyboard will support up to two encoders in hardware which is more efficient. However the above should work well enough with the pins you have chosen.
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: IR encoder pyboard V1,1

Post by dhylands » Tue May 28, 2019 2:37 pm

And here's an example that uses the hardware quadrature encoder. https://github.com/dhylands/upy-example ... ncoder3.py
Note that it also has code to generate a quadrature signal that I used for testing. You don't need that portion of the code to just use quadrature.
This example: https://github.com/dhylands/upy-example ... ncoder2.py just sets up the encoder and assumes that your have an external quadrature device.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: IR encoder pyboard V1,1

Post by OutoftheBOTS_ » Tue May 28, 2019 9:24 pm

johnbanes wrote:
Tue May 28, 2019 1:15 am
I have been googling most of the day attempting to understand the IR encoder and disk with pyboard

https://images.app.goo.gl/aTNkSUFWzMHfjQeR8

The red wire + on the encoder is connected to a 5v power source and black wire - to ground. The disk is freely spinning between the encoder sensors.

output1 on the encoder is connected pin X6 on the pyboard and output2 on the encoder is connected to X7.

I have looked at one encoder library by SpotlightKid and it is beyond my current skill level to implement. I also see the encoder Class by peterhinch, I don't understand how to use it.

Can someone help me with write code that reads this sensor when I turn the encoder wheel? Thank you!
These here are pretty good motor with gear box and quadrature encoders, I have used then a fair bit https://www.aliexpress.com/item/1-set-2 ... st=ae803_5

If you want super high performance then these coreless motors with gear box and quadrature encoders are very impressive https://www.aliexpress.com/item/Japan-N ... st=ae803_5

johnbanes
Posts: 10
Joined: Fri May 24, 2019 2:32 am

Re: IR encoder pyboard V1,1

Post by johnbanes » Tue May 28, 2019 11:50 pm

Thanks for this info I appreciate this. Looks like it is a quadrature encoder. I looked at each side of the encoder and I only see one sensor on each side.

Photoelectric Speed Sensor Encoder Coded Disc code wheel for Freescale Smart car

https://www.ebay.com/i/191674247945?chn=ps

On the tube Manufacturer: Hewlett-Packard HP Original
Supply voltage: 5V
Wiring: Red-5V Black-GND
Output Signal: 5Vp-p two quadrature signal output

It's getting me going in the right direction. Thank you!

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: IR encoder pyboard V1,1

Post by OutoftheBOTS_ » Wed May 29, 2019 12:50 am

johnbanes wrote:
Tue May 28, 2019 11:50 pm
Thanks for this info I appreciate this. Looks like it is a quadrature encoder. I looked at each side of the encoder and I only see one sensor on each side.

Photoelectric Speed Sensor Encoder Coded Disc code wheel for Freescale Smart car

https://www.ebay.com/i/191674247945?chn=ps

On the tube Manufacturer: Hewlett-Packard HP Original
Supply voltage: 5V
Wiring: Red-5V Black-GND
Output Signal: 5Vp-p two quadrature signal output

It's getting me going in the right direction. Thank you!
The new 1 you posted looks like a quadrature encoder. It will have a 2 IR LEDS on 1 side then 2 photo-transisters on the other side.

I can't see in the pic whether it has a resistor on the supply to the IR LED. Usually when I have built this type of setup myself I have used 3.3v as a supply as this is what the voltage output I want from the sensor. I usually have to put a resistor on the supply for the IR LED to make it shine at needed brightness. If the resistor is too low then the IR LED is too bright and the signal doesn't drop low enough on the output of the photo-transistor, if the resistor is too large then the IR LED isn't bright enough that the output signal goes high enough. I usually hook it up to my scope to see how high and low it is going

With a bit of google I find this https://www.homofaciens.de/technics-bas ... isc_en.htm

johnbanes
Posts: 10
Joined: Fri May 24, 2019 2:32 am

Re: IR encoder pyboard V1,1

Post by johnbanes » Sat Jul 13, 2019 4:14 pm

Thank you for the help. I'm reading the output from the encoder. Had to switch to this because it's quadrature and the sensor is already 3.3v . It fits into the cheap plastic robot gear boxes with some shortening of the motor drive shaft.

https://www.ebay.com/itm/130-Motor-DC6V ... 2749.l2649

Thanks again !

Post Reply