# Detecting a change from LOW to HIGH on a pin

I was wondering if anyone knew a way that I could detect a riding edge on a square wave that is attached to a pin on the Arduino. I will be attaching a 10MHz square wave to one of the pins and want to be able to know when that square wave goes from LOW to HIGH.

When it does go from low to high, I want to be able to tell it to do some function, such as counting the number of 10MHz square wave pulses it has received (ie start a counter), and have the arduino put out HIGH to another pin after X amount of pulses.

I am trying to artificially make this 10MHz signal the new “clock” for the arduino, I suppose.

That would sound like a good candidate for using one of the 'user interrupt' pins on the Arduino. Pins 2 and 3 ( one a 328p based board) are capable of sensing either a raising or falling or change of state signal and jumping to a user created interrupt function that can then perform whatever code you desire and when complete jumps back to where it was before the interrupt.

http://arduino.cc/en/Reference/AttachInterrupt

Lefty

then perform whatever code you desire

Within limits, of course. ISRs need to be fast. Things like Serial.print() are not. Obviously, pulseIn() and delay() are not things that belong in an ISR, either.

you will have fun counting 10 MHz pulses on a micro with a clock speed of 16 MHz!! do let us know how you get on!

@csnsc14320 - you do know fun translates to impossible don't you?

Yea, as a 10Mhz clock would have a new raising edge every 100 nano second it's really not possible for the interrupt system to honor every raising edge based on a 16Mhz clock speed.

Lefty

retrolefty:
Yea, as a 10Mhz clock would have a new raising edge every 100 nano second it’s really not possible for the interrupt system to honor every raising edge based on a 16Mhz clock speed.

Lefty

Why is that exactly? What clock rates would I be able to detect with Arduino 16MHz? How fast must the Arduino clock be to be able to count my 10 MHz?

There is less than time for two machine code instructions in 10MHz on a 16MHz system. I would say you have to run a system at a clock at least 10 time what you want to detect. That will give you 10 machine code instructions between edges, which is still not very much. You can't do this on an arduino.

The Arduino executes one instruction every clock most of the time if's and other cases take 2 to 3 clocks, so at 10Mhz you can even not have time to execute one instruction, one way is to use a micro-controller running at more than 100Mhz, the other is to use an external 74 series counter, a base 10 seems perfect it will divide your signal be a factor of 10 and now your max frequency is 100Khz, probably still to high, so use two in a chain and the max signal is now 10Khz, much easier to work with.

When it does go from low to high, I want to be able to tell it to do some function, such as counting the number of 10MHz square wave pulses it has received (ie start a counter), and have the arduino put out HIGH to another pin after X amount of pulses.

With the processor running at 16 MHz, the highest frequency that can reliably be processed is 8 MHz (Nyquist was a smart dude).

To count the pulses you will have to configure one of the timers to use an external clock source. Assuming I follow the datasheet correctly, you can use timer 0 or timer 1.

The timer can be configure to set one of its output pins high on an output compare.

Drop the frequency to 8 MHz and spend some time with the datasheet and you'll have a solution.

[quote author=Coding Badly link=topic=64678.msg472020#msg472020 date=1308765281]

When it does go from low to high, I want to be able to tell it to do some function, such as counting the number of 10MHz square wave pulses it has received (ie start a counter), and have the arduino put out HIGH to another pin after X amount of pulses.

With the processor running at 16 MHz, the highest frequency that can reliably be processed is 8 MHz (Nyquist was a smart dude).

To count the pulses you will have to configure one of the timers to use an external clock source. Assuming I follow the datasheet correctly, you can use timer 0 or timer 1.

The timer can be configure to set one of its output pins high on an output compare.

Drop the frequency to 8 MHz and spend some time with the datasheet and you'll have a solution.

[/quote]

The 10MHz signal I was inputting to the Arduino is from a clock generator, so I could try using that.

OK, so here is what I am ultimately trying to do. I have a 1 Hz square wave that I will be feeding into the arduino. Every time that it rises, I want to set another pin HIGH after a certain amount of microseconds. I need microsecond resolution so that I can specify to delay the output anywhere from say 1 microsecond up to 1 second. In essence, I am trying to delay the square wave with microsecond precision (hence the 10MHz clock).

The output pin that I am delaying will be used to power a small external LED that will represent the delayed square wave.

The 10MHz signal I was inputting to the Arduino is from a clock generator, so I could try using that.

Feeling smarter than Nyquist, eh? Let us know how that works out for you.

OK, so here is what I am ultimately trying to do...

In other words, I wasted about 15 minutes of my life to help you solve the wrong problem because you failed to include those details in your original post.

OK, so here is what I am ultimately trying to do.

Sadly this waste lot of time. You pose an a most impossible question and then we find out your thinking is way off beam in the first place.

I would do this in hardware, have a synchronous counter feeding into one side of a comparator. Feed the other side of the comparator from the arduino through some shift registers. Put the equals output to a flip flop set and clear it with the edge of your clock.

That is if there is nothing else you are holding out from us.

[quote author=Coding Badly link=topic=64678.msg472076#msg472076 date=1308770461]

Feeling smarter than Nyquist, eh? Let us know how that works out for you. [/quote]

I meant 8MHz, not 10.

In other words, I wasted about 15 minutes of my life to help you solve the wrong problem because you failed to include those details in your original post.

Grumpy_Mike:

OK, so here is what I am ultimately trying to do.

Sadly this waste lot of time. You pose an a most impossible question and then we find out your thinking is way off beam in the first place.

I am sorry for that - I was trying to figure this out in steps instead of trying to do the whole thing at once. I originally thought I needed to detect the 10MHz signal if I wanted to use this as the new "clock," but I seem to have been mistaken on what I actually needed to do :(

I would do this in hardware, have a synchronous counter feeding into one side of a comparator. Feed the other side of the comparator from the arduino through some shift registers. Put the equals output to a flip flop set and clear it with the edge of your clock. That is if there is nothing else you are holding out from us.

I will look into this and give it a try. I don't believe there is much more to know. I am delaying a 1Hz square wave with microsecond precision, the delayed signal will flash a LED, and a photo detector will detect these LED pulses.

I am sorry for any time I may have wasted and I fully appreciate the depth of these responses. I am new here and new to programming with arduinos so hopefully my future posts can be more concise and accurate.

[quoteI don't believe there is much more to know.][/quote] Only why and what you hope to achieve by this. When you say delaying is that a fixed delay which in effect is a phase shift. Or is it a micro second extension of the period.

Grumpy_Mike: When you say delaying is that a fixed delay which in effect is a phase shift. Or is it a micro second extension of the period.

I believe phase shift. We have a 1Hz TTL signal that we want to use to calibrate a detector that can detect photons with microsecond precision. In order to calibrate, we want to be able to delay or phase shift the TTL signal from 1 microsecond to 1 second and synch it up with the detector. So we just need an accurate shifting of the 1 second TTL to compare with to synch it up.

In that case, I would use a presetable counter that normally runs from the same master clock source. Then when you want to change the phase, output the required number to the counter's preset lines and when the reference 1HZ counter makes a transition arrange the preset value is clocked in. I would probably look into using an RS flip flip to handle the request / acknowledge part of this. It is old school TTL logic but it is your best bet for getting the precision you need.

Photons can go a long way in 1uS.

Grumpy_Mike: In that case, I would use a presetable counter that normally runs from the same master clock source. Then when you want to change the phase, output the required number to the counter's preset lines and when the reference 1HZ counter makes a transition arrange the preset value is clocked in. I would probably look into using an RS flip flip to handle the request / acknowledge part of this. It is old school TTL logic but it is your best bet for getting the precision you need.

Photons can go a long way in 1uS.

Using this method do I still need the comparator, or the arduino even? Or would I be using only the clock generator, the TTL signal, flip flops, and counters?

do I still need the comparator

No

or the arduino even?

Well you have to find a way of inputting the preset numbers. You could do this through a set of BCD switches if you make your clock chain count in decades. Otherwise you can dispense with it and just have a TTL generator.