How to get the frequency of a square wave in a python script

12,382

Use the time module. It has a clock function that is sensitive to 1 microsecond (1 MHz). Looking at your sensor's datasheet, it only goes up to 500 kHz, that should be sufficient resolution to get accurate frequency measurements at high light intensity.

Just calculate an average frequency of a set of input cycles.

import time

NUM_CYCLES = 10
start = time.time()
for impulse_count in range(NUM_CYCLES):
    GPIO.wait_for_edge(25, GPIO.FALLING)
duration = time.time() - start      #seconds to run for loop
frequency = NUM_CYCLES / duration   #in Hz
Share:
12,382
user3142695
Author by

user3142695

People who say nothing is impossible should try gargling with their mouths closed.

Updated on June 04, 2022

Comments

  • user3142695
    user3142695 almost 2 years

    I'm using the TSL235 (http://www.ti.com/lit/ds/symlink/tsl235.pdf) light-to-frequency converter and the Raspberry Pi. The output of the sensor is a square wave (50% duty cycle) with frequency directly proportional to light intensity.

    So I need to know (in a python script) which frequency gets to the Input GPIO-Pin of the Raspberry Pi.

    I only found a tutorial (http://playground.arduino.cc/Main/TSL235R) which shows a C-code, but I do not understand C... I'm only working with python

    Reading a GPIO Input isn't that hard so far:

    #!/usr/bin/python
    import RPi.GPIO as GPIO
    
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(25, GPIO.IN)
    
    impuls_count = 0
    # Do next lines for i.e. 1000ms:
    GPIO.wait_for_edge(25, GPIO.FALLING)
    impuls_count = impuls_count + 1
    

    I think I have to count the signals in a time intervall. But how?

    • smci
      smci about 10 years
      Well what does the documentation say? (link?) This will be heavily downvoted/flamed/closed as a 'Give me teh codez'-type question. Or if it's about specifics of interfacing with the hardware, it's offtopic on SO and should be migrated. Can you at least show us your attempt at code to read a signal from the Input GPIO-pin? Link to any relevant doc or tutorials.
    • user3142695
      user3142695 about 10 years
      No I don't want the complete code. But I do not know how to get a frequency "technically" in a python script.
    • smci
      smci about 10 years
      Then either a) show us your attempt at code to read a signal from the Input GPIO-pin b) Link to any relevant doc or tutorials
    • smci
      smci about 10 years
    • user3142695
      user3142695 about 10 years
      I updated the post with my simple code for a GPIO Input and a link to the only tutorial I found.
  • user3142695
    user3142695 about 10 years
    Great! Does it make a difference using FALLING or RISING? And do I have to set 'pull_up_down=GPIO.PUD_UP'?
  • mtadd
    mtadd about 10 years
    Which edge shouldn't matter, but you should try each to see if you prefer the result of one to the other. As per the schematic on page 5 of the sensor data sheet, it looks like the default value of pull_up_down=GPIO.PUD_OFF should be fine, as the sensor is designed to interface directly with a microcontroller I/O pin.
  • user3142695
    user3142695 about 10 years
    Thank you very much for your great help!
  • Micha93
    Micha93 almost 4 years
    for what does impulse_count stand? I think it is not needed.