How to use TCRT5000 with Arduino_DIY RPM meter

Well i bought a very inexpensive infrared optical sensor TCRT5000  from ebay.The TCRT5000 and TCRT5000L are reflective sensors which include an infrared emitter and phototransistor in a leaded package which blocks visible light. The package includes two mounting clips. The peak operating distance is about 2.5mm and the range is 0.2mm to 15mm. Typical output current under test of the phototransistor is Ic = 1mA and the emitter wavelength is 950nm.The applications is endless. T day we will learn how to use it with an arduino.tcrt5000

Make the circuit above, you can play with the resistor value of the infrared emitter for example you can put 100 to 500 Ohm resistor and see what will the results be. You can do that with the phototransistor resistor as well , put 5 to 20K resistor. For me 200 Ohm on the infrared emitter and 10K on the phototransistor works pretty well. To see some values in your screen connect SIGNAL to A0 pin of the Arduino an Upload the sketch below

int tcrt;
void setup(){
pinMode(13,OUTPUT);
Serial.begin(9600);
}
void loop(){
tcrt = analogRead(A0);
analogWrite(13,tcrt/4);
Serial.println(tcrt);
delay(150);
}

The code will print the analog value that the arduino reads on the pin A0 every 150ms
also the led o pin 13 will change brightness. The analogRead gives a value from 0 to 1024 the analogWrite has to inputs (pin,number 0-256) so we divide tcrt by 4.

You can use the TCRT5000 to make a line follower because in distance X from a white surface the input value of the sensor will very different from distance X from a black surface. I used it to calculate the RPM of a motor. I put some white tape in the shaft of the motor and then I made a black line with a marker. The code of the Arduino is the following.

 

long int count2=0;
int count1=1;
int flg=0;
int tcrt;
void setup(){
Serial.begin(9600);
}
void loop(){
tcrt = analogRead(A0);
if (tcrt<500 & flg==0){
count1++;
flg=1;
}
if (tcrt>=500) flg=0;
if(millis()/count2==1000){
 
Serial.println(count1/2);
count2++;
count1=0;
}
 
}

 

We read the value of the A0 and save it in tcrt if tcrt is less than 500 and flg=0
count1++ . I put 500 because from testing a found that with the tape I am using and distance 5mm the analogRead gives me 700-800 for the white side of the tape and 300-400 for the pained black side. You will have to test it. At first I didn’t think to put flg in the code and the RPM I was seeing was 3000+ revolutions a send. That hapend because when the sensor is seeing the black side of the tape count1 increases not by 1 but for every time we Read value less than 500, when the sensor is on the black side the arduino Reads from the sensor multiple times, the code runs at 40 Khz.To solve that when we see black for the first time we make flg=1 and count1 don’t increases more than 1. Later when we see white we make flg = 0 agen. Millis() is a method that returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days. We want every 1000ms to display the revolutions so we take millis() and divide by count2 which increases every time millis()/count2 rich 1000ms. At first count2=1 when millis()/count2==1000 we increase count2 by 1 and display the revolutions with is count1 divided by 2, we divide count1 by 2 because for its revolution we must se 2 times the black side of the tape. At the second time is millis()=1000+x so when millis()/count2==1000 (2000/2==1000) we do what we did before. Third time millis()/count2 is (3000/3==1000)…..

Usefull links:
http://www.vishay.com/docs/83760/tcrt5000.pdf
https://www.arduino.cc/en/Reference/Millis
https://www.arduino.cc/en/Reference/AnalogWrite
https://www.arduino.cc/en/Reference/AnalogRead

By Dimitris Xarir.

7 Comments on “How to use TCRT5000 with Arduino_DIY RPM meter

  1. Hi there! This is my first comment here so I just wanted to give a quick
    shout out and tell you I really enjoy reading through your articles.
    Can you recommend any other blogs/websites/forums that cover the same subjects?
    Thank you!

Leave a Reply

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