Sine to square wave converter

Discussion in 'Electronic Basics' started by Jack// ani, Mar 24, 2005.

1. Jack// aniGuest

Hi all,

How can I convert 50Hz AC to a square wave so that I can feed it to a
microcontroller? Will CD4093 work here? I will steps down the ac to
5volts then use a diode in series before hooking it to CD4093?

Thanks

2. Lord GarthGuest

This will explain how you should proceed:

http://www.web-ee.com/primers/files/AN-140.pdf

4. Lord GarthGuest

Not from here though it is slow....send an email address or I can post
it to alt.binaries.schematics.electronic

5. Jack// aniGuest

Whoops, i'm sorry the link is working! Something was wrong here!

6. John WoodgateGuest

I read in sci.electronics.design that Jack// ani
IE6 barfs badly, but Firefox retrieves it OK. I'm having the same
problem with PDFs from other sites, using IE 6 and Acrobat 6.

7. RST Engineering \(jw\)Guest

There are trivial ways to do it...it all depends on how "square" square must
be. Do you absolutely HAVE to have 50.000% duty cycle, or are you only
worried about using it as a leading (or falling) edge clock. Do you have
any spare circuits in another multi-circuit chip, or do you have room for a
dedicated chip. Tell us what matters to you and we'll have a shot at making
the sucker go.

Jim

8. Guest

If a 50% duty cycle is important, a zero-cross detector into a D
flip-flop as divide by 2 would get pretty close.
GG

9. Pooh BearGuest

Works fine for me.

Graham

10. Robert MonsenGuest

One simple way is to use a large value resistor, like 1MEG, into the
base of an NPN transistor. The emitter should be grounded, and the
collector tied to your +5V power supply rail through a 100k resistor.
Take the output from the collector of the transistor. Note that for this
to work, your power supply should have it's ground someplace near earth
ground.

One problem with this is that the base goes very negative. This can be a
problem. Thus, if you use a diode from ground to base, if the input goes
very negative (which it will, of course) the diode will keep it within
about 7/10 of a volt of the ground.

So

VCC
|
.-.
| | 100k
| |
'-'
|
o------ PIC Input
|
1MEG |
___ |/
AC IN -|___|----o----|
| |>
| |
- |
^ |
| |
| |
GND ------------o------'
(must be near neutral)
(created by AACircuit v1.28.5 beta 02/06/05 www.tech-chat.de)

This is probably going to be fast enough so that the PIC will only see
one transition for each transition of the AC line. However, if it isn't,
you can build a simple schmitt trigger out of two transistor that will
prevent false triggering.

That would be like this (which is swiped out of Art of Electronics,
Volume 2)

.--------------o--------- VCC
| |
.-. .-.
| | 1.5k | |1k
| | | |
'-' '-'
| ___ |
o---|___|--. o------Output to PIC
| 10k | |
1MEG | | |
___ |/ | |/
AC IN -|___|----o----| '-|
| |> |>
| | |
- '------o-------'
^ |
| 100R |
| ___ |
GND ------------o----|___|----'
(must be near neutral)

(created by AACircuit v1.28.5 beta 02/06/05 www.tech-chat.de)

However, I think the first circuit is probably ok.

--
Regards,
Robert Monsen

"Your Highness, I have no need of this hypothesis."
- Pierre Laplace (1749-1827), to Napoleon,
on why his works on celestial mechanics make no mention of God.

11. mikeGuest

All depends on what you're trying to do.
If all you need is line synchronization...
On PIC16F877A, I've used a voltage divider directly into the input.
The inputs are clamped. As long as you have enough series resistance to
limit the current, it should work ok. External clamps are even safer.
Transistor is better. You can spend as much as you like.
Use SW to mitigate noise.
Assume you have mains isolation.
mike

--
Return address is VALID but some sites block emails
..
Wanted, PCMCIA SCSI Card for HP m820 CDRW.
FS 500MHz Tek DSOscilloscope TDS540 Make Offer
Wanted, 12.1" LCD for Gateway Solo 5300. Samsung LT121SU-121
Wanted 13" LCD for Mitac 6133 Samsung HT13X13-201
Bunch of stuff For Sale and Wanted at the link below.
MAKE THE OBVIOUS CHANGES TO THE LINK
ht<removethis>tp://www.geocities.com/SiliconValley/Monitor/4710/

12. Fred BloggsGuest

