speed sensor/How it works/04. The comparator and the light
How it works · 04 of 10

The comparator and the light

An LM393 compares the light sensor's voltage with half of VCC. Clear, the sensor is lower, and the comparator pulls SIGNAL LOW and the red LED lights. Blocked, the sensor rises to VCC, the comparator lets go, and a 10 kΩ pulls SIGNAL up to VCC: HIGH, LED dark. There is no hysteresis, so a slow edge can flip it more than once.

Two inputs, one answer

A comparator has two inputs and one output, and it answers one question: which input is higher? On this board the plus input is the light sensor's voltage and the minus input is half of VCC, made by two equal 10 kΩ resistors. The chip is an LM393, the same one the TK29 uses.

The comparator and the light
Slot
VCC
IN+, the sensor
0.2 V
IN-, half VCC
1.65 V
SIGNAL
LOW
Whole block
about 4.6 mA
Beam clear. The light sensor conducts and pulls IN+ down to about 0.2 V, under IN−, which the two 10 kΩ hold at 1.65 V. The comparator's output transistor turns on and pulls SIGNAL to about 0.15 V: LOW. The red LED now has VCC across it and lights, about 1.2 mA. The infrared LED takes about 2.2 mA whatever the slot holds: this block is never off while it has power.

Switch between clear and blocked and follow the circuit:

  • Clear. The sensor pulls its voltage under half of VCC, so plus is lower than minus. The comparator's output transistor turns on and pulls SIGNAL down to about 0.15 V: LOW.
  • Blocked. The sensor passes nothing and R11 lifts its voltage to VCC, above half. The output transistor turns off and lets go.

Only down

The LM393 can pull its output to GND and cannot push it up. That is an open collector. R7, 10 kΩ from SIGNAL to VCC, does the pushing: when the comparator lets go, SIGNAL rises to VCC through it, and that is HIGH. So HIGH on SIGNAL is exactly VCC, which is why VCC has to be your logic voltage.

The light shows LOW

The red LED and its 1 kΩ run from VCC onto SIGNAL. While SIGNAL is HIGH both ends are at VCC and it is dark; while the comparator pulls SIGNAL down it lights, about 1.2 mA from 3.3 V and 2.9 mA from 5 V, worked out for a red LED's usual 2 V. So lit means clear. It needs no sketch, which makes it the first thing to look at when a count is wrong.

No hysteresis

The comparator answers in about a microsecond, and nothing on this board feeds its output back to its input to hold a decision once made. So while the sensor's voltage sits right at half of VCC, any wobble on it flips SIGNAL, and one slow edge can come out as several. A disc spinning at speed crosses that point too quickly for it to matter; a card eased in by hand, or a disc turning a few times a minute, can count twice. The tachometer's sketch deals with it, and the TK29 book's chatter and hysteresis shows the same effect on the same chip.

With the slot blocked the plus input sits at VCC, above the top of the LM393's input range. The sheet allows that as long as the other input stays inside the range, and half of VCC does at room temperature. At 3.3 V the margin is 0.15 V, and the sheet's range narrows when the chip is hot; the block is a bench part, not one to bake.

When it does not work

The LED is on, so SIGNAL should be HIGH, but my sketch reads 0.

They agree. The LED lights while SIGNAL is LOW, and 0 is LOW. It runs from VCC onto SIGNAL, so it only has a voltage across it when the comparator pulls SIGNAL down, which is while the slot is clear.

Can I drive an LED or a buzzer straight from SIGNAL?

Not from HIGH. HIGH is only the 10 kΩ pull-up, which can supply a fraction of a milliamp before the voltage sags. Read SIGNAL with your board and drive loads from one of its own pins.

Should I use INPUT_PULLUP?

There is no need. The board has its own 10 kΩ pull-up on SIGNAL and the comparator pulls it down, so INPUT is enough. INPUT_PULLUP only adds a second pull-up beside the first, which does no harm.

Where this goes next

Why VCC goes to 3V3 on an ESP32 and 5V only on an Uno.

VCC is your logic voltage →

Edit this page — content/books/ir-speed-sensor/the-comparator-and-the-light.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

Infrared Speed Sensor

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →