AC square wave

johndk

Senior Member
Can anyone suggest the best way to generate an ac square wave, i.e. alternating polarities, at 2 - 3kHz?

My project is to measure conductivity (salinity) of seawater while trying to avoid electrolysis as much as possible. My concept is to take an ADC reading during the positive portion of the cycle and convert that into a reasonable salinity reading. The readings will occur spaced at 10-15 min intervals using short bursts of the ac signal for the measurement. I'm hoping to minimize electrode wear by using standard gold plated header pins as the electrodes with the knowledge that they will need to be replaced every so often.

I looked at PWMOUT, but can't see how I could set that up on two pins to run at 180 degrees of each other. Probably obvious, but I'm not seeing it.

Thanks,

John
 

nick12ab

Senior Member
I looked at PWMOUT, but can't see how I could set that up on two pins to run at 180 degrees of each other. Probably obvious, but I'm not seeing it.
PWMOUT can't, but HPWM certainly can do what you want, providing you're using a supported part.
 

AllyCat

Senior Member
Hi,

Or there's even the Set-Reset Latch which (IMHO rather strangely) has Q and /Q outputs, but only a single (configurable) Set or Reset input pin.

But HPWM is probably the way to go. Incidentally, I believe the 'H' stands for "H-Bridge" not "Hardware" as for other commands.

As for the electrodes: header pins are rather small and the gold flashing probably incredibly thin, so there might be better alternatives.

Cheers, Alan.
 

hippy

Ex-Staff (retired)
Or there's even the Set-Reset Latch which (IMHO rather strangely) has Q and /Q outputs, but only a single (configurable) Set or Reset input pin.
Good point; not sure what frequency output a repeated "srset:srreset" sequence would give but worth a try.

The reason there is only one SRI input is, I believe, because it is intended to be used in analogue applications. The comparator outputs can drive both set and reset so, using an input going through a comparator, you get the second SR latch input.

I have noted in the past that Microchip describe using the SR Latch for analogue applications but never explained how one would. With the comparator input control I guess it is possible to have the SR latch output start charging a capacitor, have a comparator monitoring that reset the latch to discharge it, and repeat forever. Or something like that.

That may open the door to using a simple RC to generate lower frequency clock signals. Perhaps even help with an R or C meter based on counting output pulses or measuring their length.

Something new for the new year to try. A very early Christmas present :)
 

BESQUEUT

Senior Member
My concept is to take an ADC reading during the positive portion of the cycle and convert that into a reasonable salinity reading. The readings will occur spaced at 10-15 min intervals using short bursts of the ac signal for the measurement. I'm hoping to minimize electrode wear by using standard gold plated header pins as the electrodes with the knowledge that they will need to be replaced every so often.
ADC measurements are done in regard to zero volts. You need a real -5V to balance the 5V you send during measurement.
Not realy an AC signal : only a positive pulse during AC measurement,
and a negative one for the same time to reverse electrolyse.
To my mind, you need a +/-5V opAmp to do that...
 
Last edited:

johndk

Senior Member
ADC measurements are done in regard to zero volts. You need a real -5V to balance the 5V you send during measurement.
Not realy an AC signal : only a positive pulse during AC measurement,
and a negative one for the same time to reverse electrolyse.
To my mind, you need a +/-5V opAmp to do that...
I'm not so sure I need a -5v. If I'm using two electrodes which alternate between +5 and ground, I should think it would accomplish the same current flow directions I need to minimize electrolysis.

As to AllyCat's comment on the electrodes, I've already tried these with brief DC currents and they have enough surface area to get a good measurement. As to the gold flashing, if I can get it to last a couple of months at a time, I'll be happy. Time will tell if I can get away with this "cheap" alternative (I know we're all about cheap on this forum!). If not, I'll have to try some other metals like SS or maybe Ti. I've even thought of giving carbon rods a try.

John
 

eggdweather

