Threshold voltage of comparator

Hi, everyone. I want to know about the details that, how to set a particular value of lower threshold voltage of the internal comparator of Arduino Due microcontroller. This is because, my circuit will discharge only up to the lower threshold voltage of the comparator (not to zero voltage) and thus, i want to know the exact value. By default it is discharging up to around 1.8 volt (not exactly), however, i need precise value for it. I learn that it can be done by using control and status register for Arduino Uno. Can anyone please give the brief details regarding it.
Thanks in advance.

which comparator? do you mean a digital input or possibly analog?

digital inputs determine whether the input is HIGH/LOW based on a decision threshold similar in value to what you mentioned. that value is not software configurable

you can measure the input with an analog read having a resolution of 5V/1024 and use software to make a HIGH/LOW decision. suggest to aid hysteresis

What does the processor data sheet say (sometimes it's in the processor reference manual)?

I've never used the comparator feature (and I don't know anything about the Due) but it appears to be an actual analog comparator so you'd have to provide the 1.8V reference (or get it from the Due's DAC). Then the comparator can trigger an interrupt.

If you don't need an interrupt it's easy enough to read the analog input and use an if-statement to take action when you're above (or below) the threshold.

you can measure the input with an analog read having a resolution of 5V/1024 and use software to make a HIGH/LOW decision. suggest to aid hysteresis

The Due runs from 3.3V and it has a 12-bit ADC so (with the default reference) 3.3V reads 4095. (I know little about the Due...)

gcjr:
which comparator? do you mean a digital input or possibly analog?

digital inputs determine whether the input is HIGH/LOW based on a decision threshold similar in value to what you mentioned. that value is not software configurable

you can measure the input with an analog read having a resolution of 5V/1024 and use software to make a HIGH/LOW decision. suggest to aid hysteresis

The internal analog comparator..This comparator is present at the digital pins of the microcontroller.

suggest you read section 22 of the ATmega328p datasheet

You should share details of your circuit and your code if you want help. I had a look at the SAM data sheet, it seems like the comparator details are not published there - perhaps they are not considered relevant for normal operation.

gcjr:
suggest you read section 22 of the ATmega328p datasheet

OP stated in original post that he's using an Arduino Due. That board does not contain an ATmega328p. Rather it uses a ARM-based SAM3X8E .

We can edit the control and status register by changing the bandgap selector of the analog comparator. Please see this for arduino uno..https://www.google.com/amp/s/cadernodelaboratorio.com.br/en/the-voltage-comparator-on-the-atmega328/amp/

elangoece3691:
We can edit the control and status register by changing the bandgap selector of the analog comparator. Please see this for arduino uno..https://www.google.com/amp/s/cadernodelaboratorio.com.br/en/the-voltage-comparator-on-the-atmega328/amp/

What makes you believe that the Due processor has such a thing?

aarg:
What does the processor data sheet say (sometimes it's in the processor reference manual)?

DVDdoug:
I've never used the comparator feature (and I don't know anything about the Due) but it appears to be an actual analog comparator so you'd have to provide the 1.8V reference (or get it from the Due's DAC). Then the comparator can trigger an interrupt.

If you don't need an interrupt it's easy enough to read the analog input and use an if-statement to take action when you're above (or below) the threshold.
The Due runs from 3.3V and it has a 12-bit ADC so (with the default reference) 3.3V reads 4095. (I know little about the Due...)

It is correct but I don't want to use the internal ADC/DAC. One of the merit of my work is that my circuit will produce a digital output without using ADC.

aarg:
What makes you believe that the Due processor has such a thing?

The time resolution of due is better than uno.. i need this feature for my work..

