Measure nightsky with TSL237

You can count the time length of a number of pulses, the more you count the more the per-pulse average will ... average out.

You can count how many pulses in a measure of time and lose a fraction of a pulse. The longer the time the less the lost fraction means.

pulses/time or time/pulses, both are ratios is all. Get one, you have the other.

GoForSmoke:
You can count the time length of a number of pulses, the more you count the more the per-pulse average will ... average out.

You can count how many pulses in a measure of time and lose a fraction of a pulse. The longer the time the less the lost fraction means.

pulses/time or time/pulses, both are ratios is all. Get one, you have the other.

That sound quite logical even to me :slight_smile: is there any good library/code that i can use for this? I believe that, after counting time, i will be have to translate to time to Hz, in some way, and that Hz is more realible than just counting Hz straight from the sensor?

AHA! Now i understand the differens between pulses and freq, maybe this lib. is more suiteble for me?

http://interface.khm.de/index.php/lab/experiments/frequency-measurement-library/
But he is writning:
The library returns the measured period length in 1/16 us steps as a long integer. To get the frequency, the clock frequency has to divided by the returned value. The clock frequency is set in this example to 16000400 to compensate inaccuracies of the Arduino board.

What is that meaning for me and my arduino?

This might also work?
http://www.pjrc.com/teensy/td_libs_FreqMeasure.html

I wouldn't use a library. I'd save micros() in an unsigned long, count 10,000 pulses and save the micros() right then.
Annnnnd I think you can get micros() in an interrupt though it's stopped. If not then you'll pick up a couple micros setting a flag in the interrupt, leaving the interrupt and catching the flag in loop(). With a tight loop() you'll have micros() very quickly.

Errors --- you can measure Arduino inaccuracy and compensate. Deal with that after you get the rest working. It amounts to changing time microseconds

frequency = pulses / time

If the time is a 62845 microseconds then multiply pulses by 1 million then divide by 62845 to get Hz = cycles(pulses) per second.

10000 pulses / 62845 usecs = 10000 pulses / ( 62845 seconds / 1000000 ) = 10000 pulses x 1000000 / 62845 seconds (doing it with integers and the least loss requires 64-bit long long int) = 159121 Hz.

When you do math for science, keep the units (pulses, seconds, ergs) in the math because at the end if your units don't make sense then your math didn't either. (this is where you remember your science teachers saying the same thing)

Whats wrong with using library? Is the library i have posted of any use?

I am not that experienced so i can code that kind of things :frowning:

Thats why i use librarys and allready written code

@GoForSmoke, 10,000 pulses?!. This would take a whole life if the site is dark. The TSL237 generates generates pulses of frequencies as low as ~0.15Hz in dark skies (even lower if you use filters). That's more than 6 seconds of period, so integrating 10,000 cycles seems a bad idea.

The best approach I think is to measure first one single pulse and decide (at runtime) if you want to count frequencies (or a great amount of pulses to promediate, as GoForSmoke suggested) or you need an alternative way to get the frequency.

