Unit 2 - Notes

ECE249

Unit 2: Introduction of Arduino and Sensors

1. Analog and Digital Signals

Understanding the nature of signals is fundamental to interfacing the physical world (sensors) with the computational world (Arduino).

1.1 Analog Signals

An analog signal is a continuous signal that represents physical measurements. It can take on any value within a given range. In the real world, most signals are analog (e.g., temperature, sound, light intensity).

  • Characteristics:
    • Continuous: The signal changes smoothly over time; there are no breaks.
    • Infinite Resolution: Theoretically, between any two values, there are infinite intermediate values (limited only by noise).
    • Representation: Usually represented by a sine wave.
    • Arduino Handling: Arduino uses the ADC (Analog-to-Digital Converter) to read these signals via pins A0–A5.

1.2 Digital Signals

A digital signal is a discrete signal that represents data as a sequence of discrete values. In electronics, this is binary logic.

  • Characteristics:
    • Discrete: The signal switches between specific levels.
    • Binary: Two states only—HIGH (1, On, 5V/3.3V) or LOW (0, Off, 0V/GND).
    • Representation: Usually represented by a square wave.
    • Arduino Handling: Read/Write via Digital Pins (0–13).

Comparison: Analog vs. Digital

Feature Analog Signal Digital Signal
Continuity Continuous stream of data Discrete (step-wise) data
Values Infinite range (e.g., 0V to 5V: 1.2V, 3.45V) Finite range (0 or 1)
Noise Immunity Low (easily affected by noise) High (can ignore minor noise)
Storage Difficult to store Easy to store (CDs, Hard drives)
Example Human voice, Thermometer reading Computer data, Switch state

2. The Arduino Board

Arduino is an open-source electronics platform based on easy-to-use hardware and software. The most common board for beginners is the Arduino UNO, based on the ATmega328P microcontroller.

2.1 Technical Specifications (Arduino UNO)

  • Microcontroller: ATmega328P
  • Operating Voltage: 5V
  • Input Voltage (recommended): 7–12V
  • Digital I/O Pins: 14 (of which 6 provide PWM output)
  • Analog Input Pins: 6
  • Clock Speed: 16 MHz

2.2 Pin Configuration and Description

A. Power Pins

These pins are used to power the board or power external components.

  • Vin: Input voltage to the Arduino board when using an external power source (like a battery or adapter).
  • 5V: Outputs a regulated 5V from the regulator on the board. Used to power sensors.
  • 3.3V: A 3.3 volt supply generated by the on-board regulator.
  • GND: Ground pins (0V reference).
  • IOREF: Provides the voltage reference with which the microcontroller operates.

B. Analog Inputs (A0 – A5)

  • Function: Used to read analog sensor data (like temperature or light).
  • ADC: These pins are connected to an internal 10-bit Analog-to-Digital Converter. This maps voltages between 0 and 5V into integer values between 0 and 1023.

C. Digital I/O Pins (0 – 13)

  • Function: Can be configured as either Input (reading a switch) or Output (turning on an LED) using pinMode().
  • Logic Levels: 0V is LOW; 5V is HIGH.
  • Serial Pins (0 & 1):
    • 0 (RX): Used to receive (input) TTL serial data.
    • 1 (TX): Used to transmit (output) TTL serial data.
    • Note: Avoid using these if you are using the USB communication to the computer.
  • External Interrupts (2 & 3): These pins can be configured to trigger an interrupt on a low value, a rising or falling edge, or a change in value.
  • PWM Pins (~3, ~5, ~6, ~9, ~10, ~11): Marked with a tilde (~). They provide 8-bit Pulse Width Modulation output using the analogWrite() function. This simulates analog output (like dimming an LED) using digital pulses.
  • SPI Pins (10, 11, 12, 13): Used for SPI communication (SS, MOSI, MISO, SCK).
  • LED Pin (13): There is a built-in LED connected to digital pin 13.

D. Other Components

  • Reset Button: Restarts the program code from the beginning.
  • USB Port: Used for powering the board and uploading code from the computer.
  • Barrel Jack: For external power supply (Wall adapter).
  • ICSP Header: In-Circuit Serial Programming (for advanced users to program the chip directly).

