A/D works on Arduino board but not my custom PCB

In my code below

void setup() 
{
  DDRD = 0xFF;   // all bits of PORTD are outputs
  PORTD = 0xFF;  // all bits of PORTD are one
  
  DDRB = 0x0F;   // 4 low bits of PORTB are outputs
  PORTB = 0x0F;  // 4 low bits of PORTB are one
}

void loop() 
{
    uint16_t val = 0;  // variable to store the value read
    uint16_t analogPin = A0; //perform A/D conversion on AD0
    uint16_t average = 0;
   
    Serial.begin(9600);  //automatically uses D0 and D1

while(1)
{
    delay(500);
    val = analogRead(analogPin);  // read the input pin
    average = GetAverage();

    Serial.print("Raw Value = ");
    Serial.print(val);
    Serial.print("    ");
    Serial.print("Average = ");
    Serial.print(average);
    Serial.print("\n");


 uint16_t GetAverage()
{
    uint32_t new1 = 0;
    uint32_t analogPin = A0; //perform A/D conversion on AD0
    uint32_t total = 0;
    uint32_t average = 0;

    for (int x = 1; x <= 300; x++)
    {
      new1 = analogRead(analogPin);
      total = total + new1;
    }
    average = (total / 300);
    total = 0;
    return average;
}

On the Arduino board I connect an analog input to ADC0 (pin 23) and I print the correct value between 0 and 1023.
When I put the same chip on my custom PCB I always print the value 1023.

On both boards I see the analog voltage on pin 23(ADC0).

Sounds like there's an issue with the PCB. Maybe you should show that design.

Here is my schematic.

Here is my PCB

I don't understand how it could be the PCB since I read the analog voltage directly from pin 23.

I don't understand how it can be anything else if it works when it's not on the PCB.

On your layout it looks like pin 22 is tied to pin 21. If so that's the problem. AREF needs to be tied to VCC. 21 should be tied to 20.

If the reference voltage for your ADC is 0 volts then everything is full scale. It can't read negative voltages.

AREF is grounded on the schematic. It should have only a small capacitor to ground.

That ground connection may have damaged the chip.

Thanks Delta_G that was the problem!
I knew it had to be something, but I don't think I would have ever noticed that mistake.

Thanks, jremington, you are correct as well.
The chip seems to be working fine right now, but I think I will burn a new one anyway.

First thing I noticed is you have a reset issue, no capacitor to "reset" on power up.

On any new board you should (using a multimeter) test every pin with respect to ground and verify it is as expected.

2nd Use the IDE File example/digital/blink without delay, changing the LED pin to match your board.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.