Advertisement
microrobotics

Pololu QTRX-HD-04A Reflectance Sensor Array

Apr 24th, 2023 (edited)
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The QTRX-HD-04A Reflectance Sensor Array is a 4-channel high-resolution infrared reflectance sensor designed for detecting lines and edges. It consists of four IR emitter and phototransistor pairs that can measure the amount of infrared light reflected from a surface. This array is useful in applications like line following, edge detection, or object proximity detection.
  3.  
  4. The QTRX-HD-04A has four separate analog output pins that can be connected to the analog inputs of a microcontroller (e.g., Arduino) to read the sensor values. Each output provides a voltage proportional to the amount of IR light reflected by the surface.
  5.  
  6. To use this code:
  7.  
  8. Install the QTRSensors library in the Arduino IDE.
  9. Connect the QTRX-HD-04A Reflectance Sensor Array to your Arduino according to the product documentation: https://www.pololu.com/docs/0J13
  10. Upload the code to your Arduino board.
  11. Open the Serial Monitor to view the calibrated sensor values for each of the four channels.
  12. You can then use these sensor values for various applications, such as line following or object detection.
  13.  
  14. Here is the complete code for reading sensor values from the Pololu QTRX-HD-04A Reflectance Sensor Array using an Arduino:
  15. */
  16.  
  17. #include <QTRSensors.h>
  18.  
  19. // Define the pins connected to the QTRX-HD-04A sensor array
  20. #define QTR_PIN_1 A0
  21. #define QTR_PIN_2 A1
  22. #define QTR_PIN_3 A2
  23. #define QTR_PIN_4 A3
  24.  
  25. // Create an instance of the QTRSensorsAnalog class with the pins defined
  26. QTRSensorsAnalog qtr((unsigned char[]){QTR_PIN_1, QTR_PIN_2, QTR_PIN_3, QTR_PIN_4}, 4);
  27.  
  28. // Array to store the sensor values
  29. unsigned int sensorValues[4];
  30.  
  31. void setup() {
  32.   Serial.begin(9600); // Initialize serial communication at 9600 baud rate
  33.   delay(500); // Give the sensors some time to power up
  34.  
  35.   // Calibrate the sensors by placing them over the surface to be used
  36.   // and sliding them across the surface for a few seconds.
  37.   Serial.println("Calibrating...");
  38.   for (int i = 0; i < 400; i++) {
  39.     qtr.calibrate(QTR_EMITTERS_ON);
  40.     delay(20);
  41.   }
  42.   Serial.println("Done!");
  43. }
  44.  
  45. void loop() {
  46.   // Read the sensor values and store them in the sensorValues array
  47.   qtr.readCalibrated(sensorValues, QTR_EMITTERS_ON);
  48.  
  49.   // Print the sensor values to the serial monitor
  50.   for (int i = 0; i < 4; i++) {
  51.     Serial.print("Sensor ");
  52.     Serial.print(i + 1);
  53.     Serial.print(": ");
  54.     Serial.println(sensorValues[i]);
  55.   }
  56.   Serial.println();
  57.  
  58.   delay(500); // Add a small delay to avoid flooding the serial monitor
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement