Arduino monitoring my Triggerpin with kHz sampling rate

Hi all,

I am new to Arduino and I am trying to run a very simple datalogger which can record 4 analog inputs. Then on a second hand, I am sending a trigger with a pulse width 150 µs.

What I would like is monitoring my 4 outputs + the statut of my trigger (if it's high or not), in time. So I would need some synchronisation, I would say ~10µs ish

What I haven't found yet, is how to have my triggerPin, and records it value in time, like an oscilloscope would do (with a sampling rate of ~20 kHz at least). Just if someone has an idea about this one, it would be super useful.

Thanks

So many questions.

  • Which Arduino?
  • Four analog inputs, one digital output?
  • but wait, is it a digital input?
  • where's the data going?
1 Like

Your question is a bit confusing, please post an annotated schematic showing how you have wired the system. Be sure to show all connections, grounds, and power sources. Links to technical information on the hardware devices would also help.

Also post your code and note what works and what does not.

@camsysca @gilshultz

Sorry I did not want to write too much and I feel that by trying to do this, my query seems a bit confusing.

I am using an arduino giga r1

Here is the wiring:

4 BNC female connectors, connected to a basic voltage divider with 2 resistors to make sure that the output is between 0-2V. The - is connected to the GND, and the + to the different analog A0 to A3 from my arduino. Therefore, A0 to A3 are analog INPUT.

Then, on a second hand, I have 3.3V connected to another device to trigger, GND and pin D7 that I connect to my device to trigger it, so pin 7 is a digital OUTPUT.

The code is quite straightforward:

int Channel1 = A0;
int Channel2 = A1;
int Channel3 = A2;
int Channel4 = A3;
const int triggerPin = 7; // Pin connected to the trigger output


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial) {
    ; // Wait for serial port to connect. Needed for native USB
  }
  pinMode(triggerPin, OUTPUT); // Set the trigger pin as an output
}

void loop() {
  delayMicroseconds(50); //Wait 50 µs to fetch the data
  RunMeasurement();
}

void RunMeasurement() {
  int value1 = analogRead(Channel1);
  int value2 = analogRead(Channel2);
  int value3 = analogRead(Channel3);
  int value4 = analogRead(Channel4);

  Serial.print(micros());
  Serial.print(value1);
  Serial.print(",");
  Serial.print(value2);
  Serial.print(",");
  Serial.print(value3);
  Serial.print(",");
  Serial.println(value4);
}

Basically, I am fetching the data from my 4 analog (and it is working well) with the time t. It's a basic datalogger, and even if it's not perfectly synchronised I don't mind. As long as the delay between each of these values is constant, and I just have an offset, it's fine.

Now what I want, is while I am fetching these data, I would like to trigger the device with my PIN 7. Hence, writting something like this:

digitalWrite(triggerPin,HIGH);
delayMicroseconds(150);
digitalWrite(triggerPin,LOW);
delayMicrosecond(500); //Next trigger occurs in 500 µs

However, what I would like to do is:

  • Keep recording my data with ~20 kHz sampling rate (so delay of 50µs between each print) -> the data are going into my computer, and I just run the code for few seconds, so no problem of memory
  • Triggering my device, with a pulse width of ~150µs and a repetition rate of ~2kHz
  • Being able to record in time, my trigger, and my data, while these two steps above are running simulteanously.

I am not sure if it's possible with only 1 arduino, and if I am asking too many steps to run simulteanously.

Does it make sense?

Thanks,

It's likely not constant -- at 9600 baud, the first few observations will be quicker, and then the buffer will get filled up and it will slow down waiting for printing. Increase the baud to 115200250000. And add a comma between micros and the the observations.

You generate the trigger with your software per your picture (that is not a schematic) so you know the state of the trigger. You can read mills when starting the trigger and read it again when an event happens, subtract the starting mills and you have the time.

1 Like

@DaveX Yes true! Thanks!

@gilshultz No, I generate the trigger with the arduino with the code I wrote above


But yes I can read the time before and after my pulse to know when I start and stop the trigger. I don't know why I was fixated with the idea to read my trigger.

I will try to do something with that idea and see if I can end up with a datalogger. thanks

You can use digitalRead() on an output pin:

  Serial.print(micros());
  Serial.print(",");
  Serial.print(digitalRead(triggerPin));
  Serial.print(",");
  Serial.print(value1);
  Serial.print(",");
  Serial.print(value2);
  Serial.print(",");
  Serial.print(value3);
  Serial.print(",");
  Serial.print(value4);

And then the trick is to report in sync with the trigger toggling. That is doing a couple things at the same time, and it might be good to read up on:

I'd use micros() and a couple routines like:

uint32_t now = 0;
int triggerState = LOW;
...
void loop() {
  now = micros();
  RunTrigger();
  RunMeasurement();
}

void RunTrigger() {
  const uint32_t OnTime = 150, OffTime = 500;
  static uint32_t changeTime = 0;
  switch (triggerState) {
    case LOW:
      if (now - changeTime >= OffTime) {
        digitalWrite(triggerPin, HIGH);
        triggerState = HIGH;
        changeTime += OffTime;
      }
      break;
    case HIGH:
      if (now - changeTime >= OnTime) {
        digitalWrite(triggerPin, LOW);
        triggerState = LOW;
        changeTime += OnTime;
      }
      break;
  }
}

...where the ???Time+= ???? keeps the changes in sync with micros() and if you do something similar with RunMeasurement() the measurements will be in sync.

Still, 9600 baud isn't fast enough for the data you are asking to be printed.

(Multiple code typos corrected)

1 Like

That is called getting experience, we have all done it and generally over much simpler things.

1 Like

Thanks Dave!

I will have a look and I will read what you suggested. It's exactly what I am trying to do. For the baudrate, I am recording everything on a computer so I am more around 115200 baudrate, but I need to read a bit more about this baudrate too.

I will add the synchronisation bit to Runmeasurement() too. Quite smart idea to probe the delay over the loop. Thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.