Senior Member
Just pass the resultant square wave through a capacitor, say 1uF and what comes out is pure AC, there will be no DC component. Your testing circuit has to have two electrodes to enable current flow, so one will go to the ground of the Picaxe, the other to the capacitor.
 

BESQUEUT

Senior Member
You cannot measure an AC voltage with a Picaxe.
A you said, you will set a 5V DC to an electrode. During this time, you want to measure voltage with a Picaxe pin. So there will be a current flow from +5V to ground via the Picaxe.
This will cause a little electrolyse.
If you want to compensate, you will have to establish a current from the Picaxe ADC Pin to the other electrode.
Yes it is possible without -5V : for the reverse step, establish the ADC pin as a 5V source, and the other pin to 0V (for same time as the measuring step).
After that, simply set the two pins to same voltage (probably 0V) to stop any electrolyse.
 

AllyCat

Senior Member
Hi,

IMHO it would be better to forget about "ac" and just consider the system as a (repeating) period of time divided into two "halves", when current flows in opposite directions to cancel electrolysis. Also it's the current which needs to be measured, which can be done synchronously, i.e. between each current-reversal. I presume the current is less than the PICaxe is able to drive comfortably?

A Bridge is one way to generate a reversing current flow and you could put a resistor in series with each output pin. Then measure the voltage on each electrode (pin) relative to ground. But the PICaxe ADC operates fully between the supply rails, so a single resistor can be used, with the resistor on one electrode measuring relative to ground or to the supply, depending on the phase.

However, there are advantages in using a capacitor (probably to ground) in series with one electrode. A capacitor "guarantees" that there can be no net current flow (i.e. no dc) provided that there are no circuit connections which can "bypass" the capacitor. Also, you then no longer need a bridge configuration. It's even possible to estimate the current purely by measuring the rate of change of voltage across the capacitor, but that's a rather more "advanced" method (on which I happen to be preparing a "code snippet").

So, what I would try is two PICaxe pins connected to one end of a resistor, with the other end going to a third PICaxe pin and one electrode. The other electrode goes via a capacitor to ground. One of the pair of linked pins drives current alternately from the supply and ground, the other pin measures the voltage across the output driver FETs (an "unknown" and potentially not negligible voltage). The third pin measures the voltage on the other side of the resistor, so the current can be calculated. After each measurement the PICaxe program simply toggles the output pin.

Cheers, Alan.
 

westaust55

Moderator
As a thought, can a L293D be used as a H-bridge to allow the output to change polarity.
Could then use either 2 PICAXE outputs to control or 1 PICAXE output an a 74 series gate (eg from a hex inverter chip) between the two control pins of the L293D.
 

darb1972

Senior Member
Hi,

IMHO it would be better to forget about "ac" and just consider the system as a (repeating) period of time divided into two "halves", when current flows in opposite directions to cancel electrolysis. Also it's the current which needs to be measured, which can be done synchronously, i.e. between each current-reversal. I presume the current is less than the PICaxe is able to drive comfortably?

A Bridge is one way to generate a reversing current flow and you could put a resistor in series with each output pin. Then measure the voltage on each electrode (pin) relative to ground. But the PICaxe ADC operates fully between the supply rails, so a single resistor can be used, with the resistor on one electrode measuring relative to ground or to the supply, depending on the phase.

However, there are advantages in using a capacitor (probably to ground) in series with one electrode. A capacitor "guarantees" that there can be no net current flow (i.e. no dc) provided that there are no circuit connections which can "bypass" the capacitor. Also, you then no longer need a bridge configuration. It's even possible to estimate the current purely by measuring the rate of change of voltage across the capacitor, but that's a rather more "advanced" method (on which I happen to be preparing a "code snippet").

So, what I would try is two PICaxe pins connected to one end of a resistor, with the other end going to a third PICaxe pin and one electrode. The other electrode goes via a capacitor to ground. One of the pair of linked pins drives current alternately from the supply and ground, the other pin measures the voltage across the output driver FETs (an "unknown" and potentially not negligible voltage). The third pin measures the voltage on the other side of the resistor, so the current can be calculated. After each measurement the PICaxe program simply toggles the output pin.

Cheers, Alan.
Hi Alan

Sounds like some interesting approaches to the issue. I am interested in this concept for a future project. If you are prepared to share, I would be interested in the code snippet and accompanying "schematic snippets" if you don't mind posting your ideas.

Regards
Brad
 

johndk

Senior Member
I share Brad's interest in Alan's code snippet. Whenever you're ready to share, I'd be ready to experiment with it.

And a question re AllyCat's last paragraph. If the capacitor were sized so as to become a resonant circuit with the expected resistance range of the water being tested, could that not establish its own reversing current as an oscillator? Could that simplify the design to minimize electrolysis? And, in fact, could frequency then be used to measure the resistance?

John
 

AllyCat

Senior Member
Hi,

Thanks for your interest. :)

I've posted several snippetts recently in that section of the forum and have (at least) another three "in the pipleine". But I don't want to monopolise the forum, so perhaps I'll "promote" the current-measuring one. It will be in three parts: 1. using an external resistor, 2. using an external capacitor and 3. an external capacitor + internal comparator to measure down to sub-microamp currents. Most of the "text" is written (for the first two) but the code can't be be tested in the simulator (only with "real" hardware) and I keep finding "unexpected" behaviour (sometimes called bugs) in the PIC(axe) chips. :(

Concerning the last paragraph above: Generally a "resonant" (tuned) circuit needs an inductor (quite large for kHz operation) in addition to a capacitor and the effect of resistance is to reduce the "Q" (Quality factor) and thus reduce/stop resonance. RC "phase shift" oscillators can be designed to work with an (Op-) amplifier but I don't think are really relevant to this project. Just a little more food for thought: Are you sure that the "conductivity" is the same in both directions and/or what if it isn't?

I'll try to remember to link the finalised snippet from this thread, but if you have any specific questions please ask here (or start another thread if it's not directly relevant).

Cheers, Alan.
 

Circuit

Senior Member
Hi,
But I don't want to monopolise the forum ...
Alan, please, monopolise as much as you like! Though I am only reading this thread through interest I have to say that I always learn a lot from your in-depth knowledge of the subject. All your contributions keep the reference value of this forum at the highest level.
 

Reloadron

Senior Member
Just wondering what units you want to measure? For example conductivity would be typically measured in in siemens (S) and was formerly measured in Mhos which ironically is Ohms (resistance) spelled backwards. However, if you want to only measure salt water samples, Ocean salinity is generally defined as the salt concentration (e.g., Sodium and Chlorure) in sea water. It is measured in unit of PSU (Practical Salinity Unit), which is a unit based on the properties of sea water conductivity. It is equivalent to per thousand or (o/00) or to g/kg.So I was wondering which engineering units you wanted to have your results in? Additionally, while I understand your desire for an AC signal for your sensor normally measurements of this type, conductivity, are extremely sensitive to temperature change. Unless a know sample is tested and the system calibrated at a given temperature and all subsequent samples measured at the same temperature then shouldn't temperature compensation be a consideration? This is why just about all commercial conductivity probes include a temperature sensor and why pretty much all conductivity formulas involve temperature compensation. If you just want to measure the salinity of seawater you also may want to consider a refractometer type design which will look at the dissolved solids (salt concentration) of your solution (seawater)?

Scroll down to #2 Select a refractometer designed for the liquid you wish to measure.

This is also a good read on the subject and gives good examples of creating your own calibration solutions. Note how their simple and inexpensive circuit is built the DC output of which can be fed to your PICAXE and scaled for the engineering units.

Just My Thoughts
Ron
 
Last edited:

AllyCat

Senior Member
Hi,

Yes, I've always liked MHOs as a nice explicit unit (for the inverse of OHMs). Also, those are interesting links, but I think (s)he is wrong about ac being more dangerous than dc.

