How to fix ringing on a square wave

I am attempting to create serial communication between my android tablet (Samsung Note) and my Arduino.

Before I started implementing how to output different patterns of HIGH, LOW to generate meaningful bytes with start bit, etc. I wanted to just output a square wave from my headphone jack and monitor it on an oscilloscope.

I did that and successfully generated a square wave, but there is this horrible ringing (that is what it’s called from my understanding so far) whenever the signal switches from HIGH to LOW or the inverse. It is almost like there is a sine wave overlapped on my HIGH and LOW signal that dampens over time.

I attached a picture.

I read on other forums that a load resistor to simulate the impedance of a headphone would sometimes fix it, but that didn’t work. I put a 10uF capacitor form the audio line to ground and it actually did get rid of the ringing, but it also drastically smoothed out (in a bad way) the rise and fall of my square wave and I no longer have my nice sharp edges I was going for.

Is this just a hardware limitation of the phone? Is their circuity inside the phone that is preventing me from getting a clean square wave output? If I generate a sine wave output it is as smooth as butter, only the square wave has issues.

Don't worry about it. A digital circuit responds as long as the voltage is above the logic 1 level. As long as the ringing stays above that level, everything should be OK.

Part of it could be your measuring technique as well. Need to ensure the scope probe is well grounded with a nice short lead.

ensure the scope probe is well grounded with a nice short lead

The lead is about probably about 20 inches. And the ground is connected directly to the audio out cable which is about 15 inches. Not sure if that is too long. It is not a very good quality scope, in fact it is a very cheap one. But when I purchased it my though was something is better than nothing.

Don't worry about it. A digital circuit responds as long as the voltage is above the logic 1 level. As long as the ringing stays above that level, everything should be OK.

Keeping that in mind, is it better to put the capacitor on and smooth out the ringing (equals longer rise and fall time) or is it better to let the ringing go full force as long as it doesn't result in voltage level dropping below logic HIGH (70% VCC I think)?

How long does the ringing last? Arduino can't latch it in faster than every 62nS.
Serial comm's will be even slower; you should be okay as long as settles before the middle of the databit time.

is it better to put the capacitor on and smooth out the ringing

No, at least not such a huge capacitor.
Ringing is caused by the inductance of the line and those scope ground wires are way too big, they should be as short as possible in the order of half an inch if you can do it.

You can also get ringing from an unterminated line. The load resistor should be the same impedance as the line impedance, just putting any old resistor on will do no good. The problem is that unless you have some very good test equipment you can not tell what impedance the line is.

Only do something about it if it causes you problems, but most likely is that most of it is an artifact of the scope.

Grumpy_Mike:
Only do something about it if it causes you problems, but most likely is that most of it is an artifact of the scope.

If you are measuring with wires this is very likely.

"Proper " scope probes have a ground connection about 3 in long.
They make a very big difference when measuring fast risetimes.
Unfortunatley they are fairly expensive.

How long does the ringing last?

I attached two images of what the ringing looks like on my actual scope. It does die down by about half way through the logic HIGH or LOW.

And thanks for all the input; it sounds like it's primarily an issue with poor measurement techniques on my part and improper equipment for the task I am trying to complete.

It is not causing problems however so I will continue on as it is currently working. I may try and get a more proper scope though. Does anyone have any recommendations for an affordable one. Maybe a specific model I should look for used in good condition on ebay? I am in school current for electrical engineering, so it would probably be a worthwhile investment to get a better scope; it just can't break the bank.

IMG_000.BMP (38 KB)

IMG_001.BMP (38 KB)

CrossRoads:
Part of it could be your measuring technique as well. Need to ensure the scope probe is well grounded with a nice short lead.

Definitely not that, this is a signal out on the audio jack.

What the ringing is is a symptom of the digital filter used in the audio processing on
the phone. Its likely some sort of brickwall chebyshev filter so that if you feed it
an abrupt step change it gives ringing. Perfectly correct behaviour, and there's
nothing you can do about it unless there's some App that can install a linear-phase
digital filter instead (unlikely, but who knows with modern smartphones!).

Google "Gibbs phenomenon"

Adding a large capacitive load to the headphone output only risks blowing
the audio output stage of the phone. Don't do it!

Adding a large capacitive load to the headphone output

Just to clarify a large capacitive load is just something that has a lot of capacitance correct? So I'm assuming any capacitor at all is a bad idea and of course any circuit that acted like a capacitor would be equally bad. What size is too big. 10uF, 100uF? I don't intend to use one, just want to know for reference.

there's nothing you can do about

I was afraid that might be the case. As it stands I will likely keep playing with it as is, because the ringing doesn't ever result in the logic level HIGH being lost. I suppose under the right circumstances however it could dip below the threshold and I would start getting corrupt data.

I may look into using some other method to transmit the data. I'm not sure how easy or difficult it would be to output a sine wave of varying frequency or amplitude and then use the Arduino to measure the FM or AM and convert that to data bits. But I may explore that route.

If you are worried about it, add a Schmitt trigger buffer into the circuit. The hysteresis loop of the buffer will ensure that the ringing will be removed (unless it is very large - say >6Vpp depending on the parameters of the buffer).