3. Sensors and Transducers

A Sensor is a device that detects changes in the environment (physical quantity) and produces an electrical output corresponding to that change.

3.1 IR Sensor (Infrared Sensor)

An IR sensor is an electronic device that emits and/or detects infrared radiation to sense aspects of its surroundings.

  • Type Covered: Active IR Sensor (Obstacle detector).

  • Components:

    1. IR Transmitter (IR LED): Emits infrared light (invisible to the human eye).
    2. IR Receiver (Photodiode): Detects reflected IR light.
    3. Comparator (Op-Amp): Compares the voltage from the photodiode against a threshold set by a potentiometer.
  • Working Principle:

    1. The IR LED continuously emits IR light.
    2. If there is no object, the IR light travels forward and is lost; the photodiode receives nothing.
    3. If there is an object (obstacle), the light reflects off the object and hits the photodiode.
    4. The resistance of the photodiode drops, changing the voltage at the comparator input.
    5. The output pin goes LOW (or HIGH depending on logic) indicating an obstacle is present.
  • Black vs. White: Black surfaces absorb IR light (no reflection), while white surfaces reflect it. This principle makes IR sensors ideal for Line Follower Robots.

3.2 LDR (Light Dependent Resistor)

An LDR, also known as a photoresistor, is a passive component whose resistance varies inversely with the intensity of incident light.

  • Material: Usually made of Cadmium Sulfide (CdS).

  • Working Principle (Photoconductivity):

    • Darkness: Electrons are bound to the crystal lattice. Resistance is very high (Mega-ohms range).
    • Light: Photons hit the material, giving energy to electrons to jump to the conduction band. Resistance decreases drastically (allows current to flow).
    • Relationship: Higher Light Intensity = Lower Resistance.
  • Interfacing: Since Arduino cannot read resistance directly, an LDR is used in a Voltage Divider Circuit with a fixed resistor. The Arduino measures the voltage at the junction point via an Analog Pin (A0).

3.3 Ultrasonic Sensor (HC-SR04)

The Ultrasonic sensor measures distance by using sound waves. It works on the principle of Echolocation (similar to bats or sonar).

  • Pin Configuration: VCC (5V), Trig, Echo, GND.

  • Working Principle:

    1. Triggering: The Arduino sends a 10µs HIGH pulse to the Trig pin.
    2. Emission: The sensor transmits a sonic burst of 8 cycles at 40 kHz (ultrasonic sound).
    3. Waiting: The sound waves travel through the air, hit an object, and bounce back.
    4. Reception: The Echo pin goes HIGH when the burst is sent and stays HIGH until the reflected sound is received.
    5. Calculation: The duration the Echo pin stays HIGH is the time () taken for the round trip.
  • Distance Formula:

    • Speed of sound in air or .
    • Division by 2 is required because the time measured is for the wave to go and come back.

3.4 Temperature and Humidity Sensor (DHT11 / DHT22)

The DHT sensors are digital sensors that measure ambient temperature and humidity. They contain a chip that does analog-to-digital conversion internally and sends data via a single digital pin.

  • Internal Components:

    1. Capacitive Humidity Sensor: Measures moisture.
    2. Thermistor (NTC): Measures temperature.
    3. IC: Processes the reading.
  • Working Principle:

    • The sensor detects water vapor by measuring the electrical resistance between two electrodes. The humidity sensing component is a moisture-holding substrate with electrodes applied to the surface. When water vapor is absorbed, conductivity changes.
    • The IC reads this conductivity, converts it to data, and sends a digital signal packet to the Arduino.
  • Comparison: DHT11 vs DHT22

Feature DHT11 DHT22 (AM2302)
Cost Cheap More expensive
Range (Humidity) 20-80% 0-100%
Range (Temp) 0 to 50°C -40 to 80°C
Accuracy Low (C) High (C)
Sampling Rate 1 Hz (once every second) 0.5 Hz (once every 2 seconds)
  • Data Protocol: Unlike standard analog sensors, DHT sensors use a proprietary single-wire serial protocol. Libraries (like DHT.h) are usually required in the Arduino IDE to decode the timing of the pulses sent by the sensor.