43.6.7 Comparison Window
The ADC Controller features automatic comparison functions. It compares converted values to a low threshold or a
high threshold or both, according to the CMPMODE function chosen in the Extended Mode Register (ADC_EMR).
The comparison can be done on all channels or only on the channel specified in CMPSEL field of ADC_EMR. To
compare all channels the CMP_ALL parameter of ADC_EMR should be set.
Moreover a filtering option can be set by writing the number of consecutive comparison errors needed to raise the
flag. This number can be written and read in the CMPFILTER field of ADC_EMR.
The flag can be read on the COMPE bit of the Interrupt Status Register (ADC_ISR) and can trigger an interrupt.
The High Threshold and the Low Threshold can be read/write in the Comparison Window Register (ADC_CWR).

Similar to a comparator, but not quite the same...

This comparator is present at the digital pins of the microcontroller.

Not on the SAM3X... in this case the comparison depends on an analog conversion of an input channel.

aarg:
You should share details of your circuit and your code if you want help. I had a look at the SAM data sheet, it seems like the comparator details are not published there - perhaps they are not considered relevant for normal operation.

void setup() {
Serial.begin(9600);
}
int x, j = 0, i = 0, a = 0, g = 1, k = 0;
unsigned long t1, t2, t3, t4, t5, t6, td1[1000], td2[1000], td3[1000];
double t, tc, td11, td22, td33, tcn, tcd;
void loop() {
charge();
pinMode(10, OUTPUT);
digitalWrite(10, LOW);
t1 = micros();
for (x = 0; x >= 0; x++) {
if (digitalRead(4) == LOW)
{
t2 = micros();
break;
}
}
charge();
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
t3 = micros();
for (x = 0; x >= 0; x++) {
if (digitalRead(4) == LOW)
{
t4 = micros();
break;
}
}
while (g > 0) {
i++;
td1 = t2 - t1;
_ td2 = t4 - t3;_
* if (i > 99) {*
* td11 = (td1[j + 100] + td1[j + 1] + td1[j + 2] + td1[j + 3] + td1[j + 4] + td1[j + 5] + td1[j + 6] + td1[j + 7] + td1[j + 8] + td1[j + 9] + td1[j + 10] + td1[j + 11] + td1[j + 12] + td1[j + 13] + td1[j + 14] + td1[j + 15] + td1[j + 16] + td1[j + 17] + td1[j + 18] + td1[j + 19] + td1[j + 20] + td1[j + 21] + td1[j + 22] + td1[j + 23] + td1[j + 24] + td1[j + 25] + td1[j + 26] + td1[j + 27] + td1[j + 28] + td1[j + 29] + td1[j + 30] + td1[j + 31] + td1[j + 32] + td1[j + 33] + td1[j + 34] + td1[j + 35] + td1[j + 36] + td1[j + 37] + td1[j + 38] + td1[j + 39] + td1[j + 40] + td1[j + 41] + td1[j + 42] + td1[j + 43] + td1[j + 44] + td1[j + 45] + td1[j + 46] + td1[j + 47] + td1[j + 48] + td1[j + 49] + td1[j + 50] + td1[j + 51] + td1[j + 52] + td1[j + 53] + td1[j + 54] + td1[j + 55] + td1[j + 56] + td1[j + 57] + td1[j + 58] + td1[j + 59] + td1[j + 60] + td1[j + 61] + td1[j + 62] + td1[j + 63] + td1[j + 64] + td1[j + 65] + td1[j + 66] + td1[j + 67] + td1[j + 68] + td1[j + 69] + td1[j + 70] + td1[j + 71] + td1[j + 72] + td1[j + 73] + td1[j + 74] + td1[j + 75] + td1[j + 76] + td1[j + 77] + td1[j + 78] + td1[j + 79] + td1[j + 80] + td1[j + 81] + td1[j + 82] + td1[j + 83] + td1[j + 84] + td1[j + 85] + td1[j + 86] + td1[j + 87] + td1[j + 88] + td1[j + 89] + td1[j + 90] + td1[j + 91] + td1[j + 92] + td1[j + 93] + td1[j + 94] + td1[j + 95] + td1[j + 96] + td1[j + 97] + td1[j + 98] + td1[j + 99]) / 100;*
* td22 = (td2[j + 100] + td2[j + 1] + td2[j + 2] + td2[j + 3] + td2[j + 4] + td2[j + 5] + td2[j + 6] + td2[j + 7] + td2[j + 8] + td2[j + 9] + td2[j + 10] + td2[j + 11] + td2[j + 12] + td2[j + 13] + td2[j + 14] + td2[j + 15] + td2[j + 16] + td2[j + 17] + td2[j + 18] + td2[j + 19] + td2[j + 20] + td2[j + 21] + td2[j + 22] + td2[j + 23] + td2[j + 24] + td2[j + 25] + td2[j + 26] + td2[j + 27] + td2[j + 28] + td2[j + 29] + td2[j + 30] + td2[j + 31] + td2[j + 32] + td2[j + 33] + td2[j + 34] + td2[j + 35] + td2[j + 36] + td2[j + 37] + td2[j + 38] + td2[j + 39] + td2[j + 40] + td2[j + 41] + td2[j + 42] + td2[j + 43] + td2[j + 44] + td2[j + 45] + td2[j + 46] + td2[j + 47] + td2[j + 48] + td2[j + 49] + td2[j + 50] + td2[j + 51] + td2[j + 52] + td2[j + 53] + td2[j + 54] + td2[j + 55] + td2[j + 56] + td2[j + 57] + td2[j + 58] + td2[j + 59] + td2[j + 60] + td2[j + 61] + td2[j + 62] + td2[j + 63] + td2[j + 64] + td2[j + 65] + td2[j + 66] + td2[j + 67] + td2[j + 68] + td2[j + 69] + td2[j + 70] + td2[j + 71] + td2[j + 72] + td2[j + 73] + td2[j + 74] + td2[j + 75] + td2[j + 76] + td2[j + 77] + td2[j + 78] + td2[j + 79] + td2[j + 80] + td2[j + 81] + td2[j + 82] + td2[j + 83] + td2[j + 84] + td2[j + 85] + td2[j + 86] + td2[j + 87] + td2[j + 88] + td2[j + 89] + td2[j + 90] + td2[j + 91] + td2[j + 92] + td2[j + 93] + td2[j + 94] + td2[j + 95] + td2[j + 96] + td2[j + 97] + td2[j + 98] + td2[j + 99]) / 100;*
* j++;*
_ tcn = (((td11 - td22) / td22) * 150);_
* Serial.println(“resistance”);*
* Serial.println(tcn, 0);*
* delay(500);*
* }*
* break;*
* }*
}
void charge() {
* pinMode(10, INPUT);*
* pinMode(9, INPUT);*
* pinMode(4, OUTPUT);*
* digitalWrite(4, HIGH);*
* delayMicroseconds(5000);*
* pinMode(4, INPUT);*
}

