Sensors for Robotics
Week 3 • CMPSC 304 Robotic Agents
Press Space or → to advance
The Robot Control Loop
PERCEPTION
→
DECISION
→
ACTUATION
- Perception: Sense the environment (sensors) 👈 This week!
- Decision: Process information and plan actions
- Actuation: Execute actions (motors)
This cycle repeats continuously while the robot operates
What Are Sensors?
Sensor: A device that converts physical phenomena into measurable signals
Why robots need sensors:
- Perception: Understand the environment
- Navigation: Know where you are and where to go
- Interaction: Respond to changes
- Safety: Detect obstacles and hazards
Sensor Categories
Proprioceptive
Sense internal state
- Wheel encoders
- Motor current
- Battery voltage
- IMU (orientation)
Exteroceptive
Sense external environment
- Distance sensors
- Cameras
- Temperature
- Light sensors
Both types are essential for autonomous behavior!
Key Sensor Characteristics
| Property |
Definition |
Example |
| Range |
Min/max measurable values |
2-400 cm |
| Resolution |
Smallest detectable change |
1 mm |
| Accuracy |
How close to true value |
±3 mm |
| Precision |
Repeatability |
±1 mm |
| Response Time |
How fast it updates |
50 ms |
Accuracy vs. Precision
Accurate
Close to true value
Measuring 10cm as 10.1cm
Precise
Measurements are consistent
Always getting 9.5cm (even if wrong)
Ideal: Both accurate AND precise!
But precision is easier to achieve than accuracy
Ultrasonic Distance Sensors
Principle: Measure time for sound waves to bounce back
HC-SR04 Specifications:
- Range: 2 cm to 400 cm
- Resolution: 0.3 cm
- Angle: 15° cone
- Frequency: 40 kHz (ultrasonic)
Formula: distance = (time × speed_of_sound) / 2
Speed of sound ≈ 343 m/s at 20°C
HC-SR04 Wiring
| HC-SR04 Pin |
Arduino Pin |
Purpose |
| VCC |
5V |
Power |
| TRIG |
Any digital pin (e.g., 9) |
Trigger pulse |
| ECHO |
Any digital pin (e.g., 10) |
Echo response |
| GND |
GND |
Ground |
Note: Send 10µs HIGH pulse to TRIG, then read ECHO duration
Arduino Code: HC-SR04
#define TRIG_PIN 9
#define ECHO_PIN 10
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
// Send trigger pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure echo duration
long duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance in cm
float distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(100);
}
Ultrasonic Sensor Limitations
- ✗ Soft/angled surfaces: Absorb or deflect sound
- ✗ Small objects: May not reflect enough sound
- ✗ Narrow beam: 15° cone misses objects outside
- ✗ Speed: ~50ms per measurement
- ✗ Temperature: Speed of sound varies
✓ Best for: Detecting walls, large obstacles, measuring room dimensions
Infrared (IR) Sensors
Principle: Emit IR light and measure reflection
Two main types:
Digital (Binary)
- Obstacle: YES/NO
- Adjustable threshold
- Simple, fast
Analog (Distance)
- Continuous distance
- Voltage varies with distance
- Typical range: 10-80 cm
IR Digital Sensor Wiring
| IR Sensor Pin |
Arduino Pin |
| VCC |
5V |
| OUT |
Any digital pin (e.g., 2) |
| GND |
GND |
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
}
void loop() {
int obstacle = digitalRead(2);
if (obstacle == LOW) {
Serial.println("Obstacle detected!");
}
delay(100);
}
IR vs. Ultrasonic Comparison
| Feature |
Ultrasonic |
IR |
| Range |
2-400 cm |
10-80 cm |
| Speed |
~50 ms |
~20 ms |
| Cost |
$2-5 |
$1-3 |
| Ambient Light |
✓ No effect |
✗ Can interfere |
| Best Use |
Walls, distance |
Obstacle detect |
Environmental Sensors
Beyond distance sensing, robots need to sense their environment:
- Temperature: TMP36 sensor
- Light: Photoresistor modules
- Moisture: Soil moisture sensors
Today: Quick overview, then Activity 3 hands-on
Temperature: TMP36
Principle: Voltage output proportional to temperature
Key Points:
- Range: -40°C to +125°C
- Output: 10 mV per °C, 500 mV offset at 0°C
- Formula: temp_C = (voltage - 0.5) × 100
- Wiring: 3 pins - VCC (red), Vout (yellow), GND (black)
Tip: Check for loose connections if readings jump wildly!
Light: Photoresistor Module
Principle: Resistance changes with light intensity
Key Points:
- 4-pin module: VCC, GND, AO (analog), DO (digital)
- Use AO pin for analog readings (0-1023)
- Higher values = more light
- You'll need to determine threshold ranges (dark/bright)
Moisture: Soil Sensors
Principle: Measure soil conductivity (wetness)
Key Points:
- 3 pins: VCC, AOUT (analog), GND
- Output: Analog voltage (0-1023)
- Higher value = drier (less conductivity)
- You'll need to calibrate ranges (wet/dry)
Calibration tip: Test in air (dry) vs water (wet)
Activity 3: Environmental Sensing
Due: Thursday, Feb 6 at 11:00 AM
Your Tasks:
- Wire all 3 sensors (temperature, light, moisture)
- Get Serial Monitor output working
- Run 2+ experiments per sensor
- Document findings in experiments.md
- Push to GitHub + demonstrate to instructor
Note: Sensor issues? Document troubleshooting efforts!