Distance Analog Sensor

While struggling to grasp some of the simpler elements of this class (like resistance and voltage), I’m slowly beginning to understand how basic input-output devices work with the arduino.

I figured out how to wire up an IR sensor to activate one of the two lights. I’m guessing this is some form of the technology that cars use on their bumpers or elevators use on their doors. Shown in the gif above, the red light means “too close” or “stop,” and the green light means “not close” or “go.”

int lm = 0;

void setup() {
 // put your setup code here, to run once:
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);

void loop() {
 // put your main code here, to run repeatedly:
 lm = analogRead(A0);
 if (lm < 500) {
 digitalWrite(4, HIGH);
 digitalWrite(5, LOW);

 digitalWrite(4, LOW);
 digitalWrite(5, HIGH);

One of the challenging aspects is working with imperfect data. There were plenty of times where the numbers being sent back from the sensor were erratic. If I were to hook up a motor to this, there would need to be a level of processing to clean out the outlying numbers, some kind of smoothing algorithm.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.