Corpze, the SQM is cheap for what it does. If you take into account that those instruments need filters to cut the IR and narrow the spectral sensibility, needs some kind of optics to narrow the FoV, needs a case, an LCD, a board ... you end (in the arduino road) with an instrument that is nearly as expensive as the SQM (or even more!) and probably not as accurate (at least if you don't know what you do/need), trust me. And then, you need to calibrate the device.

If you want to build it just for learning purposes or something else, I think you need to return to the basics and stop trying to get inmediate results. I've been researching on this topic for months and I haven't found any library that just do what you want, sorry.

Just my two cents.

I had all the hardware at home, except for the sensor, UV/IR block filter and lens that on its way (5 USD worth of hardware) It is kind of a learning thingy, maybe it isn't any idéa of keep on going with this :confused:

If you count pulses for 1800 or even 180 seconds and something bright doesn't intrude, you can get less error than if count 1 or 3 seconds. Can get... because there's always a way to can't anything.

If you count pulses and see that a certain time has elapsed then you will be off by the fraction of the last pulse that didn't make it unless you exactly end the time period on a pulse start. So there you have the fraction error but that is divided by the number of pulses when you determine the average length of just 1 pulse (the inverse of frequency) so the more pulses, the better.

To do that, have an interrupt increment the pulse count and in loop() set up a timer the way it's done in BlinkWithoutDelay and when the interval is reached, copy pulse count and use that with the time interval you set up, do the math and you can have frequency. The longer the interval, the closer to precise you can be as long as something doesn't intrude on the patch of sky you're watching.

OTOH you can set up an interrupt to count some number of pulses and see how long that took, maybe completely in the IRQ. Then your loop() code just watches for the IRQ to update the how-long-it-took variable and report that. You will have pulses and time.

Why not a library?
You keep wondering what library to use. You keep wondering if the library you use is the problem. That's because you don't know what they do and it is a problem trying to troubleshoot with such an unknown.

In this case what the library does is not difficult and you will doubtless benefit from learning. Just take your time, identify what it is your don't know and learn those pieces as you put it together.

I understand :slight_smile: i thing i will give it a try, i think i finally know how you mean :slight_smile:

So you have used interrupts and know to make variables used in interrupts as volatile?
And you know that time is kept in unsigned long variables?

How long can you aim at dark sky to get measurements? 3 seconds? 30 s? 300 s? 30 minutes?

Maybe the best way will be to count pulses and see how long that takes.
....
You understand, if I stop when pulse count is done I can know how many whole usecs (microseconds) it takes but I can't know the fraction of a usec after the last full usec. If it was 400001.7 usecs, I will miss the .7. That is an unavoidable (and small) source of error.
If I stop on a time count while also counting pulses then I can know the usecs and how many pulses but not the fraction. If 100 seconds takes 50.8 pulses, my 100 second read only sees 50.
....
Since pulses are so big compared to usecs there will be more possible error counting over a set time than counting a set number of pulses and seeing how long that took. I would rather have a fraction of a usec (really, Arduino is good to 4 usecs which is still very small) as error than a fraction of a much bigger pulse as error.
What decides the method for me is the way with the smaller error. In this case, it's pretty clear?

So big open to the forum just to be sure question --- can you read micros() in an interrupt?

GoForSmoke:
So you have used interrupts and know to make variables used in interrupts as volatile?
And you know that time is kept in unsigned long variables?

How long can you aim at dark sky to get measurements? 3 seconds? 30 s? 300 s? 30 minutes?

Maybe the best way will be to count pulses and see how long that takes.
....
You understand, if I stop when pulse count is done I can know how many whole usecs (microseconds) it takes but I can't know the fraction of a usec after the last full usec. If it was 400001.7 usecs, I will miss the .7. That is an unavoidable (and small) source of error.
If I stop on a time count while also counting pulses then I can know the usecs and how many pulses but not the fraction. If 100 seconds takes 50.8 pulses, my 100 second read only sees 50.
....
Since pulses are so big compared to usecs there will be more possible error counting over a set time than counting a set number of pulses and seeing how long that took. I would rather have a fraction of a usec (really, Arduino is good to 4 usecs which is still very small) as error than a fraction of a much bigger pulse as error.
What decides the method for me is the way with the smaller error. In this case, it's pretty clear?

So big open to the forum just to be sure question --- can you read micros() in an interrupt?

No i haven´t really used interrupts more then those that allready have been written in existing code... I have read the reference about interrupts and there are two kinds? internal and external? the external ones are located on pin2 and 3 on the uno. Is one of those inputs usable for me?

I think 10sec will be the highest time i want to measure (the original SQM-meter measures on 2sec)

So 10000ms of measurements is a number i can start with.

I understand that i will lose the .x count of the pulses if the time runs out :slight_smile:

the .x part error must be seen through "logarithmic glasses"

log(10) = 1
log(11) = 1.041392685
so if the value is around 10 and the math does truncating, the max error is 4% log(10.999999) - log(10)
if the math does rounding, the max error is < 2.2% log(10.499999) - log(10)

log(100) = 2
log(101) = 2.004321374
=> error is max 0.43% ( trunc => 0.22%)

log(1000) = 3
log(1001) = 3.000434077
=> 0.05%

at 10000 it will be max ~0.005%

Corpze:
I understand that i will lose the .x count of the pulses if the time runs out :slight_smile:

If you count a certain number of pulses and then see the time, it will be the fraction of a usec that you miss, not a fraction of the much-longer pulse. So in the interrupt routine, we count pulses and get whole pulses that way (detail: have to get start time on a pulse, then count pulses and on the last get end time) and not lose the bigger (pulse is way longer than usec) fraction.

GoForSmoke:

Corpze:
I understand that i will lose the .x count of the pulses if the time runs out :slight_smile:

If you count a certain number of pulses and then see the time, it will be the fraction of a usec that you miss, not a fraction of the much-longer pulse. So in the interrupt routine, we count pulses and get whole pulses that way (detail: have to get start time on a pulse, then count pulses and on the last get end time) and not lose the bigger (pulse is way longer than usec) fraction.

So, to measure the time between a certain no of pulses, can i use the PulseIn? PulseIn uses just High or Low on a digital pin, I really want to use Rising or something like that?

NO pulse in measures one pulse.

with an IRQ based counter you can count X pulses in e.g. 10.000.000 microseconds and use that

something like this,

void setup()
{
  Serial.begin(38400);
  attachInterrupt(0, ISR, RISING);
}
void loop()
{
  unsigned long start = micros();
  interrupts();
  while (micros() - start < 10000000UL); // wait 10 seconds 
  noInterrupts();
  
  Serial.println(count);
  count = 0;
}

void ISR()
{ 
  count++;
}

Corpze:
No i haven´t really used interrupts more then those that allready have been written in existing code... I have read the reference about interrupts and there are two kinds? internal and external? the external ones are located on pin2 and 3 on the uno. Is one of those inputs usable for me?

Okay. You will be using external interrupt on pin 2 or 3 since you want to detect the starting edge of the pulse.

I think 10sec will be the highest time i want to measure (the original SQM-meter measures on 2sec)

So 10000ms of measurements is a number i can start with.

Just as long as you have a time to aim for. What is reasonable, the data will show like if 1 second consistently gets very close to 10 seconds then 1 second will be good. And if less then that will be good. Of course if 10 seconds does not give consistent values then there is a problem.

Ok, the bits and pieces are starting to fall in place now :slight_smile:

I will look in to the example sketch and try that when i have the time to do that, after a given no time (10 sec) i will be able to read the count of pulses in the serial monitor, how will i translate that in to Hz? or am i stepping ahead to fast now?

if you have x pulse in 10 seconds that makes x/10 Hz

Ok, thanks, i will give this a try!

update :slight_smile:

void loop()
{
  unsigned long start = micros();
  interrupts();
  while (micros() - start < 10000000UL); // wait 10 seconds 
  noInterrupts();
  
  Serial.print(count/10);
  Serial.print(".");
  Serial.print(count%10);
  Serial.println("Hz");
  count = 0;
}