Measuring temperatures at "no cost" is another of my pet projects, but using the DS18B20 is a really easy and accurate way to measure temperature with a PICaxe. I've invested in a bunch of the ready-made waterproof probes which cost (on ebay) hardly any more than the raw TO92 packages and are much less likely to get mislaid or mixed up with all my BC548s. ;)

@Circuit and Bear. Thanks for your kind words, I've now posted the "Part 1" snippet here. Part 2 should follow within a few days, but "part 3" needs rather "advanced" programming techniques that may need to be presented as part of a complete project description.

Cheers, Alan.
 

Reloadron

Senior Member
but I think (s)he is wrong about ac being more dangerous than dc.
Never saw that part. Reminds me of Edison electrocution an elephant to make that point. Anyway, not true but I never saw reference to it in the link. I do feel be it a DS18B20 or sensor of choosing that temperature will need to be factored in when calculating conduction or salinity.

Ron
 

SAborn

Senior Member
I have been watching this thread as I have built CF meters in the past, just some time in the past now, and fully agree with Ron, as the reading is next to useless without good temperature compensation.

I do struggle to see why one would not include an opamp in the circuit design to get suitable gain to the input reading.
Although it is common here to try to do everything with the picaxe alone, but often a little support with the correct odd chip added can achieve much better results, we frequently do it on outputs, but often see its not a common practice to do it on inputs, to me its often a downfall in design with trying to "keep it simple".

It is how people learn electronics outside of the picaxe alone, the picaxe is a great support chip, but often needs a little circuit help to be outstanding.
 

Circuit

Senior Member
... but I think (s)he is wrong about ac being more dangerous than dc.
As a clinician I thought that I should know the answer to that one, but I was puzzled. It seems that the puzzlement is ubiquitous! A quick search revealed many arguments either way with some very unseemly references to the development of the electric chair as a means of execution! All too ghastly before bedtime...I want a good night's sleep. Clearly there is no point in opening this particular debate in this forum except suffice to say that the most relevant aspect of lethality is the actual pathway that the current takes through the body, whether a.c. or d.c. Sleep well all!
 

premelec

Senior Member
At least with slow AC you've zero crossings to give some chance of letting go! [DC arcs and burns a lot - look at switch and circuit breaker ratings for same device]
 

Reloadron

Senior Member
As a clinician I thought that I should know the answer to that one, but I was puzzled. It seems that the puzzlement is ubiquitous! A quick search revealed many arguments either way with some very unseemly references to the development of the electric chair as a means of execution! All too ghastly before bedtime...I want a good night's sleep. Clearly there is no point in opening this particular debate in this forum except suffice to say that the most relevant aspect of lethality is the actual pathway that the current takes through the body, whether a.c. or d.c. Sleep well all!
I was serious in my earlier reference to Edison (Thomas Edison) killing an elephant by electrocution (Topsy the Elephant) to try and make the point that AC was the evil method for power delivery (Tesla) and his (Edison's) DC was the safer of the two. I always saw Edison as a cut throat business man more than a great inventor. A Google of Topsy the Elephant should give some results. Another good example is as mentioned the development of the electric chair.

So are we going o get back to the salinity of sea water? :)

Ron
 

johndk

Senior Member
Yes, by all means, let's get back to salinity. As I have no expertise in electrocution of elephants.

Temperature will be integrated into the calculation. That's pretty much a given.

Although I've read that 2-3kHz is the frequency most often used to measure salinity, I'm not convinced it is the best. In fact, I'm wondering if I can achieve the same effect of balancing current induced ion transfer by taking simple, brief (say 10ms) DC readings at alternating polarities. Since I'm only taking 4-6 readings an hour, I'm even wondering how much wear on my 'gold flashed' electrodes that would save. It might not even be worth the bother to reverse polarity at all.

As to another post above, I'm not opposed to additional components. The requirement, however, is that they be extremely low power consumption. My eventual deployment will be battery operated and need to function for extended periods of time.

