Qtr-1rc led on and off

Hi,

I have a Arduino Mega board conected to a QTR-1RC. All I want to do is turn on an LED when passing my finger through the snesor. I don’t get anykind of output. Can someone tell me what im missing.
conections:

This is my CODE:


#include <PololuQTRSensors.h>

#define SENSOR_PIN 2
#define LED 13
PololuQTRSensorsRC qtr((unsigned char) {SENSOR_PIN}, 1, 1000);

unsigned int sensorValues[1];

void setup()
{
pinMode (LED, OUTPUT);
pinMode (SENSOR_PIN, INPUT);

Serial.begin(9600);
}

void loop()
{
qtr.readCalibrated(sensorValues);
if (sensorValues[0] < 500)
{

digitalWrite(LED, HIGH);
Serial.println(sensorValues[0]);
} else{
digitalWrite(LED, LOW);
}

}

Hello.

You’re reading calibrated sensor values without actually calibrating the sensor. You should either add a calibration phase or change readCalibrated() to read() so that you’re working with raw values instead. I suggest you use the Arduino serial monitor to get a sense of the raw sensor values you get as you pass your finger by; depending on how far away your finger is from the sensor, you might want to use a longer timeout so you can better resolve the difference between a low-reflectance finger and no-reflectance open space (looking at the raw values in the serial monitor can help you decide if this is necessary). You also might get interference from ambient IR with your sensor in that position, so you might consider adding some additional shielding, and I recommend you keep it out of direct sunlight.

- Ben

Hi,

I included calibration and .read.
I did some modification to my code and still cant get the LED to turn on can you tell me what could be be wrong with it?

#include <PololuQTRSensors.h>

PololuQTRSensorsRC qtr((unsigned char[]) {8}, 1);

void setup()
{
pinMode(13, OUTPUT);
int i;
for (i = 0; i < 250; i++)
{
qtr.calibrate();
delay(20);
}
}
void loop()
{
unsigned int sensors[0];
int number = 0;
int position = qtr.readLine(sensors);
if (sensors[0] > 750)
{
return;
}
if( sensors[0] > 750)
number = 1;

for(int i=0;i< number;i++){
digitalWrite(13,HIGH);
delay(20);
digitalWrite(13,LOW);
delay(20);
}
delay(500);
}

The code as you’ve written it makes it impossible for the LED to ever turn on. In order to make it past:

if (sensors[0] > 750) return;

sensors[0] must be less than or equal to 750, which means the following:

if (sensors[0] > 750)

will never evaluate as true and number will never be set to 1. Also, you’re sensors array has zero elements when you want it to have one (does it even compile the way you have it written now?).

In general, your program seems way more complicated than it needs to be. Why not do something like:

void loop() 
{ 
  unsigned int sensors[1]; 
  qtr.readCalibrated(sensors); 
  if (sensors[0] > 750) 
  {
    digitalWrite(13,HIGH);
    delay(20); 
    digitalWrite(13, LOW); 
    delay(500);
  } 
}

However, I think the best approach to figuring out what is going on is to make use of the Arduino’s serial monitor. Read the raw sensor value every 100 ms or so and print it to the serial monitor so you can see what is going on in real time as you pass your finger by the sensor. From there you can complicate things by adding in calibration.

- Ben

Hi again… I put in the serial monitor from the arudino to check on real time differences in measurement

#include <PololuQTRSensors.h>

/* EMITTER_PIN = Vin/Digital Pin 2
   SENSOR_PIN = VOUT/Digital Pin 3
   GROUND = GROUND/Arduino[POWER-Ground]
*/

#define EMITTER_PIN 2
#define SENSOR_PIN 3
#define LED 13
PololuQTRSensorsRC qtr((unsigned char[]) {SENSOR_PIN}{EMITTER_PIN}, 1, 1000);

unsigned int sensorValues[1];
unsigned long time;
void setup()
{
pinMode (LED, OUTPUT);
pinMode (SENSOR_PIN, INPUT);

int i;  
  for (i = 0; i < 250; i++)  // make the calibration take about 5 seconds  
  {  
    qtr.calibrate();  
    delay(20);  
  } 
  

Serial.begin(9600);
}

void loop()
{
digitalWrite(EMITTER_PIN,HIGH)
delay(1000);
qtr.readCalibrated(sensorValues);
if (sensorValues[0] < 250)
{

digitalWrite(LED, HIGH);
 //Output to Serial
  Serial.print(sensorValues[0]); 
  Serial.print("   Reflectance ");
  Serial.print("Time Elapsed: ");
  time = (millis()/1000);
  //prints time since program started
  Serial.println(time);
  // wait a second so as not to send massive amounts of data
  delay(1000);
  Serial.println();
} else
{
digitalWrite(EMITTER_PIN,LOW);
delay(1000);
digitalWrite(LED, LOW);
 //Output to Serial
  Serial.print(sensorValues[0]); 
  Serial.print("  No Reflectance ");
  Serial.print("Time Elapsed: ");
  time = (millis()/1000);
  //prints time since program started
  Serial.println(time);
  // wait a second so as not to send massive amounts of data
  delay(1000);
  Serial.println();
}
}

