IR Reflectance Sensor for Non-Contact Tachometer

Hi all,

I am currently trying to develop a 'reflectance' based IR Tacho (Not the break beam style). I purchased an IR photo transistor from sparkfun, Intelligent Power and Sensing Technologies | onsemi , and ran this code to decode the values I'm getting from it. I'm getting wierd readings and I was wondering if I need to debounce this sensor somehow. I don't have much programming experience so any help you can give me would be much appreciated.

This is the code I'm running to collect the values.

int sensorPin = 2;
int val = 0;
void setup()
{
  Serial.begin(9600);
}

void loop()
 {
    val = analogRead(sensorPin);
    Serial.print("Analog Value: ");
    Serial.println(val);
    delay(250);
 }

These are the values I get over serial, it happens in a periodic fashion, but I have no idea why.

Analog Value: 0
Analog Value: 0
Analog Value: 0
Analog Value: 0
Analog Value: 1
Analog Value: 4
Analog Value: 6
Analog Value: 10
Analog Value: 15
Analog Value: 21
Analog Value: 29
Analog Value: 38
Analog Value: 46
Analog Value: 55
Analog Value: 66
Analog Value: 75
Analog Value: 89
Analog Value: 110
Analog Value: 143
Analog Value: 169
Analog Value: 195
Analog Value: 215
Analog Value: 223
Analog Value: 228
Analog Value: 227
Analog Value: 219
Analog Value: 202
Analog Value: 184
Analog Value: 163
Analog Value: 139
Analog Value: 112
Analog Value: 84
Analog Value: 59
Analog Value: 33
Analog Value: 6
Analog Value: 0

Also, while the values are non zero, I can put my finger in front of the sensor and it does read 0, as it should for a reflective surface.

Ples halp.

Can you post a schematic of the wiring of the sensor including the value of the current limiting resistor for the LED and pullup resistor of the phototransistor?

What do you mean when you say pullup resistor? The Schematic for the IR sensor I'm using is at the link I included in the original post, and the resistor I'm using to limit the current for the IR led is 270ohm.

Hello brandon00.

I had a similar problem.

Try to add
analogWrite(sensorPin, 0);
directly before
val = analogRead(sensorPin);

Let me know if it works, please.

Have a nice day.

I don't see the schematic for your setup in the data sheet. You need a pullup resistor with the photo transistor for the same reason you need one for a switch. Put a 30K resistor from Vcc to the collector of the phototransistor, the emitter to ground and your input (I use a digital input) to the junction of the resistor and collector. The input will be LOW when IR light is detected. Shield the sensor from ambient light to avoid false readings.

As for analogWrite before read, refer to this discussion. http://forum.arduino.cc/index.php?topic=382451.0

Have a look at this:

How fast is the spinning going to get?

Treat the signal as digital rather than analog.

Does the pin change state as you rotate the device?

Look at some of the examples of pulseIn.

Try to add
analogWrite(sensorPin, 0);
directly before
val = analogRead(sensorPin);

Let me know if it works, please.

It won't. Writing to digital pin 2 before reading analog pin 2 will not affect the value read from analog pin 2.

Despite it's stupid name, analogWrite() affects digital pins, not analog pins.

Thanks all for your help. I have tried your suggestions, but nothing seems to rectify these unexpected readings. I plan on reading the sensor and analog pin with some probes later on today. I will let you know if I make any breakthroughs. Cheers!

brandon00:
Thanks all for your help. I have tried your suggestions, but nothing seems to rectify these unexpected readings. I plan on reading the sensor and analog pin with some probes later on today. I will let you know if I make any breakthroughs. Cheers!

Well, have you tried a diode for rectification? Just kidding.

But, analogue readings are very slow. It seems impractical to use them for a tachometer, unless the RPMs are extremely low.

Are you trying to make an analog tach by charging a capacitor with pulses?

outsider:
Are you trying to make an analog tach by charging a capacitor with pulses?

Not in software, as there is no attempt to send a trigger pulse.