Sensors that are a resistor or a switch
The LDR, the NTC thermistor, the reed switch, the tilt switch and the PIR: five cheap parts, four of which have no chip in them at all.
The sensors in a starter kit divide into two piles, and the division matters more than the parts do.
In the first pile a sensor is a resistor whose value means something. It has two legs, no supply and no output pin, and on its own it does nothing: you have to build a divider around it and read the middle. The LDR and the thermistor are both this.
In the second pile a sensor is a switch something in the world operates.
Two legs again, and the sketch reads it exactly the way it reads a button:
INPUT_PULLUP and a wire to ground. The reed switch and the tilt switch are
both this.
And then there is the PIR, which is the odd one out: a module with its own chip that hands you a finished digital signal.
Every one of them has a slider or a click on the canvas for the thing it is sensing, because the world is the control.
The divider, once
Both resistive sensors want the same three components, so it is worth doing once:
5 V ──[ sensor ]──┬──[ 10 k ]── GND
│
A0
The node reads 5 V x 10k / (10k + Rsensor), so as the sensor's resistance
falls the node rises. Put the sensor on the bottom instead and it goes the
other way. The fixed resistor is usually chosen to be about what the sensor
reads in the middle of the range you care about, which puts the node near half
the supply where the ADC has most to say.
Mokxi solves this properly: the analog island around the divider is a real nodal solve, so the loading, the ADC's input impedance and any other resistor you hang off the node all do what they would on the bench.
The LDR
A light-dependent resistor: a zigzag of cadmium sulphide on a ceramic disc. Hundreds of ohms in sunlight, tens of kilohms in a room, a megohm or more with your thumb over it.
The model is the datasheet's two numbers and a slope: the resistance at 10
lux, the dark resistance, and gamma, which is how steeply it falls on log-log
paper. A GL5528 is 15 k, a megohm and 0.6, which are the defaults. A GL5537 is
the same part an order of magnitude up: set r10 to 50 000.
Drag the slider for how much light is falling on it, 0 to 5000 lux. The face of the cell brightens with it, on a logarithmic scale, because that is how an eye reads a room.
Not modeled: the light history effect, the twenty or thirty milliseconds a
real cell takes to follow a step, and the rather longer it takes to recover
from bright light. Nor any noise, temperature coefficient or part-to-part
spread. A real GL5528's r10 varies by a factor of two between two out of the
same bag, so treat the ohms as the right order and the right slope rather than a
number to calibrate a sketch against.
See it: Night light and Dark detector.
The NTC thermistor
A drop of epoxy over a bead of metal oxide. Negative temperature coefficient: the resistance falls as it warms, and steeply. A 10 k bead is 10 k at 25 °C, about 33 k at freezing and under 6 k at body heat.
The model is the beta equation, which is what a datasheet gives you:
R(T) = r25 x exp( beta x (1/T - 1/298.15) ), T in kelvin
r25 is 10 000 and beta is 3950 by default. 3435 and 3977 are the other two
numbers on the shelf and they are the same part with a different beta; a
100 k bead is r25 100 000.
The beta equation is itself an approximation: a real bead needs the three-term
Steinhart-Hart fit to stay inside a degree across its whole range, and a single beta
drifts by a degree or two at the ends. Nothing here models that, nor the bead's own
tolerance (±1% on r25 and ±1% on beta is a good part), nor self-heating from the
divider current, nor the seconds of thermal lag a bead in epoxy actually has. The
resistance changes the instant the slider does.
Drag the slider for the temperature, -40 to 125 °C. The bead goes blue when it is cold and red when it is hot.
Turning a reading back into degrees needs a logarithm, which is where this part gets interesting on a small board. The equation rearranges to
1/T = 1/298.15 + ln(R / r25) / beta
and a sketch can work it as it stands: log() is in the math library, so the
resistance from the divider and the line above are two lines of floating
point, a few hundred microseconds of software floating point on an Uno. The
Thermometer on an I2C LCD example does it the other way, the way small
boards often do: a table of what the ADC reads at each ten degrees, worked out
once on a desk, and a straight line between the two entries the reading falls
between. Over the range that matters the error from the interpolation is under
half a degree, which is smaller than the bead's own tolerance.
If you want a temperature without the arithmetic lesson, use the TMP36 instead: three legs, and the middle one is already half a volt plus ten millivolts a degree.
The reed switch
Two steel blades in a glass tube, overlapping but not touching. A magnet near it magnetizes them, they attract each other and snap shut; take the magnet away and their own springiness pulls them apart. Every door sensor, every bike computer and every washing-machine lid interlock is one of these.
Drag the slider to move the magnet. Two things are modeled on purpose.
It does not let go where it grabbed. The switch closes at about 15 mm and
holds on out to nearly 19, because once the blades are touching the magnetic
circuit through them is better. Datasheets quote that as an operate and a
release figure and Mokxi has it as close and release. It is why a door that
creaks an inch does not set an alarm off, and it is the same shape of trick a
sketch does in software when it adds hysteresis to a threshold.
The blades bounce. Not for the hundred milliseconds a rolling ball takes
(they are light and stiff, and the datasheets say well under one), but enough to
count one opening twice if the sketch believes the first edge it sees.
settle is 0.5 ms by default and 0 turns it off. Like the tilt switch's, the pattern
is fixed (the same five edges evenly spread every time) rather than the varying burst
a real contact makes.
Not modeled on the reed: the magnet's orientation and pole (the slider is distance alone, where a real switch cares a great deal which way the field runs), the contact's current and voltage ratings, and the fact that a strong enough magnet will hold the blades shut permanently.
See it: Door alarm.
The tilt switch
A metal can with two contacts and a conductive ball rolling between them. Upright the ball sits on both and the circuit is closed; tip it past about fifteen degrees and the ball rolls off. Click it on the canvas to tip it; it stays where you put it, because a box that has been knocked over stays knocked over.
The ball is the noisiest switch in a kit. A hundred milliseconds of chatter as
it lands is not unusual, which is why every tilt-sensing sketch debounces over
tens of milliseconds rather than microseconds. settle is 20 ms here.
See it: Tilt alarm.
The PIR
The HC-SR501: a white Fresnel dome on a small board, three pins, its own chip
underneath. Unlike the four above, this one hands you a finished digital
signal, so the driver is digitalRead(pin) and that is all of it.
It is not a presence detector. A PIR sees change. Somebody walking across its view triggers it; somebody sitting perfectly still in front of it does not. So the control on the canvas is "is something moving in front of it" (click the dome to start, click again to stop), and what the module does with that is its own timing:
OUTgoes high as soon as there is movement;- it stays high for
holdseconds after the movement stops. That is 2 seconds by default. On the real board it is the left trimmer, whose range is about 3 seconds to 5 minutes; - then it goes low, and for
blockseconds (2.5, and not adjustable on the real board) the module ignores everything. This is why a PIR that has just timed out seems dead for a moment, and why a sketch that does not know about it reads that low as "they have gone" when they are still standing there.
The jumper beside the pins is retrigger. In repeat, which is how the boards
usually ship, continued movement keeps OUT high indefinitely. In single,
OUT goes low hold seconds after the first movement whatever happens next,
which is what you want when one event should produce exactly one pulse.
One more thing worth knowing: OUT is 3.3 V, whatever you power the module
from, because the regulator on it feeds the logic. On a 5 V Arduino that is
still a solid high and nothing goes wrong, which is why almost nobody notices;
in a circuit that compares it against the 5 V rail, it matters.
Not modeled: the minute the real module takes to settle after power, and the
false triggers it throws while it does. The lens pattern, the range, and the
sensitivity trimmer. Nor any of the reasons a real PIR fires when nothing is there (a
draft, sunlight moving across the lens, a warm object cooling), so the output here is
a clean function of the click and nothing else, which is quieter than any real one.
hold and block are exact to the millisecond where a real board's RC timing is
±30%, and OUT is a hard 3.3 V push-pull with no rise time.
See it: Motion light, which has no board in it at all.