1
\$\begingroup\$

I am trying to create a comparator on the Teensy 3.2 to convert an IR receiving diode signal from analog to digital. Currently I am using an op amp, but that takes extra space and extra power, both of which should be limited for my application. The circuit is below (instead of putting output to LED, it goes to pin on microcontroller)

IR receiver

I'm still new to microcontrollers, and tried reading the reference manual for the microcontroller. I can see it has this function, but I'm not sure how to implement it. I thought their might be prebuilt libraries for the Arduino IDE, but had trouble finding the right thing.

The IR system is measuring the rotation of a propeller, and I would just like to have the system count how many times a blade blocks light from the emitter by looking at the falling edge of the IR receiver signal after digitizing. (In the end, too, I plan to power both off of a PWM with as low as possible duty cycle. This could affect how I do the counting in the end)

For a comparator, do I need to create the signal to compare to with a resistor divider? How do I know which teensy pins correspond to the comparator input signal? Could I set it digitally in some way in the MCU registers, avoiding the need for more circuit parts?

Does the output of the comparator come from a certain pin? Can I set it up to cause in interrupt instead and not use that pin, so that it executes code in the program and thats it? Is there a way to make the comparator output stay in a fixed state when the LEDs are both powered off?

Thanks for the advice!

\$\endgroup\$
3
  • \$\begingroup\$ What is your expected blade frequency? A two bladed model airplane propeller at 10K rpm would be ~333 Hz, though 3 times faster and nearly ten times slower could also be seen. This suggests a common remote-control type ~38 KHz modulated IR system with appropriate demodulating receiver may work. This would handle not only the digital thresholding but also reject a lot of background noise. However it could also turn out that propeller shadow is too narrow and fleeting to register as a signal gap. \$\endgroup\$ May 2, 2017 at 22:17
  • \$\begingroup\$ If you have to do it from scratch, you'll want a highpass filter, however for a throttleable engine/motor the range may pass through powerline frequency and its rectified double, which could be a source of interference from lights. \$\endgroup\$ May 2, 2017 at 22:18
  • \$\begingroup\$ Blade frequency is aboutt 200-400hz max, still tbd. Propeller shadow is fine with the op amp. Its a wind speed sensor, shouldnt have worries about powerlines. I couldnt understand the purpose of modulated remote control IR system? It seems more complicated than what I need. \$\endgroup\$
    – Alex K
    May 2, 2017 at 22:37

1 Answer 1

1
\$\begingroup\$

You'll want to take a look at chapter 32 of the MK20DX256 reference manual:

The comparator (CMP) module provides a circuit for comparing two analog input voltages. The comparator circuit is designed to operate across the full range of the supply voltage, known as rail-to-rail operation. The Analog MUX (ANMUX) provides a circuit for selecting an analog input signal from eight channels. One signal is provided by the 6-bit digital-to-analog converter (DAC). The mux circuit is designed to operate across the full range of the supply voltage.

For a comparator, do I need to create the signal to compare to with a resistor divider?

The chip contains one internal DAC for use with the comparator, so you can use that or an external voltage divider or signal:

The 6-bit DAC is 64-tap resistor ladder network which provides a selectable voltage reference for applications where voltage reference is needed.

I thought there might be prebuilt libraries for the Arduino IDE, but had trouble finding the right thing.

Unfortunately, there is no comparator support included in the Teensyduino libraries (yet), but I did find this fork of the analogComp library that supports the Teensy 3.2 comparators.

https://github.com/orangkucing/analogComp

I haven't tried it yet, but it looks like a great place to start.

How do I know which teensy pins correspond to the comparator input signal? Could I set it digitally in some way in the MCU registers, avoiding the need for more circuit parts?

Luckily, the analogComp library has good documentation, including a table mapping from K20 comparator input pins to Teensy pins. The analog MUX module allows you to select the comparator inputs via MCU registers (the analogComp library handles that for you).

analogComparator (CMP0):
 - 0. CMP0_IN0 (K20Pin 51 = TeensyPin 11)
 - 1. CMP0_IN1 (K20Pin 52 = TeensyPin 12)
 - 2. CMP0_IN2 (K20Pin 53 = TeensyPin 28)
 - 3. CMP0_IN3 (K20Pin 54 = TeensyPin 27)
 - 4. CMP0_IN4 (K20Pin 55 = TeensyPin 29)
 - 5. VREF Output/CMP0_IN5 (K20Pin 17)
 - 6. Bandgap
 - 7. 6b DAC0 Reference
analogComparator1 (CMP1):
 - 0. CMP1_IN0 (K20Pin 45 = TeensyPin 23)
 - 1. CMP1_IN1 (K20Pin 46 = TeensyPin 9)
 - 3. 12-bit DAC0_OUT/CMP1_IN3 (K20Pin 18 = TeensyPin 40/A14/DAC (Teensy 3.[12] only))
 - 5. VREF Output/CMP1_IN5 (K20Pin 17) 
 - 6. Bandgap
 - 7. 6b DAC1 Reference
analogComparator2 (CMP2 Teensy 3.[12] only):
 - 0. CMP2_IN0 (K20Pin 28 = TeensyPin 3)
 - 1. CMP2_IN1 (K20Pin 29 = TeensyPin 4)
 - 6. Bandgap
 - 7. 6b DAC2 Reference

Does the output of the comparator come from a certain pin?

Yes, it can. You'd have to write that code yourself though, since that's not currently supported in the analogComp library. Look for COMPO, CMP0_OUT, CMP1_OUT, and CMP2_OUT in the reference manual.

Each of the three comparators has only one output pin mapping available (I'm continuing the table here):

 - CMP0_OUT (K20Pin 50 = TeensyPin 13)
 - CMP1_OUT (K20Pin 49 = TeensyPin 10)
 - CMP2_OUT (K20Pin 27 = TeensyPin 24)

Can I set it up to cause in interrupt instead and not use that pin, so that it executes code in the program and thats it?

Yes, that's the intended operation. The analogComp library makes that easy:

analogComparator.enableInterrupt(myFunction[, event]);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.