Advertisement
microrobotics

Pololu QTR Reflective Array 31 Channel - RC OUT

Apr 22nd, 2023
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. To use the Pololu QTR-31RC Reflective Array with an Arduino, you will first need to install the "QTRSensors" library. Open the Arduino IDE, navigate to "Tools" > "Manage Libraries..." and search for "QTRSensors" by Pololu. Install the library and restart the Arduino IDE.
  3.  
  4. Next, connect the Pololu QTR-31RC Reflective Array to your Arduino as follows:
  5.  
  6. Connect the sensor's VCC pin to the Arduino 5V pin.
  7. Connect the sensor's GND pin to the Arduino GND pin.
  8. Connect the 31 sensor outputs (OUT0 to OUT30) to 31 digital input pins on your Arduino. Note that most Arduino boards do not have 31 available digital input pins. You might need to use an Arduino Mega or a similar board with more digital input/output pins.
  9. Now, upload the following code to your Arduino:
  10.  
  11. This code initializes the QTR-31RC Reflective Array using the QTRSensors library and calibrates the sensors in the setup() function. In the loop(), the calibrated sensor readings are obtained using the readCalibrated() function, and the sensor values are printed to the serial monitor every 500 milliseconds.
  12.  
  13. After uploading the code to your Arduino and opening the serial monitor, you should see the calibrated sensor values being printed.
  14.  
  15. You can modify the code to control other devices or make decisions based on the sensor readings according to your project requirements, such as line following or detecting objects on a conveyor belt.
  16. */
  17.  
  18. #include <QTRSensors.h>
  19.  
  20. const uint8_t sensorCount = 31;
  21. uint8_t sensorPins[sensorCount] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52};
  22.  
  23. QTRSensorsRC qtr(sensorPins, sensorCount, 2000, QTR_NO_EMITTER_PIN);
  24.  
  25. unsigned int sensorValues[sensorCount];
  26.  
  27. void setup() {
  28.   Serial.begin(9600);
  29.  
  30.   // Calibrate the sensors by placing the array over a white surface and a black surface
  31.   Serial.println("Calibrating...");
  32.   for (uint16_t i = 0; i < 400; i++) {
  33.     qtr.calibrate();
  34.     delay(20);
  35.   }
  36.   Serial.println("Calibration complete.");
  37. }
  38.  
  39. void loop() {
  40.   qtr.readCalibrated(sensorValues);
  41.  
  42.   Serial.print("Sensor values: ");
  43.   for (uint8_t i = 0; i < sensorCount; i++) {
  44.     Serial.print(sensorValues[i]);
  45.     Serial.print(i < sensorCount - 1 ? ", " : "");
  46.   }
  47.   Serial.println();
  48.  
  49.   delay(500);
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement