Soooo I am on the same boat as earlier people. I have this IR sensor I am trying to hookup to an Arduino One currently, but will eventually be on the adafruit Pro trinket. I am using the USB for power and have tried this code both in 5v and 3.3v and am having issues. I currently have it printing the voltage output because I was noticing the inconsistencies. Also for the power function, that was pretty much trial and error until I got a number that fit for the distance I was at.
2 questions based on the 5v setup
-
The sensor is giving inconsistent numbers when doing various lengths. Like one time i measure 100 cm, it will give me about 520. If it go to a different length then come back, or reupload, then next time it will be 490 or such. Is there a connection fault or is it the usb issue?
-
Am I even close on the conversion formulas used? I looked at different examples through the previous posts and years and found this one to, I think, supposedly be the most accurate at the end result, but it currently isn't working for me.
Any help is greatly appreciated, or if you have the code, even more so!!!
If you also have a better suggestion about how to do it and can give me some examples, that would be helpful too
#define sensorPin 0
#define VOLTS_PER_UNIT .00488F // (.0049 for 10 bit A-D) :1/v *225@5 -.887 , 148.5@3.3: -.391087
float volts;
float inches;
float proxSens = 0;
float cm;
void setup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
}
void loop() {
proxSens = analogRead(sensorPin);
volts = (float)proxSens * VOLTS_PER_UNIT; // ("proxSens" is from analog read)
cm = 225 * pow(volts,-.887); // same in cm
Serial.println(proxSens);
//Serial.print(cm);
Serial.print(' ');
delay(200);
}