# Frequency Modulation

Hey all, I am attempting to inject a frequency into an arduino, and essentially read the frequency using PulseIn, and then use micros() along with a digitalWrite (or analogWrite) to send the same frequency out on another pin.

Basically, frequency in... frequency out. This would be the start of a frequency modulation project I would like to build on to, but first I need a good foundation. Later on I would like to adjust the frequency and duty cycle via additional inputs. Possibly something as simple as a potentiometer.

The problem is this.. I don't think I have a good understanding of how PulseIn works and in conjunction with digitalWrite.. and would I be better off using analogWrite since in the future I want to adjust the pulse width of the output pin?

This is my current code:

``````int outState = LOW;
long previousMicros = 0;

void setup() {

pinMode(frequencyIN, INPUT);
pinMode(frequencyOUT, OUTPUT);
Serial.begin(9600);
}

void loop()
{

duration = pulseIn(frequencyIN, HIGH);

Serial.println(duration);

unsigned long currentMicros = micros();

if(currentMicros - previousMicros > duration) {

previousMicros = currentMicros;

if (outState == LOW)
outState = HIGH;
else
outState = LOW;

digitalWrite(frequencyOUT, outState);
}
}
``````

There is a big problem though.. as an example I can input a 6 kHz. frequency at 50% duty cycle and I get like 3 Hz. out.

This remains consistent.. If it was 8 kHz. it would be 4 Hz. Out by a multiple of 1000 Hz. and divided by 2.

I am trying to figure out why this is, and this is where I need some help as I am not knowledgeable enough to solve this on my own. Is it because I am only reading the HIGH PulseIn input? Are there some things that could be better about my programming code that would help me with my future endeavor?

Just one thing to point out before you get too disheartened - serial prints at 9600 are slow. A "Serial.println" of a three digit number is going to take at least four milliseconds.

Also, it is always best if you post your whole sketch, not just the bit you think you should post.

Ok, but all I was trying to use the Serial function for was to view a snapshot of what value PulseIn was reading in microseconds.. does this speed directly affect my programs ability to receive the input frequency, read it and output it on another pin?

Also, this is all of the code for this program thus far. As I said at this point I just want a simple frequency in, frequency out program.. A frequency comes in.. the program reads its period length and then using that information outputs an identical frequency on another pin.

Like I said, if you posted all your code, it might be easier to help.

Ugh.. man, do you think I am lying to you in some way?.. that is my code.. you ask as if I should wave a magic wand.. In my code as displayed.. I am simply reading a pin via PulseIn, and then taking that value in microseconds and using it as a comparison to the micros() function to give me a time that is being written to the output pin.. if there is problem with the code.. which I'm sure there is then thats fine.. that is what I am here for.. for advice, but don't expect that I am hiding something because my code looks very simple or looks incomplete..

I may be an idiot (when it comes to programming), but I have nothing to hide.. I don't think I am utilizing the unsigned long "duration" properly to achieve the output I want.. I need insight into how to go about this.. I snatched the idea from the BlinkWithoutDelay example in the arduino tutorials if that helps you see where I am coming from.

Ugh.. man, do you think I am lying to you in some way?.. that is my code..

Let's just say you're being economical with the whole truth. How do you think I [u]know[/u] this?

Your time variables should all be the same type for consistent math operations: unsigned long. Right now you have long, unsigned long, and undeclared type duration.

The 50% is from you only switching the LED when you see High pulses. You could do something like check for a low pulse after seeing a high pulse and switch the LED there, then the frequency would match. Or swithc the LED back after some predetermined amount of time so it is always turned on for the same pulse width, with different duration hi & lo times.

Ok, great.. thanks for the advice on the math.. makes total sense. Since I'm not up on my programming skills.. it is easy to over look things like that.

Yea, in my first post I touched on the second part. I figured that I needed to count the time LOW using PulseIn, but as this is my first time messing with this function.. I just didn't know.

I just figured I would pick the minds of the many while I was at school.. when I get home I'm going to add some code to accommodate this new insight, and I'll post up the results.

Thanks all.