Go Down

### Topic: Potentiometer cuts off at one end of spectrum (Read 1 time)previous topic - next topic

#### kevinsa5

##### May 17, 2010, 05:28 am
Hello, apologies in advance if there's already been a topic on this. I can't for the life of me figure out a good way to word it.

So I'm making a music toy where it reads a pot and outputs a square wave to a speaker, simple enough. It's also got switches and LEDs, but that's beside the point. My problem is this: at one end of the pot's rotation, the reading goes to zero before it actually finishes rotating. like, there is a range where it's just all zero. Also, right before it hits that range, the pitch increases much faster. Going by that, it's probably a log pot, correct? I'm pretty sure it's a 10k, but not positive.

in case it's relevant, I have +5v and ground on the two outside pins and analog pin in the middle.

#### kevinsa5

#1
##### May 17, 2010, 05:52 am
hmm. I'd rather not get another pot just because I have this one and it's already soldered in and everything. eh, poor planning.

Hmm. I assumed it was a log pot and assumed that I wanted linear to get rid of the fast rise of pitch on the higher end. Here's the (uncommented, sorry) code I wrote to try to space it out some:

Code: [Select]
`int speakerPin = 13;int potPin = 5;float frequency = 0;void setup(){  pinMode(potPin, INPUT);  pinMode(speakerPin, OUTPUT);}void loop(){  frequency = analogRead(potPin);  frequency = log(wavelength) / log(100000);    // This is the line I'm talking about below  digitalWrite(speakerPin, HIGH);  delayMicroseconds((1000 * wavelength) + 1000);  digitalWrite(speakerPin, LOW);  delayMicroseconds((1000 * wavelength) + 1000);}`

I messed around with a bunch of different values for the second line for determining the frequency and that came out okay. I think the higher the value of where I have 100000 now, the better the range of tone. However, the increase in pitch is not even. As pitch increases, the increases in pitch increases, if that makes sense. Any ideas on how make each degree of rotation of the pot an even increase in pitch?

#### kevinsa5

#2
##### May 17, 2010, 06:12 am
Okay. tomorrow I'm going to run it through processing and see what happens.

What exactly do you mean by a transfer function? Is that just a way to change the returned frequency to the desired frequency?

Yeah, that's not a big deal. I don't mean for this to be accurate or anything, I just want a wide range of pitch.

Thanks for all you help!

#### mowcius

#3
##### May 17, 2010, 10:09 am
Highly unlikely that the pot is a log pot...

Rubbish pots do this. Not much else I can say. If you want to solve it then get a new one!
I have had a load of crap pots over the years and you learn where not to buy from or what projects you can get away with having it in. Some things require decent pots though (although they can be expensive!).

Mowcius

#### Grumpy_Mike

#4
##### May 17, 2010, 11:13 am
Try swapping the +ve and -ve of the two ends over. If the problem goes to the other end of the scale then you have a log pot.

#### PaulS

#5
##### May 17, 2010, 12:09 pm
Code: [Select]
`  frequency = analogRead(potPin);  frequency = log(wavelength) / log(100000);    // This is the line I'm talking about below`

What's the purpose of reading the analog pin that the pot is connected to, if you don't use the value?

log is a pretty slow function. log(100000) is a constant. Compute it once, not every pass through loop.

#### LMI

#6
##### May 17, 2010, 01:34 pm
Are you sure your potentiometer is ok. Potentiometers can crack, the carbon track in them breaks. Then when you turn it, there is a sudden jump when you go over the break.

One way to break a pot is use them with a power supply certain way. If pot is near one end a large current may flow and overheat the track.

#### Groove

#7
##### May 17, 2010, 01:41 pm
It's always best to post all of your sketch.
Code: [Select]
`int speakerPin = 13;int potPin = 5;float frequency = 0;void setup(){  pinMode(potPin, INPUT);  pinMode(speakerPin, OUTPUT);}void loop(){  frequency = analogRead(potPin);  frequency = log(wavelength) / log(100000);    `
As PaulsS pointed out, you're not using "frequency", and you haven't declared "wavelength".

#### kevinsa5

#8
##### May 17, 2010, 06:13 pm
Hmm. I just realized that it could be a problem of how sound works as opposed to how the arduino works. I wanted the return of the pot to directly correlate to the delay between pulses. However, sound pitch doesn't increase linearly when compared with time between pulses. ie an increase in 5 milliseconds will take a high note down a lot but won't change a low note much. So, I think my reasoning was wrong and that explains why the pitch increases so drastically at the higher end.

However, it still doesn't explain the blank space. It seems like a pot shouldn't have a range where it has the same resistance. Perhaps I'm using it wrong?

As for the ivariable confusion, that's my bad. "wavelength" and "frequency" are the same for the sketch's purposes. I changed one to the other halfway through and didn't get them all, I guess.

#### kevinsa5

#9
##### May 17, 2010, 11:28 pm
I have found success! Once I figured out how sound actually works and wrote it all out, I figured that I wanted an exponential function as opposed to a root, which was what I had originally. Here's the final code:

Code: [Select]
`int speakerPin = 13;int potPin = 5;float frequency = 0;void setup(){  pinMode(potPin, INPUT);  pinMode(speakerPin, OUTPUT);}void loop(){  frequency = analogRead(potPin);    frequency = (sq(frequency) / 100);    digitalWrite(speakerPin, HIGH);  delayMicroseconds(frequency);  digitalWrite(speakerPin, LOW);  delayMicroseconds(frequency);}`

And I'll probably end up putting a small capacitance sensor in there too to have actual notes instead of a constant sound. I might actually end up doing one for pitch and one for volume like a theremin, but that's later.

Here's a picture of the final product, just to show it off some:

and the top and bottom compartments:

And I'm not sure if it'll embed the pictures correctly, so here are the links:

http://img535.imageshack.us/i/photo10dr.jpg/
http://img245.imageshack.us/i/photo11xj.jpg/
http://img257.imageshack.us/i/photo12kz.jpg/

I guess you're right about the pot not being good quality. I got the scale pretty good and all, but the blank space is still there at the end. Oh well. Just for future reference, any recommendations for quality pot distributors?

Thanks for all the help, everyone!

Go Up

Please enter a valid email to subscribe