Chuyển đến nội dung

Bài 15 · Analog and PWM

Voltage Dividers

Learn the voltage divider formula, use it to read resistive sensors like LDRs and thermistors, scale 5 V signals down for 3.3 V boards and measure battery voltage.

An Arduino analog pin measures voltage, but many sensors change their resistance: a light-dependent resistor (LDR) gets less resistive in bright light, a thermistor changes with temperature, a flex sensor with bending. A voltage divider turns resistance into voltage, and it is also the way to scale a voltage down to a range the board can handle. It is two resistors and one formula, and it will appear in a large share of your projects.

The circuit and the formula

Two resistors in series connect between a supply voltage Vin and ground. The voltage at the junction between them is a fraction of Vin:

TEXT
Vout = Vin × R2 / (R1 + R2)

R1 is the resistor on the supply side (top), R2 the one on the ground side (bottom), and Vout is measured across R2.

flowchart TD
    A["Vin"] --> R1["R1"]
    R1 --> J["Vout (to analog pin)"]
    J --> R2["R2"]
    R2 --> G["GND"]

Figure: the junction between R1 and R2 gives Vout = Vin x R2 / (R1 + R2).

If R1 = R2, then Vout is exactly half of Vin. A potentiometer is a divider whose ratio you change by turning the knob. The divider only gives that voltage if almost no current is drawn from the junction; the ADC input draws almost none, which is why it works.

C Stand-in: calculate divider outputs

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Reading a resistive sensor

Put the sensor on one side of the divider and a fixed resistor (usually near the sensor's mid-range resistance) on the other. As the sensor's resistance changes, Vout moves and analogRead() follows.

An LDR might measure about 1 kΩ in bright light and 100 kΩ in the dark. With a 10 kΩ fixed resistor on the ground side and the LDR on top (between 5 V and the junction), bright light gives a high voltage and darkness a low one:

C Stand-in: LDR in a divider, from resistance to reading

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The relation is not linear, but for a night light that only needs "bright or dark", that does not matter. For a thermistor you convert the resistance using a formula from the datasheet (the Steinhart-Hart or Beta equation) before turning it into degrees. Solve the divider for the unknown resistor:

TEXT
Rsensor = R2 × (Vin / Vout − 1)       when the sensor is R1 (top)
C Stand-in: recover an unknown resistance from a reading

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Notice that the formula needs only the ratio Vout/Vin; the supply voltage cancels out. That is a useful property: the result does not depend on whether the board runs at 5 V or 3.3 V.

Level shifting and measuring higher voltages

The ADC cannot measure more than its reference voltage. To measure a larger voltage, divide it down. For a 12 V battery and a 5 V ADC, a 30 kΩ over 10 kΩ divider gives 12 × 10 / 40 = 3 V. A 2:1 ratio is also the standard trick for a 5 V logic output feeding a 3.3 V board input: 1 kΩ above and 2 kΩ below gives 5 × 2 / 3 = 3.33 V.

C Stand-in: design a divider for a battery monitor

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The computed 18.8 kΩ is not a standard value. Rounding down to 18 kΩ would put 5.14 V on the pin at 14.4 V, over the limit, so we round up to 22 kΩ, which gives 4.5 V at full charge and a safe margin. Always check the worst case, not the typical case.

Divider limits

  • Current drain. A divider always passes current (V / (R1 + R2)). For battery projects use large values (100 kΩ and up) and add a small capacitor to the ADC pin to help it charge.
  • High source resistance. The ADC wants a source under about 10 kΩ. A divider with 100 kΩ resistors needs a 100 nF capacitor from the junction to GND, and slower sampling.
  • Loading. Anything connected to Vout draws current and changes the voltage. A divider is for measuring, not for powering a load.
  • Tolerance. Standard resistors are ±5% (or 1% for metal film); that error carries into your reading.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. In Vout = Vin x R2 / (R1 + R2), where is Vout measured?

    Vout is the voltage between the junction and ground, which is the voltage across R2.
  2. 2. R1 = 10 kohm, R2 = 20 kohm, Vin = 6 V. What is Vout?

    6 x 20 / 30 = 4 V.
  3. 3. Why does the divider reading of a resistive sensor not depend on the supply voltage once you use the ratio?

    analogRead measures Vout relative to the same supply that feeds the divider, giving a pure ratio.
  4. 4. What is a drawback of a divider with very small resistors?

    Current is V divided by the total resistance, so small resistors draw more.

Summary

  • A voltage divider outputs Vout = Vin × R2 / (R1 + R2).
  • It turns a changing resistance (LDR, thermistor) into a voltage the ADC can read.
  • Solve the formula backward to compute the sensor's resistance from a reading.
  • Dividers scale high voltages down for the ADC and 5 V signals down for 3.3 V inputs.
  • Mind current drain, source impedance, loading and resistor tolerance.

Next: PWM and analogWrite

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.