Potentiometers unstable values

Hi all,

I'm having a problem that should be related to the way I wired my circuit. The hardware is a box with 4 pot, 4 buttons and 4 leds, the software is a simple 4 step sequencer that generates the sound using this algorithm: http://mouro.info/signal-generator-using-arduino-and-dds/

http://www.youtube.com/watch?v=8voOXVFoBbk

in the video you can hear (at 0':20") that the sound changes without a reason. I'm quite sure the problem is with pots, I think the voltage is unstable and varies, so the values that I "analogRead" changes even if pots are untouched.

I tried to disactivate the leds but it's still bugged.

This is an ugly schematics I did to explain how pots and leds are wired to ground:

Do you have an idea about the reason behind this analogRead flickering (if it's this) ?

I'd hook up a multimeter to the each of the potentiometers and actually watch the voltage at each terminal. If they are steady, then your problem is in the code; if not, then try to figure out what's wrong with the wiring. I don't see anything wrong with how you've wired it up... although I'm a little unclear as to why you have a resistor before the ground. Is that for the LEDs? I'd put your ground leg of the pots directly to ground.

Hi, thanks for your reply. To check the voltage I should touch one pin of the multimeter on the middle leg and one on one of the side leg, is it correct?

And, yes, the resistor is there for the LEDs.

I'll also try to put the ground leg directly to ground.

It’s basically impossible to tell what is going on because we still have no idea how your circuit is wired.

You have four pins that say to ATMega, to which pins on the ATMega?
You say you have four pots and four pushbuttons, I only see one pot and no push buttons.
Pots have three pins, you only show two.
What at the other legs of the LEDs connected to?
How big is the pot and the resistor?

From what I can infer you have +5V connected to four inputs, with a pot and resistor to ground. You should always being reading +5V on the inputs (unless your pot and resistor are small enough to drag the power supply down–which is a very bad thing).

Do you sample the potmeter with AnalogRead()? You might consider taking the average of 4 readings per potmeter to stabilize the reading.

#define SAMPLES 4
val = 0;
for (int i=0; i< SAMPLES ; i++) val += analogRead(potPin); 
val /= SAMPLES ;

@Tim Williams: in the schematics (ugly I said ;-) ) there are 4 pots, the middle leg is on the opposite side (look at bottom, where I wrote "To ATMega").

Bytheway, you're right, I need to post a complete schematics, I'll try to write it as soon as I can.

@robtillaart: it's a smart idea, but I'd prefer to find the problem and (possibly) solve it, but I'll try anyway your code, thanks!

Ok, here is another ugly schematics (I can't use a CAD, yet):

The atmega circuit is based on this schema, then I added my components:

and fed by a 9V battery.

Thanks that schematic and picture are much clearer.

One problem I see with the circuit is that the current from all four LEDs goes through the 220 ohm resistor, which is also hooked up to the four pots. When the LED is switched on, the current through that resistor will increase causing a higher voltage drop, which in turn causes a change in the voltage across the four pots, which will cause a change in voltage coming out of the four ports and being read by the microcontroller.

The four pots should be tied from +5V to GND. If you need the 220 ohms to prevent GND from being selected you should have four 220, one for each pot, but I don't think it is really needed.

If the 220 ohm resistor is the current limiting resistor for the LEDs, you should have one resistor of each LED.

On the push buttons 1Meg seems too high, typical is 10K or so. I also hope you have it misdrawn as you show it with the input connected directly to +5V. See http://arduino.cc/en/Tutorial/Button.

I'm not convinced these are your problem, but you should fix them nonetheless.

If the 220 ohm resistor is the current limiting resistor for the LEDs, you should have one resistor of each LED.

yes, it was intended for that.

On the push buttons 1Meg seems too high, typical is 10K or so

mmm I don't understand this. I mean, 10K is enough or 1M is too much? If I got the resistor it's needed to drop the voltage on the input port, so a LOW value is read, then I wouldn't understand why 1M should be too much.

I also hope you have it misdrawn as you show it with the input connected directly to +5V

Yes! I exchanged gnd with voltage.

I just noticed there's another sign! When a note is played the power led is less bright, but only with headphones plugged in. Is it a normal behaviour?

mmm I don't understand this. I mean, 10K is enough or 1M is too much? If I got the resistor it's needed to drop the voltage on the input port, so a LOW value is read, then I wouldn't understand why 1M should be too much.

In an ideal world there wouldn't be a problem. However, the inputs are not ideal. There is some current that flows through the resistor when the switch is closed and the pin is read. If the resistor is very high compared to the internal apparent resistance of the input, the voltage drop across the resistor can cause cause it to be read as high instead. Unless you are really worried about current draw (critical battery application) it is much better to err on the side of caution and use a lower resitance.

I still don't understand how you have the pushbuttons wired. If you exchange GND and +5V it still won't work as drawn. As drawn +5V is connected directly to the pin. Exchanging GND and +5V results in GND being constantly connected. The input pin needs to be between the switch and the resistor.

As for the power LED being less bright, I could see that as the headphones probably have a low impedence and those could cause your voltage to sag.

Unless you are really worried about current draw (critical battery application) it is much better to err on the side of caution and use a lower resitance.

ok, got it.

I still don't understand how you have the pushbuttons wired. If you exchange GND and +5V it still won't work as drawn. As drawn +5V is connected directly to the pin. Exchanging GND and +5V results in GND being constantly connected. The input pin needs to be between the switch and the resistor.

you're paying much attention! :) you're right, here is the correct schema:

As for the power LED being less bright, I could see that as the headphones probably have a low impedence and those could cause your voltage to sag.

ok.. then, to fix the problem it would help a resistor before the +5V wire of the headphones? (I don't wanna be spoon-feed but I can't find any resource on the web, maybe because I don't know what to search for exactly). thanks for the help!

should I need one of those http://en.wikipedia.org/wiki/Buffer_amplifier ?

What you are looking for is simple audio amplifier, yes it is a type of buffer amplifier, but you need to have a pot on its input as well (volume control). There are a huge number of audio amplifier either using transistors or op-amp(s).

I wouldn't connector your speaker until you have one as:

I=V/R or in this case I=5V/8ohms = 625mA when you should not exceed 40mA per pin.

We still haven't solved the problem with noise on the pots. It probably best to solve this problem before adding other things into the circuit. You could even pull the LEDs out while we continue.

I suggest you add 100nF capacitors to the inputs of the ADC as shown.

You want to physically mount these close to the ATMega. I also note on your breadboard you don't have decoupling capacitors across Vcc and GND and AVcc and AGND. Add two capacitors there as well. You should probably add another between Aref and AGND. All these capacitors help to stabilize the voltages at the ATMega. Ideally, you would want to add a ferrite bead (or inductor) between Vcc and AVcc, but I doubt you have one laying about so we will ignore that for now.

Also it would help if you used the same symbol for an LED as the rest of the world and didn't invent your own.

I don't understand this. I mean, 10K is enough or 1M is too much?

Yes 1M is too big for a pull down resistor, it will not sink enough current for the input to record a reliable logic zero.

We still haven't solved the problem with noise on the pots. It probably best to solve this problem before adding other things into the circuit. You could even pull the LEDs out while we continue.

Oh, ok, I had got that the problem was depending on the low impedance of the headphones. I'll modify the circuit as you suggest and let you know about the result.. thanks again.

Also it would help if you used the same symbol for an LED as the rest of the world and didn't invent your own

sorry, I didn't even notice my symbol were wrong, but neither they looks so differents to me.

An LED looks like this:-

The long thin end is the cathode and the triangle end is the anode. The way you have drawn it you can't tell what wire is connected to what as you have both wires going into the triangle end.

I know, I know.. but especially because the LEDs are working we can suppose the legs are correctly wired, despite my schematics.

Ok.. I'm going to start the "refactoring". I have a doubt, is it correct if I wire the grounds in this way?

thanks

Ok.. I'm going to start the "refactoring". I have a doubt, is it correct if I wire the grounds in this way?

That will likely work. There is nothing 'wrong' with it, but if we take some time we can do better and learn a few things about grounding along the way.

There have been volumes of work dedicated to proper grounding techniques. In an ideal world, connecting any ground to another ground will work. Daisy-chain, ground plane, grids, star networks, they should all work the same. The ground should have the same potential everywhere and no signal will influence another.

Of course in practice it isn't that easy. Even short traces (or wires) have some inherent resistance. They also cause inductive cross talk between signals i.e. two wires side-by-side act as a tiny transformer so that the signal on one causes noise on the other and vice versa.

One method for minimizing this for sensitive signals (our analog in this case) is to create a seperate single-point gound. All analog grounds should tie to this point directly via the shortest route. AGND of the ATMega should also tie to this point. Then a single wire should be connected directly to the power supply.

Note in the drawing that the digital items run on seperate return wires but we don't worry about daisy-chaining the grounds together, as digital signals are less sensitive to issues with noise.

A similar technique can be used for the +5V and the pots and AVcc . As I mentioned earlier, you would want a ferrite bead between Vcc and AVcc to help filter the noise.

Hi Tim,

many thanks for the explanation, it's really clear and unravels some doubts I had. I've started to apply your mods, firstly I unplugged all LEDs and all pots except one. I wired the 100n capacitor and the sound frequency now is constant, but on low frequencies it sounds "rough" (I'll record it to let you hear it) but I think it could be improved using a LPF (correct?).

The meaning of the capacitors before the pots is a sort of filter to cut off peaks, is it right?