Simultaneously do action and read Analog Input?

I’m building a model combustion engine. It does not work perfectly; on occasion the sparkplug does not ignite the fuel mixture. I bought an Arduino-compatible flame sensor. An ATTINY controls the sparkplug and flame sensor. What I want to do, is when the sparkplug goes off, to simultaneously take a reading of the flame-sensor. All of my attempts to do this have failed, as the sensor always returns a no-flame value.

I know the sensor works; I’ve tried some test programs and they work fine. And there is a little LED on the sensor that turns on when it detects a flame, and that LED does flash when the engine fires properly (but this value is not passed along).
The sensor returns a proper “is-flame” value when I hold a match in front of the sensor when the engine is firing, so it must be a timing issue.

Code segment:
digitalWrite(sparkPlugPin, HIGH);
flameSensorValue = analogRead(flameSensorPin);
if (flameSensorValue <= 400) {[audio beep]};

I never hear any beeps (except if I hold a lit match in front of the sensor).
It takes about 100 microseconds to read an analog input, and I think this delay must be responsible for my missed readings. So do I need to somehow read the sensor pin 100 microseconds before I initiate the sparkplug?

Does anyone have any ideas how I can properly get my readings? Remember I’m using a ATTINY so I can’t use extension libraries.

How long does it take for the flame to grow from the spark to big enough that the sensor can detect it? How long does it take for the sensor's output to pass your detection threshold once it has a flame in front of it?

I think you're reading it too fast. Try a delay of a millisecond or ten and see if you get better readings.

Alternatively use an analog comparator to threshold the flame sensor reading and feed that to a digital input.

MorganS:
How long does it take for the flame to grow from the spark to big enough that the sensor can detect it?

Fast. It’s a very small engine. So I’m guessing… near instantaneous?

MorganS:
How long does it take for the sensor’s output to pass your detection threshold once it has a flame in front of it?

The little LED on the sensor lights up on a successful engine light. So I don’t know how long exactly, but the micro-explosion is long enough and strong enough to be detected by the sensor.

MorganS:
I think you’re reading it too fast. Try a delay of a millisecond or ten and see if you get better readings.

That was my first guess. So I built an array:

Code Segment:
digitalWrite(sparkPlugPin, HIGH);
int flameSensorValueArray[51];
for (int i=1; i <= 50; i++){
fSensor = analogRead(flameSensorPin);

  • }*
  • for (int j=1; j <= 50; j++){*
  • if (flameSensorValueArray[j] <= 400)*
  • [audio beep code];*
  • }*
  • }*
    So beep if flame is detected between 100 microseconds and 5000 microseconds after the sparkplug goes off. And… no beeps. Unless I hold a lit match in front of the sensor… then 50 beeps. I also tried delays in 1,2,3,4 milliseconds… no beeps.
    > MorganS:
    > Alternatively use an analog comparator to threshold the flame sensor reading and feed that to a digital input.
    I have no idea what you mean. Can you please explain this in detail please?

There’s no such thing as instantaneous. Flames take time to burn from the initial (tiny) spark. The sensor takes time to respond - it’ can’t instantly report there’s a flame in front of it. Light has a defined speed although you could consider it close enough to instant for this purpose.

Your for() loop has no control over the timing. You are relying on the un-measured delay of analogRead(). Use a method that lets you actually control the timing in milliseconds or microseconds.

I still think you’re reading it too fast. 50ms might be too fast. That looks instant to your eye but it’s really a long time for sensors and electronics.

It’s possible that the sensor output is too short for the Arduino to detect. If it goes ‘on’ and ‘off’ again within 100us then analogRead() won’t see it. To capture this, you need to turn it into a digital input. A comparator is a type of opamp which has a simple digital output depending on the relative voltage of its two inputs. You set a constant reference voltage (2V might be appropriate, based on your magic number of 400 in the code) with a pair of resistors and connect that to one input of the comparator. Then the other input goes to the analog sensor. If it detects a flame, the digital input is raised.

In this way it’s possible to detect something which is as fast as one clock period of the Arduino processor (16MHz). You would need to use an interrupt pin for this and a little more work programming an interrupt. With more analog circuitry to “stretch” the input pulse, you can detect even shorter events.

MorganS: I still think you're reading it too fast. 50ms might be too fast. That looks instant to your eye but it's really a long time for sensors and electronics.

Thank you, you helped me solve my problem :) Readings are now coming in, at around 3 milliseconds after the sparkplug goes off (but it varies a bit, plus or minus a few milliseconds). Not sure why it wasn't working before; maybe because I was using Delay() and this time I'm using DelayMicroseconds().

MorganS: It's possible that the sensor output is too short for the Arduino to detect. If it goes 'on' and 'off' again within 100us then analogRead() won't see it. To capture this, you need to turn it into a digital input. A comparator is a type of opamp which has a simple digital output depending on the relative voltage of its two inputs. You set a constant reference voltage (2V might be appropriate, based on your magic number of 400 in the code) with a pair of resistors and connect that to one input of the comparator. Then the other input goes to the analog sensor. If it detects a flame, the digital input is raised.

I would like to understand this (but I'm still not). Do you know of a link to a tutorial or full example somewhere?