The simplest sensors in electronics aren't complex chips, they're resistors whose value changes with something in the environment. You'll meet two of them a lot: the LDR for light and the NTC for temperature. They're very cheap, need no libraries or protocols, and any microcontroller can read them with one analog pin and one resistor.
The LDR (light dependent resistor)
A resistance that falls as light increases:
- In the dark it can reach about a megohm.
- In bright light it drops to a few kilohms or less.
Uses: street lights that switch on by themselves at night, rough light measurement, and alarms that trigger when someone breaks a beam.
Drawbacks: relatively slow (it takes tens of milliseconds to respond, and longer coming out of darkness), not accurate, and every part differs from the next. If you need accurate measurement in lux, use a digital sensor such as the BH1750.
The NTC thermistor
A resistance that falls as temperature rises (negative temperature coefficient). The most common is the 10k NTC, meaning 10 kΩ at 25 °C.
Uses: measuring the temperature of lithium batteries, 3D printers, air conditioners and fridges.
Note: the relationship is not linear, so you need an equation (such as the Beta equation or Steinhart-Hart) to convert resistance to temperature. The equation and code are covered in detail in the NTC thermistor article.
There's also the PTC, which does the opposite (its resistance rises with temperature) and is used as a self-resetting fuse. More in the fuse article.
Wiring: a voltage divider
A microcontroller doesn't measure resistance, it measures voltage. So we put the sensor with a fixed resistor as a voltage divider:
- 5 V → sensor → reading point → fixed resistor → ground.
- The reading point goes to an analog pin.
int raw = analogRead(A0);
The order sets the direction: with the sensor on top, the reading rises when its resistance falls (more light or higher temperature). Swap them and the reading falls. Both are correct; just pick one and remember it in your code.
Choosing the fixed resistor: make it close to the sensor's value in the middle of the range you care about, so the reading changes as much as possible across that range. For a 10k NTC around room temperature: 10 kΩ. For an LDR: 10 kΩ is a good start for indoor light. If you only want to detect darkness, a larger resistor (47 or 100 kΩ) distinguishes dusk from full darkness better.
With an ESP32: connect the divider to 3.3 V, not 5 V, because its ADC pins top out at 3.3 V.
Expected NTC readings
A 10k thermistor (β = 3950) with a 10 kΩ fixed resistor, thermistor at the bottom (towards ground), on 5 V:
| Temperature | Thermistor resistance | Voltage at A0 |
|---|---|---|
| 0 °C | about 33.6 kΩ | 3.85 V |
| 25 °C | 10 kΩ | 2.5 V |
| 50 °C | about 3.6 kΩ | 1.32 V |
Notice the difference between 0 and 25 °C is 1.35 V, but between 25 and 50 °C it's only 1.18 V. That's what non-linear means, and why we need the equation instead of dividing by a fixed number. The NTC temperature calculator does it for you and generates ready code.
Example: a lamp that turns on by itself at night
The simple approach is to compare the reading with one number: below it, turn on; above it, turn off. The problem is that at dusk the reading wobbles around that number, so the lamp "flickers" on and off repeatedly, which wears out the relay.
The solution is called hysteresis: two numbers, not one.
const int ON_BELOW = 300; // below this: dark, turn on
const int OFF_ABOVE = 400; // above this: light, turn off
bool lampOn = false;
void loop() {
int light = analogRead(A0); // LDR on top: more light = bigger number
if (!lampOn && light < ON_BELOW) { lampOn = true; digitalWrite(7, HIGH); }
if (lampOn && light > OFF_ABOVE) { lampOn = false; digitalWrite(7, LOW); }
delay(200);
}
The gap between 300 and 400 prevents flickering. Tune both numbers to your own lighting with the Serial Monitor. If the lamp runs on 220 V, pin 7 drives a relay module, and read the safety rules before you touch mains wiring.
The ready-made LDR module
The small blue module with an LDR and a potentiometer has two outputs:
- AO: the same divider voltage, connected to an analog pin.
- DO: a digital output from an LM393 comparator: HIGH or LOW depending on whether the light is above or below a level you set with the potentiometer.
DO is handy if you want a light/dark decision without code, or you're working without a microcontroller at all. The comparator idea itself is in the comparator article.
Or is a digital sensor better?
| Sensor | Measures | Advantage | Drawback |
|---|---|---|---|
| LDR | Rough light | Cheapest and simplest wiring | Not accurate, slow |
| BH1750 | Light in lux | Accurate, I2C | Costs more, needs a library |
| NTC | Temperature | Cheap, small, handles high temperatures | Needs an equation and calibration |
| DS18B20 | Temperature | About ½ °C accuracy, waterproof version available | A bit slower |
| DHT22 | Temperature and humidity | Both in one sensor | A reading only every two seconds |
If you need an accurate number to display or log, a digital sensor saves time. See the DS18B20 article and the temperature and humidity sensors article. If you need a simple decision (light or dark, hot or not) or a high temperature inside a device, the LDR and NTC are enough and won't let you down.
Tips
- Self-heating: the current through a thermistor warms it slightly. Keep the current small (resistors in kilohms, not ohms).
- Average several readings to reduce jitter.
- Long wires pick up noise: put a 100 nF capacitor between the reading pin and ground.
- Shield the LDR from the light it controls: if the lamp shines on the sensor, it will think it's daytime and switch itself off. Point it away or put it in a small tube.
- For high temperature accuracy, digital sensors such as the DS18B20 or I2C chips give you the temperature directly.
FAQ
Can an LDR work outdoors in the sun? Yes, but in direct sunlight its resistance drops so much that all readings end up close together. Use a smaller fixed resistor (say 1 kΩ) to distinguish between high light levels, and protect it from rain.
My NTC reads the temperature wrong by a constant amount? Most likely the β or the 25 °C resistance differs from the values in your code, or the fixed resistor isn't exactly 10 kΩ. Check against an accurate thermometer and adjust; details in the NTC thermistor article.