Plz guide code that Aruduino MEGA can read from ADC converter

Plz guide code that Aruduino MEGA can read from ADC converter ?

ADC : ADCS7478

ex)
#include <SPI.h> // include the SPI library
void setup()
{
Serial.begin(9600);
SPI.begin();
digitalWrite(SS,HIGH);
SPI.setBitOrder(LSBFIRST);
SPI.setDataMode(SPI_MODE1);
SPI.setClockDivider(SPI_CLOCK_DIV16);

}

void loop() {

Serial.print();
}

from the datasheet, IMHO there is no need to used SPI with this device.

something like this may work to get the ADC read:
(Compiles, NOT tested!)

#define CS_PIN 7
#define CLK_PIN 8
#define DATA_PIN 9

uint8_t read_ad7478(){
  int temp;
  digitalWrite(CLK_PIN,LOW);
  digitalWrite(CS_PIN,LOW);
  
  for( int i=0; i<2; ++i){ //2 loop since fist data received is unstable according to datasheet
    temp = 0;
    for( int j=0; j<16; ++j){ //loop to receive ADC value
      digitalWrite(CLK_PIN,LOW);
      digitalWrite(CLK_PIN,HIGH);
      temp = ((temp<<1) | digitalRead(DATA_PIN));
    }
  }
 
  digitalWrite(CS_PIN,HIGH);
  
  temp >>=4;

  return (uint8_t) temp;

}

void setup() {
  Serial.begin(115200);
  pinMode(DATA_PIN,INPUT_PULLUP);
  pinMode(CLK_PIN, OUTPUT);
  
  pinMode(CS_PIN, OUTPUT);
  digitalWrite(CS_PIN,HIGH);

  Serial.println("AD7478 sample Code");
}

void loop() {
  uint8_t adc_val = read_ad7478();

  Serial.print("adc value = ");
  Serial.println(adc_val);

  delay(500); //perform ADC read every 500ms
}

if you keep the CS pin permanently low then you can have 'continuous' ADC in IMHO ie only very first read would need to be rejected and all subsequent ADC read should be valid.

hope that helps...

Thanks for your code. but I'm not good at sw code.
So, could u help me for SPI code ?
Thanks

@orakai

as this is a common habit if somebody posts for the first time to write

"welcome to the arduino-Forum"

having fullfilled this habit I get straight to the point:

To me you gave a pretty negative image of yourself and I want to explain why

  1. you did not read "how to get the best out of this forum
  2. you used "plz" instead of taking the effort of writing "please" in the titel
  3. you posted a code that is below a compilable minimum
  4. you did not post a link to the datasheet of your ADC-chip
  5. you did not post the code as a code-section
  6. you wrote a few poor words

which again shows that you try to minimise your own effort and to maximise the work for your potenttal helpers

Your next answer is

OK at least saying "thank you"

Still using such shortaged words like "u" "sw code" again demonstrates lazyness instead of own effort.

The standard-reaction to such postings is:
no reaction at all
or
a very short - not very helpful - answer with links where you can read up how it is done yourself

english seems not to be your native language. If your english is limited you should use google-translate. Limited english and not using google-translate to me is arrogant

so to change your picture from rather negative to much more positive you should really read and then obey the recommendations given here.

best regards Stefan

2 Likes

how 'easier' would that be for you if we used SPI in the code?! code is code! :flushed:

did you tried the shared code atleast... :thinking:

Means your code will never return anything greater that 255. What is the point of a 10 or 12 bit A/D returning only a byte?

1 Like

image

kindly please check the datasheet vs OP (post #1)....

Kindly please check what is the point of using an 8 bit external A/D when the Mega already has many 10 bit A/D converters. The only point in using an external A/D is to get better resolution than you get for free.

But technically you are perfectly correct.

1 Like

Hello
I made a code that can read the data of the ADCS7478 part(I attached it) from Arduino.
I'm still studying because I'm a beginner and I have a lot of room for improvement.
Any advice regarding the code would be appreciated.

#include <SPI.h> // include the SPI library

void setup() {
Serial.begin(9600); //Initalise port to print in serial terminal

SPI.begin();
SPI.setBitOrder(LSBFIRST);
SPI.setDataMode(SPI_MODE3); //CPOL=1, CPHA=1
SPI.setClockDivider(SPI_CLOCK_DIV8); // Sets clock for SPI communication at 8 (16/8=2Mhz)

pinMode(SS,OUTPUT); // Set SPI slave select pin as output
digitalWrite(SS,HIGH); // disable Slave Select

}

void loop() {
digitalWrite(SS,LOW);//Enables SPI

{
byte receivedData=SPI.transfer(0x00); //Send the mastersend value to slave also receives value from slave
Serial.print(receivedData);
}

digitalWrite(SS,high);//Disables SPI
delay(1000); // delay in between reads for stability

}
adcs7478_ADC.pdf (1.7 MB)
adcs7478_ADC.pdf (1.7 MB)

I realized a lot of things I did wrong. Thank you for your advice.

I'm sorry sir. I attached code.

#include <SPI.h> // include the SPI library

void setup() {
Serial.begin(9600); //Initalise port to print in serial terminal

SPI.begin();
SPI.setBitOrder(LSBFIRST);
SPI.setDataMode(SPI_MODE3); //CPOL=1, CPHA=1
SPI.setClockDivider(SPI_CLOCK_DIV8); // Sets clock for SPI communication at 8 (16/8=2Mhz)

pinMode(SS,OUTPUT); // Set SPI slave select pin as output
digitalWrite(SS,HIGH); // disable Slave Select

}

void loop() {
digitalWrite(SS,LOW);//Enables SPI

{
byte receivedData=SPI.transfer(0x00); //Send the mastersend value to slave also receives value from slave
Serial.print(receivedData);
}

digitalWrite(SS,high);//Disables SPI
delay(1000); // delay in between reads for stability

}

void loop() {
  digitalWrite(SS,LOW);//Enables SPI

// {    You do not need this brace.
  byte receivedData=SPI.transfer(0x00); //Send the mastersend value to slave also receives value from slave
//  Serial.print(receivedData);
  Serial.println(receivedData);
//}    You do not need this brace.

  digitalWrite(SS,high);//Disables SPI
  delay(1000); // delay in between reads for stability

}

What do you mean by "read data from Arduino Mega", Do you mean you want to capture the output from the print statements?

If so, you can:

  • Use the Arduino Serial monitor, then copy/paste the content to a text editor.
  • Use a terminal program with a log to file capability (e.g. putty)
  • Use a custom program that you write (e.g. in python, C, Java, Processing or heaps more options) read from the serial port and write the data to a file directly.

If you mean something else, please be more clear as to what you mean by "read data from...".

Also, I've modified your code a little:

  • You won't need the two braces that I have commented out - they add no value and have no effect on the logic of your program.
  • You probably want println (not print). Otherwise all of your output will be clumped together on a single very long line - e.g. allclumpedtogetheronasingleverylonglinemakingitdifficulttoreadlateron.

Hi,

To add code please click this link;

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Why are you posting this twice:

Repeated post

Are you hoping to get a second (different?) opinion?

Anyway, here is my reply from the other copy of this post:

void loop() {
  digitalWrite(SS,LOW);//Enables SPI

// {    You do not need this brace.
  byte receivedData=SPI.transfer(0x00); //Send the mastersend value to slave also receives value from slave
//  Serial.print(receivedData);
  Serial.println(receivedData);
//}    You do not need this brace.

  digitalWrite(SS,high);//Disables SPI
  delay(1000); // delay in between reads for stability

}

What do you mean by "read data from Arduino Mega", Do you mean you want to capture the output from the print statements?

If so, you can:

  • Use the Arduino Serial monitor, then copy/paste the content to a text editor.
  • Use a terminal program with a log to file capability (e.g. putty)
  • Use a custom program that you write (e.g. in python, C, Java, Processing or heaps more options) read from the serial port and write the data to a file directly.

If you mean something else, please be more clear as to what you mean by "read data from...".

Also, I've modified your code a little:

  • You won't need the two braces that I have commented out - they add no value and have no effect on the logic of your program.
  • You probably want println (not print). Otherwise all of your output will be clumped together on a single very long line - e.g. allclumpedtogetheronasingleverylonglinemakingitdifficulttoreadlateron.

Hi,
Do you have a particular application for your project, or is it just experimental?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

But you did attach the code not as a code-section.
Read this tutorial how to attach code as a code-section .

and as a general hint:
If you want to speed up finishing your project
now you should really take 20 minutes of your real expensive time where each minute is worth one million dollars
and finally read

best regards Stefan


```cpp
#include <SPI.h>  // include the SPI library

void setup() {
  Serial.begin(9600);  //Initalise port to print in serial terminal

  SPI.begin();
  SPI.setBitOrder(LSBFIRST);
  SPI.setDataMode(SPI_MODE3);           //CPOL=1, CPHA=1
  SPI.setClockDivider(SPI_CLOCK_DIV8);  // Sets clock for SPI communication at 8 (16/8=2Mhz)

  pinMode(SS, OUTPUT);     // Set SPI slave select pin as output
  digitalWrite(SS, HIGH);  // disable Slave Select
}

void loop() {
  digitalWrite(SS, LOW);  //Enables SPI


  byte receivedData = SPI.transfer(0x00);  //Send the mastersend value to slave also receives value from slave
  Serial.println(receivedData);


  digitalWrite(SS, high);  //Disables SPI
  delay(1000);             // delay in between reads for stability
}

hello it is particular application.
It is a system called wireless power transmission to convert analog information containing phase and amplitude information into digital and process the data.

I am planning to do FFT after reading digital 8bit data from ADC in Arduino. Which method would be convenient?

  1. Use the Arduino Serial monitor, then copy/paste the content to a text editor.
  2. Use a terminal program with a log to file capability (e.g. putty)
  3. Use a custom program that you write (e.g. in python, C, Java, Processing or heaps more options) read from the serial port and write the data to a file directly.

Thanks you

I'll keep that in mind.