An IR sensor simulator: an FC-51 obstacle sensor on an Arduino
Mokxi is a free online IR sensor simulator that runs in your browser, with nothing to install and no account needed. An FC-51 infrared obstacle sensor, the two-LED module in most robot car kits, on pin 2 of an Arduino Uno, with an LED on pin 13. Bring an obstacle toward it and the LED lights once it is in range, so you can check the wiring and the code before the module arrives.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
What you can do here
In the circuit above, or in the editor it opens.
- Bring an obstacle from 80 cm in to 1 cm and see where it is caught
- Swap a white card for a black object, which has to come much closer
- Turn the board’s threshold and move the range from about 9 cm to 29 cm
- Put a multimeter on OUT: 5 V when clear, close to 0 V when blocked
- Count objects going past, or stop a motor when one appears
- Add a second sensor and steer a robot round things
What the running circuit shows
OUT goes low, and the sketch is a button sketch.
The FC-51 shines infrared from its clear LED and watches for it to bounce back into the dark receiver beside it. A comparator on the board turns that into one digital output: OUT sits at 5 V while nothing is there and drops close to 0 V once enough light comes back. With the threshold in the middle, where a new board usually arrives, a white card is caught at 13 cm and missed at 14 cm.
The sketch running here is the plain button sketch: pin 2 set to INPUT_PULLUP, and the LED on pin 13 lit while the pin reads LOW. That is not a shortcut. To an Arduino the FC-51 looks exactly like a pushbutton to ground, so the code that reads a button reads this sensor, active LOW and all. If your own sketch lights the LED when nothing is there, it is testing for HIGH.
Click the swatch to make the obstacle black and it has to come in to 3 cm before the sensor sees it, because a black surface sends back only a small part of the light a white one does. Then open the sensor’s properties and change its threshold, the trimmer on a real board: at 0.9 a white card is caught at 29 cm, and at 0.1 only at 9 cm.
How the sensor is modeled
The physics behind the numbers on the listing.
There is no datasheet for the FC-51 board, only listings that give 2 to 30 cm and a 35 degree cone, so the model works from the physics. The light that comes back falls with the square of the distance and scales with how much the surface reflects, and the board’s LM393 comparator trips when the receiver’s voltage crosses the threshold. A white card is taken to reflect 90 percent and a black object 5 percent.
What it leaves out: sunlight, which is full of infrared and makes a real one see things outdoors that are not there; the width of the cone, since anything on the slider is straight in front; colors between white and black; and the board’s response time. Like the real board, the comparator has no hysteresis, so an object held right at the edge can make a real module flicker. If you came for the three-pin receiver that reads a TV remote, that is a different part, with its own tutorial.
Questions
Is it free?
Yes. The circuit on this page, every part in it and the editor it opens in are on the free plan, with no card.
Do I need an account?
No. The circuit runs the moment the page does, and the editor opens without one. A free account is only for saving a project to the cloud and sharing it by link.
How far can an FC-51 IR sensor detect?
About 2 to 30 cm on a white surface, set by the trimmer. In this model the middle of the trimmer catches a white card at 13 cm, and the two ends catch it at about 9 and 29 cm. A black object has to come about four times closer.
Why does my IR sensor read LOW when it sees something?
Because the comparator pulls OUT to ground when enough light comes back, and the board’s obstacle lamp lights at the same moment. Read it with digitalRead and test for LOW, the same as a button wired to ground.
Can an IR obstacle sensor measure distance?
Not really. It gives one bit, near or not near, and where it switches depends on the surface as much as on the distance. For a distance in centimeters, use an HC-SR04 ultrasonic sensor or a VL53L0X.
More simulators
Each one opens on a circuit that is already running.
Put an obstacle in front
Open the circuit, bring the obstacle in and watch the LED. Nothing to install, and no account needed.