Since you didn’t ask any questions or say happens when you run that code, I’m going to assume you’re not looking for further help and are just sharing something that works for you now. If you do still have problems, however, (a) you should print the raw sensor value, not the calibrated ones, since the calibration phase complicates things and can cause problems if not done right, and (b) you should avoid complicating things with emitter pin control until you have it first working with VIN held constantly high. If something isn’t working, the goal should be to simplify until you get to the simplest thing that exhibits the problem, not make it much more complicated.

- Ben

Yes, the sensor is finally integrated with the Arduino Mega. Thanks, I’m just posting a code 2 contribute for future Pololu customers using Arduino’s. I cleaned up the code. and it works perfectly. At first I thought I burnt the pins when I sodered the header pins. Glad it works awesome So from what I can understand, sensorValues[1] is an array. Can I access a location from the array to turn on the LED. Like [quote] if (sensorValues[20] < 500) [/quote] and if the value stored at that location is less then 500 turn on the LED?


#include <PololuQTRSensors.h>

/* 5V = Vin
SENSOR_PIN = VOUT
GROUND = GROUND-Arduino[POWER]
*/
#define SENSOR_PIN 3
#define LED 13
PololuQTRSensorsRC qtr((unsigned char[]) {SENSOR_PIN}, 1, 2000);

unsigned int sensorValues[1];
unsigned long time;
void setup()
{
pinMode (LED, OUTPUT);
pinMode (SENSOR_PIN, INPUT);

int i; 
for (i = 0; i < 250; i++) // make the calibration take about 5 seconds 
{ 
qtr.calibrate(); 
delay(20); 
} 


Serial.begin(9600);
}

void loop()
{
qtr.read(sensorValues);
if ((sensorValues[0] >= 0) && (sensorValues[0] <= 500))
{     
digitalWrite(LED, HIGH);
//Output to Serial
Serial.print(sensorValues[0]); 
Serial.print(" Reflectance ");
Serial.print("Time Elapsed: ");
time = (millis()/10);
//prints time since program started
Serial.println(time);
// wait a second so as not to send massive amounts of data
delay(10);
Serial.println();
}
else
{
digitalWrite(LED, LOW);
//Output to Serial
Serial.print(sensorValues[0]); 
Serial.print(" No Reflectance ");
Serial.print("Time Elapsed: ");
time = (millis()/10);
//prints time since program started
Serial.println(time);
// wait a second so as not to send massive amounts of data
delay(10);
Serial.println();
}
}

I’m glad to hear it’s working well for you now.

Yes, sensorValues is an array, and its elements are being written by the library functions you are calling to read the sensors. You can access the array elements to obtain the reading of an individual sensor and perform some action based on that reading. However, in your particular case, there is only one sensor, and your sensorValues array only has one element. Trying to access the 21st array element with code like:

if (sensorValues[20] < 500)

is bad as you are exceeding the bounds of your array and reading from some random place in memory. The compiler doesn’t treat this kind of thing as an error, so it’s up to you to make sure you don’t try to access an element outside the bounds of your array. In your particular case, the only valid array index you can use is 0, as in sensorValues[0].

- Ben

After tinkering with this sensor, what i want to do now is count a number of movements? Any ideas on how to go on about this… I was thinking its going to be some sort of loop condition implementing the sensorValues[0].

What you ultimately want to make is a finite state machine. You need to figure out what states you care about and how to transition between them based on the sensor input, and put that all inside a loop that monitors your sensor.

It might be as simple as something like:

loop forever:
while sensor sees nothing, wait
count the motion
while sensor sees something, wait

Though you’re probably going to want to make it a bit fancier. For example, you might want to add hysteresis to avoid accidentally counting one motion as multiple motions, and you might want to add code to perform some sanity checks on the sensor readings to ensure that you’re only counting things you consider to be “motions”. You also you might want your program to be doing other computations rather than just waiting around for the sensor to see something, in which case you could restructure your main loop.

- Ben

Just a point, on that first photograph there is no current limiting resistor on the LED. This risks damaging both the LED and the Arduino. It is a myth that this is an OK thing to do.

Hello.

Thanks for pointing that out. Driving an LED from an I/O line without an appropriate current-limiting resistor is a practice we strongly discourage.

- Ben

Edit: Just to clarify, I’m talking about the red LED plugged directly into the Arduino. The IR LED on the QTR sensor has a built-in current-limiting resistor, so no external resistors are required to use that part.

Hi there,

I don’t want to open another thread because my question is highly related with the topic although not completely.

I would like to know how the RC circuit works, lets say step by step.

The main question is, when you set the digital pin to OUPUT and the value as HIGH, why is the condenser charged?

And when you set the digital as INPUT, why it gets discharged?

Thank you.

Hello.

I think this would have been more appropriate as its own topic.

When you drive the sensor output high, you put both sides of the capacitor at the same voltage and discharge the capacitor, which has the same effect as charging the output node (it is equivalent to charging a capacitor with one side connected to ground). When you stop charging the output node, the capacitor can slowly charge through the phototransistor, with the rate of charging being a function of how much IR is incident on the phototransistor. This has the same effect as discharging the output node (it is equivalent to discharging a capacitor that is connected to ground).

- Ben

Ben,

thanks for the answer.

This part:

was what I missed.

However, where does the energy saved in the capacitor before discharging it go? Is it “absorbed” by the pin?

Thanks