Need maths help designing lgarithmic input for adc

moorea21

Senior Member
I think I can eventually bodge my way to a solution to this, but if possible I'd like to do a proper job of optimising this on paper first. Only trouble is, my maths isn't up to it...

Have a look at the attached jpeg. The circuit is very simple; a version of it with all 10K pots has been tested, and it works very well, but the spacing between output values is far from linear, obviously.
If sourcing the correct value resistors was not a problem, how would I calculate what resistor values to use to get even spacing between each adc value? (1-31.) Outputs in an ideal world would be 8, 16... 240, 248.

There is probably an optimum value for the total resistance of the resistor 'array', from the picaxe's point of view, and obviously the value of the resistor between adc and ground will affect the overall spread of adc values that would be seen.

How should I go about this? The circuit design has to be as shown, btw. I'm aware there are better ways to do this, but they don't work for my application.
 

Attachments

premelec

Senior Member
Please be more specific on what you want - it appears linear not log - and why is the pattern of resistors chosen necessary? Ohm's law as usual can make the calculations - on a spread sheet or by hand. The configuration makes it more complicated... messy... ;-0
 

AllyCat

Senior Member
Hi,

So you have a 31-way mechanical switch with zero contact resistance? If the "switches" have some resistance or voltage drop then the calculation will be more complicated.

Let's assume a 10k load resistance (RL) at the ADC input (it's the highest value recommended by Microchip). So at the last position (31) you want an output of +5v * 248 / 256 ? Then the relative voltage drops across R31 and RL is 8 : 248 , so R31 = 10k * 8 / 248 = 323 ohms.

For position 30, the total resistance is 10k * 16 / 240 = 667 ohms, so R30 = 667 - 323 = 344 ohms, then R29 = 1.034k - 667 = 367 ohms, etc..

The total resistance of R16 to R31 should be exactly 10k (to give an output of 128 / 256 = 1/2) and the total value of all the resistors R1 to R31 (excluding RL) = 10k * 248 / 8 = 310k (if my calculations are correct).

Cheers, Alan.
 

moorea21

Senior Member
Thanks Alan, your calculations make sense and were correct.

The '31-way mechanical switch' is a musical string, and each of the 31 positions is a metal fret. There appears to be negligable resistance between the two.

And thanks premelec, for the note about complicated/messy solutions; unfortunately the configuration of the adc and resistors needs to be so, so that any contact of the string onto multiple frets (tonally 'below' the highest one) won't affect the note that eventually gets played; only the 'highest' note stopped should sound. I will order a big pile of assorted +_1% 1/4W resistors, and try to get reasonably close to the calculated values.

Thanks for the help, just needed to get the general concept clear. As I said, I'm not very mathematically minded...
 

hippy

Technical Support
Staff member
unfortunately the configuration of the adc and resistors needs to be so, so that any contact of the string onto multiple frets (tonally 'below' the highest one) won't affect the note that eventually gets played; only the 'highest' note stopped should sound.
I doubt that's going to work. I would expect you'll get some third note detected. You can model it to check.

Also, now you have explained the application; it doesn't have to be the single string of 30-odd resistors. There are likely better solutions.
 

AllyCat

Senior Member
Hi,

A musical scale is itself logarithmic, but presumably that's not what you're trying to do.

Actually, a set of standard 5% resistors from the "preferred" E12 or E24 series may be good enough, except for the "Load" resistor which needs to be as accurate as possible. Each "step" in the ADC value is only 8 units, so 5% accuracy could be sufficient. The trick is to avoid the "errors" accumulating, by keeping track of the actual values employed. For example if you have to use a preferred value a little below that calculated, then the next resistor should err to the higher side, etc..

Alternatively, the program could be "self-calibrating" by using a lookup table (in EEPROM), or another curve-fitting method.

Cheers, Alan.
 

premelec

Senior Member
I agree with hippy... however is this setup both for a stringed acoustic output and electric output to detect the fret?
 

moorea21