elangoece3691:
void setup() {
Serial.begin(9600);
}
int x, j = 0, i = 0, a = 0, g = 1, k = 0;
unsigned long t1, t2, t3, t4, t5, t6, td1[1000], td2[1000], td3[1000];
double t, tc, td11, td22, td33, tcn, tcd;
void loop() {
charge();
pinMode(10, OUTPUT);
digitalWrite(10, LOW);
t1 = micros();
for (x = 0; x >= 0; x++) {
if (digitalRead(4) == LOW)
{
t2 = micros();
break;
}
}
charge();
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
t3 = micros();
for (x = 0; x >= 0; x++) {
if (digitalRead(4) == LOW)
{
t4 = micros();
break;
}
}
while (g > 0) {
i++;
td1 = t2 - t1;
_ td2 = t4 - t3;_
* if (i > 99) {*
* td11 = (td1[j + 100] + td1[j + 1] + td1[j + 2] + td1[j + 3] + td1[j + 4] + td1[j + 5] + td1[j + 6] + td1[j + 7] + td1[j + 8] + td1[j + 9] + td1[j + 10] + td1[j + 11] + td1[j + 12] + td1[j + 13] + td1[j + 14] + td1[j + 15] + td1[j + 16] + td1[j + 17] + td1[j + 18] + td1[j + 19] + td1[j + 20] + td1[j + 21] + td1[j + 22] + td1[j + 23] + td1[j + 24] + td1[j + 25] + td1[j + 26] + td1[j + 27] + td1[j + 28] + td1[j + 29] + td1[j + 30] + td1[j + 31] + td1[j + 32] + td1[j + 33] + td1[j + 34] + td1[j + 35] + td1[j + 36] + td1[j + 37] + td1[j + 38] + td1[j + 39] + td1[j + 40] + td1[j + 41] + td1[j + 42] + td1[j + 43] + td1[j + 44] + td1[j + 45] + td1[j + 46] + td1[j + 47] + td1[j + 48] + td1[j + 49] + td1[j + 50] + td1[j + 51] + td1[j + 52] + td1[j + 53] + td1[j + 54] + td1[j + 55] + td1[j + 56] + td1[j + 57] + td1[j + 58] + td1[j + 59] + td1[j + 60] + td1[j + 61] + td1[j + 62] + td1[j + 63] + td1[j + 64] + td1[j + 65] + td1[j + 66] + td1[j + 67] + td1[j + 68] + td1[j + 69] + td1[j + 70] + td1[j + 71] + td1[j + 72] + td1[j + 73] + td1[j + 74] + td1[j + 75] + td1[j + 76] + td1[j + 77] + td1[j + 78] + td1[j + 79] + td1[j + 80] + td1[j + 81] + td1[j + 82] + td1[j + 83] + td1[j + 84] + td1[j + 85] + td1[j + 86] + td1[j + 87] + td1[j + 88] + td1[j + 89] + td1[j + 90] + td1[j + 91] + td1[j + 92] + td1[j + 93] + td1[j + 94] + td1[j + 95] + td1[j + 96] + td1[j + 97] + td1[j + 98] + td1[j + 99]) / 100;*
* td22 = (td2[j + 100] + td2[j + 1] + td2[j + 2] + td2[j + 3] + td2[j + 4] + td2[j + 5] + td2[j + 6] + td2[j + 7] + td2[j + 8] + td2[j + 9] + td2[j + 10] + td2[j + 11] + td2[j + 12] + td2[j + 13] + td2[j + 14] + td2[j + 15] + td2[j + 16] + td2[j + 17] + td2[j + 18] + td2[j + 19] + td2[j + 20] + td2[j + 21] + td2[j + 22] + td2[j + 23] + td2[j + 24] + td2[j + 25] + td2[j + 26] + td2[j + 27] + td2[j + 28] + td2[j + 29] + td2[j + 30] + td2[j + 31] + td2[j + 32] + td2[j + 33] + td2[j + 34] + td2[j + 35] + td2[j + 36] + td2[j + 37] + td2[j + 38] + td2[j + 39] + td2[j + 40] + td2[j + 41] + td2[j + 42] + td2[j + 43] + td2[j + 44] + td2[j + 45] + td2[j + 46] + td2[j + 47] + td2[j + 48] + td2[j + 49] + td2[j + 50] + td2[j + 51] + td2[j + 52] + td2[j + 53] + td2[j + 54] + td2[j + 55] + td2[j + 56] + td2[j + 57] + td2[j + 58] + td2[j + 59] + td2[j + 60] + td2[j + 61] + td2[j + 62] + td2[j + 63] + td2[j + 64] + td2[j + 65] + td2[j + 66] + td2[j + 67] + td2[j + 68] + td2[j + 69] + td2[j + 70] + td2[j + 71] + td2[j + 72] + td2[j + 73] + td2[j + 74] + td2[j + 75] + td2[j + 76] + td2[j + 77] + td2[j + 78] + td2[j + 79] + td2[j + 80] + td2[j + 81] + td2[j + 82] + td2[j + 83] + td2[j + 84] + td2[j + 85] + td2[j + 86] + td2[j + 87] + td2[j + 88] + td2[j + 89] + td2[j + 90] + td2[j + 91] + td2[j + 92] + td2[j + 93] + td2[j + 94] + td2[j + 95] + td2[j + 96] + td2[j + 97] + td2[j + 98] + td2[j + 99]) / 100;*
* j++;*
_ tcn = (((td11 - td22) / td22) * 150);_
* Serial.println("resistance");*
* Serial.println(tcn, 0);*
* delay(500);*
* }*
* break;*
* }*
}
void charge() {
* pinMode(10, INPUT);*
* pinMode(9, INPUT);*
* pinMode(4, OUTPUT);*
* digitalWrite(4, HIGH);*
* delayMicroseconds(5000);*
* pinMode(4, INPUT);*
}
[/quote]
please ignore the irrelevant things. Actually, the circuits works based on the principle of charging and discharging of the capacitor. the charging time was fixed as 5 ms, so that the voltage across the capacitor is always VDD (3.3 Volt for Arduino due). Then, the resistance of the sensor can be found by making the discharge of the capacitor through different paths. here, i used pin 4 for charging (while discharge pin 4 will act as high impendence) and pin 10 and 9 for discharging. Initially, the charging was done by making pin 4 as high state and pin 10 and 9 as high impedance state. after 5 ms, pin 4 becomes high impedance and 10 becomes low state. so that the discharge will occur through the path which contains the resistances Rx and R1 (in code it is 150 ohm). after, reaching the lower threshold (around 1.8 volt for Arduino due) it will once again charge for the fixed time duration (i. e., 5 ms). later, the pin 9 will become as low state, so that the discharge will occur through only R1. using these two discharge times (td11 and td22) rx can be found using (((td11 - td22) / td22) * 150).
Doubts:
1. the discharge is happens up to the lower threshold voltage (not to ground). thus, there is a drift in lower threshold voltage causes error in the resistance measurement (i. .e., tcn). how to stabilize the lower threshold voltage without using ADC.
2. the threshold is varying from (1.6 to 1.8). this causes a huge error in output. the circuit assumes the threshold is same. similarly, how to stabilize the higher threshold voltage also.
these are the main issues facing in my works.
please help me :confused:


