# Generating Square Wave with with modulated carrier frequency

Dear All
I am trying to generate square wave of 18 KHZ with 50% duty cycle by Arduino UNO. There is Potentiometer on Pin A0 which i have to use to mix the carrier on the trailing edge of the 18KHZ waves.
The Board that is working in my company is imported.They have used ATMEGA 8 on the board .I have traced their board waveforms on 20 MHZ oscilloscope.That are attached here .It is recorded at differant POT position at A0 on their board
How can I generate the same waveform using Arduino UNO.
And another point is that I am unable to see the carrier waveforms may be due to its frequency greater than 2 MHZ

18 KHZ 50% Duty Pot at 50% .pdf (623 KB)

18 KHZ 50% Duty Pot at75% .pdf (602 KB)

18 KHZ 50% Duty Pot at 90% .pdf (610 KB)

I am not sure why you have posted images (as I gather they are) in "pdf" format. It is rather inconvenient to view them as it tends to require software other than the browser. Pdf is generally used for printable documentation, which this does not appear to be, or obfuscation, which annoys many here.

A .jpg or .png makes it much easier to view - and answer accordingly.

You really should explain the curious use of "square wave" and "carrier" in the same description.

Try this:
1/18000 = 55.56uS, 1/2 of that is 27.78uS Can use blink without delay, with reading from the pot used to adjust the times.
Not sure how close to 18KHz you can get.

Use a button to determine if a pot reading should be taken; D2 Use writes to PIN register to toggle the output; D3

So something like this:

``````// variable declarations
unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedTime;
unsigned long 5uS = 55UL;
int analogVal;
// etc.

void setup(){
pinMode (2, INPUT_PULLUP);
pinMode (3, OUTPUT);
digitalWrite (3, LOW); // just in case
}
void loop(){
if ((PIND & 0b00000100) == 0){ // is button on D2 pressed (to GND)
analogVal = analogRead(A0)>>5; // result is 0 to 31
}
currentTime = micros();  // all time elements are unsigned long
elapsedTime = currentTime - previousTime;
if (elapsedTime >=55uS){  // 55uS defined as 55UL
previousMicros = previousMicros + 55uS; // set next rising edge
PIND = 0b00001000; // toggle D3

while (micros() <= ((previousMicros + 28) + analogVal)){
// hang out until half period of 18 KHz plus pot delay
}
// end waiting for half period
PIND = 0b00001000; // toggle D3, falling edge
} // end time period check
} // end loop
``````

tone()

Description

Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a piezo buzzer or other speaker to play tones. Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency. Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega). It is not possible to generate tones lower than 31Hz.

http://arduino.cc/en/reference/tone

toneAC()

Replacement to the standard Arduino tone library with twice the volume, higher quality and higher frequency.

Purpose The library is named toneAC because it produces an alternating current (AC) between two pins. The ATmega's PWM takes care of this so the accuracy is exact. When you send a tone to a speaker with the standard tone library, the loudest is at 50% duty cycle (only on half the time). Which at 5 volts, is like sending only 2.5v to the speaker. With toneAC, we're sending out of phase signals on two pins. So in effect, the speaker is getting 5 volts instead of 2.5, making it nearly twice as loud. The sound quality difference has to do with allowing the Arduino's PWM to take care of everything and careful programming. Longer piezo life happens because instead of driving the transducer disc only ever in one direction (deforming the disc and reducing sound and quality), it drives it in both directions keeping the disc uniform.

Features Nearly twice the volume (because it uses two out of phase pins in push/pull fashion) Higher quality (less clicking) Capability of producing higher frequencies (even if running at a lower clock speed) Nearly 1.5k smaller compiled code Bug fixes (standard tone library can generate some odd and unpredictable results) Can set not only the frequency but also the sound volume Less stress on the speaker so it will last longer and sound better Disadvantages are that it must use certain pins and it uses two pins instead of one. But, if you're flexible with your pin choices, this is a great upgrade. It also uses timer 1 instead of timer 2, which may free up a conflict you have with the tone library. It exclusively uses port registers for the fastest and smallest code possible.

http://playground.arduino.cc/Code/ToneAC

Is that scope not locked on? What am I looking at? I agree about PDF vs jpg or png. Why sideways?

tone() does change the duty cycle tho, it is only 50%. Need custom code to change duty cycle based on a reading from analog input.

Sorry, that was supposed to say tone does NOT change the duty cycle. Can't seem to modify posts today, forum locks up on me (IE 8.0, grrrr).

CrossRoads: Sorry, that was supposed to say tone does NOT change the duty cycle. Can't seem to modify posts today, forum locks up on me

