I have received a few messages asking me to give a bit more detail into the individual bits of the robots I have built so far. Thus I am starting this series of posts in which I will cover a single sensor\actuator integration to an Arduino board per post.
Today we will have a look at how to use a Sharp IR Sensor.
The IR sensor has 3 pins – 2 pins used to power the sensor and a signal pin that is used to communicate its reading.
As shown in the image above, connect the red pin to the 5V pin on the Arduino, the black to the GND pin and the yellow signal pin to any one of the analog pins, for the purpose of this example we will use A4.
Below is the code used to get a reading from the sensor and print it out using the Serial.println:
#define IR_PIN A4 int IRDist = 0; void setup() { Serial.begin(9600); } void loop() { pinMode(IR_PIN,INPUT); IRDist = analogRead(IR_PIN); Serial.println(IRDist); delay(1000); }
I suggest using this code to experiment and see what readings get generated by placing the sensor at different distances from an obstacle.
analogRead(IR_PIN) will give you just voltage at pin A4. How you convert it to distance?
LikeLiked by 1 person
That is correct, to convert to voltage can be done as follows:
IRDistmm = ((67870.0 / (IRDist – 3.0)) – 40.0);
I have however noticed that I need to manually adjust the constant values above for each sensor to get an accurate reading.
LikeLike
Thanks for following my blog! I hope you like my writing. If you do I’m giving away a free book on Smashwords http://smashwords.com/books/view/456166
LikeLike