2

I have an Arduino code which runs decently.

// Read in 10-bits Magnetic Encoder AEAT-6010-A06  into Arduino Uno
// Sampling

// Declarate

const int CSn = 4; // Chip select
const int CLK = 7; // Clock signal
const int DO = 8; // Digital Output from the encoder which delivers me a 0 or 1, depending on the bar angle..
int analogPin = 3;
int val = 0;
unsigned int sensorWaarde = 0;

void setup() {
  Serial.begin(115200);
  //Fix de tris

  pinMode(CSn, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DO, INPUT);

  //Let's start here
  digitalWrite(CLK, HIGH);
  digitalWrite(CSn, HIGH);
}



void loop() {

  val = analogRead(analogPin);
  // val = val - 670;
  sensorWaarde = readSensor();
  delayMicroseconds(1); //Tcs waiting for another read in
}

unsigned int readSensor() {
  unsigned int dataOut = 0;

  digitalWrite(CSn, LOW);
  delayMicroseconds(1); //Waiting for Tclkfe

  //Passing 10 times, from 0 to 9
  for (int x = 0; x < 10; x++) {
    digitalWrite(CLK, LOW);
    delayMicroseconds(1); //Tclk/2
    digitalWrite(CLK, HIGH);
    delayMicroseconds(1); //Tdo valid, like Tclk/2
    dataOut = (dataOut << 1) | digitalRead(DO); //shift all the entering data to the left and past the pin state to it. 1e bit is MSB
  }

  digitalWrite(CSn, HIGH); //
  Serial.print(dataOut);
  Serial.print("");
  Serial.print(" ");
  Serial.println(val);
  Serial.print(" ");

  delay(2);
  return dataOut;

}

From the delay data i assumed that i will be having approximately a sampling rate of 500 Hz, however it seems that the sampling rate is approximately 300+- Hz, without a proper confirmation.

My system is to be run in a Magnetic Encoder by Avago, but i have difficulties determining the sampling rate.

If possible, may i know what code should i use to indicate the time and sampling number?

3

At 115200 baud your serial prints will take 1/11520 seconds per byte.


Serial.print("");

What does that do?


I count 14 bytes here:

  Serial.print(dataOut);  // 5 bytes, say
  Serial.print("");       // zero bytes, lol
  Serial.print(" ");      // one byte
  Serial.println(val);    // 5 bytes + cr/lf = 7 bytes
  Serial.print(" ");      // 1 byte

Multiply 1/11520 (0.0000868) by 14 and you get 1215 µs.


Now add in your delays:

  delay(2);  // 2000 µs

And this:

 for (int x = 0; x < 10; x++) {
    ...
    delayMicroseconds(1); //Tclk/2
    ...
    delayMicroseconds(1); //Tdo valid, like Tclk/2
  }

Another 20 µs.


  val = analogRead(analogPin);   // 104 µs
  ...
  delayMicroseconds(1); // 1 µs

Total:

1215 + 2000 + 20 + 104 + 1 = 3340 µs

Inverse:

1 / 0.003340 = 299

So yes, I would expect around 299 iterations per second. That isn't counting the time for the other code to execute.


Suggestions

  • Get rid of the serial prints
  • Get rid of delay(2)
| improve this answer | |
  • Wow, there's this kind of calculation. Thanks alot, that really2 helps alot!!! i just added, extra spaces for trial. Thank you – NickyNic Sep 8 '15 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.