I need to generate a 50 percent duty cycle square wave. I was thinking to use simple code like the one below:
void setup()
{
pinMode(8, OUTPUT);
}
void loop()
{
// A total delay of 4 ms = 1/0.004 = 250 Hz…
digitalWrite(8, HIGH);
delay(2);
digitalWrite(8, LOW);
delay(2);
}
I am getting the 50 percent duty cycle with the code above, but not the higher frequency or different volts. I need the output to be 11 MHZ at first then after about a couple thousand squares are generated switch to 12 mhz. The count could be achieved by using a while (count) statement. However, how would I raise the frequency.
Could I attach output pin 8 to a crystal and pull up resistor to achieve 11 and 12 MHz? If so which resistor and Crystal should I use? As a side note, how would I change the output to -1 to 3.3v?
jjrr007:
I need the output to be 11 MHZ at first then after about a couple thousand squares are generated switch to 12 mhz.
With a 16MHz processor frequency, your chances of doing that in software are basically nil, and the frequencies are so close to the clock frequency that I don't see how you'd achieve it in hardware, either. I suspect in this case you'd be better off using an external oscillator with the required frequency and duty cycle - you might use the Arduino to control the output frequency or switch between two different oscillators.
In addition to what jjrr007 wrote: you can't get an output of -1V.
The Arduino board doesn't have a negative voltage on the board.
You need to generate a negative voltage, perhaps with a DC-DC converter, and use that with an OpAmp to map the output between -1 and 3V.
If the -1V requires little current, you could use an Arduino output, a 120 ohm protection resistor, a capacitor and two diodes and a capacitor again to generate a negative voltage with those 5 components.
I think they are called a charge pump. http://cladlab.com/electronics/components/capacitors#charge-pumps
Thanks for the replies. I will try the charge pump- sounds interesting.
About the frequencies, are we saying that it will not work using an external crystal or resonator? I apologize if I wasn't clear. I was just thinking it would be simpler to attach the pin to an external crystal (or resonator) and resistor to get the frequency. Would it not work because of the duty cycle or something else? If it would work, what resistor and crystal would I use?
That is a cool chip, it is not expensive and 0.5% accuracy.
What is the problem with it ?
Do you already use one ? There are ways to combine more I2C devices with the same address (special I2C mux chip, a simple 74HC... mux, chip selects, etc.).
Or are analog pins A4 and A5 for the I2C bus in use ? Perhaps you could use a mux chip for the analog signals.
I have used it and it's nice a chip, but take the accuracy with a "grain of salt".
Say I only want to generate a signal of 8 MHz. From programming this chip, I can only do this by dividing 133 MHz by a number (or numbers) using a bit code from the wire library. So the closet I can get to say 8 MHz is 7.7 Mhz on my oscilloscope. That's far away from what I want, but still accurate from what it's designed to do.
The reason why I can't use I2c for this process is a long explanation (too much for here). Basically, I don't know what address to send the signal to using I2c.
I like the idea of simply using crystals and resistors on one pin and switch the frequency. Is it possible to do this or should I just focus on using a chip oscillator for the alternating frequency square waves?