How to Hookup and Program a QTR-8 Sensor Array

If you haven’t been following along from the start, feel free to start on the main article Build A Custom PID Line Following Robot From Scratch which includes every step regarding how to build this robot. Maybe you just found this article while trying to find help attaching a QTR sensor to your robot and that’s fine to, this article should help you. I’ll show you how to program a QTR sensor array, let’s get started

Parts

As far as parts go, assuming you’ve already built the robot we just need the following items

  • QTR-8 sensor (can be an 8A or an 8RC)
  • Breadboard wires, in this case I used Dupont style wires with pins attached
  • Two screws with nuts to mount the QTR sensor (you could just simply use hot glue or double sided tape)
  • Finally, our robot that we’ve built so far

Watch the video

As always, there is a video you can watch if you’re more into that sort of thing. This video covers attaching the sensor and testing the line array. I actually go through the process showing you any issues I ran into and how I resolved them.

The hookup

I attached the power and ground to 5V and ground and I also attached my “led on” pin or emitter pin to 5V since I don’t care to turn the emitters on and off, I just want them on all the time. For the sensor readings, I fed those into teensy pins 16-23 which equates to analog pins A2-A9, either value will work. For full disclosure, I had pin A9 (or 23) attached to sensor 8 and A2 (or 16) was attached to sensor 1

The code

Honestly, as shown in the video I just used the example code from the qtr-sensors-arduino library. Also, as mentioned in the video, there is a bug in the code which may only be with Teensy boards but it causes the reading to overflow and bounce between 0 and 1000 which gives very bad output. With that said, you can use my library located at https://github.com/gberl001/qtr-sensors-arduino and has the fix in place.

Example

Here’s the example I used, don’t forget you need to update the following pieces to work with your robot.

  • NUM_SENSORS – This will be the number of sensors you have
  • EMITTER_PIN – If you aren’t using one, be sure to change this to QTR_NO_EMITTER_PIN to save 400µs

The Rollover Bug

The bug I encountered may only be related to the compiler for Teensy, I’m not sure. I would assume that if this were a bug on Arduino boards then there would be lots of people complaining. However, the issue was that there was a mix of unsigned and signed values in the calibration adjustment calculation. This resulted in an unsigned final value so if your sensor ever read below the minimum calibration, the calibrated value would roll over from 0 to 4,294,967,295 and then be constrained to 1000.

The simple fix was to change the unsigned calmin, calmax variables to signed however I changed denominator as well for good measure, we don’t need the variable to be unsigned so I’m not sure why it was explicitly set to unsigned.

 

One comment

Leave a Reply

Your email address will not be published.