## **Lab 2** | Working with Raspberry Pi Sensors ### Design of Autonomous Systems ### CSCI 6907/4907 - Section 86 ### Prof **Sibin Mohan** --- ## Objectives - Understand how sensors communicate with Raspberry Pi - Work with onboard RoverPiHAT sensors - Integrate and test an external ultrasonic sensor --- ## Equipment Required - Raspberry Pi 4B (or similar) - RoverPiHAT (with header pins, spacers, and screws) - MicroSD Card (32GB or larger) - USB to UART Cable - Power Supply (USB-C) - HC-SR04 Ultrasonic Sensor --- ## Basic Linux Shell Commands ### General Commands - `ls`, `cd`, `pwd`, `mkdir`, `rm`, `cp`, `mv`, `cat`, `nano` ### Raspberry Pi-Specific - `raspi-config`: Configure SSH, Wi-Fi, _etc _ --- ## How Devices Communicate ### GPIO overview - General-purpose input/output pins for connecting devices - Protocols: - **I2C**: Used for RoverPiHAT sensors - **SPI, UART**: For high-speed or serial communication --- ## how devices communicate ### devices in linux - devices represented as files (e g., `/dev/gpio`) - interact via libraries or directly through shell commands --- ## RoverPiHAT sensors overview ### key features - **accelerometer**: measures acceleration - **gyroscope**: tracks rotation/tilt - **magnetometer**: compass for magnetic north - **temperature Sensor**: measures temperature of the IMU - **ultrasonic Sensor Port**: connection of external ultrasonic sensors --- ## explanation of accelerometer code - **library imports**: - `smbus`: enables I2C communication with the MPU6050 sensor - `time`: adds delays for consistent data collection - **initialization**: - `MPU6050(bus=1, address=0x68)`: creates a sensor instance - `write_byte_data(0x6B, 0)`: wakes the MPU6050 from sleep mode . --- ## explanation of accelerometer code [contd.] - **data collection**: - `get_acceleration()`: reads X, Y, Z acceleration (in g) - `get_gyroscope()`: reads X, Y, Z rotation (in °/s) - `get_temperature()`: reads temperature (in °C) - **output**: - prints acceleration values in a readable format - adds a 1-second delay for clarity between readings --- ## using an external ultrasonic sensor ### connections - `VCC` → `5V` - `GND` → `GND` - `TRIG` → GPIO18 - `ECHO` → GPIO24 --- ### validation - measure distances using Python scripts - real-time output to verify accuracy --- ## explanation of ultrasonic sensor code - **pin setup**: - `TRIG` → GPIO18: trigger pin to send sound waves - `ECHO` → GPIO24: receives the reflected echo - **working principle**: - **trigger Pulse**: sends a short ultrasonic pulse (10 μs) - **echo Measurement**: measures the time taken for the echo to return --- ## explanation [contd.] - **distance calculation**: - Time (duration) × Speed of Sound (343 m/s) ÷ 2 - divided by 2 because the wave travels to the object and back - **real-time output**: - continuously measures and prints distance in centimeters --- ## assignment question: understanding ultrasonic sensors - write a detailed paragraph → explain how ultrasonic sensor works - your explanation should include: - the basic principle behind ultrasonic sensing - the role of the **trigger** and **echo** pins - how distance is calculated using time for sound waves to travel - real-world applications for ultrasonic sensors --- ## submission guidelines: - write your response in your own words - submit as a typed document (**markdown**) by **12 PM tomorrow** - ensure clarity and conciseness (**150-200 words**) --- ## summary - set up the raspberry pi with roverpihat - explored onboard sensors for motion, orientation, and altitude - integrated an external ultrasonic sensor to measure distances - learned to write python scripts to interact with sensors in real time