It depends on what powers your logic circuit and what it needs the 50Hz
square wave for. You can't just couple a voltage off the line into a GND
referenced circuit and expect it to work trouble free. Also, most
circuits are looking for zero-crossing on the AC- waveform and this is
difficult to get with the massive attenuation required of a line voltage
in conjunction with the fairly large uncertainties of typical Schmitt
trigger thresholds and hysteresis. You will get a square wave, but it
may not be of much use.

14. Terry PinnellGuest

Agreed. I could probably have made a movie of it in the time it would
take a PIC-er to just get started! Here it is:
http://www.terrypin.dial.pipex.com/Images/LED-Sequencer4MB.wmv

15. marikaGuest

thanks for this great freakin link!!!!

mk5000

"...aside from uknova.com. Anyone know any good ones?
While uknova is a good site for certain shows, I find that you usually
get zero response to requests for stuff not already torrented. The
fact that the mods don't allow you to bump your requests on the forum
doesn't help either, as the volume of traffic means that your post
soon drops off the page... "-- a single locust

16. Bob MonsenGuest

This took 5 minutes to code (adapted another project). 15 if you include
the setup, programming, testing, and bugfix (I inadvertently forgot to
call init(), and it didn't work the first time). Only one chip, a pot, 5
resistors, and 4 leds. Sadly, I don't have a movie camera, or I would
record it. This is compiled with the free 'hitech lite' C compiler, and is

The PIC12F675 is an 8 pin uC, which supports ADC, timer interrupts, and
such. Pin 7 is used as the ADC input, so changing the voltage changes the
rate of change of the display, from once every 2 seconds to as fast as it
can go. The period is about Vin/Vcc * 2048 for Vin > 0.

Pin 6, 5, 3, and 2 are A, B, C, and D. Pin 4 is the reset, pin 1 is Vcc,
and pin 8 is GND. A-D can source up to 20mA, so a 1k resistor between it
and the LED limits current to about 3.5mA.

(for the curious, it uses a 1ms interrupt to do delays. The result of
the high byte of the AD is between 0 and 255. Thus 8 * AD is from 0 to
2040, and just delaying that number of ms interrupts provides the ability
to change the period from fast to slow)

BTW, I'm sure we all appreciate JF's and Terry's efforts to help people
out. I wish everybody's posted circuits were as well thought out, tested,
and nicely presented as the ones they post.

Regards

---------------------------------------------------------------------------
#include <pic.h>

__CONFIG(UNPROTECT & BORDIS & MCLREN & PWRTDIS & WDTDIS & INTIO);

unsigned int g_ticks; /* Ticks since startup */

#define TICKS_PER_SECOND 1000

#define LED_A GPIO1
#define LED_B GPIO2
#define LED_C GPIO4
#define LED_D GPIO5

void init_gpio(void)
{
GPIO = 0;
}

void init_tmr0(void)
{
/* prescaler is 1:4, GPIO pullups are disabled */
OPTION = 0x81;
}

void init_tmr1(void)
{
T1CON = 0; /* Disabled */
}

void init_comparator(void)
{
CMCON = 0x7; /* Turn off the comparator */
VRCON = 0x0;
}

void init_a2d(void)
{
ANSEL = 0x11; /* Fosc/8 and RA0 is analog */
ADON=1; // turn on the A2D conversion module
}

void init_ee()
{
}

void init_interrupts(void)
{
INTCON = 0xA0;
}

void init(void)
{

TRISIO = 0x01; /* RA0 is ad input, rest are outputs */

init_gpio();
init_tmr0();
init_tmr1();
init_comparator();
init_a2d();
init_interrupts();
}

unsigned int get_voltage (void)
{
GODONE=1; /* initiate conversion */

while(GODONE) /* wait for result */
{
continue;
}

}

void wait_ms(unsigned int x)
{
unsigned int temp = g_ticks + x;
while (temp != g_ticks);
}

void main(void)
{
int state = 0;

init();

LED_A = 1;
LED_B = 1;
LED_C = 0;
LED_D = 0;

for (;
{
unsigned int ms_to_wait = get_voltage() * 8;

wait_ms(ms_to_wait);
switch(state)
{
case 0:
LED_A = 0;
LED_C = 1;
state++;
break;

case 1:
LED_B = 0;
LED_D = 1;
state++;
break;

case 2:
LED_C = 0;
LED_A = 1;
state++;
break;

case 3:
LED_D = 0;
LED_B = 1;
state = 0;
break;
}
}
}

static void interrupt isr(void)
{
T0IF = 0;
TMR0 = 6;

g_ticks++;
}

18. CliffGuest

On Mon, 28 Nov 2005 14:51:55 -0600, John Field
You think that resistivity controls the speed of light
I think I heard that a good vacuum has a lot of resistanc
but a fairly high speed of light
The speed of light goes down as things become more
conductive

19. Fred AbseGuest

On Tue, 29 Nov 2005 07:31:11 +0000, Jasen Betts wrote
Most are :-
Run the LTSpice simulation below, three cases of 1000 miles of RG59-U
terminated. shorted, and open at 1 MHz. You'll find all 3 cases loo
lik
75 ohms. Constants are per Belden data

LTSpice is free, if you didn't already know that, and runs under WINE
(well at least here it does.

RG59-U.as

Version
SHEET 1 880 68
WIRE -128 144 -128 3
WIRE -128 160 -128 14
WIRE -128 320 -128 24
WIRE 48 320 -128 32
WIRE 48 320 48 6
WIRE 80 320 48 32
WIRE 80 320 80 17
WIRE 112 32 -128 3
WIRE 112 64 48 6
WIRE 112 144 -128 14
WIRE 112 176 80 17
WIRE 128 320 80 32
WIRE 128 352 128 32
WIRE 240 176 208 17
WIRE 240 320 128 32
WIRE 240 320 240 17
WIRE 288 64 208 6
WIRE 288 320 240 32
WIRE 288 320 288 6
WIRE 384 32 208 3
WIRE 384 144 208 14
WIRE 384 320 288 32
WIRE 384 320 384 22
FLAG 128 352
SYMBOL ltline 160 160 R
SYMATTR InstName O
SYMATTR Value RG59
SYMBOL voltage -128 144 R
WINDOW 123 0 0 Left
WINDOW 39 0 0 Left
SYMATTR InstName V
SYMATTR Value SINE(0 1 1e6
SYMBOL res 368 240 M18
WINDOW 0 36 76 Left
WINDOW 3 36 40 Left
SYMATTR InstName R
SYMATTR Value 7
SYMBOL ltline 160 48 R
SYMATTR InstName O
SYMATTR Value RG59
TEXT -152 456 Left 0 !.model RG59U LTRA(len=5.28e6 R=51.6e-3 L=0.115
C=20.5p
TEXT -162 506 Left 0 !.tran 0 1m 0 1
TEXT 496 456 Left 0 ;(Belden 8263

RG59U.pl

[Transient Analysis

Npanes:
Active Pane:

traces: 1 {524290,0,"v(n001)/Ia(O1)"
X: ('u',0,0,8e-005,0.0008
Y[0]: (' ',0,0,10,100
Y[1]: ('_',0,1e+308,0,-1e+308
Units: "Ohm" (' ',0,0,0,0,10,100
Log: 0 0
GridStyle:
}

traces: 1 {524291,0,"v(n001)/Ia(O2)"
X: ('u',0,0,8e-005,0.0008
Y[0]: (' ',0,0,10,100
Y[1]: ('_',0,1e+308,0,-1e+308
Units: "Ohm" (' ',0,0,0,0,10,100
Log: 0 0
GridStyle:
}

traces: 1 {524292,0,"v(n001)/Ia(O3)"
X: ('u',0,0,8e-005,0.0008
Y[0]: (' ',0,0,10,100
Y[1]: ('_',0,1e+308,0,-1e+308
Units: "Ohm" (' ',0,0,0,0,10,100
Log: 0 0
GridStyle:

20. Terry GivenGuest

NunYa Bidness wrote
thats not correct. case in point, it is quite feasible to analyze a
conductor as comprising an n-tuple of (very thin) strands, completely
ignoring strand-to-strand conduction (if the strands are thin enough)
I
once hacked up a Mathcad worksheet that did exactly that, and i
agreed
well with messrs Vandelac & Ziogas. slow though :

if your argument were correct, multiple parallel strands of magne
wire
in a xfmr would behave just like litz, and it doesnt

A friend also had a problem a few years back with a litz cable, abou
1"
OD, with many thousands of strands. the strands were woven int
smaller
bundles, IIRC about 5mm OD, and these were then woven into the larger
cable. A screw-up by the manufacturer meant the centre two bundle
were
not woven at all, but ran down the center of the cable. When they
started pumping a few hundred kW into the cable, losses were extremel

high. Close inspection of a cable segment showed why, and severing th

central bundles solved the problem
the bundle doesnt do the pushing, its the H field

Cheer
Terr