AnalogRead order

Hello everybody!

I have a problem, I found its causes, I have a workaround but I would like to understand what happens !

I need to use multiple potentiometers with analogRead on an Arduino Nano (ATmega328P old bootloader). I had mysterious interferences although my potentiometers are totally separated.

When I use this code (see attachment):

  //analogRead(A0);
  p1=analogRead(A1);
  p2=analogRead(A2);
  
  Serial.print(p1);
  Serial.print("   ");
  Serial.println(p2);

Then if I move the potentiometer A2, the A1 value varies. The A1 value is not the same than the A2 value, but there’s clearly an interference, which doesn’t seems normal to me as of course I would like my potentiometers to be independent.

If I use this code (see attachment):

  analogRead(A0);
  p1=analogRead(A1);
  p2=analogRead(A2);
  
  Serial.print(p1);
  Serial.print("   ");
  Serial.println(p2);

Then everything works perfect, my two potentiometers are independent.

The only difference is that I have called analogRead(A0) before calling analogRead(A1), even though I don’t care about A0.

Can somebody tell me why it makes a difference?

I’ll tell you some more about the circumstances, but I think it doesn’t change anything to my question…

Of course, my original program was way more complex, I have had a very hard time isolating the origin of this interference. I’m actually working on a robot and I use two physical joysticks so I need to call analogRead on A0 and A1 for the first joystick and A2 and A3 for the second joystick. I noticed than when I called analogRead(A1) before analogRead(A0), then I had an interference between A1 and A3, although they are connected to different joysticks.

All my potentiometers are connected to 5V, GND and an analog in. I’m pretty confident with my setup, especially as my program works perfectly as long as I call analogRead(A0) before analogRead(A1).

Thanks folks!

this_does_not_work.ino (216 Bytes)

this_works.ino (214 Bytes)

What value of resistance? The pot should be 10K or less.

When switching channels there can be a "ghost" of the voltage from the previous channel. If the extra time doesn't matter, read each channel twice and take the second reading.

It can also help to add a small (10 to 100 nF) capacitor to each input and ground.

For reading multiple analog inputs, it's often advised to do a dummy read first of an input and next do the actual read.

  analogRead(A1);
  p1=analogRead(A1);
  analogRead(A2);
  p2=analogRead(A2);
  
  Serial.print(p1);
  Serial.print("   ");
  Serial.println(p2);

Why is that case not handled in the core?

Thanks !

This doesn't work :

p1=analogRead(A1);
delay(500);
p2=analogRead(A2);
delay(500);

This neither :

p1=analogRead(A1);
p1=analogRead(A1);
p2=analogRead(A2);
p2=analogRead(A2);

Potentiometers seem to be 5k.

How are the pots wired? Are you using a breadboard?

The following sketch works fine for me on UNO with test voltage 3.3V and 5V in A1 and A2.

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int p1 = analogRead(A1); //test volt 3.3V
  int p2 = analogRead(A2);  //test volt 5V
  Serial.println(p1, HEX);
  Serial.println(p2, HEX);
  delay(1000);

}

sm12xz.png

Note: Any loose connection does disturb the measurements and applies noise which appears as crosstalk between the channels.

sm12xz.png

pjdestexhe:
Thanks !

This doesn't work :

p1=analogRead(A1);
delay(500);
p2=analogRead(A2);
delay(500);

This neither :

p1=analogRead(A1);
p1=analogRead(A1);
p2=analogRead(A2);
p2=analogRead(A2);

Potentiometers seem to be 5k.

What does it doesn't work mean?

So if you put all the analog inputs to ground and read them you get?

So if you put all the analog inputs to MCU V+ you get?

Does your MCU have one analog to digital converter and the inputs are multiplexed or do you have 1 analog converter for each analog input pin?

What would be the consideration of operation if there were 1 analog converter per pin or 1 analog converter that is multiplexed?

How are the pots wired? Are you using a breadboard?

Yes, I'm using a breadboard. Each potentiometer is connected to the ground, the 5V and an analog in as mentioned earlier, check the sketch I join. Again, I'm pretty confident with my setup, especially as my program works perfectly as long as I call analogRead(A0) before analogRead(A1).

What does it doesn't work mean?

So if you put all the analog inputs to ground and read them you get?

I first tried to double read the values or to add delays and it didn't change anything : when I move one joystick to the left, A2 reads around 30 which is all right, but A1 reads around 300 while it should keep reading around 512 as I leave I centered. When I move one joystick, the values I read on the other one varies although I leave it still. That's what I mean when I say it doesn't work.

I disconnected everything to measure the resistances with my multimeter, and when I reconnected it my program works perfectly, even without my workaround. That's what I hate the most : when something sometimes works and sometimes doesn't. I know you'll say I must have changed something in the connections, but I'm positive : it's the very same setting. I've been working on this stuff for days, sometimes it works sometimes it doesn't. Today it works, tomorrow it won't...

So there is most probably a problem of loose connection indeed.

In my opinion my question remains : in some configurations (probably caused by loose connections), there are interferences that can be avoided by adding an absurd analogRead(A0). How comes ?

Then what can I do to improve my connections? I can try to resolder my wires (I use a big joystick scavenged on a R.C. helicopter control on which I soldered Dupont wires), but I need a breadboard to connect everything. Suggestions?

I join a picture of my setup.

Thanks !

Fix the loose connections and the other problems may go away. Breadboards are notorious for bad connections, especially ones with oxidized or burned tracks due excessive current (motors, etc.).

For a semipermanent setup like shown in the photo, don't use a breadboard at all. Use a protoboard like this one, and solder the connections.

It seems that connections are an issue, in that case. It may be time to get rid of the breadboard and solder the connections. Using a double sided through hole board to connect the wires for the joysticks.

The Mini can be placed on the perf board using a connector; such as a 2.54mm Breakaway PCB Board 40 Pin Male and Female Pin Header Connectors. The connectors can be cut to size. 30g wire is a convenient size for the interconnecting wires.

Hi,
Can you please post a COMPLETE copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Not a fritzy image.
You have more hardware connected than in that image.

Tom... :slight_smile:

Hi,
Try this code.
Set your monitor baud to 115200.

int rawYval;
int rawXval;
int JoystickYPin = A1;
int JoystickXPin = A2;

void setup()
{
  Serial.begin(115200);
  Serial.println("=============JOYSTICK TEST CODE==============");
  delay(1000);
}

void loop()
{
  rawYval = analogRead(JoystickYPin);
  rawYval = analogRead(JoystickYPin);
  rawXval = analogRead(JoystickXPin);
  rawXval = analogRead(JoystickXPin);

  Serial.print("RawYval = ");
  Serial.print(rawYval);
  Serial.print("\t RawXval = ");
  Serial.println(rawXval);
  delay(100);
}

Tom.... :slight_smile:

Thanks !

As required, here's a COMPLETE :slight_smile: copy of my circuit (yes, it took me some time to do the drawing, but I might use it for future lessons :).)

It's mainly an Arduino nano plugged on a breadboard, with a mini joystick, a full-sized joystick and a NRF24L01 wireless transmitter, plugged on a 5V->3.3V adapter. The other stuff including the LCD screen is not connected yet.

Does somebody think that this circuit could explain the weird behavior of my setup? I think it doesn't bring more information than my fritzy image, but if I was sure that I was right I would'nt ask for help :slight_smile: .

If I tried to summarize your help, I would say that nobody sees any reason for the program to work better with a different order of the analogRead's, and that everybody blames loose connections. I would agree with that.

It's no good news for me because I'm a teacher, so I like breadboards because it's easier to use and safer for my students, and I can reuse the equipment.

So could you help me with loose connections? If I use perfboards and pin headers so that I can keep dupont wires, will I have better connections ? Or do I have to give up dupont wires as well as breadboards ? Is a male/female dupont wire connection better than a breadboard connection ? Or worse ?

My arduino nanos are presoldered with pin headers down so they're meant to be plugged on breadboards. It would be a pain in the ass the give up the breadboards and desolder/resolder all my nanos (I have a dozen of them for my students). Is there a way to improve connections and keep the breadboards ? Like find a solution for the 5V and GND pins and keep the breadboard for the rest?

Thanks !

What's inside the heat shrink tubing? It's notorious for hiding bad connections.

Or do I have to give up dupont wires as well as breadboards ?

Not if the connections to the header pins are soldered and the Dupont connectors are soldered or properly crimped.

But certainly ditch the breadboard for a protoboard (reply #9). That should have been the very first step toward the final configuration shown.

Hi,
Thanks for the extra info.
Can you place the NRF away from the wiring, it would be best to be attached to the left hand wall of your enclosure, this will also ensure efficient RF radiation and reception.

Tom.... :slight_smile:

It's possible you miswired something so what you've drawn doesn't match reality. Or that the ADC is picking up interference from the NRF when it's transmitting. Both seem very unlikely. It's also possible that the NRF is drawing a lot of current while transmitting which is causing the 5V rail to sag. Ideally it should be using a separate power supply or at the very least have a large electrolytic capacitor across the power supply as close to the NRF module as possible.

The bad connection theory is the most likely, especially since reconnecting everything fixed it. That's the limitation of a breadboard, you just have to accept bad connections will happen and checking them should be one of your first troubleshooting steps. Internally breadboards have a strip of metal bent into a U shape that grip the end of the wire. Over time they loosen up and can be bent back if you have the time and patience to fix them.

Thank you very much!

Do you think that if I was to solder female headers on this, it would work better than a breadboard ?

It would surprise me, why would Dupont to Dupont conections be better than breadboard to Dupont ?

Is there a way to check the quality of my connections? I tried to measure the resistance between different points of my circuit, and it's never more than 1 Ohm.

Thanks !

Do you think that if I was to solder female headers on this, it would work better than a breadboard ?

That protoboard is EXACTLY what I recommended in reply #9 and reply #15.

Why do you bother to post if you don't read the replies?

I recommend to solder the wires to the protoboard, or use male headers with female Dupont connectors.

Driver for five-phase stepper motor: