Advertisement
microrobotics

ITR9909

Mar 31st, 2023
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The ITR9909 is an infrared reflective sensor 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 ITR9909 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.  
  10. const int IR_LED_PIN = 3;     // IR LED connected to pin 3
  11. const int IR_RECEIVER_PIN = A0; // Phototransistor connected to analog pin A0
  12.  
  13. void setup() {
  14.   Serial.begin(9600);
  15.   pinMode(IR_LED_PIN, OUTPUT);
  16.   digitalWrite(IR_LED_PIN, HIGH); // Turn on the IR LED
  17. }
  18.  
  19. void loop() {
  20.   int sensorValue = analogRead(IR_RECEIVER_PIN);
  21.   Serial.print("Sensor Value: ");
  22.   Serial.println(sensorValue);
  23.   delay(500); // Wait 500ms between readings
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement