About pins ...

OK, so I got my first Arduino (RobotDyn CH340/A 2650) several months ago, and I've been playing with it ... experimenting .... learning ... tons of Googling ... Ive even successfully used a few shields ... but I'm struggling with something that I feel should be very easy to grasp and I've decided to post my dilemma here.

I seem to be struggling with inputs and accuracy in reading voltages. You see, when I set a pin as an input because I want to read some voltage (between 0 and 5 volts of course), the behavior of the Arduino does not seem to be consistent from one pin to the next, and I want to start a project that would require some consistency in this thing. I'm starting to wonder if this unit is faulty.

When you set an analog pin such as A10 as an input, then start reading the pin value without any voltage applied to it, is it normal to get readings in the 500 range? When I take the pin to ground, SOMETIMES it goes to zero (depending on the pin), and sometimes it makes no change at all. Then when I apply a small voltage to it, sometimes it changes, sometimes it doesn't and again it seems to be depend on the pin im using (usually between A10 and A15).

Second question: Ive noticed that when I declare an int to something like A8, it actually comes out as 62 when I print it to serial. So that got me wondering about the literal pinouts etc., and in my searching, I looked at this link for my unit, but according to that page, pin 62 is ground ... so what gives?

Is there a way to test this thing and the inputs to make sure they are functioning properly? Some kind of uniform thing I can do say with a AA batter or something that I can apply to certain pins and get a known value etc.?

Thank you,

Mike Sims

"When you set an analog pin such as A10 as an input, then start reading the pin value without any voltage applied to it, is it normal to get readings in the 500 range?" A floating input pin can be all over the place. Try a 100k pullup resistor to provide a steady reading when nothing is connected. Use a 1K, 5K, 10K pot with legs at Gnd and 5V, wiper to an input pin. Should see steady reading with values reported at ~ level/4.88mV.

Physical pin numbers are not the same as the software mapped pin numbers. Header pins D0 and D1 connect to chip pin 2 and 3 for example. D62 = A8 = physical pin 89 for example.

CrossRoads: Use a 1K, 5K, 10K pot with legs at Gnd and 5V, wiper to an input pin. Should see steady reading with values reported at ~ level/4.88mV.

OK, I'll give that a shot.

CrossRoads: Physical pin numbers are not the same as the software mapped pin numbers. Header pins D0 and D1 connect to chip pin 2 and 3 for example. D62 = A8 = physical pin 89 for example.

So then how does someone wrap their head around all of this seeming inconsistency?

(deleted)

spycatcher2k: If you search for Arduino MEGA2560 Pin out - all pins and functions are listed there. :)

You mean like the one I linked to in my original question? Yeah, its clear as mud :-)

CrossRoads:
Use a 1K, 5K, 10K pot with legs at Gnd and 5V, wiper to an input pin. Should see steady reading with values reported at ~ level/4.88mV.

So to alleviate any confusion, here is a diagram of exactly how I have the Arduino connected as you suggested, using a 10k trim pot.

Here is the code I used to obtain the information:

#include <Wire.h>
#include <LiquidCrystal.h>
#include <LCDKeypad.h>

template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }

#define p1 A8
#define p2 A12

LCDKeypad lcd;

void setup() {

lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd << "A8  A12";
pinMode(p1, INPUT);
pinMode(p2, INPUT);

}

void loop() {
    lcd.setCursor(0,1);
    lcd << "         ";
    lcd.setCursor(0,1);
    lcd << String(analogRead(p1)) + " " + String(analogRead(p2));
    delay (500);
}

Here are the readings I get as I move the swiper on the pot:

Meter    A8      A12
------   ---     ----
  0      520      0
  1.04   521      522
  2.27   520      522
  3.03   522      520

See what I mean by inconsistent?

(deleted)

spycatcher2k: Nope - like this one

Mega Pin Out

Now that is very nice! Thank you!

(deleted)

spycatcher2k:
Nope - like this one

Mega Pin Out

I cleaned up the pic a little and made it easier to reference…

spycatcher2k: pinMode(p1, INPUT); pinMode(p2, INPUT);

If you remove these lines, the pins are already inputs, you could read the pins here to 'prime' them.

I removed those lines as you suggested, but the results are the same.

(deleted)

spycatcher2k: Can you connect A8 direct to GND on the mega, and A12 direct to 5V and then to 3.3V and post the results.

OK, here are those results with A8 connected directly to ground and A12 on the Arduino 5V pin then the 3.3v pin:

A8    A12
------------
511   510 (5v)
513   514 (3v)

(deleted)

spycatcher2k: Your ADC is Broken! :(

That's not what I wanted to hear, but I've suspected as much. Any idea how this could happen? And I suppose there's no way to repair it ... that a replacement board is now my only option? Should I not get the cheap-o Chinese version again / and what do you recommend for a Mega dev board or something other than a Mega??

Do not throw your Arduino out just yet.

Remove all wires from your Mega and upload this sketch. This connects the ADC to two internal signals (GND and the 1.1V bandgap reference) and reads them. It should print out 0 and 161 (give or take a few) to the Serial monitor. If you still get 512-ish with this sketch, your chip is broken. If you get the expected values, your ADC is fine and the external circuit is most likely faulty.

#include "wiring_private.h"

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
  ADMUX = 0x40 + B011111;
  sbi(ADCSRA, ADSC);

  while(bit_is_set(ADCSRA, ADSC));

  Serial.println(ADC);

  ADMUX = 0x40 + B011110;
  sbi(ADCSRA, ADSC);

  while(bit_is_set(ADCSRA, ADSC));

  Serial.println(ADC);
}

void loop() {
  // put your main code here, to run repeatedly:
  
}

Jiggy-Ninja: It should print out 0 and 161 (give or take a few) to the Serial monitor.

Nope, it gives me 0 and 512

:-(

Jiggy-Ninja: If you get the expected values, your ADC is fine and the external circuit is most likely faulty.

Well, since I wasn't actually using a circuit to do my tests (I had a scope plugged directly into the port), I had little hope that I would get a good reading with your sketch.

I suppose there is no way to simply repair this board? Perhaps replacing the main chip would be cheaper than purchasing a new board?

Thank you for your help.

Mike Sims

EasyGoing1: Nope, it gives me 0 and 512

Well that's unfortunate.

:-( Well, since I wasn't actually using a circuit to do my tests (I had a scope plugged directly into the port), I had little hope that I would get a good reading with your sketch.

I suppose there is no way to simply repair this board? Perhaps replacing the main chip would be cheaper than purchasing a new board?

Thank you for your help.

Mike Sims

Do you have a second Arduino that you can use to flash the new chip with the bootloader? If you do, and you think you can handle replacing a 100 pin TQFP, then by all means go for it. :)