Senior Member
Thanks hippy, but having built and tested it with all 10k resistors there are no 3rd notes. I've looked into lots of ways of acheiving this, there are a few threads on here started by me a couple of years ago. It all got very complicated, life intervened and it got shelved.
It's a digital only 'instrument', basically a violin synthesiser, which will use pwm outputs to piezo buzzers to produce a note. Very crude, I know, but it's only for me to practice left hand technique rather than to actually play in front of people.
Thanks Alan, I'll make sure to use the best resistors first, I have plenty of less precise resistors of odd values to use. As long as all the adc values are tested and known, I can code for outputs in a 'select case' block, with ranges of adc values applied, rather than just single values, so even if some of the resistors 'wander' a bit over time, it should still work. Easy to recalibrate if not, too.
 

hippy

Technical Support
Staff member
You are correct; there won't be any third notes with a linear resistor chain. Any multiple shorts will simply make the furthest end of that chain 'disappear'.

I think I was thinking about the effect of shorting part of a track on a traditional pot with wiper. It is however not that electrically.
 

premelec

Senior Member
@moorea21 Take a look at this linear pressure pot; https://www.sparkfun.com/products/8679 ; i don't know what lengths these are manufactured in but could work well in your application if I understand it... Also a single wire pushed to a backplane conductor can work well - [practical resistance of various wire alloys comes into play...] perhaps even a skinny nichrome wire - fed into a high impedance input on a PICAXE gives you a lot of latitude as to contact resistance [source impedance] from wire to backplane.
 

moorea21

Senior Member
I've never heard of 'soft pots' before, that was the sort of thing I was originally trying to build, using vhs tape and a lot of swearing. I think maybe these are a pretty new thing? Or I managed to miss them when I started toying with this idea a couple of years or so ago. Apparently they even do one thats 500mm long; I wonder if its possible to reliably cut them to length...

I had considered a nichrome wire wrapped around something like a lollipop stick, with the windings changing their spacing to change the resistance/length ratio to make a logarithmic scaled pot. One (of four) strings at +5V would be the 'wiper' in my circuit diagram, each wire being switched on/read/switched off one at a time.

What is a backplane conductor? I googled it, nothing definitive came up.
 
Last edited:

premelec

Senior Member
I mean a conductive part along the neck below the wire by 'backplane' term [ordinarily a backplane has to do with RF design of circuit boards and such]. The wire would have a current through it producing a linear voltage drop along it's length. Say a 50 ohm wire with 100 ma drive current. I'm not sure I have your physical instrument situation in mind [need pictures...] - it's possible, and has been done in practical situations, [e.g. pot on fuel tank float to read linear electric fuel gauge ] . However I was thinking you'd just do the linear voltage conversions to log whatever with a PICAXE... READADC10 gives over 1000 points from zero to V+ . Nonlinear resistive structures are problematic to design - in your case you _could_ make a circuit board like conductor which varied in width along it's length thus producing non-linear resistance change with distance.... Maybe there's a CAD program to do that... ;-0


On cutting soft pots - ask the manufacturer - I don't know about cutting - I'd think it might be possible as you only need to find the conductor the the end you'd cut off - but refastinging to the resistive material could be a problem. I'd probably go with two overlapping soft pots varying the overlap and using two READADC10s and some clever program to sort out what you read and perhaps ave an ambiguous location in the process... [BTW I think they've been around for a while - and other strange pots like ones that use photoconductive material for the 'slider']
 
Last edited:

moorea21

Senior Member
I'd forgotten about readadc10. Judging by the values I got from the adc on the circuit with all 10k pots, readadc10 would have enough resolution to handle a soft pot. An idea for next iteration, maybe.
An option for output might be to use the picaxe to output precise voltages from its serial output pin to drive a good quality V>F chip. I think I have a few somewhere. That could give a basis for a better sounding output than I get using pwms and setfreq/DIV, dut and dur to tweak the output frequencies gives a few duff notes.
 

premelec

Senior Member
As I don't fully know the scope of your project it's hard to say what might work - if you need vibrato emulation that could be too fast for a PICAXE response - also what waveshape you want - likely a sawtooth has some good overtone content. Simply varying the current feeding a capacitor governed ST osc could work... probably some digital devices that would work... so many ICs so little time...
 
Top