Trigger and output

Hi,
I want to use ARDUINO UNO R3 to realize a function. I connect the an analog input DC source to the board, and when the analog input DC source is smaller than a threshold voltage, the board will generate a digital voltage at PWM ports with specific delay. what is the delay from the board detecting the threshold voltage to generate a digital voltage? Can it be smaller than 1ms? Can someone give me some advice? I really appreciate it. Thanks.

Here is a link to Nick Gammon's excellent tutorial on analogRead() speed and other issues.Gammon Forum : Electronics : Microprocessors : ADC conversion on the Arduino (analogRead)

what is the delay from the board detecting the threshold voltage to generate a digital voltage? Can it be smaller than 1ms?

Yes, the default analogRead() is around 110 microseconds. There will be other code required to compare to a threshold and write the output, but I think that you will have no trouble being under 1ms.

If you write and post some specific code, I'm sure this forum will help you get a good measurement of execution speed.

Thanks for your prompt reply and information. Do you have some link for the code to compare with a threshold voltage?

Do you have some link for the code to compare with a threshold voltage?

Take a look at the IfStatementConditional example in the ide.

File>Examples>05Control>IfStatementConditional

Thanks for your information. I have wrote two programs. The delay from the board detecting the threshold voltage to generate the specific value voltage is much bigger than what I expected.

The first program:
const int analogPin = A0; // pin that the sensor is attached to
const int ledPin = 5; // pin that the LED is attached to
const int threshold = 500; // an arbitrary threshold level that’s in the range of the analog input
int val=0;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}

void loop() {
// read the value of the potentiometer:
val = analogRead(analogPin);

// if the analog value is high enough, turn on the LED:
if (val< threshold) {
analogWrite(ledPin, 255);
} else {
analogWrite(ledPin, 0);
}

// print the analog value:
Serial.println(val);
delay(0.1); // delay in between reads for stability
}

For the first program, the delay is about 4ms.

Second program:

const int analogPin = A0; // pin that the sensor is attached to
const int ledPin = 5; // pin that the LED is attached to
const int threshold = 500; // an arbitrary threshold level that’s in the range of the analog input
int val=0;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}

void loop() {
// read the value of the potentiometer:
val = analogRead(analogPin);

delay(200);

// if the analog value is high enough, turn on the LED:
if (val< threshold) {
analogWrite(ledPin, 255);
} else {
analogWrite(ledPin, 0);
}

// print the analog value:
Serial.println(val);
delay(1); // delay in between reads for stability
}

For the second program, the delay is about 350ms.

Could someone give me some advice to decrease the delay less than 1ms?

I’m not sure how you benchmarked you loops, but here is a modified version of your sketch for you to try.

Here are some observations about what you have done.

  1. learn how to use the code tags found at the icon </> at the top left of the toolbar. Post you code within the words which come up
so your code looks like this
  1. Serial printing is slow. In the sketch I have attached, see the effect of changing the baud rate, and eliminating the printing.

  2. delay(.1) is not valid syntax. If you want a short delay, use delayMicroseconds().

const byte analogPin = A4;
const int ledPin = 5;
const int threshold = 500;
int val = 0;

const int iterations = 1000;
unsigned long totals;

void setup() {
  pinMode(ledPin, OUTPUT);
 //Serial.begin(9600);
 Serial.begin(115200);
}

void loop() {
  unsigned long startTime = micros ();
  for (int i = 0; i < iterations; i++)
  {
    val = analogRead(analogPin);
    totals += val;

    if (val < threshold) {
      analogWrite(ledPin, 255);
    } else {
      analogWrite(ledPin, 0);
    }
    //Serial.println(val);
    //delayMicroseconds(100); 
  }

  unsigned long endTime = micros ();

  Serial.print ("Average result = ");
  Serial.println (totals / iterations);

  Serial.print ("Time taken = ");
  Serial.println(endTime - startTime);

  Serial.print ("Average Time taken = ");
  Serial.print((endTime - startTime) / iterations);
  Serial.println(" microseconds");
  while (1) {}; //halt loop  
}

Thanks for your help. I really appreciate it. When I changed the baud bit, the delay is decreased a lot. And now it is less than 1ms.