SEND ADC DATA OVER SERIAL PORT OF ALL ANALOG PINS AT 256HZ SAMPLING RATE

Hello All,

Iam using Arduino UNO and Iam trying to read all the 6 analog input pins at a sampling rate of 256Hz per second and further send the data through serial communication to a visual basic application. My issue is that , Iam not able to achieve the correct speed of sending data and also I doubt my sampling rate code.I tried checking the sampling rate by toggling a digital pin inside the timer interrupt but Iam not getting the correct sampling rate. My Baud Rate is 57600bps. Below is my code.

#include "TimerOne.h"

int sensorPin1 = A0;     
int sensorPin2 = A1;
int sensorPin3 = A2;
int sensorPin4 = A3;
int sensorPin5 = A4;
int sensorPin6 = A5;
int ledPin = 13;      // select the pin for the LED
int sensorValue1 = 0;  // variable to store the value coming from the sensor
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;
int sensorValue5 = 0;
int sensorValue6 = 0;
int x  = 0;
float mytime = 0;
bool senddata = false;
char ch1;
char ch2;
char ch3;
char ch4;
char comm;
int y = 0;
int k = 1;

void setup() {
  // declare the ledPin as an OUTPUT:
   pinMode(ledPin, OUTPUT);
   Serial.begin(57600);

 cli();//stop interrupts
  //set timer1 interrupt at 256Hz
  TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
  TCNT1  = 0;//initialize counter value to 0
  // set timer count for 1khz increments
  OCR1A = 7812;// = (16*10^6) / (256*8) - 1 // 256 hz sampling rate
   // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS11 bit for 8 prescaler
  TCCR1B |= (1 << CS11);
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);
  sei();//allow interrupts
}

ISR(TIMER1_COMPA_vect)
{
  sensorValue1 = analogRead(sensorPin1);
   // delayMicroseconds(50); // delay the next analog read to remove crosstalk between pins
    sensorValue2 = analogRead(sensorPin2);
   // delayMicroseconds(50);// delay the next analog read to remove crosstalk between pins
    sensorValue3 = analogRead(sensorPin3);
   // delayMicroseconds(50);// delay the next analog read to remove crosstalk between pins
    sensorValue4 = analogRead(sensorPin4);
   // delayMicroseconds(50);// delay the next analog read to remove crosstalk between pins
    sensorValue5 = analogRead(sensorPin5);
   // delayMicroseconds(50);// delay the next analog read to remove crosstalk between pins
sensorValue6 = analogRead(sensorPin6);
  //  delayMicroseconds(50);// delay the next analog read to remove crosstalk between pins
    senddata = true;
    digitalWrite(ledPin,LOW);
}

void loop() {
  //  while (x == 256)
  //  {
  //
  //  }

  if (Serial.available() >0)
  {
    comm = (char)Serial.read(); // receive one byte from PC application to start sending data.

  }

if (comm == 'p') // send data only when you receive 'p' from the PC application
{
      if (senddata == true)
      {
        Serial.println(sensorValue1);
      Serial.println(sensorValue2);
      Serial.println(sensorValue3);
         Serial.println(sensorValue4);
         Serial.println(sensorValue5);
         Serial.println(sensorValue6);
         senddata = false;
         digitalWrite(ledPin,HIGH);
      }
}

}

is it possible to achieve 256 hz sampling rate at all the analog pins and send that data over serial port in arduino UNO?

Any help will be appreciated. Iam stuck in my project

Try 115200 baud. You need at least 77000 baud if all of your values are 4 digits.

You are waiting for the PC to send a 'p'. Is the PC doing that 256 times a second?

Hi ,

The PC sends 'p' only once to notify the Arduino to start sending data.

However when I toggle the led pin in timer interrupt it is not giving 256hz of toggling frequency. It is somewhere around 40 Hz only.

Don't have any delayMicroseconds() in an ISR.

...R

The code you show sets the ledPin low every run through the timer interrupt, as far as I can see it toggles only once after each request from the PC then remains in an off state.

My bad, I missed the bool pointed out in the following post

bool senddata = false;

Variables shared between interrupt routines and the main program must be declared “volatile” or changes in their values may be ignored.

Try

volatile bool senddata = false;