## The Capstone: Autonomous Robotics & The Internet of Things
When sensors, actuators, processing algorithms, and wireless communications converge, the result is an autonomous intelligent machine: a robot capable of perceiving its environment, deciding upon a course of action, and navigating physical space without human intervention.
§
1. Autonomous Obstacle-Avoiding Rover Architecture
TEXTSYSTEM SCHEMATIC
+---------------------------------------------------------------------------------+
| AUTONOMOUS OBSTACLE-AVOIDING ROVER ARCHITECTURE |
| |
| [ HC-SR04 Ultrasonic Sensor on Servo Turret ] |
| | |
| v (Measures Distance Ahead) |
| +---------------------------------------------------------------------------+ |
| | DECISION LOGIC MATRIX (Arduino Uno Microcontroller) | |
| | - If Distance > 30 cm: Drive FORWARD full speed | |
| | - If Distance <= 30 cm: STOP -> Sweep Turret Left/Right | |
| | -> Turn toward whichever direction offers maximum clearance | |
| +---------------------------------------------------------------------------+ |
| | | |
| v (PWM + Direction) v |
| +-----------------------+ +---------------------------+ |
| | Left Motor H-Bridge | | Right Motor H-Bridge | |
| | (L298N Channel A) | | (L298N Channel B) | |
| +-----------------------+ +---------------------------+ |
| | | |
| v v |
| [ Left Wheel Motor ] [ Right Wheel Motor ] |
+---------------------------------------------------------------------------------+§
2. Power Isolation & Decoupling Engineering
Connecting DC motors directly to the Arduino's 5V pin causes immediate crashes:
- 1Voltage Sag: DC motors draw massive surge current upon starting, dragging the 5V line below 4.0V and triggering the ATmega328P's Brown-Out Detector (BOD), causing endless random reboots.
- 2Inductive Kickback (Back-EMF): When a spinning motor stops, its magnetic coils collapse, generating high-voltage spikes (often 30V to 80V!) that can fry microcontroller silicon.
TEXTSYSTEM SCHEMATIC
CORRECT DUAL-RAIL POWER SCHEMATIC:
[ 7.4V - 11.1V LiPo Battery ]
|
+------------------------> [ Motor Driver V_Power (L298N) ]
| |
+---> [ 7805 5V Regulator ] v (Powers Motors)
| [ DC Motors ]
v
[ Arduino 5V Pin ] (Clean logic power!)
|
+---------+---------+
| COMMON GROUND BUS | (Connect ALL Grounds together!)
+-------------------+§
3. Complete Autonomous Rover Firmware
CPPSYSTEM SCHEMATIC
// =============================================================================
// PRODUCTION AUTONOMOUS OBSTACLE-AVOIDING ROBOT
// =============================================================================
#include <Servo.h>
// Ultrasonic Sensor Pins
const uint8_t TRIG_PIN = A1;
const uint8_t ECHO_PIN = A2;
const uint8_t SERVO_PIN = 10;
// L298N Motor Driver Pins
const uint8_t ENA = 5; // Left Motor Speed (PWM)
const uint8_t IN1 = 6; // Left Motor Dir 1
const uint8_t IN2 = 7; // Left Motor Dir 2
const uint8_t IN3 = 8; // Right Motor Dir 1
const uint8_t IN4 = 9; // Right Motor Dir 2
const uint8_t ENB = 11; // Right Motor Speed (PWM)
Servo turretServo;
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENB, OUTPUT);
turretServo.attach(SERVO_PIN);
turretServo.write(90); // Center turret
delay(500);
}
void loop() {
int distanceAhead = measureDistance();
if (distanceAhead > 30) {
driveForward(180);
} else {
// Obstacle detected! Halt and evaluate path options
haltMotors();
delay(200);
// Reverse slightly to avoid collision
driveReverse(150);
delay(300);
haltMotors();
// Scan Left
turretServo.write(160);
delay(400);
int leftDist = measureDistance();
// Scan Right
turretServo.write(20);
delay(400);
int rightDist = measureDistance();
// Recenter turret
turretServo.write(90);
delay(200);
// Turn toward the path of maximum clearance
if (leftDist > rightDist) {
turnLeft(200);
delay(400);
} else {
turnRight(200);
delay(400);
}
}
}
int measureDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
uint32_t duration = pulseIn(ECHO_PIN, HIGH, 25000);
if (duration == 0) return 999; // Clear path
return duration / 58.2;
}
void driveForward(uint8_t speed) {
analogWrite(ENA, speed);
analogWrite(ENB, speed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void driveReverse(uint8_t speed) {
analogWrite(ENA, speed);
analogWrite(ENB, speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void turnLeft(uint8_t speed) {
analogWrite(ENA, speed);
analogWrite(ENB, speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void turnRight(uint8_t speed) {
analogWrite(ENA, speed);
analogWrite(ENB, speed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void haltMotors() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}