ADC operation on ARDUINO Nano with ATmega168PA microprocessor

Hi! I am using ARDUINO NANO with ATmega168PA microprocessor. I am now taking oscillograms through the ATmega168PA ADC. I read various literature on this topic, but did not find the answer to my question. The fact is that I noticed that oscillograms of different shapes are shown on my DSO150 oscilloscope and in the EXCEL table (the data is transferred to EXCEL from ARDUINO NANO via the COM port). I am attaching an PDF file to my theme.
PLX-DAQ_for_forum.pdf (204.5 KB)
I also attach a photo of the waveform on my FNIRSI DSO 150 oscilloscope.


My ARDUINO code is the following:

uint16_t/*unsigned int*/ u;
int row_excel;
int values[100] = {};
int interruptArray[100] = {};
int oneValue;
int i;
void setup() {


  cli();                  // instruction to disable interrupts immediately
  TCCR1A = 0b00000000;    // OC1A Disabled
  TCCR1B = 0b00001010;    // divider 8, match reset 1A (CTC mode)
  OCR1A = 0x07D0;         //0x07D0 1ms 0x0014 100us    0x00FA      1ms
  TIMSK1 = 0b0000010;     // start interrupt A timer 1

  sei();                  // instruction to enable interrupts

  SMCR |= (1 << SM1) | (1 << SM0) | (0 << SM2);
  /*** Настройка АЦП ***/
  ADCSRA |= (1 << ADEN) // Enable ADC

            | (1 << ADPS2) | (0 << ADPS1) | (1 << ADPS0); // converter prescaler by 128
  ADMUX |= (0 << REFS1) | (1 << REFS0) // supply voltage AVcc
           // | (1 << ADLAR)
           | (0 << MUX0) | (0 << MUX1) | (0 << MUX2) | (0 << MUX3); //input PC1

  Serial.begin(128000);     // setting the port speed (bps, baud)
  Serial.println("CLEARDATA"); // cleanup excel sheet
  Serial.println("LABEL,Time,DATA,NumInterrupt,VOLTAGE"); // column headings
}

void loop() {

  row_excel++; // line number + 1
  Serial.print("DATA, TIME,"); // record in excel current date and time
  int j = 0;

  oneValue = values[j];
  Serial.print(oneValue);
  Serial.print(",");
  int oneNumInterrupt = interruptArray[j];
  Serial.println(oneNumInterrupt);

  j++;
  Serial.println(row_excel);

}


ISR(TIMER1_COMPA_vect) {      //timer 1 coincident interrupt handler A, counter reset to 0



  ADCSRA |= (1 << ADSC)    // Let's start the transformation
            /*| (0 << ADPS0) | (1 << ADPS1) |(0 << ADPS2)*/ ; // divisor by 8

  while ((ADCSRA & (1 << ADIF)) == 0); // Waiting for the conversion completion flag
  //u = ADCH << 2 | ADCL >> 6;
  //uint8_t theLOW = ADCL;
  //u = ( /*ADCL*/  ADCH << 8 | theLOW ); // Reading ADC
  u = ADC;

  //u = (uint16_t) (ADCL + (ADCH*256));

  //u = ADCL + (((uint16_t)(ADCH))<<8);
  i = 0;
  
  values[i] = u;
  interruptArray[i] = i;
  //values[i] = u;

  i++;
  
}

Tell me why the different shape of the oscillograms? I have a feeling that my information is being lost somewhere.

Which Arduino pin is connected to the oscilloscope ?
Are you expecting that a reading from the ADC is somehow going to condition the output which you see on the oscilloscope ?

You don't appear to be guarding against overflows :

i++  // i is int ;

I am using pin A0 for ADC.
I want my Arduino Nano with the ATmega168PA microcontroller to give the same waveform in shape and values ​​as my oscilloscope.
Yes, I don't overflow the i variable. This variable is used to output data to the values[ ] array.

OK. You have then an external signal generator creating a wave form.
You are simultaneously displaying that wave form on your oscilloscope and feeding it into analog pin A0 in your Nano (Atmega168).
You are sampling that waveform on the Nano (taking 100 samples) and plotting the results using a plotting program.
You are puzzled that the graph so plotted does not match the trace on the oscilloscope.
Is that more or less it ?

Show the connection (circuit) between the signal generator and the scope/nano. Say also what sort of signal it is. To me it looks a bit like the top half of a sine wave missing the negative component.

running the following code on a UNO

// UNO timer1 interrupts reading ADC

// interupt every .1mSec - 10000 Samples per second

#include <TimerOne.h>

const int LED_pin = 13;
int analogPin = 0;

volatile unsigned long int count;

#define SAMPLES 500
int data[SAMPLES + 100] = {0};
volatile int index = 0, done = 0, i;

// called on timer1 interrupt
void printFunc(void) {
  sei();                          // enable interrupts
  if (index < SAMPLES)            // acquire samples ?
    data[index++] = analogRead(analogPin);  // read the input pin
  else {
    if (done == 0) done = 1;
    count++;
  }
}

void setup()
{
  Serial.begin(115200);
  pinMode(LED_pin, OUTPUT);
  digitalWrite(LED_pin, LOW);
  Timer1.initialize(100); //Initialize timer with .1 mSec second period
  Timer1.attachInterrupt(printFunc);
  // Serial.print("TIMER1 Setup Finished. reload = ");
}

void loop()
{
  static long start = millis();
  while (done == 0) ; //return;     // if acquisition complete return
  static boolean output = HIGH;
  if (count == 10000) {
    digitalWrite(LED_pin, output);
    count = 0;
    output = !output;
  }
  if (done == 2) return;   // if acquisition complete return
  /* Serial.print("Time for 500 samples ");
  Serial.print(start = millis() - start);
  Serial.print("mSec  ");
  Serial.print(SAMPLES * 1000L / start);
  Serial.println(" Samples per second");
*/  for (i = 0; i < SAMPLES; i++) // print samples for plot
  {
    Serial.print(i); Serial.print(' ');
    Serial.println(data[i]);
  }
  // return;       // required for plot - comment out for following print
  done = 2;
}

scope displays
image

the serial plotter displays

gives similar results on a Nano with a ATmega328P

If it is a lowish frequency sine wave, I'd probably connect the signal generator to the Nano / Oscilloscope like this:

I am attaching a wiring diagram. This is indeed a half-wave after the diode has rectified the signal.
Wiring diagram for connecting a transformer to an arduino ADC.pdf (15.7 KB)

I uploaded your code to my arduino nano ATmega328P. The result is provided in the image.

Very similar to my code. I don't understand why it didn't work for me like you?

I am using a signal generator to give the half sine waves

if you attach your scope to A0 what does it show?

if I attach my probe to A0 it will show this:

Everything shows well on the oscilloscope, my waveform, which is taken by the Arduino controller, does not get tired of me.

strange that the scope shows a half sine wave signal yet the Arduino displays the result in post #8
I will see if I can replicate your test circuit

Thank you! I think that it would help to solve such a problem faster.

In this circuit the diode presents a high impedance to discharging the parasitic capacitance on the A0 input pin. Try the circuit with a 10k Ohm resistor added between A0 and GND.

tested using circuit

scope displays

and Arduino Serial Plotter displays

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