0

I have a silly question.
Today I saw a function for arduino, that is pulseIn(). So to test what it does I applied a rectangular wave at a pin same as the arduino reference page did and it worked fine. There was a line in the reference page which stated :-
Works on pulses from 10 microseconds to 3 minutes in length.
So as it seems it will work upto 100KHz signal. Is there any way to read signals of much higher frequency than this? like in the range of MHz or so?

1
  • It depends how you define "read"... Google "Arduino Frequency Counting".
    – Majenko
    Jul 2, 2018 at 17:41

2 Answers 2

1

Reading high frequency signals can be done. It's possible to read signals up to about 6MHz or maybe even higher (depending on the system clock frequency).

That is, as long as all you want to know is the fundamental frequency of the signal.

Pin 5 of the Arduino Uno (PD5) is connected to Timer 1 and can be set to be the clock source for that timer. By connecting that to your signal and allowing the timer to count up from zero using that signal as the clock source, and then waiting for a pre-defined time period, you can easily find how many clock pulses have arrived from your signal in that time. From that, you can calculate the frequency of the signal.

The Timer/Counter can be clocked by an internal or an external clock source. The clock source is selected by the Clock Select logic which is controlled by the Clock Select bits in the Timer/Counter control Register B (TCCR1B.CS[2:0]).

Since all you are working with us the number of pulses over a given period you lose any information such as the duty cycle of the signal.

You should read the datasheet section 20: "TC1 - 16-bit Timer/Counter1 with PWM" for more detail about how you can use the timer to count the frequency of a signal.

2
  • 2
    TC1 is a synchronous timer. Its T1 input is synchronized to the system clock before going to the edge detector. This means that each signal level must last more than one clock cycle in order to be reliably sampled, which puts an upper bound on the input frequency at F_CPU/2, assuming a 50% duty cycle. Jul 3, 2018 at 8:18
  • 1
    @EdgarBonet indeed. Recommended is system clock / 2.5, so 6.4MHz on a 16MHz board.
    – Majenko
    Jul 3, 2018 at 8:33
0

It depends on the processor. Lets assume Uno/Atmega328p at 16MHz. You might get an order of magnitude faster by writing a pulsein() function in assembler. Finding out sounds like a fun project!

To go much beyond that, you'll probably need hardware support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.