# Frequency Counting on a Raspberry PI

I am trying to count a square wave on a Raspberry PI.

I tried a variety of approaches: http://abyz.co.uk/rpi/pigpio/examples.html (Freq counter 1 and 2) http://www.dnacoil.com/tools/measuring-light-intensity-with-the-tsl-235-and-a-raspberry-pi/#comment-2850

Both approaches seem to top out around a frequency of 300 which is not useful for direct light conditions .(Ok for low light.)

Data sheet of sensor: TSL235

Is there a better method for getting a square wave, 50% duty cycle on the PI

• If you are using Python and if you want anything fast and useful, write your programs in C/C++. You will never go back to Python (unless for fancy slow programs) – KyranF Mar 6 '15 at 16:33
• Never would touch Python for something like this : c all the way!!! – Joe Mar 6 '15 at 17:13
• haha great, well can you explain what a frequency of 300 means? only 300 Hz? – KyranF Mar 6 '15 at 17:20
• ah i see, it counts the pulse trains from that light sensor. Well perhaps 300 is the maximum sensitivity of that sensor? The value shown in the tutorial is only 47.0 "avg value" so surely 300 is a lot? – KyranF Mar 6 '15 at 17:24
• on the ardunion I can get higher frequencies from the spec sheet it should top out near 1MHZ (roughly 793) .. first chart in the datasheet on page 2. But the PI tops at ~ 300 so its only recieving half the light... Figure Direct sunlight is around 1100 W/M^2 ... so its not a 1 to 1 mapping from W/M^2 to Frequency .. – Joe Mar 6 '15 at 21:08

The Pi is not really the best tool for the job. You have an entire operating system vying for processing time, with many processes running at the same time as yours. The only way you would be able to do it reliably would be to directly interface with the hardware and use timers and such to do the job.

Certainly trying to do anything where reading rapidly from a GPIO is going to only work at low speeds, simply because your program isn't running all the time - it's sharing time with everything else the Pi is doing.

You would be better off using a dedicated microcontroller as an interface to the Pi which takes the square wave, calculates the frequency, then reports it over a serial protocol, such as the UART or SPI connections.