threshold voltage level and blinking problem

I am using arduino pro mini 3.3 V,8 Mhz.vcc of the this arduino is coming from AC-DC converter with regulator 3.3 volt.I am listening analog data from voltage divider approximately 250 milivolts.

I want to basically,If analog voltage will reach 250 milivolts then led on.Otherwise led off.I am using avarage 100 ms for voltage data.

But,I have seen led on blinking for threshold voltage.Such as,The voltage level is changing 248-254.led on,led of,led on...I don't want to blink.

What can you do in such situations?Voltage level and slightly variable voltage?

Thanks

• The internal reference of 1.1v might be better as reference. For the blinking at the threshold, add hysteresis to your sketch.
– Jot
Commented Dec 8, 2018 at 18:09

What is the sensetive ac voltage stabilization method coming from voltage divider and it contains noise filter capacitor and take avarage?

Calculating an average by using 3/4 of the current average and 1/4 of the new measurement (exponential average) works well for me in a lot of situations. You can adjust the constants as you see fit, as long as they add to 1.0. I use powers of 2 and shifting for maximum performance:

``````// Exp. average: 3/4 old avg + 1/4 new data, with rounding
int16_t xpavg(int16_t newdat, int16_t avg){

// (3 * avg + 1 * newdat) / 4
return( (((avg<<2) - avg + newdat) + 2) >> 2 );
}
``````

As Jot mentioned in his comment you can write code to follow a hysteresis. This means, that the LED is only turned on, when the analog value is a bit above the threshold, and only turned off, when it's a bit below the threshold. You can see this behavior in Schmitt Triggers, which work that way (though they are getting the hysteresis via a corresponding electrical circuit, not through code). You wrote, your voltage readings are noisy in the range of about 10mV.

Depending on how your current code is structured, you can reach this goal by using if statements:

``````if( value > threshold + margin ){
digitalWrite(LED_pin, HIGH);
} else if( value < threshold - margin ) {
digitalWrite(LED_pin, LOW);
}
``````

You can choose a good value for margin (this depends on the desired behavior). Start with a margin of 7mV (which should filter out the noise of 10mV) and change it, until it works as intended.

• Thanks for answer.I want to ask twto question. 1)If margin coming slightly larger,what can be done?If we apply same think,the range will grow. 2)What is the sensetive ac voltage stabilization method coming from voltage divider and it contains noise filter capacitor and take avarage?I am searching software method? Commented Dec 8, 2018 at 19:20
• Sorry, I don't understand your questions (seems, that english is not your native language). The margin would be a constant, which you choose depending on the noise of your analog signal and the desired behavior. You can even define 2 margins for turning on and off at different margins. And what do you mean with the voltage divider and capacitor? You wrote, that you have a signal, that is changing between 248mV and 254mV (noise). What I proposed is a software method. Commented Dec 8, 2018 at 19:28
• I am sorry for grammar mistakes.If noise range of the voltage datas 248 mv-258mv then you are true.I can add margin to my code.But,such as If noise range of the voltage datas 190mv-300mv,What can i do?If I add to margin my code,it will be a waste of time for the switch threshold voltage level.Thanks. Commented Dec 9, 2018 at 11:27
• If my proposal is too much of a compromise, you only can try to reduce the noise of your signal. Reducing noise in an analog signal can be a complex thing. You can add a low pass filter (combination of capacitor and resistor). You can try to isolate the electronics from the source of the noise (can be a running motor, a power line nearby, etc...). You can try to convert the pure analog signal to a differential signal for the distance from the source to the Arduinos pins (noise gets induced in both lines the same). Difficult to answer, without knowing more about the appliance. Commented Dec 9, 2018 at 11:38