# Want to build a square wave generator

Discussion in 'The Projects Forum' started by MattStrike, Dec 15, 2011.

1. ### MattStrike Thread Starter New Member

Jun 12, 2010
14
0
I would like to build a square wave generator:
50% duty cycle
0v to 5v peak to peak
adjustable frequency from 50Hz to 300Hz by controlling an input voltage from about 0vdc to 5vdc.

The tricky part for me is making the frequency adjustable. The input voltage won't generally go below .05vdc or above 4.95vdc for this control. .05v will ideally generate 50Hz, and 4.95v generate 300Hz, but want to learn enough to be able to tune this as the control voltage may not be perfect in the field.

Edit: I've built a generator using a 555 timer before using a schematic on this forum.

Thanks!

Last edited by a moderator: Dec 15, 2011
2. ### crutschow Expert

Mar 14, 2008
13,003
3,232
Try using a (CD)4046 PLL circuit. It has a linear VCO that should do what you want. That's a lot easier than trying to do it with a 555.

3. ### wayneh Expert

Sep 9, 2010
12,103
3,038
And just in case anyone thinks about using the control pin 5 - it won't give enough range of adjustment. Maybe 60-240Hz at the max. The duty cycle will change a lot also. If the OP could live with those problems, manipulating pin 5 could become an option.

4. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,435
1,305
An old solution is to use a typical 555 astable with a pot to generate the varying frequency x2, then put the signal through a flip-flop, this gives freq x1 with exactly 50% duty squarewave.

5. ### thatoneguy AAC Fanatic!

Feb 19, 2009
6,357
718
That's a cool trick.

Though I can't think of one to vary the frequency from 40 to 300Hz from an incoming voltage. Digital resistors don't have that sort of range.

I'd have to go with VCO + PLL on this over a 555.

6. ### hiohsilver New Member

Jul 18, 2011
1
0
did you consider using a XR2206 IC. Check out the specs on it.

7. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,435
1,305
Thank you Thatoneguy, I missed the requirement for voltage controlled frequency.

The range of 40-300 Hz should be easy enough with a simple comparator VCO and adjusting the threshold voltage, but of course that simple method would not give a linear voltage->freq conversion.

If that is needed (like so many small projects fussy requirements these days) I would just reach for an 8pin PIC with ADC like a 12F675, and program it for ADC in 0-1023 steps and 30-400 Hz out, xtal locked perfect duty squarewave out and perfect linearity. Total parts; a \$1 PIC, xtal, 2 xtal caps and a 0.1uF cap.

8. ### MattStrike Thread Starter New Member

Jun 12, 2010
14
0
Problem is I don't have any experience with programming a PIC (yet). I am willing to learn though. The simpler/smaller the circuit is the better, but not a requirement. What equipment is needed?

9. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,435
1,305
A PIC and a PIC programmer like a PICkit2, and some free assember or compiler software, and lots of time and effort to learn how a PIC works and how to program it.

It would be a slow way to make this one project (unless you are already an experienced programmer) but every project gets faster and easier after the first. Fortunately the internet has a massive amount of free information and helpful tutorials etc.

It can be a hard process for someone who is an expert in electronics to change to doing tasks in code on a PIC, but if you are not an electronics expert and need to learn either electroncs methods or PIC microcontroller methods then I (personally) think the time is better spent learning PICs as you wil be able to do so much with the same code tools, and the need for specialised electronics is less. It is also easier to copy someones code and just paste it into your project to add features, than to copy someone's electronic design that needs you to buy chips and make PCBs etc...

But other people may argue that opinion!

10. ### thatoneguy AAC Fanatic!

Feb 19, 2009
6,357
718
Microcontrollers are pretty much a split camp here.

3 years ago, it was near heresy to bring up a uC solution in a project that begged for one, now they are suggested, but the problem is the user doesn't feel comfortable.

However, in every case so far, once the user gets the PicKit2 and demo board, and either Mikro C or Boost C (free with mild limitations), they are off like there's no tomorrow making new projects.

I try to find a simple way to do something with existing logic or a timer, so that if something breaks in the future, the part number can be replaced.

However, once you dabble in programming, once you do a few projects with a microcontroller, projects which you thought were WAY beyond your abilities could ever be are suddenly "straightforward, AND you can improve it!".