Is it possible to use a 'touch' measurement to get a capacitive reading without exposing any electrodes?

John


John
 

Reloadron

Senior Member
Is it possible to use a 'touch' measurement to get a capacitive reading without exposing any electrodes?
Not that I am aware of for conductivity or salinity measurements. I would use a circuit like this which I linked to earlier. Since your readings will not be continuous (4 to 6 readings an hour) taking a reading every 10 o 15 min you very likely could just use DC. In my link where the sensor is shown excited by the AC signal I would guess DC Excitation could be used. Low power should not be an issue since the system will take a reading and that's it till the next reading. At this point I have no idea as to what power consumption would be. Guessing you may want to run samples and store the results? I like the link because it includes how to make up your test samples. A Google of salinity meter circuit will bring up about a dozen circuits which can be worked from and tailored for your specific needs. Again, I am not aware of any touch capacitive circuit that would meet your needs, maybe another member knows of one?

Ron
 

AllyCat

Senior Member
Hi,

We've covered a lot of this ground in the (even longer) "hedgehog" thread. ;)

Pure water has rather "strange" dielectric properties and impure water (e.g. saline) is basically a conductor. Conductors don't make good dielectrics for capacitors, so I agree that a capacitive method is unlikely to succeed.

I can see that there might be an argument for using "real" (sine-wave) ac, versus dc. But if making a fundamental dc measurement, I really can't see why you wouldn't reverse it occasionally - it's not difficult to do. Also, didn't the previous link(s) refer to a general build up of "crud" (my word) on the electrodes? Will they remain usable even if there is only an occasional current flow? Personally I don't know. Corrosion is a major problem in most "seaside" environments.

Power consumption shouldn't be a problem; even when running, a PICaxe only requires less than 1 mA and it can sleep (and switch off everything else) for 99+% of the time.

Cheers, Alan.
 

fernando_g

Senior Member
I do struggle to see why one would not include an opamp in the circuit design to get suitable gain to the input reading.
Although it is common here to try to do everything with the picaxe alone, but often a little support with the correct odd chip added can achieve much better results, we frequently do it on outputs, but often see its not a common practice to do it on inputs, to me its often a downfall in design with trying to "keep it simple".
My sentiments exactly.
Even an inexpensive and ubiquitous LM358, when applied properly, can do wonders with respect to signal conditioning before the ADC.
 

SAborn

Senior Member
My sentiments exactly.
Even an inexpensive and ubiquitous LM358, when applied properly, can do wonders with respect to signal conditioning before the ADC.
That is exactly the approach I would take to develop a simple project like this, I would also use the other half of the LM358 to produce the square wave to the probes, back in the past days when built a simular circuit, I used a 555 timer to drive the square wave, but with a picaxe the pwm should do the same result.

The advantage with using an opamp is its not limited to a 5v supply.
It also allows for adjustment to inputs and outputs for calibration.

There seem to be other members working on a solution to the problem cause, so I will see what results are produced, as I simply don't have the time of late to dedicate to development in this project.

I would also suggest using stainless steel probes spaced at around 10mm and 10mm long as this is easy value to work with.
Some SS 316 wire set in epoxy resin will work fine, just cut and file each probe square on the ends, as it is important to have close to exact surface area of both probes so they are exact to each other as close to possible.
Small things make a difference regardless of the circuit used.

Good luck with your project.
 

hippy

Ex-Staff (retired)
It probably comes down to try it and see. There are plenty of articles which suggest DC resistance measurements of salinity does work, then it's a matter of doing that accurately and preventing build-up of crud and corrosion which affect resistance readings.

It seems to me that briefly applying a voltage, taking a reading, and giving an AC burst to 'clean the probes' is a good way to start, perhaps reading resistance in the other phase to get a better average.

Then it's a case of experimenting and analysing as to what frequency AC burst is best and how things can be improved otherwise.
 
Top