Unequal Time Readings Arduino UNO

I am using an Arduino UNO to read the voltage of two devices, aiming to synchronize them. I am using a digital pin of the Arduino to read voltage from Device A and an analog pin to read voltage from Device’s B Torque analog pin.

I measure time using a Real-Time Clock (Adafruit DS3231).

While writing the Arduino script's first draft, I connected a potentiometer to a 3.3 voltage source. By turning the control knob of the potentiometer, I created different analog voltage inputs simulating the behavior/readings of the Device's B analog Torque pin when the participant applies Torque. For example, when the control knob was turned all the way in one direction, 0 volts were going to the analog pin. I interpreted that as the participant was applying zero Torque in Device B. When the control knob was turned all the way in the other direction, 3.3 volts were going to the pin, and I interpreted that as the participant was applying max Torque in Device B.

During this process, everything looked good. The Arduino's recording time matched the recording time of Device A (Please see Figure 1).

Figure 1 shows that I recorded data from Device A for 20 (sec) during a 60 (sec) period, so I was expecting to see a 20 (sec) 'ON' window in the Arduino data (Panel A). Also, I was expecting that the time file of the Device A software would be 20 (sec) long (Panel B). Indeed, the Pane A. displays that the reading of the Arduino's digital pin was 20 (sec), the Pane B. displays that the readings from Device A software were 20 (sec), and the Panel C displays that the time series of the two devices are matched.

However, when I replaced the potentiometer with the Device B. The Device's A recording time does not match the recording time of Arduino's digital pin. This is mainly because the Arduino reads the Ultrasound as 'ON' earlier than Device'A software (left box) (Please see Figure 2).

Do you have any ideas why this is happening?

P.s. Please let me know if I have not provided enough information and you need more.

Thank you
Nikos

Figure 1

Figure 2

Can you please show us the code you are using to read the voltage on your digital pin? Are you sure you can do that? Usually a digital pin returns a 0 or a 1 depending on the voltage being 0 or being above some certain limit, then it returns a 1.
Paul

My bad, let me clarify. When I said reading voltage on the digital pin, I meant 0 or 1.
In other words, when the user turns ‘ON’ the Device A, the digital pin reads 1 (Figure1, panel A, y-axis labels as Device =ON).

So yes, you are right. Sorry for this miscommunication.

Additionally, I forgot to mention that I am reading the data using PuTTY.

Below is an example of the Arduino Code

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"

RTC_DS3231 rtc;

float ZeroVoltageV = 0.00;
int ZeroVoltageU = 0;
int Ultra = 8;

void setup () {
  Serial.begin(115200);

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
}

void loop () {
  DateTime now = rtc.now();
  int sensorValueV = analogRead(A1); // Velocity
  float voltageV = sensorValueV * (3.3 / 1024.0);

  int UltraVal = digitalRead (Ultra);

  if (UltraVal == 1 && voltageV <1.09) {
    Serial.print(ZeroVoltageV);
    Serial.print(2); // Ulrasound ON-OFF event
    Serial.print(now.second(), DEC);
    Serial.println();

  }
  else if (voltageV >= 1.09 && UltraVal == 1) {

    Serial.print(voltageV);
    Serial.print(UltraVal);
    Serial.print(now.second(), DEC);
    Serial.println();


  }
  else {

    Serial.print(ZeroVoltageV);
    Serial.print(ZeroVoltageU);
    Serial.print(now.second(), DEC);
    Serial.println();

  }
}

Thank you
Nikos

Did you actually verify those values in a test program before you made the assumption in your final code? A potentiometer will likely never show 0 voltage and may never show your maximum voltage. You need to calibrate you device.
Paul

Thanks for the response.

Yes, I did. However, the timing issue is in the digital pin while the potentiometer is connected with the analog pin. I connected the potentiometer to simulate Device B's voltage behavior (i.e., fluctuations). So I do not get under which condition the calibration of Device B (analog pin) will influence the recording time of Device A in the digital pin.

Nikos

What is Device A? How do you know the on signal is instantaneous? Please look at the data sheet for the Uno processor and see the voltage level when a pin returns a 1 and when it returns a 0. What voltage are you expecting on that pin? If is a slowly ramping signal, then big effect on your timing.
Paul

Device A is an ultrasound device, and I expect 5V or 0V.

It is rapid. Panel C of Figure 1 shows no delay (i.e., rapid signal). This is also in line with what I understand reading the manual.

Although, when I am connecting Device B, a dynamometer, in the analog pin, it seems that the signal of the Ultrasound (Device B) changes behavior and ramping slowly, as you said (Figure 2).

Any ideas why this is happening?

Thank you for your time

Nikos

It's your device.

I would print the raw voltage read from the analog port and your “actual” volts .There maybe something going wrong with your conversion to volts ???

I wouldn’t bother converting to actual volts any way , just use the raw analog values , quicker easier .

Which of the three devices (Arduino, Device A, or Device B)?

Thanks for the reply. Do you think the time delay is caused due to conversion to volts?

Sorry, but mu test equipment cannot reach that far.

Probably not to a noticeable effect - but it could be a source of your problem , so take it out for now and just print out your values and take it from there .

( if you are comparing with , say 1volt on a 3.3 v max input then 1v is about “300” , so read your analog input as an int and just compare to 300)

Solved.

It was a coding issue.
I wrote the simplest code that could read and print voltage intervals using millis().

Thank you
Nikos

p.s. This solution does not answer why the time issue appeared only when Device B was connected. I will keep looking at that.