So, today, instead of being the leper minority, the uC crowd (Mostly PIC, some AVR) has nearly 50% support on this forum, possibly more!

A uC is just another tool in the box, and once you have one, it's like a hammer and every project looks like a nail. The problem comes up when you pick up on a project you worked on a while back. You didn't put any notes on it because you planned on getting back to it, and you have no clue what it is supposed to even do, and can't find firmware or notes on it.

If nothing else, a PICAXE will suit your needs well, pretty speedy and under \$4 to boot! Easy to program, I think the full starter kit is \$8, compared to \$49 for PIC.

11. ### MattStrike Thread Starter New Member

Jun 12, 2010
14
0
I think I will need some help getting started with the PIC. I am familiar with programming, nothing complicated (VB and Java).

How does a PIC work? What supporting circuitry would I need? What kind of costs are involved? The pickaxe seems like it would be a good choice, but do these things have to be connected to a PC all the time to work or something?

I think I'd like to build one with the PIC, and one using component, then compare them side by side.

I couldn't find a datasheet on the 4046 PLL circuit mentioned earlier. Who makes the chip?

12. ### thatoneguy AAC Fanatic!

Feb 19, 2009
6,357
718
A PICKit 2 (\$35)+ 44 pin Demo board(\$25) or 28 pin board (\$25)

BoostC and Mikro C are compilers that have very generous limitations for a beginner, you won't bump into the 2k Word code "wall" anytime soon with small projects.

The PICs themselves need 5 wires hooked up (Vpp, V+, GND, PGC, PGD), the PGC and PGD are I/O pins that can be used when not programming. This is for ICSP Programming and In Circuit Debugging, which is straightforward and simple if you don't try to save money by getting a "clone" or DIY programmer.

Once programmed, the program is semi-permanent on the IC, so you can plug it in and use it as is, and it starts running the program when power is applied. They have an internal Oscillator, so the support parts are essentially 1 0.1uF cap between Vdd and Vss to keep the power "clean". The PLL Oscillator is internal, so speeds up to 48Mhz can be obtained without a crystal, though I find 8-20Mhz fast enough for most purposes.

The PICAXE uses a bootloader system, so you don't need the PICKit 2, when you turn it on, it checks to see if the data cable is attached, and if so, downloads the new program. So there's a short delay on boot with the PICAXE, but only 1/10th of a second maybe. Cable + Instructions + PICAXE kit is about \$8 and is programmed in BASIC, which is a free compiler for the PICAXE series.

13. ### MattStrike Thread Starter New Member

Jun 12, 2010
14
0
Sorry it's been a while...

The pickaxe kit is now \$30 unless I'm looking in the wrong spot.

Once I get the PIC, should I start a new thread when I'm ready to begin programming when I have (many) questions?

14. ### thatoneguy AAC Fanatic!

Feb 19, 2009
6,357
718
All you really need is Here

I suppose you could get the full starter kit, but I'd just buy a couple 20x2 PICAXE processors and a dowload cable, put it together on a solderless breadboard for around \$18 w/the X2, which is a very nice PIC with lots of peripherals.

For \$12 more, having a board that is known to work, the book, and programming cable may be worth it for you if wary.

The site linked above is the "Main" PICAXE distribution site, and they are speedy with shipping.

15. ### MattStrike Thread Starter New Member

Jun 12, 2010
14
0
Ok thanks!

Would I need the 20-pin microcontroller for this simpler project? I figured there would only be 1x input and 1x output so the smaller 8-pin chip would work, but I know what happens sometimes when I start thinking.

Whats the difference between the 20X2 and the 20M2?

Feb 19, 2009
6,357
718
17. ### PaulEE Member

Dec 23, 2011
423
32
Learning microcontroller programming is one of the most useful things you can learn...the possibilities of what they enable you to do as a student, hobbyist, engineer, etc. are limitless. It also allows you to reconfigure code as necessary, as opposed to rewiring a breadboard, for example.

FPGAs are also very useful but I'd work with microcontrollers first

My personal favorite 8-pin microcontroller is the PIC 12F683. It has on-board ADC and PWM modules, among other goodies.