Skip to content

Detailed reference for sensors used in e-Yantra projects.


RPLidar A1 / A2

SpecA1A2
Range0.15 – 12 m0.15 – 18 m
Scan Rate5.5 Hz10 Hz
Angular Resolution< 1°< 0.9°
InterfaceUSB (UART)USB (UART)
bash
sudo apt install ros-humble-rplidar-ros
ros2 launch rplidar_ros rplidar_a1_launch.py

MPU-6050 IMU

6-axis IMU (3-axis gyroscope + 3-axis accelerometer) over I2C.

python
import smbus2

MPU_ADDR = 0x68
bus = smbus2.SMBus(1)
bus.write_byte_data(MPU_ADDR, 0x6B, 0)  # Wake up

def read_word_2c(reg):
    high = bus.read_byte_data(MPU_ADDR, reg)
    low  = bus.read_byte_data(MPU_ADDR, reg + 1)
    val  = (high << 8) + low
    return val - 65536 if val >= 0x8000 else val

def get_accel():
    return (read_word_2c(0x3B) / 16384.0,
            read_word_2c(0x3D) / 16384.0,
            read_word_2c(0x3F) / 16384.0)

def get_gyro():
    return (read_word_2c(0x43) / 131.0,
            read_word_2c(0x45) / 131.0,
            read_word_2c(0x47) / 131.0)

HC-SR04 Ultrasonic

SpecValue
Range2 – 400 cm
Accuracy±3 mm
InterfaceGPIO (Trigger + Echo)
python
import RPi.GPIO as GPIO
import time

TRIG, ECHO = 23, 24
GPIO.setmode(GPIO.BCM)
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)

def get_distance():
    GPIO.output(TRIG, True)
    time.sleep(0.00001)
    GPIO.output(TRIG, False)
    while GPIO.input(ECHO) == 0: pulse_start = time.time()
    while GPIO.input(ECHO) == 1: pulse_end   = time.time()
    return round((pulse_end - pulse_start) * 17150, 2)  # cm

Raspberry Pi Camera v2

python
from picamera2 import Picamera2
import cv2

picam2 = Picamera2()
picam2.configure(picam2.create_preview_configuration(main={"size": (640, 480)}))
picam2.start()

frame = picam2.capture_array()
cv2.imshow("Camera", frame)
cv2.waitKey(0)

Wheel Encoders

python
import RPi.GPIO as GPIO

ENC_L, ENC_R = 17, 27
counts = [0, 0]

def cb_left(ch):  counts[0] += 1
def cb_right(ch): counts[1] += 1

GPIO.setmode(GPIO.BCM)
for pin, cb in [(ENC_L, cb_left), (ENC_R, cb_right)]:
    GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.add_event_detect(pin, GPIO.RISING, callback=cb)

References

Released under the MIT License.