Sunday 23 November 2014

Optical Position Encoder with Arduino

Common applications of position encoders are:
  • DC motor position / velocity control
  • Servo-mechanism Position / velocity control
  • Computer printer
  • Numerically controlled machinery
  • RPM sensors in robotics
    Photo-Interrupter
    Front end of an ordinary optical position encoder, used for these tasks, is a slotted PhotoInterrupter /Opto-Interrupter module with an IR LED & a Photo Transistor/Diode mounted facing each other enclosed in plastic body. When light emitted by the IR LED is blocked because of the alternating slots of the encoder disc (also known as index disc), conduction level of the photo transistor/diode changes. This change can be detected by a discrete hardware or by a microcontroller. In short, a photo-interrupter is composed of an infrared emitter on one side and an infrared detector on the other By emitting a beam of infrared light from one side to the other, the photo-interrupter can detect when an object passes between them, breaking the beam.


    Encoder/Index Disc
    As we need to create pulses, an encoder disc/index disc is very necessary here. An easy way to make it is with a thin transparent acrylic, thin transparent adhesive transparency sheet and a laser printer. Take a drawing software and draw the black stripes as shown in the picture. Print it to the transparency sheet and fix it on the round-shaped thin transparent acrylic. As an alternative, you can make the disc from black acrylic and cut out the white spaces.


    An important note: Physical width of stripes and spaces is a most critical factor. Look at the datasheet of the photo-interrupter to find its slit width. It is better to set minimum witdh of the stripes (and also of the spaces) two times (x2) the slit width of the photo-interrupter. For example, if the slit width is 1mm, the width of the stripes and spaces should be 2 mm. If the RPM of the disc is 60, then we have 1 disc turn/second. If the disc has 36 stripes then the pulse frequency is 36Hz, which can easily be handled by the photo-interrupter.

    The Hardware
    To get started, just create a small test circuit as shown here with a photo- interrupter and an Arduino. This allows you to experiment and make sure all things works as per your expectation. The 10K resistor (R2) is a pull-up resistor. Value of the first resistor (R1) depends on the photo-interrupter you use. In this set-up, the onboard LED (at D13) of the Arduino board is usually in off mode, and when the beam broken the LED goes on. Auxillary output available from D12 of the Arduino can be used to monitor the encoded signal on an oscilloscope, for example.

     The Arduino Sketch

    1. /*
    2. -Arduino Position Encoder
    3. -Using a generic photo-interrupter
    4. -Basic Test Sketch 1 / June 2014
    5. -Tested at TechNode Protolabz
    6. -www.electroschematics.com/
    7. */
    8. const int encoderIn = 8; // input pin for the interrupter
    9. const int statusLED = 13; // Output pin for Status indicator
    10. const int pulseOutput = 12; // Pulse output pin for external interfacing
    11. int detectState=0; // Variable for reading the encoder status
    12. void setup()
    13. {
    14. pinMode(encoderIn, INPUT); //Set pin 8 as input
    15. pinMode(statusLED, OUTPUT); //Set pin 13 as output
    16. pinMode(pulseOutput, OUTPUT); // Set Pin 12 as output
    17. }
    18. void loop() {
    19. detectState=digitalRead(encoderIn);
    20. if (detectState == HIGH) { //If encoder output is high
    21. digitalWrite(statusLED, HIGH); //Turn on the status LED
    22. digitalWrite(pulseOutput,HIGH); // Give a logic-High level output
    23. }
    24. else {
    25. digitalWrite(statusLED, LOW); //Turn off the status LED
    26. digitalWrite(pulseOutput,LOW); // Give a logic-Low level output
    27. }
    28. }
      Test Procedure
      Bring the photo-interrupter connected with the hardware to your encoder disc and do the test. Connect the D12 output to an oscilloscope and run your encoder disc by hand, or by using a low rpm dc motor. If you don’t have an oscilloscope, watch the onboard LED (D13) to note the pulse output. In this case, try to turn the disc slowly by hand to see the pulse activity directly.

     This is the very basic code you need. We have the hardware and the basic software up and running. Now you can upgrade it to whatever you want it to do.


1 comment:

Tv Channel Solution said...

If You are a professional live streamer and want to live your video on a digital platform?
Buy the best multi sim cellular network 5g bonding encoder and get the best 4k output in
your camera and best battery backup.

Post a Comment