elangoece3691:
please ignore the irrelevant things. Actually, the circuits works based on the principle of charging and discharging of the capacitor. the charging time was fixed as 5 ms, so that the voltage across the capacitor is always VDD (3.3 Volt for Arduino due). Then, the resistance of the sensor can be found by making the discharge of the capacitor through different paths. here, i used pin 4 for charging (while discharge pin 4 will act as high impendence) and pin 10 and 9 for discharging. Initially, the charging was done by making pin 4 as high state and pin 10 and 9 as high impedance state. after 5 ms, pin 4 becomes high impedance and 10 becomes low state. so that the discharge will occur through the path which contains the resistances Rx and R1 (in code it is 150 ohm). after, reaching the lower threshold (around 1.8 volt for Arduino due) it will once again charge for the fixed time duration (i. e., 5 ms). later, the pin 9 will become as low state, so that the discharge will occur through only R1. using these two discharge times (td11 and td22) rx can be found using (((td11 - td22) / td22) * 150).
Doubts:

  1. the discharge is happens up to the lower threshold voltage (not to ground). thus, there is a drift in lower threshold voltage causes error in the resistance measurement (i. .e., tcn). how to stabilize the lower threshold voltage without using ADC.
  2. the threshold is varying from (1.6 to 1.8). this causes a huge error in output. the circuit assumes the threshold is same. similarly, how to stabilize the higher threshold voltage also.
    these are the main issues facing in my works.
    please help me :confused:

Vtl is the lower threshold voltage in the attachment and similarly rx is the sensor resistance...The value of R1 given in code is 150 ohm.