Control with devices
This is where everything comes together: a sensor delivers a voltage, the converter turns it into a number, the program decides, and an isolated output drives mains power. The two cases in the program —temperature and lighting— are the most common and the ones that best show the complete chain.
01The control chain
Every automatic control system has the same five links, and each one contributes its own error and delay:
02Measuring temperature
| Sensor | Output | Range | What makes it special |
|---|---|---|---|
| NTC | Resistance | −40 to 150 °C | Cheap and sensitive, but nonlinear: it needs a table or an equation. Read with a divider. |
| LM35 | 10 mV/°C | 2 to 150 °C | Linear and goes straight to the ADC. It cannot measure below zero without a negative supply. |
| PT100 | 100 Ω at 0 °C | −200 to 600 °C | The industrial standard: very stable and linear. It needs a bridge and an instrumentation amplifier. |
| Thermocouple | ~40 µV/°C (type K) | −200 to 1200 °C | For furnaces. A tiny signal: needs a dedicated amplifier and cold-junction compensation. |
| DS18B20 | Digital, 1-Wire | −55 to 125 °C | Comes calibrated and with a converter inside: it connects with a single data wire. |
A 10 kΩ NTC at 25 °C with B = 3950, in a divider with a fixed 10 kΩ resistor at 5 V, read with a 10-bit ADC.
- At 25 °C: R = 10 kΩ. The divider delivers 5 × 10/(10+10) = 2.50 V → 512 counts.
- At 50 °C (323.15 K): the exponent is 3950 × (1/323.15 − 1/298.15) = −1.025, so R = 10 k × 0.359 = 3.59 kΩ. Voltage = 5 × 3.59/13.59 = 1.32 V → 270 counts.
- Between 25 and 50 °C the reading changes by 242 counts: almost 10 counts per degree. More than enough resolution.
In the program you do not solve for the logarithm: you store a table of counts versus temperature and interpolate. It is faster and it avoids the math library, which on a small micro takes up half the flash memory.
A 10 kΩ NTC with 2.5 V dissipates 0.6 mW; the package heats up and distorts its own measurement. The fix is to power the divider only during the measurement —from a micro pin— or to raise the resistor values. It is a classic error that shows up as “the sensor always reads one degree too high.”
03On-off control with hysteresis
It is the simplest and most widely used control: if the temperature is below the setpoint, it is turned on; if it is above, it is turned off. Done exactly like that, it does not work: at the threshold, noise and tiny variations make the relay switch dozens of times per minute until it is destroyed.
// Control with hysteresis: two thresholds, not one
if (temp < CONSIGNA - HISTERESIS) calefactor(1); // 24 °C: turns on
if (temp > CONSIGNA + HISTERESIS) calefactor(0); // 26 °C: turns off
// Between 24 and 26 nothing is done: the previous state is kept
- Too narrow: very stable temperature, but the actuator switches all the time. A mechanical relay is good for about 100,000 operations: at one per minute it is used up in two months.
- Too wide: few switchings, but the temperature oscillates and may not be acceptable for the process.
- Rule of thumb: start with 2 % of the working range and adjust by watching how many times per hour it switches.
- It also pays to set a minimum on time and off time: it protects compressors and motors, which do not tolerate repeated starts.
On-off control always oscillates around the setpoint. If the process demands holding the temperature within a tenth of a degree —a laboratory oven, a reflow soldering station— you need a proportional-integral-derivative controller, which instead of deciding on or off calculates how much power to deliver, and applies it with slow PWM or with whole-cycle control. PID is developed in full in Year 7, in Industrial Electronics II.
04Lighting control
The typical sensor is an LDR in a divider, or a photodiode if speed or linearity is needed. The interesting part is in the actuator: an incandescent lamp is not regulated the same way as an LED.
| Load | Method | Precautions |
|---|---|---|
| DC LED | PWM above 200 Hz, or regulated current | Below 100 Hz the flicker is visible; if it is going to be filmed, above 1 kHz is better. Perceived brightness is not proportional to the duty cycle: the eye responds logarithmically. |
| Incandescent on mains | Phase control with a triac | It is the classic dimmer. It generates interference and needs a filter. The whole circuit is at mains potential. |
| Mains LED (“dimmable”) | Only if the manufacturer states it | An ordinary LED lamp on a triac dimmer flickers, buzzes or does not turn on: its switching power supply does not tolerate the chopping. |
| Fluorescent tube | Dimmable electronic ballast | It is not regulated by phase. |
| Heater or thermal load | Whole cycles with zero crossing | No interference and no visible flicker, because thermal inertia averages it out. |
To fire a triac at the right instant, the micro needs to know when the mains crosses zero. It is detected with an optocoupler powered from the mains through a large resistor: it delivers a pulse 100 times per second. From that pulse the delay is counted —from 0 to 10 ms— and the triac is fired. That same pulse also serves as a very accurate 50 Hz timebase, and as a power-failure detector.
05Switching power safely
Real contacts: it works for DC and AC, and isolates by construction. It needs a transistor, a flyback diode and a coil current of tens of milliamperes. It wears out, is slow (milliseconds) and makes noise, but it tolerates overloads.
An optotriac plus a triac. No moving parts, silent, millions of operations, and with zero-crossing switching built in on most. The trade-offs: AC only, it dissipates heat (needs a heat sink) and if it fails it does so shorted, with the load on.
Between the microcontroller circuit and anything connected to the mains there must be an isolation barrier: an optocoupler, an optotriac or a relay coil. Never a direct connection, not even “just for the control signal.” On the circuit board, that barrier is respected with 2.5 to 4 mm of spacing and, if needed, a milled slot under the component (PCB). A MOC3021 to fire the triac and a 4N25 to read the zero crossing solve almost every case.
A unit that holds 24 to 26 °C with a 1500 W heating element on 220 V:
- Sensor: 10 k NTC in a divider, powered from a pin to avoid self-heating.
- Reading: 10-bit ADC, average of 16 samples, conversion table to tenths of a degree.
- Decision: hysteresis of ±1 °C and a minimum time of 60 s in each state.
- Actuator: the heating element draws 1500/220 = 6.8 A. A 25 A solid-state relay is used —oversized for the inrush peak and so that it runs cooler— with a heat sink.
- Safety: a mechanical thermostat with manual reset in series, independent of the program. If the micro hangs with the output on, that thermostat cuts power anyway.
Item 5 is not optional: no program is a protection. Safety is hardwired.
06In the lab
Measure the NTC resistance by immersing it in water at different temperatures, with a reference thermometer. Build the table and plot it. Compare it with the exponential equation: it will fit well in the middle and worse at the extremes, which is exactly why tables are used.
Program the control with ±0.2 °C hysteresis and count how many times the relay switches in five minutes. Repeat with ±1 °C and with ±2 °C. Plot switchings against band width and estimate the relay's service life in each case. This is the lab that turns a design decision into a number.
Regulate an LED with PWM from 0 to 100 % in equal steps and note from what point the change becomes noticeable. Then apply a quadratic curve to the duty cycle and repeat: the change is perceived as even. This is gamma correction, and it explains why linear regulation “does not feel linear.”
With an isolation transformer and reduced voltage, detect the zero crossing with an optocoupler and measure on the oscilloscope the spacing between pulses: it should be 10 ms. Use that pulse to fire a triac with a variable delay and verify the phase control on a small lamp.
On the finished thermostat, make the micro hang with the output on (for example, with an infinite loop) and check that the mechanical thermostat in series cuts power anyway. Discuss what other failures the software does not cover: a stuck relay, a disconnected sensor, a cut wire.
07Common errors
| Symptom | Usual cause |
|---|---|
| The relay switches nonstop at the threshold | No hysteresis, or it is too narrow for the measurement noise. |
| The sensor reads one degree too high | NTC self-heating: lower the current or power the divider only while measuring. |
| The measured temperature jumps when the load turns on | Noise conducted through the supply or coupled into the sensor cable. Filter, shield, and do not share the power ground. |
| The micro resets when the relay switches | The flyback diode is missing, or the decoupling capacitor, or the supply cannot handle the coil spike. |
| The LED lamp flickers with the dimmer | It is not a dimmable lamp: its switching power supply does not tolerate phase chopping. |
| The triac stays on and does not turn off | Inductive load without a snubber, or permanent firing due to noise on the gate. |
| Brightness changes abruptly at the start and hardly at all at the end | Gamma correction is missing: the eye's perception is not linear. |
| The oven overshoots the setpoint and then drops too far | Thermal inertia: on-off control always overshoots. PID corrects it. |
| With the sensor disconnected the unit heats at full power | The program does not validate the reading. A reading of 0 or full scale must be interpreted as a fault and shut off. |
08Self-assessment
What is the difference between open-loop command and control?
Control has feedback: it measures the result and corrects. Open-loop command acts blindly. Turning on a heater with a switch is open-loop command; holding 25 °C by measuring the temperature is control.
Why does on-off control need hysteresis?
Because with a single threshold, noise and tiny variations around the setpoint make the actuator switch continuously. With two thresholds there is a dead band in which nothing is done.
A 10 kΩ NTC in a divider with 10 kΩ, at 25 °C and 5 V: what voltage and how many counts does a 10-bit ADC give?
The two resistors are equal: 2.50 V, which on a 5 V reference is 512 counts out of 1024. It is the exact midpoint of the scale.
Why is a table preferred over the NTC equation?
Because computing exponentials and logarithms on a small micro is slow and pulls in the math library, which takes up a lot of flash. A table with linear interpolation gives the same practical result in a few microseconds.
What advantages and disadvantages does a solid-state relay have compared with a mechanical one?
In its favor: no moving parts, silent, millions of operations, switches at the zero crossing. Against: AC only, dissipates heat and needs a heat sink, and when it fails it usually stays shorted, with the load on.
What is zero-crossing detection for?
To know when each half-cycle begins and be able to fire the triac with the delay that corresponds to the phase control. Along the way, it gives a very stable timebase of 100 pulses per second and warns if the power is cut.
Why is the brightness of an LED not perceived as proportional to the duty cycle?
Because the eye responds in an approximately logarithmic way: it distinguishes changes at low levels very well and very little at high ones. It is corrected by applying a curve —gamma— to the PWM value.
A 1500 W heating element on 220 V: what current does the actuator switch?
I = P/V = 1500/220 = 6.8 A. A relay or SSR well above that is chosen —15 or 25 A— so that it runs cool and tolerates the initial peak.
What should the program do if the sensor is disconnected?
Detect it: a reading of zero or full scale is not a valid temperature. When that happens it must turn the load off and raise an alert, never keep controlling with impossible data.
Why can safety not depend on the program?
Because the program can hang, the output can be left active and the relay can stick. The protection has to be independent and hardwired: a manual-reset thermostat in series, a thermal fuse, a mechanical limiter.