I wouldn't be blaming Windoze/ IE. Not sure why anyone in the engineering business would use IE however ...

If it is giving you the"Broken" page, just keep on using "Reload" and "Resend" the form information.

No, our settings at work keep things from working. If I try and open 2 forum pages for example, the first stops working, I have to close IE and start all over. Can’t even open a 2nd page, do a search for a topic, and paste it into the 1st.
As to why IE, we’re the equivalent of an international conglomerate, and corporate security is way high on the list, so things are slow to change. Certainly won’t change to satisfy a little forum run from outside the US.

CrossRoads: we're the equivalent of an international conglomerate, and corporate security is way high on the list, so things are slow to change.

Which means that security probably isn't very good I suspect! :astonished:

Sorry for late The .PDF file is only attached due to Size limitation. The purpose of this wave form is to Drive Feed forword Half Bridge which is fed the power to a Normal EI core inductor which is used to generate the Smooth Mechanical Vibration. We have tried it by feeding 18 to 20 KHZ square wave but vibration result is not so smooth as compare to the imported controller.Whose waveform i have attached on previous description. I am attaching again in .jpg

Sorry for late The .PDF file is only attached due to Size limitation. The purpose of this wave form is to Drive Feed forword Half Bridge which is fed the power to a Normal EI core inductor which is used to generate the Smooth Mechanical Vibration. We have tried it by feeding 18 to 20 KHZ square wave but vibration result is not so smooth as compare to the imported controller.Whose waveform i have attached on previous description. I am attaching again in .jpg [D:\Office\COMM\PSA_1314\Sales\Catlogs_Manuals\Vibrator\Traced Pulses[/img]

We can’t files that are a path on your hard drive. Try Additional Options and Attach the file again.
What happens when you try my sketch?
I made a few small changes so it compiles:

``````// variable declarations
unsigned long currentTime;
unsigned long previousTime;
unsigned long elapsedTime;
unsigned long duration = 55UL;
int analogVal;
// etc.

void setup(){
pinMode (2, INPUT_PULLUP);
pinMode (3, OUTPUT);
digitalWrite (3, LOW); // just in case
}
void loop(){
if ((PIND & 0b00000100) == 0){ // is button on D2 pressed (to GND)
analogVal = analogRead(A0)>>5; // result is 0 to 31
}
currentTime= micros();  // all time elements are unsigned long
elapsedTime = currentTime - previousTime;
if (elapsedTime >=duration){  // 55uS defined as 55UL
previousTime = previousTime + duration; // set next rising edge
PIND = 0b00001000; // toggle D3

while (micros() <= ((previousTime + 28) + analogVal)){
// hang out until half period of 18 KHz plus pot delay
}
// end waiting for half period
PIND = 0b00001000; // toggle D3, falling edge
} // end time period check
} // end loop
``````

I have also modified your sketch as you modified and take trial.But I found that There is Only Change in Duty cycle of the waveform while POT rotation. Please take a Look of Orignal wave form that seems somethink diffrent.
Please suggest the sketch for the same.

Thanks

nehe_bhimaji:
I have also modified your sketch as you modified and take trial.But I found that There is Only Change in Duty cycle of the waveform while POT rotation. Please take a Look of Orignal wave form that seems somethink diffrent.
Please suggest the sketch for the same.

Thanks
[Another image of Wave forms at Pot Position around 75%]

Looks like it's working to me. Frequency is not changing, and width of the positive portion is varying.

Dear Sir
I Have tried your modified sketch result is like that

1. When Switch D2 Pressed I get Square wave roughly 18 KHZ but nothing change while POT varies from 0 to 100%
its image is attached
2.When Switch D2 Open I get some Notches in wave its image is attached

I have also check the Pot input at A0 input it seems from 0 to 5V varying while POT turns from 0 to 100%
Please suggest necessary changes in sketch.If possible explain sketch in details so that I will think on it
Thanks

nehe_bhimaji: Dear Sir I Have tried your modified sketch result is like that 1. When Switch D2 Pressed I get Square wave roughly 18 KHZ but nothing change while POT varies from 0 to 100% its image is attached 2.When Switch D2 Open I get some Notches in wave its image is attached

I have also check the Pot input at A0 input it seems from 0 to 5V varying while POT turns from 0 to 100% Please suggest necessary changes in sketch.If possible explain sketch in details so that I will think on it Thanks

Please go through following you tube link .There is Video of waveforms that I expect from Arduino UNO http://youtu.be/fcaQiYgS3Ts Thanks

I can see space-time in your images. 3200x2400....

Dear sir Have you check my you tube video link Please check and suggest some sketch how I can generate the same Thanks