Hi Codnes, I think what you should focus on first is getting sensor data from your sensor. This is pretty basic but it's important to know what the data means since in it's raw for it can seem like an arbitrary number.
int IR;
void setup()
{
Serial.begin(9600); //Initialize serial with baud rate 9600
}
void loop()
{
x = analogRead(2); //Read value from analog pin 2 (You can change this if needed)
Serial.println(IR); //Print the value from the sensor over the serial monitor
As I mentioned before, this will return a value from the sensor but it's more or less an arbitrary number. It is useful though to test your sensor to make sure it's working. If you put your hand in front of the sensor and move it, you should see the numbers going up or down. If you don't, the sensor might not be working or is not connected to the analog pin the Arduino is reading. You might also notice that the numbers from this code fluctuate a little bit even if you have an object sitting in front of it that isn't moving. This is just noise in the signal and there are ways around it.In order to get an actual measurement out of that number we need to add another line that does the math for us. From what I have gathered the math used to turn the output from this sensor into distance in centimeters is Distance (cm) = 4800/(SensorValue - 20). So that can be added to the above code like so..
int IR;
void setup()
{
Serial.begin(9600); //Initialize serial with baud rate 9600
}
void loop()
{
x = analogRead(2); //Read value from analog pin 2 (You can change this if needed)
int CM = (4800 / (IR -20)); //Convert the raw value from the sensor to CM
Serial.println(CM); //Print the sensor value in CM
This should have your Arduino outputting sensor data and giving you a relatively accurate measurement in CM. The last step in your request is to light an LED when the sensor picks up an object within 50CM. This is pretty basic as well and can be added into the code above. In your loop you will be checking the value from "CM" to see if it's "less than, or equal to" 50CM and under the condition that this is "True" light the LED.
Along with your LED, you will want a resistor. Anything between 220 ohms and 1k ohms should work fine. Connect the LED with your resistor in series between GND and one of the digital pins on your Arduino. I'll say D13 since it's right next to GND but you can use what ever pin you prefer. Let's get to the code..
int IR;
void setup()
{
pinMode(13, OUTPUT); //Set digital pin 13 as an output
Serial.begin(9600); //Initialize serial with baud rate 9600
}
void loop()
{
x = analogRead(2); //Read value from analog pin 2 (You can change this if needed)
int CM = (4800 / (IR -20)); //Convert the raw value from the sensor to CM
Serial.println(CM); //Print the sensor value in CM
if (CM <= 50)
{
digitalWrite(13, HIGH); //This turns the pin HIGH which will light the LED
}
else
{
digitalWrite(13, LOW); //Turn the pin LOW and thus turn off the LED
}
Basically the loop will now always have pin 13 set LOW unless the sensor value in CM is less than or equal to 50. Each pass of the loop looks at this if / else set and should only light the led if the if statement returns true. This is very useful logic when you're trying to do things like make a robot turn away from a wall rather than run into it.
You need to make sure your Arduino board is connected to your computer to be able to see the serial outputs. From the Arduino IDE you can open the serial monitor after uploading your sketch and when the micro-controller reboots, you should start seeing output.
I hope the above examples are helpful to you.