Common Sensors

  • Temperature and humidity: LM35 (analog, 10 mV per °C), thermistor, DHT11 (digital, about ±2 °C and 20 to 80 percent humidity) and DHT22 (better accuracy).
  • Motion:PIR sensor detects the infrared change caused by a moving warm body and gives a digital HIGH output; an accelerometer (MPU6050) measures movement and tilt.
  • Distance:ultrasonic sensor (HC-SR04) measures the time of an echo: distance = (time × 343 m/s) / 2.
  • Light, gas and sound: LDR, MQ-series gas sensors and sound modules.

Sensors are read with digitalRead() for digital values and analogRead() (a 0 to 1023 value on a 10-bit ADC) for analog values, or through buses such as I2C and SPI.

Common Actuators

LED and buzzer (digital output), relay (switches a high-power load such as a lamp or pump), DC motor (speed by PWM, direction through an H-bridge driver), servo motor (angle set by the width of a pulse) and stepper motor (moves a fixed angle per step). PWM (pulse-width modulation) switches an output on and off quickly; the duty cycle (percentage of time ON) sets the average voltage, so it controls LED brightness and motor speed. In Arduino, analogWrite(pin, 0 to 255) gives PWM.

Arduino and Raspberry Pi

Arduino boards (Uno, Nano) are microcontroller boards programmed in C/C++ with the Arduino IDE through two functions: setup() (runs once) and loop() (runs forever). They have no operating system, respond in real time and use little power. ESP32 and ESP8266 boards add Wi-Fi. Raspberry Pi is a single-board computer running Linux, programmed in Python and others, with USB, HDMI, networking and GPIO pins. It suits gateways, edge processing, cameras and databases. A common design uses Arduino or ESP32 for sensing and a Raspberry Pi as the gateway. TinkerCAD Circuits can simulate Arduino circuits without hardware.

Definition: PWM. Pulse-width modulation, a technique that switches a digital output on and off rapidly so that the proportion of time it is on (the duty cycle) sets the average power delivered to a device.

Table 3.1: Arduino and Raspberry Pi

Feature Arduino Raspberry Pi
Type Microcontroller board Single-board computer
Operating system None Linux
Language C/C++ Python, C, and others
Strength Real-time, low power Processing, networking
Role Sensing and actuation Gateway and edge

Example 3.1: PWM brightness and servo angle

Problem: An LED is driven with analogWrite(pin, 64) at 5 V. A servo uses a 1 ms pulse for 0 degrees and a 2 ms pulse for 180 degrees (linear). Find the duty cycle, average voltage, and the pulse width for 15 degrees.

Solution:

  1. Duty cycle = 64 / 255 = 25.1 percent.
  2. Average voltage = 0.251 × 5 = 1.25 V.
  3. Pulse width = 1 ms + (15 / 180) × 1 ms = 1.083 ms.

Result: The LED gets a 25.1 percent duty cycle (about 1.25 V average) and the servo needs a 1.083 ms pulse for 15 degrees.

val, vcc = 64, 5.0
duty = val / 255
print("Duty cycle (%):", round(100 * duty, 1))
print("Average voltage (V):", round(duty * vcc, 2))
angle = 15
print("Servo pulse (ms):", round(1 + angle / 180, 3))

Output

Duty cycle (%): 25.1
Average voltage (V): 1.25
Servo pulse (ms): 1.083

Common mistakes

  • Powering a motor directly from an Arduino pin.
  • Expecting analogWrite to give a true analog voltage; it is PWM.
  • Using a Raspberry Pi where a simple microcontroller would do.
  • Forgetting pull-up or pull-down resistors on switch inputs.

Key takeaways

  • Sensors in (analogRead, digitalRead), actuators out (digitalWrite, PWM).
  • Arduino: setup() once, loop() forever.
  • PWM duty cycle sets average power.
  • Raspberry Pi is a Linux computer for gateway and edge work.

Practice questions

  1. What is PWM?
  2. Differentiate setup() and loop().
  3. When is a Raspberry Pi better than an Arduino?

Answers to the practice questions

  1. Rapid switching of an output so the duty cycle sets the average power.
  2. setup() runs once at the start; loop() runs again and again.
  3. When Linux, networking or heavy processing is needed, as in a gateway or edge node.

Sources

  • Official BCA syllabus Internet of Things, course code 25BCA406DS01, Unit III