I am producing a data stream and feeding to a gpio pin. THe data is sampled at 2kbps. i want to dump this data into a file after a certain length of reading . how should i proceed with this ?

  • 1
    Not without seeing your code we can't.
    – Majenko
    Commented Jul 8, 2016 at 9:04
  • 1
    My guess is that you are reading the port to often and not a multiple of the frequency of the wave. So if your wave is 1Hz and you want to see 101010 then read at 1Hz not 4.25Hz. Commented Jul 8, 2016 at 9:25
  • You could clarify your question by describing the frequency of your test signal, and the sampling frequency. Unless you have an accurate reference clock (connecting the source and the sampling), you will need to oversample the signal, at least twice the toggle frequency, otherwise you will get a misleading result. Commented Jul 8, 2016 at 16:59

1 Answer 1


Without seeing your code it's impossible to tell just what is happening, but I would guess you are reading and printing the level of the wave, not the transitions from one level to another.

For instance:

enter image description here

As you can see, just reading regularly and printing it (which itself is quite slow) results in 11001101 rather than the 10101 that you want. That's because the speed of your reading/printing and the frequency of your wave are completely separate things and out of step with each other. There's nothing to tie one to the other.

Instead you need to look for the transitions from LOW to HIGH and from HIGH to LOW. Only when the input changes should you look to see what the new level is and print that.

enter image description here

As you can see the 1 and 0 are just after the change in level, since it's impossible to do anything during a change, only once the change has happened.

You might code that as something like:

void setup() {
    pinMode(3, INPUT);

void loop() {
    static byte oldValue = LOW;     // Place to store the previous level
    byte newValue = digitalRead(3); // Read the current level
    if (newValue != oldValue) {     // The input has changed
        oldValue = newValue;        // Save the current level
        Serial.print(newValue);     // Print it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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