Catto / Topic Map · Digital Electronics III Year 6
Digital Electronics III · 144 h · Topic 4 of 7

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.

Control Microcontrollers Sensors Hysteresis Isolation

01The control chain

Every automatic control system has the same five links, and each one contributes its own error and delay:

Sensorquantity → V Conditioningamplify and filter MicrocontrollerA/D · decide · PWM Isolationopto · relay Actuatorheater · lamp feedback: the sensor measures the effect of what the actuator did No feedback means open-loop command, not control: the system cannot tell if it got what it wanted
Figure 1. The complete chain. The closed loop is what sets a control system apart from simple open-loop command: the decision is made by comparing the measurement with the setpoint.

02Measuring temperature

SensorOutputRangeWhat makes it special
NTCResistance−40 to 150 °CCheap and sensitive, but nonlinear: it needs a table or an equation. Read with a divider.
LM3510 mV/°C2 to 150 °CLinear and goes straight to the ADC. It cannot measure below zero without a negative supply.
PT100100 Ω at 0 °C−200 to 600 °CThe industrial standard: very stable and linear. It needs a bridge and an instrumentation amplifier.
Thermocouple~40 µV/°C (type K)−200 to 1200 °CFor furnaces. A tiny signal: needs a dedicated amplifier and cold-junction compensation.
DS18B20Digital, 1-Wire−55 to 125 °CComes calibrated and with a converter inside: it connects with a single data wire.
Worked example · Reading a 10 kΩ NTC

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.

R(T)=R25·eB(1T−1T25) With temperatures in kelvin: T25 = 298.15 K.
  • 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.

Self-heating

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.

26 °C · turns off 24 °C · turns on heater temperature The dead band keeps the relay from switching nonstop. The wider it is, the fewer operations and the more oscillation.
Figure 2. Hysteresis, animated. The heater turns on only when the temperature drops below the lower threshold and turns off when it passes the upper one. Between the two there is a dead band: the wider it is, the fewer switchings and the more the temperature oscillates.
// 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
How to choose the band
  • 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.
When on-off is not enough: PID

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.

LED with PWM duty cycle 15 % duty cycle 45 % duty cycle 80 % switches thousands of times per second Lamp with triac firing at 140° · 7 % of power firing at 90° · 50 % of power firing at 40° · 93 % of power chops each half-cycle of the mains The LED is regulated in DC at high frequency; the mains lamp, by deciding when the triac fires.
Figure 3. Two ways to regulate, animated. On the left, PWM on a DC-powered LED: it switches thousands of times per second and the eye integrates. On the right, phase control on the mains: the triac is fired later or earlier within each half-cycle.
LoadMethodPrecautions
DC LEDPWM above 200 Hz, or regulated currentBelow 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 mainsPhase control with a triacIt 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 itAn ordinary LED lamp on a triac dimmer flickers, buzzes or does not turn on: its switching power supply does not tolerate the chopping.
Fluorescent tubeDimmable electronic ballastIt is not regulated by phase.
Heater or thermal loadWhole cycles with zero crossingNo interference and no visible flicker, because thermal inertia averages it out.
Zero crossing

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

Electromechanical relay

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.

Solid-state relay

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.

The isolation rule

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.

Example · A complete thermostat

A unit that holds 24 to 26 °C with a 1500 W heating element on 220 V:

  1. Sensor: 10 k NTC in a divider, powered from a pin to avoid self-heating.
  2. Reading: 10-bit ADC, average of 16 samples, conversion table to tenths of a degree.
  3. Decision: hysteresis of ±1 °C and a minimum time of 60 s in each state.
  4. 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.
  5. 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

Lab 1 · The NTC curve

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.

Lab 2 · Hysteresis, measured

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.

Lab 3 · Brightness and perception

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.”

Lab 4 · Zero crossing

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.

Lab 5 · The program is not the protection

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

SymptomUsual cause
The relay switches nonstop at the thresholdNo hysteresis, or it is too narrow for the measurement noise.
The sensor reads one degree too highNTC self-heating: lower the current or power the divider only while measuring.
The measured temperature jumps when the load turns onNoise 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 switchesThe flyback diode is missing, or the decoupling capacitor, or the supply cannot handle the coil spike.
The LED lamp flickers with the dimmerIt is not a dimmable lamp: its switching power supply does not tolerate phase chopping.
The triac stays on and does not turn offInductive 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 endGamma correction is missing: the eye's perception is not linear.
The oven overshoots the setpoint and then drops too farThermal inertia: on-off control always overshoots. PID corrects it.
With the sensor disconnected the unit heats at full powerThe 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.

Development of the topic “Control with devices” of Digital Electronics III (Year 6), based on the “Curriculum Proposal – Second Cycle of the Technical-Vocational Track, Secondary Education – Electronics,” Ministry of Education of the Province of Córdoba, DGETyFP. Back to the Topic Map · catto.ar