Good idea!

I would need to find some way to boost my headphones voltage though, I guess they probably make audio transformers and such. Because the output voltage with volume all the way up is a little shy of 1V and that would not cut it. I looked up a couple Schmitt trigger datasheets and most all of them had a typical positive threshold voltage of 1.xx V (and that is with a low Vcc too).

Look into this
http://www.digilentinc.com/Products/Detail.cfm?NavPath=2,842,1018&Prod=ANALOG-DISCOVERY&CFID=5006422&CFTOKEN=b9a343aa8c1b0e12-1C852523-5056-0201-02212643C7A25105
This is what the EE students use at RPI (Rensselaer Polytechinic Institute) one of the top engineering schools in the US.

Looks to be a very good device with a lot of functionality for a very good price. Thanks for the link!

It specifies you must have a specific class that required said device to qualify for student pricing. Unfortunately I am only in my first year of school and so I don't have a specific class I could claim a need to have this for. So I would be paying the $240 price, but even that is not unreasonable. Alternatively I could wait until I either have a class that required such tools, or maybe I could just go talk to a professor and see if they would confirm with the seller that I do 'need' it for their class.

What’s the amplitude of the signal now? (2V, from the scope traces?) I was thinking that you would have trouble driving a digital pin from an audio output. If you’re reading the signal from an analog pin, you can (maybe) implement the Schmitt trigger in software. Otherwise, you can implement the Schmitt trigger and level conversion at the same time with a low-voltage op-amp.
http://www.pcbheaven.com/wikipages/The_Schmitt_Trigger/?p=1

For running communications over an audio channel, you might want to review some of the now-forgotten audio “MODEM” techniques that they used to use for stuff like r ecording data on cassette tapes. You may not need some of the features that this provides, but they are designed to get data through in spite of decidedly non-digital circuit behavior. Here’s one reference: http://en.wikipedia.org/wiki/Kansas_City_standard

No I had a subdivision of 0.2V on those scope pictures and it was going up just shy of 5 divisions (so about 1V). I measured Vmax using the scope and it was about 960mV, so just barely short of 1V.

I wasn't planning on driving a digital pin, but rather an analog pin. My inital plan was to set the voltage reference to the internal 1.1V reference on the Uno; giving my plenty of resolution for my 1V max output wave.

The only reason for the voltage increase would be to use the schmitt trigger buffer, so if the op amp schmitt trigger circuit takes care of the schmitt trigger then I no longer have a need for the buffer IC. That is find, but then I would need raise my analog voltage reference on my arduino. Which is also fine.

I will definitely take a look at those old standards. Thanks!

Assuming it is a digital signal you want, the attached circuit would take your noisy 0Vmin to 1Vmax signal and convert it into a clean 5V logic signal which can be fed directly into a digital input of an Arduino (no need for analogue).

The trigger has threshold voltages of 0.8V and 0.2V for low-high and high-low respectively meaning the input signal can have up to a 1.4Vp-p noise signal and still be correctly converted into digital.

Make sure that if you use it, the op-amp is a rail-to-rail type and is compatible with having 0V on its negative supply terminal. Alternatively an analog comparator could be used instead of the op-amp which is probably a better choice for this sort of circuit - something like an MCP6541.

SchmittTrigger1V.png

wes000000:

How long does the ringing last?

I attached two images of what the ringing looks like on my actual scope. It does die down by about half way through the logic HIGH or LOW.

And thanks for all the input; it sounds like it's primarily an issue with poor measurement techniques on my part and improper equipment for the task I am trying to complete.

It is not causing problems however so I will continue on as it is currently working. I may try and get a more proper scope though. Does anyone have any recommendations for an affordable one. Maybe a specific model I should look for used in good condition on ebay? I am in school current for electrical engineering, so it would probably be a worthwhile investment to get a better scope; it just can't break the bank.

What is the bandwidth of your scope ?
Maybe you could invest in a proper test probe ?

@TomCarpenter

'westfw' posted the same circuit; it looks like a very good options for my application. Thanks for the application specific schematic!

What is the bandwidth of your scope ?

The analog bandwidth is 200kHz which is quite low, compared to this one "http://www.digilentinc.com/data/products/analog-discovery/AnalogDiscovery.pdf" which I am considering purchasing.

At the time though I purchased this cheapy oscilloscpe just cause I wanted something to do a few small PWM projects with and it only cost me like $50. But now that I am doing some more in depth projects I really do need an upgrade.

Hi, Wes, reading through the posts, you say that the ringing occured when you used the headphones, in all the test you have done, have you looked at the output of the arduino with no load, and then say 330 Ohm resistor as the only load?

Use your engineering education establishment and ask to use one of their lab scopes, it will show your lecturers/tutors that you have a keen interest and want to learn outside of school, the electronics field.

When I went to University, long long time ago in the dreamtime, this sort of thing was encouraged, the number of car CDI kits and home stereo amps that were built and tested, was almost as high in number as the number of practicals we used to do.

(Yes, remember when you could build a Stereo Amp cheaper than buying one, and it had lots of discrete components to let the smoke out off, but they were reapairable.)

Tom....... :slight_smile: