Arduino UNO sampling rate


I have a project in which a sinusoidal voltage is measured using Arduino UNO and the data should be saved in a text file that contains the measured voltage and the time ( or index) in which each reading is taken.

I already have the code that measures the voltage, which I got from Arduino examples.

As shown below:

  Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

 This example code is in the public domain.

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:

My question is that is there any way to set the sampling rate to be 2400 samples/s ? Also how can I save each reading in a text file?

I would be grateful if anyone could help me.


To control the sampling rate, use the approach demonstrated in the 'blink without delay' example sketch to make the voltage reading code run at regular intervals.

Look at the example sketches for the SD library to see how to create a file and write text to it.

Please use [ code ] [ /code ] tags when posting code, to prevent the forum software from munging it.

Thanks alot for your assistance. I really appreciate that. I'll look at the examples right now. Also thank you for notifying me about the code. I'm new here and that was the first post.