Chapter 5
Sensors, Actuators and Hardware Interfacing
Diploma in IOT
Embedded Systems for IoT
•
Diploma
Sensors, Actuators and Hardware Interfacing
Introduction
An Internet of Things device connects the physical world to software. Sensors measure physical conditions such as temperature, light, pressure, motion, or humidity. The embedded controller processes these measurements and communicates them to a local or cloud application. Actuators perform the reverse function: they convert electrical commands into physical actions such as switching a lamp, moving a motor, opening a valve, or displaying information.
Reliable IoT hardware interfacing requires more than connecting wires. The designer must consider signal type, voltage and current limits, timing, electrical noise, grounding, isolation, calibration, protection, and safe power switching. A sensor may produce an analog voltage, a digital logic level, a pulse train, or data through a communication bus. Similarly, an actuator may require a current greater than a microcontroller pin can provide. Correct interface design therefore combines electronics, embedded programming, measurement theory, and practical construction.
Learning Objectives
After completing this chapter, a student should be able to:
Distinguish analog, digital, pulse-based, and bus-connected sensors.
Explain GPIO, ADC, DAC, PWM, pull-up, pull-down, and debouncing.
Calculate ADC resolution and interpret sensor readings.
Design basic interfaces for LEDs, relays, motors, buzzers, and displays.
Apply signal conditioning, calibration, filtering, and voltage-level compatibility.
Explain the purpose of isolation, protection components, and proper grounding.
Construct and test a basic prototype using a schematic and suitable wiring practices.
Diagnose common hardware-interface faults in an IoT device.
1. The Measurement and Control Chain
A typical embedded IoT node follows this chain:
Physical quantity → sensing element → signal conditioning → ADC or digital interface → microcontroller → driver stage → actuator → physical result.
A temperature sensor may convert temperature into a voltage. An ADC converts that voltage into a binary number. Firmware interprets the number and decides whether to activate a fan. The fan generally cannot be connected directly to a GPIO pin, so a transistor or motor-driver circuit supplies the required current.
Sensor to actuator signal chain
Figure: Sensor to actuator signal chain
Figure: General signal chain from a physical quantity to an actuator.
2. Sensors and Signal Types
2.1 Analog sensors
An analog sensor produces a continuously varying electrical quantity. Examples include thermistors, photoresistors, strain gauges, potentiometers, and many gas sensors. The output may be a voltage, current, or resistance. Because a microcontroller processes digital values, an analog voltage normally passes through an ADC.
A sensor output must remain within the ADC input range. If an ADC accepts 0–3.3 V, applying 5 V can produce an incorrect reading or permanently damage the input. A voltage divider, amplifier, or level-shifting circuit may be needed.
2.2 Digital sensors
A digital sensor may provide a simple logic state, such as a limit switch, or may transmit coded data. I²C, SPI, UART, 1-Wire, and CAN are common interfaces. Digital communication reduces the need for the controller to interpret a small analog voltage, but it introduces requirements involving addresses, clock rates, timing, pull-up resistors, and protocol software.
2.3 Pulse and frequency outputs
Some sensors encode a measurement as pulse frequency, pulse width, or pulse count. A flow meter, for example, may produce one pulse for a defined volume. The controller measures the number of pulses during a time interval. Pulse inputs should be protected from excessive voltage and configured with suitable pull-up or pull-down resistors.
2.4 Sensor selection
Selection should consider range, accuracy, resolution, response time, repeatability, operating temperature, power consumption, environmental protection, interface type, and long-term availability. A sensor with high nominal accuracy is not useful if the installation introduces vibration, condensation, electromagnetic interference, or a temperature range outside its specification.
3. GPIO and Digital Interfacing
A general-purpose input/output (GPIO) pin can usually be configured as an input or output. In input mode, the pin measures a logic level. In output mode, it drives a logic-high or logic-low state. The logic thresholds are specified by the microcontroller manufacturer; they should never be inferred solely from the supply voltage.
3.1 Pull resistors
A floating input has no defined voltage and may change state because of noise. A pull-up resistor connects the input weakly to the supply, while a pull-down resistor connects it weakly to ground. A push-button circuit commonly uses a pull-up: the input reads high when the button is open and low when the button connects it to ground.
The resistor must be low enough to resist noise but high enough to limit unnecessary current. When a 3.3 V supply is connected to ground through a 10 kΩ resistor while a switch is pressed, the current is approximately:
I = V/R = 3.3 V / 10 000 Ω = 0.33 mA
Here, I is current, V is voltage, and R is resistance. This calculation assumes the switch resistance is negligible compared with the resistor.
3.2 Switch debouncing
Mechanical contacts do not change state cleanly. They may bounce for several milliseconds, causing multiple transitions. Debouncing can be implemented in hardware with an RC network and a logic input, or in software by accepting a new state only after it remains stable for a specified interval. A robust program records the transition time, waits for the debounce interval, and then verifies the input again.
3.3 LED interfacing
An LED requires a current-limiting resistor. For a simple series circuit:
R = (VS − VF) / IF
R is resistance in ohms, VS is the supply voltage, VF is the LED forward voltage, and IF is desired forward current in amperes. The equation assumes a single LED, a resistive current limiter, and a DC supply. For a 3.3 V supply, a red LED with VF = 2.0 V and a desired current of 5 mA requires:
R = (3.3 − 2.0) / 0.005 = 260 Ω
A standard 270 Ω resistor is a practical choice. The GPIO current rating and total port-current limit must also be respected.
4. ADCs, DACs and Analog Signal Conditioning
4.1 ADC operation and resolution
An n-bit ADC represents an input using 2ⁿ code levels. An ideal ADC with reference voltage VREF has an approximate voltage step, or least significant bit (LSB), of:
VLSB = VREF / (2n − 1)
The denominator convention depends on how endpoint codes are defined; the expression above is commonly used for the code range 0 through 2ⁿ − 1. It is an idealized resolution calculation and excludes offset error, gain error, noise, and nonlinearity.
For a 12-bit ADC with a 3.3 V reference:
VLSB = 3.3 / 4095 ≈ 0.806 mV
The ideal conversion from code N is:
VIN ≈ N × VREF / (2n − 1)
If N = 2048, n = 12, and VREF = 3.3 V, then VIN ≈ 1.650 V. The result assumes a stable reference and an input within the ADC range.
4.2 Voltage dividers
A voltage divider scales an input voltage. With resistors R1 and R2 in series, where R2 is connected to ground:
VOUT = VIN × R2 / (R1 + R2)
The equation assumes the output is unloaded or that the load resistance is included in the effective value of R2. For a 5 V signal reduced to approximately 3.3 V, resistor values must be selected so that the maximum possible input, not merely its nominal value, remains safe.
4.3 Signal conditioning
Signal conditioning improves compatibility and measurement quality. Typical functions include:
amplification of small signals;
attenuation of excessive voltages;
buffering with an operational amplifier;
low-pass filtering to reduce high-frequency noise;
protection using series resistors, clamps, or transient suppressors;
linearization of nonlinear sensors;
isolation between electrically different domains.
For a first-order RC low-pass filter, the cutoff frequency is:
fC = 1 / (2πRC)
R is resistance in ohms, C is capacitance in farads, and fC is frequency in hertz. Frequencies substantially below fC pass with little attenuation, while higher frequencies are increasingly attenuated. The formula assumes an ideal first-order network and negligible loading.
A filter should not be chosen only because it reduces noise. If a sensor changes rapidly, excessive filtering creates delay and may hide important events.
4.4 Calibration
Calibration establishes the relationship between the measured code and a reference quantity. A two-point linear calibration can be written as:
y = a x + b
where x is the raw measurement, y is the corrected value, a is gain, and b is offset. Using two known reference points (x1,y1) and (x2,y2):
a = (y2 − y1)/(x2 − x1), b = y1 − a x1
This method assumes the sensor response is sufficiently linear over the operating range. Calibration should record the reference instrument, environmental conditions, date, and uncertainty. A calibrated system is not necessarily accurate outside the range or conditions used for calibration.
4.5 DACs and PWM as analog substitutes
A digital-to-analog converter (DAC) produces an analog voltage or current from a digital code. An ideal n-bit unipolar DAC has an approximate output step of VREF/(2n − 1), subject to the architecture and datasheet definitions.
Pulse-width modulation (PWM) rapidly switches a digital output. The duty cycle is:
D = tON / T
where tON is the on-time and T is the period. For an ideal low-pass load, the average output is approximately:
VAVG ≈ D × VH
where VH is the high-level voltage. This approximation applies to loads or filters that respond primarily to average value. PWM is suitable for LED brightness and motor speed control, but it is not automatically a low-noise precision DAC.
5. Driving Actuators
5.1 Why a driver is required
A microcontroller GPIO is a signal source, not a general-purpose power supply. Motors, solenoids, relays, heaters, and lamps may draw high current or generate voltage transients. A driver stage separates logic control from load power.
A low-side N-channel MOSFET is commonly used for a DC load. The GPIO drives the gate through an appropriate connection, the source is connected to ground, and the load connects between the positive supply and the drain. A gate resistor may limit transient current, and a gate-to-ground resistor may ensure that the MOSFET remains off during reset.
5.2 Flyback protection
Inductive loads store energy in their magnetic field. When current is interrupted, the collapsing field can generate a large voltage. A flyback diode is connected across a DC relay coil or motor, reverse-biased during normal operation and forward-biased during the turn-off transient. The diode must have suitable reverse-voltage, current, and recovery specifications.
For rapidly switched loads, a standard diode may be too slow or may reduce turn-off speed. A TVS device, snubber, or specialized driver may be more appropriate.
5.3 Relays
A relay provides electrical switching and, in some configurations, galvanic separation between control and load circuits. A relay module may include a transistor driver, flyback diode, indicator, and optocoupler. The designer must still verify contact voltage, contact current, inrush current, insulation rating, creepage, clearance, and load category.
Mains voltage is hazardous. A low-voltage IoT prototype should not expose mains conductors on a breadboard. Mains switching requires an enclosed, compliant assembly and appropriate electrical safety practice.
5.4 Motors
DC motors produce electrical noise and a high starting current. Motor speed can be controlled by PWM, while direction requires an H-bridge. Stepper motors require a current-controlled driver and a suitable stepping sequence. Servomotors commonly accept a periodic control signal whose pulse width represents the target position, but the exact timing range is device-dependent.
The motor supply should generally be separate from the logic supply, with a common reference where non-isolated control is used. Decoupling capacitors should be placed close to the driver and motor supply pins. The power supply must tolerate starting and stall current.
6. Displays and Human–Machine Interfaces
Indicators include LEDs, seven-segment displays, character LCDs, graphical LCDs, and OLEDs. A display interface may use GPIO, shift registers, I²C, SPI, or a parallel bus. Multiplexed displays reduce pin count but require periodic scanning and current management.
Displays are loads with timing and power requirements. An I²C display normally requires pull-up resistors on SDA and SCL. The bus voltage must be compatible with every connected device, and the total pull-up strength must suit the bus speed and capacitance. A display should not be powered from a GPIO pin unless the device current is explicitly within the pin specification.
7. Voltage Compatibility, Grounding and Isolation
Logic levels are defined by voltage ranges, not labels such as “3.3 V device” or “5 V device.” A 3.3 V output may not satisfy the guaranteed high-input threshold of a 5 V receiver. Conversely, a 5 V output may exceed the absolute maximum rating of a 3.3 V input.
Possible solutions include resistor dividers for one-direction, low-speed signals; dedicated unidirectional level translators; bidirectional translators for buses; open-drain interfaces; and logic buffers. The correct solution depends on speed, direction, voltage range, current, and bus topology.
Ground loops, long wires, and shared motor currents can introduce measurement errors. Good practice includes short signal paths, suitable decoupling, star or controlled grounding where appropriate, separation of noisy power paths, and twisted or shielded cables for sensitive signals. Shielding must be connected according to the system’s EMC strategy; connecting every shield indiscriminately can create new problems.
Isolation is appropriate when circuits have different ground potentials, when long external cables are involved, or when safety requires separation. Optocouplers, isolated DC–DC converters, digital isolators, and isolated transceivers are common methods. Isolation is meaningful only when the PCB layout and power paths preserve the isolation barrier.
8. Schematic and PCB Prototyping Practice
A schematic is the engineering description of electrical relationships; a breadboard is a temporary physical implementation. Begin with a block diagram, then draw the schematic with named nets, connector labels, supply rails, protection parts, and test points. Include decoupling capacitors near integrated circuits and show unused pins according to the device documentation.
Before powering a prototype:
Confirm polarity and supply voltage.
Check for unintended shorts with power removed.
Verify resistor, capacitor, diode, and transistor orientation.
Confirm that all grounds are connected as intended.
Measure the supply before connecting the controller.
Test the sensor and actuator separately.
Increase complexity gradually and record observations.
Breadboards are useful for low-frequency, low-current experiments, but they have parasitic capacitance, unreliable contacts, and poor suitability for motors, switching power, high-speed buses, and mains circuits. A PCB provides repeatability and shorter connections. PCB design should consider trace width, return-current paths, thermal dissipation, connector strength, test access, creepage, clearance, and enclosure requirements.
9. Worked Integration Example: Automatic Ventilation Node
The following illustrative case concerns a small greenhouse ventilation controller. A digital temperature sensor reports temperature to a 3.3 V microcontroller. A 12 V brushless fan is switched by a MOSFET. An LED indicates an alarm, and a push-button allows manual override.
Background and problem
The greenhouse overheats when temperature rises above a target value. The fan draws more current than a GPIO can supply, and the electrical noise from the fan causes occasional controller resets.
Analysis
The design requires a digital sensor interface, a power driver, protection, user input, and a control policy. The fan supply must support its running and starting current. The sensor must be located away from the fan’s hot motor and switching node. A hysteresis band is needed to prevent rapid on–off cycling.
Approach and solution
The sensor is connected to a compatible digital bus with the required pull-up resistors. The fan is connected to the 12 V rail and switched by a logic-level N-channel MOSFET. A flyback path or the protection specified by the fan-driver manufacturer is installed. A separate bulk capacitor is placed near the fan connection, while smaller ceramic capacitors are placed close to the controller and sensor.
The firmware uses two thresholds: the fan turns on at 30 °C and turns off at 28 °C. If T is measured temperature and S is the fan state, the policy is:
S = ON if T ≥ 30 °C S = OFF if T ≤ 28 °C
Between 28 °C and 30 °C, the previous state is retained. This hysteresis prevents frequent switching when the measured temperature fluctuates near a single threshold. The push-button is debounced in software, and the LED is driven through a resistor.
Outcome
The fan operates without overloading the controller pin, and the bulk capacitor and improved grounding reduce resets. Testing is performed at minimum, nominal, and maximum supply conditions. The final system should also be tested with a blocked fan, sensor disconnection, and controller reset to ensure that the actuator enters a safe state.
Learning points
The case demonstrates that successful interfacing depends on the complete electrical system. Sensor accuracy alone cannot solve poor grounding, and correct firmware cannot compensate for an undersized driver or unsafe power arrangement.
10. Testing, Fault Diagnosis and Professional Practice
A structured test process begins with inspection, continuity testing, supply verification, and single-function tests. An oscilloscope or logic analyser is valuable for observing PWM, serial buses, switch bounce, and supply transients. A multimeter is sufficient for many voltage, resistance, and continuity checks, but it may miss brief disturbances.
IoT equipment may collect environmental or personal data and may control safety-relevant equipment. Designs should fail safely, restrict unauthorized actuator commands, protect credentials, and document assumptions. Electrical products may be subject to local wiring, EMC, radio, and product-safety regulations. A classroom prototype is not automatically suitable for commercial deployment.
11. Contemporary Developments
Modern sensor modules increasingly integrate signal conditioning, calibration memory, digital conversion, and self-diagnostics. Smart actuators may expose current, temperature, fault, and position feedback. Edge processing can detect events locally, reducing bandwidth and improving response time. Machine-learning models can assist predictive maintenance, but they do not replace electrical protection, calibration, or deterministic safety controls.
Designers are also adopting modular connectors, standardized industrial buses, low-power wireless sensor nodes, energy harvesting, and digitally controlled power stages. These developments increase capability but also increase software, cybersecurity, and lifecycle responsibilities. A technically advanced module remains unsuitable if its supply limits, protocol behavior, environmental rating, or long-term support are unknown.
Best-Practice Checklist
Read the complete datasheet, including absolute maximum ratings.
Never exceed the input voltage or GPIO current specification.
Prevent floating inputs with defined biasing.
Use current limiting for LEDs and suitable drivers for power loads.
Protect inductive loads from turn-off transients.
Check logic-level compatibility in both directions.
Use stable references and calibrate measurements where accuracy matters.
Place decoupling and bulk capacitors according to current paths.
Separate noisy actuator wiring from sensitive analog wiring.
Prototype incrementally and test fault conditions.
Keep hazardous voltages enclosed and isolated.
Document the schematic, firmware assumptions, calibration data, and test results.
Chapter Summary
Sensors translate physical conditions into electrical signals, while actuators convert controller commands into physical action. Interfacing begins by identifying whether a signal is analog, digital, pulse-based, or bus-connected. GPIO inputs require defined logic states, and switches often require debouncing. ADC resolution, reference voltage, voltage dividers, filtering, and calibration determine how accurately analog measurements can be interpreted. DACs and PWM can generate controlled analog-like outputs, but their limitations must be understood.
Power actuators should normally be driven through transistors, MOSFETs, bridges, or dedicated modules rather than directly from GPIO pins. Motors and relay coils require protection against inductive transients. Voltage-level compatibility, grounding, decoupling, isolation, and PCB layout are essential to dependable operation. Finally, safe prototyping requires systematic testing, documentation, environmental consideration, and compliance with applicable regulations.
Key Terms
Sensor: A device or element that responds to a physical quantity.
Actuator: A device that converts an electrical command into physical action.
GPIO: A configurable digital input/output connection on a controller.
ADC: An analog-to-digital converter that produces a digital code from an analog input.
DAC: A digital-to-analog converter that produces an analog output from a digital code.
PWM: Pulse-width modulation using duty cycle to control average power or signal value.
Resolution: The smallest ideal change represented by a measurement or conversion system.
Reference voltage: A voltage used as the scale for an ADC or DAC.
Pull-up resistor: A resistor that biases a signal toward the positive supply.
Pull-down resistor: A resistor that biases a signal toward ground.
Debouncing: Suppression of false transitions caused by mechanical switch bounce.
Signal conditioning: Processing or protection that makes a signal suitable for measurement or control.
Calibration: Establishing and applying a relationship between an instrument reading and a reference.
Hysteresis: Different switching thresholds for activation and deactivation.
Flyback diode: A diode that limits the voltage produced when an inductive load is switched off.
Level shifter: A circuit that translates logic signals between voltage domains.
Galvanic isolation: Separation that prevents direct conductive current between circuit sections.
Decoupling capacitor: A capacitor placed near a circuit to supply transient current and reduce noise.
Duty cycle: The fraction of each period for which a periodic signal is active.
Creepage and clearance: Safety distances over surfaces and through air between conductors.
Review Questions
Short Answer Questions
Why should a motor generally not be connected directly to a microcontroller GPIO pin?
What is the purpose of a pull-up resistor on a digital input?
Define ADC resolution and state two factors that limit practical measurement accuracy.
Why is a flyback diode used with a relay coil?
What is switch debouncing, and how can it be implemented?
Long Answer Questions
Explain the complete signal path from an analog temperature sensor to a switched fan, including conditioning, ADC conversion, firmware, and power driving.
Describe the design and testing considerations for a PWM-controlled DC motor interface.
Compare analog sensors, digital sensors, and pulse-output sensors in terms of wiring, processing, noise, and application suitability.
Explain voltage-level compatibility, common-ground requirements, and galvanic isolation with suitable examples.
Discuss best practices for converting a breadboard prototype into a reliable PCB-based IoT product.
Analytical / Application Questions
A 12-bit ADC uses a 3.3 V reference. Calculate its ideal LSB size and estimate the input voltage for code 3000. State the assumptions used.
Design a resistor-limited LED interface for a 5 V supply, an LED forward voltage of 2.1 V, and a desired current of 8 mA. Select a practical standard resistor and calculate the actual approximate current.
An IoT pump causes a controller to reset whenever it starts. Propose a systematic electrical investigation and at least four design changes that could reduce the problem.
Multiple Choice Questions with Answers
Question 1
Which component normally converts an analog sensor voltage into a digital value for a microcontroller?
A. DAC
B. ADC
C. Relay
D. Oscillator
Correct Answer: B. ADC
Question 2
A 12-bit ADC provides how many distinct binary code values?
A. 12
B. 256
C. 1024
D. 4096
Correct Answer: D. 4096
Question 3
What is the main purpose of a flyback diode across a DC relay coil?
A. To increase coil resistance during normal operation
B. To limit the voltage transient when current is interrupted
C. To convert the coil current into an analog signal
D. To provide optical isolation
Correct Answer: B. To limit the voltage transient when current is interrupted
Question 4
For an ideal PWM signal driving a suitable averaging load, increasing the duty cycle generally causes the average output voltage to:
A. Decrease
B. Remain exactly zero
C. Increase
D. Become independent of the supply
Correct Answer: C. Increase
Question 5
A digital input that is left without a defined pull-up or pull-down is described as:
A. Isolated
B. Floating
C. Calibrated
D. Multiplexed
Correct Answer: B. Floating
Question 6
What is the principal reason for using a transistor or MOSFET driver with a motor?
A. The driver removes the need for a power supply
B. The driver allows the GPIO to handle a high-power load safely
C. The driver converts every motor into a sensor
D. The driver guarantees zero electromagnetic noise
Correct Answer: B. The driver allows the GPIO to handle a high-power load safely
Question 7
For a first-order RC low-pass filter, the cutoff frequency is given by which expression?
A. fC = 2πRC
B. fC = R/C
C. fC = 1/(2πRC)
D. fC = C/(2πR)
Correct Answer: C. fC = 1/(2πRC)
Question 8
A 3.3 V microcontroller input is connected directly to a 5 V logic output. What is the primary concern?
A. The input may exceed its permitted voltage rating
B. The signal will always become optical
C. The ADC resolution will automatically double
D. The output will necessarily become floating
Correct Answer: A. The input may exceed its permitted voltage rating
Question 9
Why is hysteresis used in the greenhouse fan example?
A. To increase ADC bit depth
B. To prevent rapid switching near one threshold
C. To eliminate the need for a motor driver
D. To reverse motor direction
Correct Answer: B. To prevent rapid switching near one threshold
Question 10
Which practice is most appropriate before applying power to a new prototype?
A. Connect the largest available power supply immediately
B. Ignore polarity because protection is automatic
C. Inspect wiring, check for shorts, and verify the supply voltage
D. Remove all decoupling capacitors
Correct Answer: C. Inspect wiring, check for shorts, and verify the supply voltage