I recently acquired some Pololu QTR-1RC sensors (digital sensors), and I need to get readings from them. I tried using the Pololu libraries but the pins in the library don’t exactly mesh with my arduino mega pins. So I decided to try the other way:
The typical sequence for reading a sensor is:
Set the I/O line to an output and drive it high
Allow at least 10 us for the 10 nF capacitor to charge
Make the I/O line an input (high impedance)
Measure the time for the capacitor to discharge by waiting for the I/O line to go low
The time it takes for the capacitor to discharge is the value I need to read.
I need help fleshing out how this is going to exactly work with my Arduino Mega…Here’s the code I have so far:
int sensorpin = 37;
int time = 0;
//to hold the time it took for the cap to discharge
void setup() {
Serial.begin(9600);
// For Printing the time it took for the cap to discharge to me
}
void loop() {
pinMode(sensorpin, OUTPUT);
digitalWrite(sensorpin, HIGH);
delayMicroseconds(10);
pinMode(sensorpin, INPUT);
//My question here is that do I need to set the digital I/O pin to LOW even though I switched it to INPUT to get the effect I need?
digitalWrite(sensorpin, LOW);
// I am stuck on how to count the time elapsed of the I/O pin going from HIGH to LOW. I know it will need a digitalRead(); and possibly a millis(); or micros();, though I do not know how to put it together.
(pseudo-code){
while(!digitalRead(sensorpin) == “HIGH”) {
time++;
or
micros(); ?
}
}
Serial.print(time);
}
Thoughts? Sorry if its a bit hard to understand, this is my first post, and I will clarify again if needed tomorrow morning (getting late).