Need Help - How to Print Square Wave Frequency From Light-to-Frequency Sensor?

Hi all. Another beginner question here.

Before I lay out my question in detail, I wanted to point out that I attached a high-resolution image collage of the work I've done so far. You can download it below and zoom around to see detailed photos. It shows that I have wired the sensor correctly according to the datasheet schematic, and that I'm getting the results I expected on the oscilloscope (without LED vs. with LED).


Anyways, here goes my question

I am attempting to read the frequency data from an IR light sensor with my Arduino and print it to the serial monitor. The sensor in question is the TSL245 from Taos. It has a peak sensitivity of about 940 nm. It outputs a square wave with a frequency that is proportionate to the amount of light it receives.

For convenience, HERE IS THE DIGIKEY PAGE, and HERE IS THE DATASHEET.

THIS ARTICLE says that there are two ways to read frequency data from a sensor with the Arduino. I have pasted text from that article below:

Method 1 - Using FreqCounter library. It can measure frequencies up to several MHz, and is very precise. However, measurement pin is fixed to digital pin 5. Also it may affect Arduino PWM outputs, increasing their duty cycles. In case you don't use PWM and you need to measure frequency of just one signal, FreqCounter is an excellent choice

Method 2 - Using pulseIn() function from standard Arduino libraries. The technique I'm suggesting below is free from FreqCounter limitations, so you can use it on any pin. However, it's minimal wave period is 10 uS, with corresponding maximum measurable frequency of 50 kHz

In the image collage below, you'll see that when the sensor is fully saturated with the IR LED's light, the scope displays a frequency of 10 kHz. For this reason, I believe Method 2 would be fine. (Where 50 kHz is the upper limit) This would free me from the need to use pin 5 like in Method 1. Also, I don't believe I'd have to build a preamp like in Method 1.So Method 2 (pulseIn function) it is.

Below, I modified the code for the simple PulseIn function to include pin 9, which you'll see in the image below is the input pin I've wired to my Arduino.

https://www.arduino.cc/en/Reference/PulseIn

int pin = 9;
unsigned long duration;

void setup()
{
  pinMode(pin, INPUT);
}

void loop()
{
  duration = pulseIn(pin, HIGH);
}

Now I'm wondering where I go next? I have used the serial print function for AnalogRead values. This thread had a similar subject but never resolved the issue.

I am thinking this is what I should add next but I know it's incorrect. It doesn't compile and I'm not sure I even have the right idea

Can anyone lend a hand? Much appreciated

int pin = 9;

unsigned long duration;



void setup()

{
 Serial.begin (9600);
  pinMode(pin, INPUT);

}



void loop()

{

  duration = pulseIn(9, HIGH);
  value = pulseIn(pin 9, HIGH);
    Serial.print(value);
    Serial.println(": value.");
}

Hi all. Another beginner question here. (Also posted this in the sensor forum, but there is overlap here since it's a programming question)

Before I lay out my question in detail, I wanted to point out that I attached a high-resolution image collage of the work I've done so far. You can download it below and zoom around to see detailed photos. It shows that I have wired the sensor correctly according to the datasheet schematic, and that I'm getting the results I expected on the oscilloscope (without LED vs. with LED).


Anyways, here goes my question

I am attempting to read the frequency data from an IR light sensor with my Arduino and print it to the serial monitor. The sensor in question is the TSL245 from Taos. It has a peak sensitivity of about 940 nm. It outputs a square wave with a frequency that is proportionate to the amount of light it receives.

For convenience, HERE IS THE DIGIKEY PAGE, and HERE IS THE DATASHEET.

THIS ARTICLE says that there are two ways to read frequency data from a sensor with the Arduino. I have pasted text from that article below:

Method 1 - Using FreqCounter library. It can measure frequencies up to several MHz, and is very precise. However, measurement pin is fixed to digital pin 5. Also it may affect Arduino PWM outputs, increasing their duty cycles. In case you don't use PWM and you need to measure frequency of just one signal, FreqCounter is an excellent choice

Method 2 - Using pulseIn() function from standard Arduino libraries. The technique I'm suggesting below is free from FreqCounter limitations, so you can use it on any pin. However, it's minimal wave period is 10 uS, with corresponding maximum measurable frequency of 50 kHz

In the image collage below, you'll see that when the sensor is fully saturated with the IR LED's light, the scope displays a frequency of 10 kHz. For this reason, I believe Method 2 would be fine. (Where 50 kHz is the upper limit) This would free me from the need to use pin 5 like in Method 1. Also, I don't believe I'd have to build a preamp like in Method 1.So Method 2 (pulseIn function) it is.

Below, I modified the code for the simple PulseIn function to include pin 9, which you'll see in the image below is the input pin I've wired to my Arduino.

https://www.arduino.cc/en/Reference/PulseIn

int pin = 9;
unsigned long duration;

void setup()
{
  pinMode(pin, INPUT);
}

void loop()
{
  duration = pulseIn(pin, HIGH);
}

Now I'm wondering where I go next? I have used the serial print function for AnalogRead values. This thread had a similar subject but never resolved the issue.

I am thinking this is what I should add next but I know it's incorrect. It doesn't compile and I'm not sure I even have the right idea

Can anyone lend a hand? Much appreciated

int pin = 9;

unsigned long duration;



void setup()

{
 Serial.begin (9600);
  pinMode(pin, INPUT);

}



void loop()

{

  duration = pulseIn(9, HIGH);
  value = pulseIn(pin 9, HIGH);
    Serial.print(value);
    Serial.println(": value.");
}

This would free me from the need to use pin 5 like in Method 1. Also, I don't believe I'd have to build a preamp like in Method 1.So Method 2 (pulseIn function) it is.

I'd recommend using Method 1 the Frequency Counter Library (or possibly Frequency Measure).

There is no need for a pre-amp. Your sensor has a 5v output.

What did you have in mind for pin 5 that puts it off limits?

I believe that Frequency Measure will have more precision than pulseIn and has the built in averaging of reading many pulses over a time period. The article you site averages 1000 pulseIn measurement to get an average. You might as well just count the number of pulses occuring over a time period.

The specs indicate that you should observe a far higher pulse frequency than 10Khz. and you shouldn't start with any limits to your measurements due to pulseIn. You will not find that implementing a count will be more difficult than using pulseIn.

Frequency is calculated by counting the number of pulses received in one second.
I would do this using a external interrupt, say on pin 2, on each rising edge.
In the interrupt service routine, update a counter each time it is called.
In the loop, every second, print the value of the counter and reset it.
Of course, you can use another interval than one second, but then adjust the pulse count accordingly to obtain the frequency.

pulseIn() is intended for measuring the duration of a pulse, and this is dependent on the duty cycle of the sensor output.

Do not cross-post. Threads merged.