Go Down

Topic: Generating Square Wave with with modulated carrier frequency (Read 3438 times) previous topic - next topic

nehe_bhimaji

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

Please see attached waveform pdf at different Analog input at A0
Thanks in Advance

Paul__B

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.

CrossRoads

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:
Code: [Select]

// 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

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

polymorph

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
Steve Greenfield AE7HD
Nick Gammon on multitasking Arduinos:
http://gammon.com.au/blink
http://gammon.com.au/serial
http://gammon.com.au/interrupts

polymorph

Is that scope not locked on? What am I looking at? I agree about PDF vs jpg or png. Why sideways?
Steve Greenfield AE7HD
Nick Gammon on multitasking Arduinos:
http://gammon.com.au/blink
http://gammon.com.au/serial
http://gammon.com.au/interrupts

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

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 (IE 8.0, grrrr).
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Paul__B


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.

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Paul__B


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!   :smiley-eek:

nehe_bhimaji

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

nehe_bhimaji

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]

CrossRoads

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:
Code: [Select]

// 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
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

nehe_bhimaji

Thanks For your quick Reply
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


Thanks For your quick Reply
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%]

Go Up