[solved] Arduino ADC Sketch to ATMega328P-PU Breadboard does not work

Hi members,
I signed up in this forum because I have a problem which drives me crazy since days.

I have a working sketch on a Arduino UNO R3 board with an simple external 500kHz Receiver.
The external circuit is powered by a PWM Output Pin of the Arduino. The Output of the receiver will be fed back to the arduino into the analouge A0 pin. Depending on the Signalstrength coming back into the Arduino the receiver Power on the PWM Output is regulated - this is a kind of initial start procedure. As soon as the Output value of the receiver reaches a certain level, the voltage coming from the PWM Output will then be fixed.
Then the Receiver is calibrated and the normal program starts running in the arduino. It listens on the A0 port and if a spike occures a counter increases. So far the function which is working perfect on the Arduino board.

I produced a PCB for the Receiver and the ATMega328P-PU circuit togehter to free the arduino board.
The ATMega328P-PU circuit is equiped with a 5V stabilized Power Supply (LM7805, with 100µF and 100nF on the input side and on the output side). The ATMega328P-PU runs with an external 16MHz clock with 2x22pF and a 16MHz quartz). The VCC and the AVCC Pin is connected to 5V, The AREF pin is connected to ground via 100nF.
Everything is controlled many many times. The PCB is correct but the calibration procedure does not work. The circuit starts but the PWM output stays at 0 and the switches to the normal running mode.

It is the same code and the same circuit used with the Arduino board and the external receiver.

In the meantime I am "death by google".... I don´t have any idea why my code does not work on the external board. I tried 3 ATMega328P-PU "hoping" that one of them is damaged. Everytime the same result.

Some things I have in mind - which I can not verify because of an google overflow with no detailed informations.

-) Is there any special procedure to upload a special bootloader when I use an external 16MHz quartz?
-) Is there any special bootloader for an external breadboard?
-) Is there any special code to use for the ADC when using on an external breadboard?

I will attach some codesnipps of my code.
I am really disapointed in the moment because I do not have a plan how my project can continue.
Perhaps some of you is wanting to support me - that would be very helpful. Especially with detailed descriptions. I am quite new to the Arduino technology.

Thank you very much and best regards
FTF

Here are some code parts. If someone is interrested in the whole code then please send me a message. I will provide the code then.

defining the Pins

const int U_Empfaenger_PIN=9;
const int IN_Signal_PIN=A0;

Defining the I2C 20x4 LCD display

//I2C LCD Display Settings
#define I2C_ADDR    0x3F
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

Prescaler definition of the ADC

// Define various ADC prescaler
const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);

in the "void setup()"

  pinMode(IN_Signal_PIN, INPUT);
  pinMode(U_Empfaenger_PIN, OUTPUT);

  // set up the ADC
  ADCSRA &= ~PS_128;  // remove bits set by Arduino library
  ADCSRA |= PS_32;    // set our own prescaler to 32

The calibration code is

void Kalibrieren()
{
  unsigned long Startzeit=0;
  unsigned long Endzeit=0;
  int Intervall=1000;
  int Signal= analogRead(IN_Signal_PIN);
  int U_Empfaenger=0;
  int AmpMax=0;
  int AmpWork=0;
  int Trigger=0;
  int i=0;
    
  do
  {
    unsigned long Startzeit=millis();
    if (Startzeit - Endzeit > Intervall)
    {
      lcd.setCursor(0, 0);
      lcd.print("#Messungen ");
      lcd.setCursor(12, 0);
      lcd.print(i);
      lcd.setCursor(0, 1);
      lcd.print("Receiver ");
      lcd.setCursor(12, 1);
      lcd.print(U_Empfaenger);
      lcd.setCursor(0, 3);
      lcd.print("Version 1_2_2");
      Endzeit = Startzeit;
      U_Empfaenger=U_Empfaenger+1;
      analogWrite(U_Empfaenger_PIN, U_Empfaenger);
      i=0;
    }    
    else
    {
      i=i+1;
      Signal = analogRead(IN_Signal_PIN);
      if (Signal > 500)
      {
        Trigger=Trigger+1;
        AmpMax=U_Empfaenger;
      }
    }
  }while (Trigger < 1);
  AmpWork=AmpMax;
  U_Empfaenger=(int)(AmpWork*0.5);
  analogWrite(U_Empfaenger_PIN, U_Empfaenger);  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("U_Empfang ");
  lcd.setCursor(0, 1);
  lcd.print(U_Empfaenger);
  delay(1000);
}

It is me again,
perhaps the following information is important:
I use the Arduino IDE version 1.0.5r2
For uploading the bootloader to a blank ATMega328P-PU I use the Sparkfun IVR ISP shield.
IFor this I upload the ArduinoISP Sketch to one Arduino, plug the Sparkfun Shield on the Arduino, choose as Board Arduino Uno and install bootloader

To upload a sketch to the ATMega328P-PU I use the same procedure - except the upload button. Here I use the Upload with programmer function in the Arduino IDE

You have grounded your A ref pin? Why? It isn't on the Uno R3.

Wire the arduino on a bread board and like the Shrip kit here, and set up an LED and resistor on what would be pin 13, and see if you can upload the blink sketch. Then try your circuit.

The Arduino board contains a 6 channel (8 channels on the Mini and Nano, 16 on the Mega), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. The input range and resolution can be changed using analogReference().
It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.

with an simple external 500kHz Receiver.

The Output of the receiver will be fed back to the arduino into the analouge A0 pin

at what rate ?

Hi Chilli,
Thanks for your reply!
YESSS, thats a really good question why I grounded AREF!
My answer is simple. I googled tons of forums and some of the entries hat the AREF on +VCC, some of the answers had a floating AREF and some of them had a grounded AREF (via 100nF).
The most "strongest" answers were to ground the AREF pin via 100nF.
To be honest, I do not know the really correct way to do it and I also do not know if this is important generally.

I will try your suggestion (shrimp) with the blink sketch on Tuesday evening and will get back to you.

best regards and thank you

ChilliTronix:
You have grounded your A ref pin? Why? It isn't on the Uno R3.

Wire the arduino on a bread board and like the Shrip kit here, and set up an LED and resistor on what would be pin 13, and see if you can upload the blink sketch. Then try your circuit.

Hi Raschemmel!
Thank you also for your reply!

I have to be more detailed! The 500kHz is the HF part. I use a TA7642 to demodulate the 500kHz AM signal to the NF signal. The NF signal is then fed in to a BC547 which makes the amplification of the NF signal. This signal is then fed into the Analog Pin A0 of the Arduino. In the moment with the ADC Prescaler the Arduino does about 30000 measurements per second.
best regards

raschemmel:
analogRead() - Arduino Reference

at what rate ?

Read this for barebones design considerations.

AtmelAVR042 AVR Design Considerations.pdf (236 KB)

Or, go here for a refresher on breadboarding an Arduino:
Nick Gammon's site

aVcc should be tied to Vcc. The 100nF cap can be placed as close to the AVcc & Ground as possible. On more sophisticated designs, an inductor bridges AVcc to Vcc and bypass caps are placed on both terminals of the inductor and the other ends are grounded.

Ray

Hi!
Thank you all for your thoughts and tips!
I had a couple of hours checking everything and found a capacitor on my PCB which was on a definitely wrong place. This was the problem.
So I can say that the ADC works perfect on my external PCB with the ATMega328P.
Sometimes the errors are to simple :wink:

The AREF topic is still not clear to me. The PCB runs with a open AREF port and it runs with a 100nF grounded port.

not sure what is the right way.

Anyway, thank you for your help and best regards!