<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									Pico Robotics: Pololu QTRX Reflectance Sensor - Raspberry Pi				            </title>
            <link>https://forum.dronebotworkshop.com/raspberry-pi/pico-robotics-pololu-qtrx-reflectance-sensor/</link>
            <description>Discussion board for Robotics, Arduino, Raspberry Pi and other DIY electronics and modules. Join us today!</description>
            <language>en-US</language>
            <lastBuildDate>Sun, 08 Mar 2026 04:03:30 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>Pico Robotics: Pololu QTRX Reflectance Sensor</title>
                        <link>https://forum.dronebotworkshop.com/raspberry-pi/pico-robotics-pololu-qtrx-reflectance-sensor/#post-48517</link>
                        <pubDate>Thu, 25 Jul 2024 16:18:07 +0000</pubDate>
                        <description><![CDATA[I noticed that @tmatatu is using the Pololu QTR reflectance sensor with Arduino, so I thought that I’d pass along what I’m doing with that sensor on the Pico.  The short version is that I ha...]]></description>
                        <content:encoded><![CDATA[<p>I noticed that @tmatatu is using the Pololu QTR reflectance sensor with Arduino, so I thought that I’d pass along what I’m doing with that sensor on the Pico.  The short version is that I had to add: gpio_disable_pulls(sensorPin); to get it to work.</p>
<p>I’m interested in doing some more work on evolving robot controllers, but this time with the coordination of a swarm of robots to move “food” to a “nest”.<span class="Apple-converted-space">  </span>So my use case is to use one QTRX reflectance sensor <a href="https://www.pololu.com/product/4341" target="_blank" rel="noopener">https://www.pololu.com/product/4341 </a>to allow the robots to recognize when they are over some dark area (the nest).</p>
<p>It was very straightforward to port the routines I needed from the Pololu QTR Reflectance Sensor Library <a href="https://github.com/pololu/qtr-sensors-arduino" target="_blank" rel="noopener">https://github.com/pololu/qtr-sensors-arduino </a>because my use case is so simple and I’m only using one sensor.  I used target_link_libraries(test, pico_stdlib) in the CMakeList.txt file.<span class="Apple-converted-space">  </span>So nothing very exciting there…</p>
<p>Here’s the code.</p>
<pre contenteditable="false">/*
This is a watered down version of the Pololu library that runs their QTRX IR 
reflectance sensors. I have just a single sensor board of the RC variety so
this is very simple. No dimming of IED or calibration steps or readLineBlack
or readLineWhite calls... Just read raw sensor values.
*/

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include "pico/time.h"
#include "pico/stdlib.h"

#define sensorPin 13 // Out pin on QTRX sensor
#define emitterPin 14 // Control pin on QTRX sensor
uint16_t qtrxTimeout = 2500; // 2500us


void emitterOn(uint16_t emitter) {
  gpio_put(emitter, 1); // set emitter pin to HIGH
  sleep_us(300);
  return;
}

void emitterOff(uint16_t emitter) {
  gpio_put(emitter, 0); // set emitter pin to LOW
  sleep_us(1200);
  return;
}

uint16_t readQTRX() {
  uint16_t sensor = qtrxTimeout;
  uint16_t time;
  uint64_t startTime;

  gpio_set_dir(sensorPin, GPIO_OUT); // set direction of GPIO to out
  gpio_put(sensorPin, 1); // drive pin high
  sleep_us(10); // charge line for 10 us

  startTime = time_us_64();
  time = 0;
  gpio_set_dir(sensorPin, GPIO_IN); // set direction of GPIO to in

  while (time &lt; qtrxTimeout) {
    time = time_us_64() - startTime;
    if ((gpio_get(sensorPin) == 0) &amp;&amp; (time &lt; sensor))
      sensor = time; // record the first time the line reads low
  }
  return sensor;
}

void readSensor(uint16_t *sensorValue) {
  emitterOn(emitterPin);
  *sensorValue = readQTRX(sensorPin);
  emitterOff(emitterPin);
  return;
}


int main()
{
  stdio_init_all();
  gpio_init(sensorPin); // Emable I/O set to GPIO_SIO and set to input.
  gpio_init(emitterPin);
  gpio_set_dir(emitterPin, GPIO_OUT);
  gpio_disable_pulls(sensorPin);

  uint16_t sensorValue;

  printf("waiting for usb host");
  while (!stdio_usb_connected()) {
    printf(".");
    sleep_ms(500);
  }
  sleep_ms(2000);

  while (true) {
    readSensor(&amp;sensorValue);
    printf("voltage went low in %ld us\n", sensorValue);
    // Larger sensor values mean less reflectance, lower values more reflectance.
    sleep_ms(250);
  }
}

</pre>
<p> </p>
<p>Tom</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/raspberry-pi/">Raspberry Pi</category>                        <dc:creator>THRandell</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/raspberry-pi/pico-robotics-pololu-qtrx-reflectance-sensor/#post-48517</guid>
                    </item>
							        </channel>
        </rss>
		