Unit 6: Introduction of Arduino and Sensors

PHY175 — Modern Physics And Electronics 8 min read

I. Foundations of Sensor-Based Electronics

Arduino-based sensing systems connect physical quantities in the environment to electronic computation and control. A sensor detects a quantity such as light, distance, temperature, or infrared radiation; its electrical output is read by a microcontroller, processed according to a program, and used to drive an output device.

A. Defining Characteristics

The operation of every system in this unit depends on a chain from physical input to useful response.

  • Measurement chain: A typical system follows physical quantity -> sensor -> electrical signal -> Arduino -> output, where the output may be an LED, buzzer, display, motor, or transmitted data.
  • Transduction: A sensor is a transducer because it converts one form of energy or physical variation into an electrical quantity such as voltage, resistance, or timed pulses.
  • Signal conditioning: Components such as voltage dividers, amplifiers, and comparators adapt a raw sensor output to the voltage and logic levels accepted by Arduino.
  • Microcontroller role: The Arduino executes stored instructions, samples input pins, performs calculations, and changes output pins in response.
  • Electrical reference: Voltage is measured relative to ground (GND), so connected modules normally require a common ground with the Arduino.
  • Safe operation: On a 5 V Arduino Uno, input voltages should remain between 0 V and 5 V; excessive voltage or current can damage the ATmega328P microcontroller.

II. Electrical Signal Forms

Electrical information reaches a microcontroller mainly as continuously variable analog levels or discrete digital states.

A. Analog and digital signals

Analog signals represent a range of values, whereas digital signals use a finite set of logic states.

  1. Analog signals
    • Continuous variation: An analog voltage can ideally take any value within a range; an LDR voltage might change from 1.2 V to 3.7 V as illumination changes.
    • Arduino conversion: The Uno has a 10-bit analog-to-digital converter (ADC), so analogRead() converts approximately 0-5 V into integers from 0 to 1023.
    • Resolution: With a 5 V reference, one ADC count represents approximately:
TEXT
Resolution = Vref / 1024 = 5 V / 1024 = 4.88 mV per count

Here, Vref is the ADC reference voltage.

  1. Digital signals
    • Discrete levels: Binary signals use LOW and HIGH, conventionally associated with logic 0 and logic 1.
    • Input and output: digitalRead(pin) detects a state, while digitalWrite(pin, HIGH) drives an output near the board's supply voltage.
    • Timed digital data: Digital information may also be encoded in pulse duration or pulse sequences, as in ultrasonic Echo pulses and DHT sensor communication.
    • Contrast: Analog transmission preserves magnitude but is sensitive to noise; digital transmission gives clear states and supports encoded data but has finite resolution or timing constraints.

III. Arduino Uno Board

The Arduino Uno is a development board built around the ATmega328P microcontroller, providing accessible pins, power regulation, USB communication, and a programmable environment.

A. Arduino board (pin configuration and description)

The Uno's pin groups connect sensors and actuators to a 5 V, 16 MHz microcontroller.

  • Digital pins 0-13: Fourteen pins can be configured with pinMode() as INPUT, INPUT_PULLUP, or OUTPUT.
    • Pins 0 (RX) and 1 (TX) support serial communication.
    • Pins 2 and 3 support external interrupts.
    • Pins marked ~ (3, 5, 6, 9, 10, 11) provide pulse-width modulation through analogWrite().
  • Analog pins A0-A5: Six ADC inputs measure analog voltage; they may also be used as digital I/O. A4 is SDA and A5 is SCL for I2C communication.
  • SPI pins: Digital pins 10-13 provide SS, MOSI, MISO, and SCK functions; these are also available through the ICSP header.
  • Power pins: 5V and 3.3V supply modules, GND is the zero-volt reference, and Vin accepts an external input before regulation. IOREF indicates board logic voltage.
  • Reference and reset: AREF can provide an external analog reference, while pulling RESET low restarts the program.
  • Board interfaces: The USB connector supports programming, serial communication, and power; the DC jack accepts a suitable external supply. The onboard L LED is connected to pin 13.
  • Programming pattern: Initialization belongs in setup(), which runs once; repeated sensing and control belong in loop().
CPP
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}

B. Applications and Limitations

Arduino simplifies prototyping but does not remove electrical and timing constraints.

  • Applications: Common uses include data logging, alarms, automatic lighting, environmental monitoring, robotics, and basic Internet of Things nodes.
  • Current limit: I/O pins are intended for small currents, commonly designed around 20 mA or less per pin; motors and high-power loads require driver circuits.
  • ADC limitation: The Uno ADC measures voltage, not resistance or physical quantities directly, and its 10-bit readings can fluctuate because of noise or supply variation.
  • Memory and speed: The ATmega328P has limited memory and processing capacity compared with a computer, so complex imaging or large datasets need more capable hardware.

IV. Infrared Detection

An infrared sensing module detects infrared radiation, often to identify nearby objects without mechanical contact.

A. IR sensor

A common reflective IR sensor emits invisible infrared light and tests whether an object reflects part of it back.

  • Emitter: An IR LED commonly radiates near 940 nm, beyond visible red light.
  • Receiver: A photodiode or phototransistor changes current when reflected IR reaches it; dark and shiny surfaces produce different reflection levels.
  • Comparator module: Modules often use an LM393 comparator and an adjustable potentiometer to convert the receiver voltage into a digital output.
  • Connections: Typical pins are VCC, GND, and OUT; some boards also expose an analog output. Many obstacle modules make OUT LOW when an object is detected.
  • Arduino reading:
CPP
const int irPin = 2;

void setup() {
  pinMode(irPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  Serial.println(digitalRead(irPin));
  delay(50);
}
  • Applications: IR sensing supports obstacle detection, line-following robots, object counters, and short-range proximity switches.
  • Limitations: Sunlight, remote controls, target color, angle, and surface reflectivity can cause unreliable readings; reflective modules do not measure distance accurately.

V. Light-Dependent Resistance

An LDR is a passive light sensor whose resistance changes with incident illumination.

A. LDR

An LDR, or photoresistor, normally has high resistance in darkness and lower resistance under bright light.

  • Material behavior: Incident photons create additional charge carriers in the photosensitive semiconductor, increasing conductivity.
  • Nonlinear response: Resistance may fall from hundreds of kilohms or more in darkness to a few kilohms in strong light, but exact values depend on the device.
  • Voltage divider: Because Arduino measures voltage rather than resistance, the LDR is paired with a fixed resistor R:
TEXT
Vout = Vcc x R / (RLDR + R)

Here, Vcc is the supply voltage, RLDR is LDR resistance, R is the fixed resistor connected to ground, and Vout goes to an analog pin.

  • Interpretation: In this arrangement, brighter light lowers RLDR, increasing Vout and the analogRead() result. Reversing the divider reverses that trend.
  • Applications: LDRs are used in automatic streetlights, display brightness control, light alarms, and day-night detection.
  • Limitations: They respond relatively slowly, vary between units, and do not provide calibrated lux measurements without calibration.

VI. Ultrasonic Distance Measurement

An ultrasonic sensor determines distance by timing the round trip of a sound pulse whose frequency is above human hearing.

A. Basic principle of ultrasonic sensor

A module such as the HC-SR04 transmits an ultrasonic burst and measures the echo time from a target.

  • Triggering: A HIGH pulse of about 10 microseconds on Trig commands the module to transmit a burst, typically eight cycles at 40 kHz.
  • Echo timing: The Echo pin remains HIGH for the measured round-trip travel time t.
  • Distance equation:
TEXT
d = v x t / 2

Here, d is one-way distance, v is the speed of sound, and t is round-trip time. Division by 2 accounts for travel to the object and back.

  • Practical conversion: Near room temperature, v is approximately 343 m/s, giving:
TEXT
d (cm) approximately t (microseconds) / 58
  • Worked example: For an Echo duration of 1160 microseconds, distance is approximately 1160 / 58 = 20 cm.
  • Applications: Ultrasonic sensors support parking aids, tank-level estimation, robot navigation, and non-contact presence detection.
  • Limitations: Soft materials absorb sound, angled surfaces deflect echoes, nearby objects create false returns, and common modules have a blind zone near 2 cm. Temperature also changes sound speed.

VII. Digital Temperature and Humidity Sensing

DHT sensors combine a temperature element, humidity element, and internal controller that sends calibrated digital data to Arduino.

A. Temperature sensor (DHT11/DHT22)

The DHT11 and DHT22 measure both ambient temperature and relative humidity but differ in range, accuracy, and sampling rate.

  1. DHT11

    • Temperature: Typical specified range is 0-50 degrees C, with accuracy around +/-2 degrees C.
    • Humidity: Typical range is 20-80% RH, with accuracy around +/-5% RH.
    • Sampling: Readings should generally be spaced by at least 1 second.
  2. DHT22

    • Temperature: Typical specified range is -40 to 80 degrees C, with accuracy around +/-0.5 degrees C.
    • Humidity: Its range extends approximately 0-100% RH, with typical accuracy around +/-2% to +/-5% RH.
    • Sampling: Readings should generally be spaced by at least 2 seconds.
  • Sensing principle: A capacitive humidity element changes capacitance with absorbed water vapor, while a thermistor changes resistance with temperature.
  • Digital interface: The sensor uses one data line with precisely timed pulses; it is not read with analogRead(). A pull-up resistor, often 4.7-10 kilohms, holds the data line HIGH.
  • Library use: A DHT library handles timing and checksum validation, returning temperature in degrees Celsius and humidity as percent relative humidity.
  • Heat-index context: Temperature and relative humidity can be combined to estimate perceived heat, but this calculated value is not another direct sensor measurement.
  • Limitations: DHT devices are slow, can return invalid NaN readings when sampled too quickly, and should be positioned away from self-heating electronics, condensation, and direct sunlight.