Hallo liebe Leute, ich versuche gerade einen Ohmmeter mit dem Arduino DUE aufzubauen. Ich habe als festen Widerstand 1k verwendet. Den Poti habe ich auf 140 Ohm gestellt bzw. der vermeintlich unbekannte Widerstand ist ca. 140 Ohm. Die Referenzspannung liegt bei 5V. Als Ausgabe des unbekannten Widerstandes bekomme ich jedoch 230 Ohm und nicht 140 Ohm.
1 | #define PIN_MEASURE A0
|
2 | |
3 | |
4 | #define V_IN 5.0 // source voltage in Volt
|
5 | #define REF_R2 1000 // value of the known resistor in Ohm
|
6 | |
7 | void setup() |
8 | {
|
9 | Serial.begin(9600); |
10 | pinMode(PIN_MEASURE, INPUT); |
11 | }
|
12 | |
13 | void loop() |
14 | {
|
15 | measureResistance(); |
16 | delay(10000); |
17 | |
18 | }
|
19 | |
20 | void measureResistance() |
21 | {
|
22 | // measure 10 times and calculate the average
|
23 | long reading = 0; |
24 | for (byte i = 0; i < 10; i++) { |
25 | reading += analogRead(PIN_MEASURE); |
26 | }
|
27 | reading = (long)(reading / 10); |
28 | |
29 | Serial.print("A0: "); |
30 | Serial.println(reading); |
31 | |
32 | // calculate voltage
|
33 | float voltageR1 = (V_IN / 1023.0) * reading; |
34 | Serial.print("Voltage over R1: "); |
35 | Serial.print(voltageR1, 2); |
36 | Serial.println(" V"); |
37 | |
38 | // formula: R1 = R2 * (U2/U1)
|
39 | int resistance = REF_R2 * (voltageR1 / (V_IN - voltageR1)); |
40 | Serial.println("Resistor R1: " + String(resistance) + " Ω\n\n"); |
41 | }
|