17 KiB
Interact with the physical world using sensors and actuators
Sketchnote by Nitya Narasimhan. Click the image for a larger version.
This lesson was part of the Hello IoT series from the Microsoft Reactor. It was delivered in two videos: a 1-hour lesson and a 1-hour office hour session that explored the lesson in more detail and answered questions.
🎥 Click the images above to watch the videos
Pre-lecture quiz
Introduction
This lesson introduces two key components of IoT devices: sensors and actuators. You'll get hands-on experience with both by adding a light sensor to your IoT project and then incorporating an LED that reacts to light levels, effectively creating a nightlight.
In this lesson, we'll cover:
What are sensors?
Sensors are hardware devices that detect and measure properties of the physical world, sending this information to an IoT device. They can measure a wide range of things, from environmental factors like air temperature to physical interactions like movement.
Common types of sensors include:
- Temperature sensors - measure air temperature or the temperature of objects they are in contact with. These are often combined with air pressure and humidity sensors for hobbyist and developer use.
- Buttons - detect when they are pressed.
- Light sensors - measure light levels, including specific colors, UV light, IR light, or general visible light.
- Cameras - capture visual data by taking photos or streaming video.
- Accelerometers - measure movement in multiple directions.
- Microphones - detect sound, either general sound levels or directional sound.
✅ Research the sensors in your phone. What can it measure?
All sensors share a common trait: they convert what they detect into an electrical signal that can be interpreted by an IoT device. The way this signal is interpreted depends on the sensor and the communication protocol used.
Use a sensor
Follow the appropriate guide below to add a sensor to your IoT device:
Sensor types
Sensors can be classified as either analog or digital.
Analog sensors
Analog sensors are among the simplest types. They receive a voltage from the IoT device, adjust this voltage based on their components, and return a modified voltage that represents the sensor's reading.
🎓 Voltage measures the force that moves electricity from one point to another, such as from the positive terminal of a battery to the negative terminal. For example, a standard AA battery is 1.5V (volts) and can push electricity with a force of 1.5V. Different devices require different voltages to operate. For instance, an LED can light up with 2-3V, while a 100W filament bulb needs 240V. Learn more about voltage on the Voltage page on Wikipedia.
A potentiometer is an example of an analog sensor. It’s a dial that can be rotated between two positions, and the sensor measures the rotation.
The IoT device sends an electrical signal to the potentiometer, such as 5 volts (5V). As the potentiometer is adjusted, it changes the voltage that comes out. For example, if the potentiometer is set to the "off" position, 0V will come out. If it’s set to the "on" position, 5V will come out.
🎓 This is a simplified explanation. Learn more about potentiometers on the potentiometer Wikipedia page.
The voltage returned by the sensor is read by the IoT device, which can then respond accordingly. For example, an analog temperature sensor based on a thermistor changes its resistance based on temperature. The output voltage can be converted into a temperature value in Kelvin, Celsius, or Fahrenheit using code.
✅ What happens if the sensor returns a higher voltage than was sent (e.g., from an external power source)? ⛔️ DO NOT test this.
Analog to digital conversion
IoT devices are digital—they only work with 0s and 1s. Analog sensor values must be converted to digital signals before they can be processed. Many IoT devices have analog-to-digital converters (ADCs) for this purpose. Sensors can also use ADCs via connector boards. For example, in the Seeed Grove ecosystem, analog sensors connect to specific ports on a "hat" attached to a Raspberry Pi. This hat has an ADC that converts the voltage into a digital signal.
Imagine an analog light sensor connected to an IoT device running at 3.3V, returning a value of 1V. This 1V needs to be converted into a digital value. For example, the Seeed Grove light sensor outputs values from 0 to 1,023. At 3.3V, a 1V output would correspond to a value of 300. The IoT device would then convert this value into binary, such as 0000000100101100
.
✅ If you’re unfamiliar with binary, research how numbers are represented using 0s and 1s. The BBC Bitesize introduction to binary lesson is a great starting point.
From a coding perspective, libraries provided with sensors handle this conversion. For example, the Grove light sensor’s Python library lets you call the light
property, or the Arduino library lets you use analogRead
to get a value like 300.
Digital sensors
Digital sensors also detect changes in electrical voltage but output a digital signal. They either measure two states or use a built-in ADC. Digital sensors are increasingly common because they eliminate the need for external ADCs.
The simplest digital sensor is a button or switch, which has two states: on or off.
IoT devices can directly measure this signal as 0 or 1. If the voltage sent matches the voltage returned, the value is 1; otherwise, it’s 0.
💁 Voltages are rarely exact due to resistance in components, so there’s usually a tolerance. For example, Raspberry Pi GPIO pins work on 3.3V and interpret signals above 1.8V as 1 and below 1.8V as 0.
- 3.3V goes into the button. The button is off, so 0V comes out, giving a value of 0.
- 3.3V goes into the button. The button is on, so 3.3V comes out, giving a value of 1.
More advanced digital sensors measure analog values and convert them to digital signals using built-in ADCs. For example, a digital temperature sensor uses a thermocouple to measure voltage changes caused by temperature. Instead of returning an analog value, it converts the reading to digital data and sends it to the IoT device.
Digital sensors can send more detailed or even encrypted data. For example, a camera captures images and sends them as digital data, often in a compressed format like JPEG. It can also stream video by sending frames or compressed video streams.
What are actuators?
Actuators are the opposite of sensors—they convert electrical signals from an IoT device into physical actions, such as emitting light or sound or moving a motor.
Common types of actuators include:
- LED - emits light when turned on.
- Speaker - produces sound, ranging from simple buzzers to audio speakers that play music.
- Stepper motor - rotates a specific amount, such as turning a dial 90°.
- Relay - acts as a switch controlled by an electrical signal, allowing a small voltage to control larger voltages.
- Screens - display information, ranging from simple LED displays to high-resolution monitors.
✅ Research the actuators in your phone. What can it do?
Use an actuator
Follow the appropriate guide below to add an actuator to your IoT device. You'll use a sensor to control an actuator, creating an IoT nightlight that turns on an LED when light levels are too low.
Actuator types
Like sensors, actuators can be analog or digital.
Analog actuators
Analog actuators take an analog signal and convert it into a physical action, with the action varying based on the voltage supplied.
One example is a dimmable light, where the brightness depends on the voltage provided.
Just like with sensors, IoT devices operate using digital signals, not analog. To send an analog signal, the IoT device requires a digital-to-analog converter (DAC), which can either be built into the IoT device itself or included on a connector board. This converter transforms the 0s and 1s from the IoT device into an analog voltage that the actuator can use.
✅ What do you think would happen if the IoT device sends a voltage higher than what the actuator can handle?
⛔️ DO NOT test this out.
Pulse-Width Modulation
Another way to convert digital signals from an IoT device into an analog signal is through pulse-width modulation (PWM). This method involves sending a series of rapid digital pulses that mimic an analog signal.
For instance, PWM can be used to control the speed of a motor.
Imagine controlling a motor powered by a 5V supply. You send a short pulse to the motor, switching the voltage to high (5V) for 0.02 seconds. During this time, the motor rotates one-tenth of a turn, or 36°. The signal then pauses for 0.02 seconds, sending a low signal (0V). Each on-off cycle lasts 0.04 seconds, and the cycle repeats.
This means that in one second, you send 25 pulses of 5V, each lasting 0.02 seconds, causing the motor to rotate. Each pulse is followed by a 0.02-second pause at 0V, during which the motor does not rotate. Since each pulse rotates the motor by one-tenth of a turn, the motor completes 2.5 rotations per second. Using a digital signal, you've made the motor rotate at 2.5 rotations per second, or 150 revolutions per minute (a common measure of rotational speed).
25 pulses per second x 0.1 rotations per pulse = 2.5 rotations per second
2.5 rotations per second x 60 seconds in a minute = 150rpm
🎓 When a PWM signal is on for half the time and off for the other half, it is called a 50% duty cycle. Duty cycles are expressed as the percentage of time the signal is in the "on" state compared to the "off" state.
You can adjust the motor speed by changing the duration of the pulses. For example, using the same motor, you can keep the cycle time at 0.04 seconds but reduce the "on" pulse to 0.01 seconds and increase the "off" pulse to 0.03 seconds. This results in the same number of pulses per second (25), but each "on" pulse is half as long. A shorter pulse rotates the motor by one-twentieth of a turn, and at 25 pulses per second, the motor completes 1.25 rotations per second, or 75 RPM. By modifying the pulse duration of a digital signal, you've halved the speed of an analog motor.
25 pulses per second x 0.05 rotations per pulse = 1.25 rotations per second
1.25 rotations per second x 60 seconds in a minute = 75rpm
✅ How would you ensure the motor rotates smoothly, especially at low speeds? Would you use a small number of long pulses with long pauses, or many very short pulses with very short pauses?
💁 Some sensors also use PWM to convert analog signals into digital signals.
🎓 You can learn more about pulse-width modulation on the pulse-width modulation page on Wikipedia.
Digital actuators
Digital actuators, like digital sensors, either have two states controlled by a high or low voltage or include a built-in DAC to convert a digital signal into an analog one.
A simple example of a digital actuator is an LED. When the device sends a digital signal of 1, a high voltage is applied, lighting up the LED. When the device sends a digital signal of 0, the voltage drops to 0V, and the LED turns off.
✅ Can you think of other simple two-state actuators? One example is a solenoid, which is an electromagnet that can be activated to perform tasks like moving a door bolt to lock or unlock a door.
More advanced digital actuators, such as screens, require digital data to be sent in specific formats. These actuators often come with libraries that simplify the process of sending the correct data to control them.
🚀 Challenge
In the last two lessons, the challenge was to list as many IoT devices as you could find in your home, school, or workplace and determine whether they are built around microcontrollers, single-board computers, or a combination of both.
For each device you listed, identify the sensors and actuators it is connected to. What is the purpose of each sensor and actuator in these devices?
Post-lecture quiz
Review & Self Study
- Learn about electricity and circuits on ThingLearn.
- Explore the different types of temperature sensors in the Seeed Studios Temperature Sensors guide.
- Read about LEDs on the Wikipedia LED page.
Assignment
Research sensors and actuators
Disclaimer:
This document has been translated using the AI translation service Co-op Translator. While we aim for accuracy, please note that automated translations may include errors or inaccuracies. The original document in its native language should be regarded as the authoritative source. For critical information, professional human translation is advised. We are not responsible for any misunderstandings or misinterpretations resulting from the use of this translation.