Unit 4 - Notes
Unit 4: Introduction of Arduino and Sensors
1. Arduino Board (Arduino Uno)
Arduino is an open-source electronics platform based on easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. We will focus on the Arduino Uno, one of the most popular boards in the Arduino family.
1.1 Overview of Arduino Uno
The Arduino Uno is a microcontroller board based on the ATmega328P microcontroller. It provides a set of digital and analog input/output (I/O) pins that can be interfaced with various expansion boards (shields) and other circuits.
Key Specifications:
- Microcontroller: ATmega328P
- Operating Voltage: 5V
- Input Voltage (recommended): 7-12V
- Input Voltage (limits): 6-20V
- Digital I/O Pins: 14 (of which 6 provide PWM output)
- Analog Input Pins: 6
- DC Current per I/O Pin: 20 mA
- DC Current for 3.3V Pin: 50 mA
- Flash Memory: 32 KB (ATmega328P) of which 0.5 KB used by bootloader
- SRAM: 2 KB
- EEPROM: 1 KB
- Clock Speed: 16 MHz
1.2 Pin Configuration and Description
The pins on the Arduino Uno can be grouped into several categories based on their function.
A. Power Pins
These pins are used to supply power to the Arduino board and to connected components.
- VIN (Voltage In): The input voltage to the Arduino board when it's using an external power source (as opposed to 5 volts from the USB connection). You can supply voltage through this pin, or, if supplying voltage via the power jack, access it through this pin.
- 5V: This pin outputs a regulated 5V from the regulator on the board. The board can be powered from the DC power jack (7 - 12V), the USB connector (5V), or the VIN pin of the board (7-12V).
- 3.3V: A 3.3 volt supply generated by the on-board regulator. Maximum current draw is 50 mA. Useful for powering sensors that require 3.3V.
- GND (Ground): Ground pins. There are multiple GND pins on the board, all of which are interconnected.
- IOREF (Input/Output Reference): This pin provides the voltage reference with which the microcontroller operates. A shield can read the IOREF pin voltage and select the appropriate power source or enable voltage translators on the outputs for working with the 5V or 3.3V.
B. Digital I/O Pins
There are 14 digital I/O pins, labeled 0 to 13.
- Functionality: Each of these 14 pins can be used as an input or an output, using the
pinMode(),digitalWrite(), anddigitalRead()functions in Arduino programming. - Operating Voltage: They operate at 5 volts.
- Current Limit: Each pin can provide or receive a maximum of 40 mA but 20 mA is the recommended operating current.
- Internal Pull-up Resistors: Each pin has an internal pull-up resistor (disconnected by default) that can be enabled using
pinMode(pin, INPUT_PULLUP);.
C. PWM (Pulse Width Modulation) Pins
Six of the 14 digital I/O pins are marked with a tilde ~ symbol. These are pins 3, 5, 6, 9, 10, and 11.
- Functionality: These pins can provide an 8-bit PWM output by using the
analogWrite()function. - Principle: PWM is a technique for getting an analog-like result with digital means. The digital signal is switched between on (HIGH) and off (LOW) at a very fast rate. The duty cycle (the percentage of time the signal is HIGH) determines the average voltage. For example, a 50% duty cycle on a 5V pin results in an average voltage of 2.5V.
D. Analog Input Pins
There are 6 analog input pins, labeled A0 to A5.
- Functionality: These pins are primarily used to read analog signals from sensors. They are connected to an on-board 10-bit Analog-to-Digital Converter (ADC).
- Resolution: The 10-bit ADC means it can map input voltages between 0 and 5 volts into integer values between 0 and 1023.
0V->05V->1023
- Usage: The
analogRead()function is used to read the value from a specified analog pin. - Alternate Function: These pins can also be used as digital I/O pins if needed (referred to as pins 14-19).
E. Special and Communication Pins
Some pins have special functions beyond general digital I/O.
-
Serial Communication (UART):
- Pin 0 (RX - Receive): Used to receive serial data.
- Pin 1 (TX - Transmit): Used to transmit serial data.
- Note: These pins are connected to the USB-to-serial chip and are used for communication with the computer (for uploading code and using the Serial Monitor). It is best to avoid using them for other purposes if you are using serial communication.
-
I²C (Inter-Integrated Circuit) Communication:
- Pin A4 (SDA - Serial Data): The data line for I²C communication.
- Pin A5 (SCL - Serial Clock): The clock line for I²C communication.
-
SPI (Serial Peripheral Interface) Communication:
- Pin 10 (SS - Slave Select): Selects which SPI device to communicate with.
- Pin 11 (MOSI - Master Out Slave In): The line for the Master (Arduino) to send data to the Slave (peripheral).
- Pin 12 (MISO - Master In Slave Out): The line for the Slave to send data to the Master.
- Pin 13 (SCK - Serial Clock): The clock signal that synchronizes data transmission.
-
Reset Pin:
- RESET: Bringing this pin LOW will reset the microcontroller. It is typically used for adding a reset button to shields which block the one on the board.
2. Basic Principle of Ultrasonic Sensor
The ultrasonic sensor is a device used for non-contact distance measurement. The most common model for hobbyists and educational purposes is the HC-SR04.
2.1 Components
An ultrasonic sensor module typically consists of:
- Ultrasonic Transmitter: Emits a high-frequency sound pulse (typically 40 kHz).
- Ultrasonic Receiver: Detects the reflected sound pulse (the echo).
- Control Circuitry: Manages the operation of the transmitter and receiver and provides a timed output pulse.
2.2 Working Principle
The sensor works on the principle of SONAR (Sound Navigation and Ranging). It measures the time it takes for a sound pulse to travel to an object and return.
The process is as follows:
-
Triggering the Sensor: The microcontroller (e.g., Arduino) sends a short high-level pulse (typically 10 microseconds) to the
TRIG(Trigger) pin of the sensor. -
Transmitting the Pulse: In response to the trigger pulse, the ultrasonic module's transmitter automatically sends out a burst of eight 40 kHz sound waves. This sound is beyond the range of human hearing.
-
Listening for the Echo: Immediately after sending the pulse, the module's control circuitry sets the
ECHOpin to HIGH and starts listening for the reflected sound waves. -
Receiving the Echo: The sound waves travel through the air, hit an object in their path, and reflect back towards the sensor. The receiver detects this reflected echo.
-
Calculating the Time of Flight: When the receiver detects the echo, the
ECHOpin is immediately set to LOW. The duration for which theECHOpin was HIGH is equal to the time taken for the sound to travel from the sensor to the object and back. This is known as the Time of Flight (TOF).
2.3 Distance Calculation
Once the time of flight is measured, the distance can be calculated using a simple formula:
Distance = (Speed of Sound × Time of Flight) / 2
- Speed of Sound: The speed of sound in air is approximately 343 meters per second (or 34.3 cm/ms, or 0.0343 cm/µs) at room temperature.
- Time of Flight: This is the duration for which the
ECHOpin was HIGH, measured by the microcontroller. - Division by 2: We divide by two because the measured time is for the sound to travel to the object and return. We only need the one-way travel time to calculate the distance to the object.
Example Calculation (in microseconds):
- Let's say the Arduino
pulseIn()function measures theECHOpin high time as588 µs. - Speed of sound ≈ 0.0343 cm/µs.
- Distance = (0.0343 cm/µs × 588 µs) / 2
- Distance = 20.1684 / 2
- Distance ≈ 10 cm
3. Basic Principle of Temperature Sensor
A temperature sensor is an electronic device that measures the temperature of its environment and converts the input data into electronic data to record, monitor, or signal temperature changes.
3.1 Types of Temperature Sensors
- Thermistors (NTC/PTC): Resistors whose resistance changes significantly with temperature. NTC (Negative Temperature Coefficient) resistance decreases as temperature rises.
- Thermocouples: Use the Seebeck effect, where a voltage is produced at the junction of two dissimilar metals, proportional to the temperature.
- Semiconductor-based ICs (Integrated Circuits): These are the most common in electronics projects. They are linear and easy to use. The LM35 and DHT11 are popular examples.
3.2 Working Principle of LM35 (Analog Sensor)
The LM35 is a precision integrated-circuit temperature sensor whose output voltage is linearly proportional to the Celsius (Centigrade) temperature.
- Principle: It is based on the principle that the forward voltage of a semiconductor diode is temperature-dependent. The internal circuitry is designed to produce a linear output.
- Key Characteristic: The sensor is calibrated to have a linear output of 10 millivolts (mV) per degree Celsius (°C).
- At 0 °C, the output is 0 mV.
- At 25 °C, the output is 250 mV.
- At 100 °C, the output is 1000 mV (1 V).
- Interfacing with Arduino:
- The LM35 has three pins:
VCC(power, 4-20V),Vout(analog output), andGND(ground). - The
Voutpin is connected to one of Arduino's analog input pins (A0-A5). - The Arduino's
analogRead()function reads the voltage from the pin, converting it to a 10-bit digital value (0-1023).
- The LM35 has three pins:
- Calculation:
- Read the raw ADC value:
int rawValue = analogRead(A0);(This will be 0-1023). - Convert ADC value to voltage: The Arduino ADC has a 5V reference.
float voltage = (rawValue / 1023.0) * 5.0;(This gives voltage in Volts). - Convert voltage to Temperature: Since 10mV = 1°C, or 1V = 100°C:
float temperatureC = voltage * 100.0;
- Read the raw ADC value:
3.3 Working Principle of DHT11 (Digital Sensor)
The DHT11 is a basic, low-cost digital temperature and humidity sensor.
- Principle: It uses a capacitive humidity sensor and a thermistor to measure the surrounding air. An internal IC performs the measurements and outputs a digital signal on the data pin.
- Components:
- NTC Thermistor: Measures temperature. Its resistance decreases as temperature increases.
- Humidity Sensor: A moisture-holding substrate with electrodes. As humidity changes, the conductivity of the substrate changes, which changes the resistance.
- Microcontroller: An 8-bit chip inside the sensor converts the analog readings from the sensors into a digital signal.
- Communication:
- The DHT11 uses a proprietary single-wire digital protocol.
- The microcontroller (Arduino) first sends a "start signal" to the DHT11 to request a reading.
- The DHT11 responds and sends back a 40-bit data packet containing the humidity and temperature information, along with a checksum for error checking.
- This complex timing-based communication is handled by dedicated Arduino libraries, making it easy for the user.
- Interfacing:
- The DHT11 has 3 or 4 pins (
VCC,Data,GND, and sometimesNC- Not Connected). - The
Datapin is connected to a digital I/O pin on the Arduino. - A pull-up resistor (typically 4.7kΩ to 10kΩ) is required between the
VCCandDatapins to keep the line HIGH when it's not transmitting. Many breakout modules include this resistor.
- The DHT11 has 3 or 4 pins (
4. Basic Principle of IR Sensor
An Infrared (IR) sensor is an electronic device that measures and detects infrared radiation in its surrounding environment.
4.1 Components
Every IR sensor consists of two main parts:
- IR Emitter (Transmitter): This is an IR LED (Light Emitting Diode) which emits infrared radiation at a specific wavelength (e.g., 940 nm). This light is invisible to the human eye.
- IR Detector (Receiver): This is usually a photodiode or a phototransistor that is sensitive to IR light of the same wavelength as the emitter. Its resistance changes when it is exposed to IR radiation.
4.2 Working Principle
The basic principle is to send an IR signal from the emitter and then detect if that signal is received by the detector. This can be done in two primary modes.
A. Reflective Mode (Obstacle/Proximity Sensing)
This is the most common configuration for obstacle detection and line-following robots.
- Setup: The IR emitter and detector are placed next to each other, pointing in the same direction.
- Operation:
- The IR LED continuously emits an infrared beam.
- Case 1: No Obstacle: If there is no object in front of the sensor, the IR beam travels outwards and does not reflect back to the detector. The detector receives no IR light (other than ambient).
- Case 2: Obstacle Present: If an object is placed in front of the sensor, the IR beam hits the object's surface and is reflected. A portion of this reflected light is captured by the IR detector.
- Output:
- When the detector receives the reflected IR light, its properties (resistance/current) change.
- This change is processed by an on-board circuit, often using a comparator IC (like LM393), to produce a digital output.
- If an object is detected, the output pin goes LOW (or HIGH, depending on the module's design).
- Most modules have a potentiometer to adjust the sensitivity, which effectively changes the detection distance.
B. Transmissive Mode (Beam-Break/Interrupt Sensing)
This mode is used for counting objects, detecting shaft rotation (with an encoder wheel), or creating a security beam.
- Setup: The IR emitter and detector are placed facing each other, with a gap in between.
- Operation:
- The IR LED continuously emits a beam directly at the detector.
- Case 1: Path Clear: The detector continuously receives the IR beam, and the output signal is in a steady state (e.g., HIGH).
- Case 2: Path Interrupted: When an object passes between the emitter and detector, it blocks the IR beam. The detector no longer receives the IR light.
- Output: The interruption of the beam causes a change in the detector's state, which triggers a change in the output signal (e.g., from HIGH to LOW). This signal change can be used to count objects or trigger an event.