Advertisement
microrobotics

TCRT5000 Reflective Photosensor

Mar 31st, 2023 (edited)
3,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The TCRT5000 is a reflective photosensor that consists of an IR LED and a phototransistor in a single package. Here's a simple Arduino code to read the sensor's output and display it on the Serial Monitor:
  3.  
  4. To wire the TCRT5000 sensor, connect its anode (IR LED's longer leg) to the IR_LED_PIN (pin 3) on the Arduino and the cathode (IR LED's shorter leg) to ground. Connect the collector (phototransistor's flat side, longer leg) to the IR_RECEIVER_PIN (analog pin A0) on the Arduino, and the emitter (phototransistor's round side, shorter leg) to ground. Add a pull-up resistor (typically 10kΩ) between the IR_RECEIVER_PIN (analog pin A0) and the 5V pin on the Arduino.
  5.  
  6. This code will continuously read the sensor's output and print the readings on the Serial Monitor. The sensorValue will vary depending on the amount of infrared light reflected by the object in front of the sensor.
  7. */
  8.  
  9. const int IR_LED_PIN = 3;     // IR LED connected to pin 3
  10. const int IR_RECEIVER_PIN = A0; // Phototransistor connected to analog pin A0
  11.  
  12. void setup() {
  13.   Serial.begin(9600);
  14.   pinMode(IR_LED_PIN, OUTPUT);
  15.   digitalWrite(IR_LED_PIN, HIGH); // Turn on the IR LED
  16. }
  17.  
  18. void loop() {
  19.   int sensorValue = analogRead(IR_RECEIVER_PIN);
  20.   Serial.print("Sensor Value: ");
  21.   Serial.println(sensorValue);
  22.   delay(500); // Wait 500ms between readings
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement