Tutorial

Lesson 6 โ€” Build Your First Mini-Bot ๐Ÿค–

Lesson 6 โ€” Build Your First Mini-Bot ๐Ÿค–

๐Ÿง  Concept

Integrate sensors + motors to build a simple robot that follows a line and stops for obstacles.

๐Ÿงญ Build overview

Mini-Bot Build Overview Diagram

๐Ÿงฉ Combine modules

  • IR array (L/C/R) for line detection
  • HC-SR04 for obstacle distance
  • L298N / motor driver + two DC motors
  • Optional: SG90 servo to pan the ultrasonic
๐Ÿ’ก Keep wiring tidy. Zip ties = sanity.

๐Ÿงช Pseudocode โ†’ plan

loop:
  d = read_ultrasonic_cm()
  if d > 0 and d < 12: stop_motors()
  else:
    sensors = read_IR()
    steer_based_on(sensors)

๐Ÿ’ป Full Sketch (Arduino C/C++)

// Mini-bot: line follow + obstacle stop
// Pins (adjust as needed)
const int L=4, C=5, R=6;
const int ENA=9, IN1=8, IN2=7;
const int ENB=3, IN3=2, IN4=10;
const int TRIG=11, ECHO=12;

void motors(int l, int r){ analogWrite(ENA, abs(l)); analogWrite(ENB, abs(r));
  digitalWrite(IN1, l>=0); digitalWrite(IN2, l<0);
  digitalWrite(IN3, r>=0); digitalWrite(IN4, r<0); }

long distance_cm(){
  digitalWrite(TRIG, LOW); delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  long us = pulseIn(ECHO, HIGH, 30000UL);
  if(us==0) return -1;
  return us/29/2;
}

void setup(){
  pinMode(L,INPUT_PULLUP); pinMode(C,INPUT_PULLUP); pinMode(R,INPUT_PULLUP);
  pinMode(ENA,OUTPUT); pinMode(IN1,OUTPUT); pinMode(IN2,OUTPUT);
  pinMode(ENB,OUTPUT); pinMode(IN3,OUTPUT); pinMode(IN4,OUTPUT);
  pinMode(TRIG,OUTPUT); pinMode(ECHO,INPUT);
}

void loop(){
  long d = distance_cm();
  if(d>0 && d<12){ motors(0,0); delay(50); return; }

  int l = !digitalRead(L), c = !digitalRead(C), r = !digitalRead(R);
  int base=130, t=80;
  if(c && !l && !r) motors(base,base);
  else if(l && c && !r) motors(base-turn, base+turn);
  else if(!l && c && r) motors(base+turn, base-turn);
  else if(l && !c && !r) motors(0, base);
  else if(!l && !c && r) motors(base, 0);
  else motors(0,0);
  delay(20);
}

๐Ÿ Skeleton (Raspberry Pi โ€“ Python)

# Mini-bot skeleton (gpiozero)
from gpiozero import DigitalInputDevice, Motor
import RPi.GPIO as GPIO, time

L=DigitalInputDevice(17); C=DigitalInputDevice(27); R=DigitalInputDevice(22)
left=Motor(forward=5, backward=6); right=Motor(forward=13, backward=19)
TRIG, ECHO = 23, 24

GPIO.setmode(GPIO.BCM)
GPIO.setup(TRIG, GPIO.OUT); GPIO.setup(ECHO, GPIO.IN)

def distance_cm():
    GPIO.output(TRIG, False); time.sleep(0.0002)
    GPIO.output(TRIG, True); time.sleep(0.00001); GPIO.output(TRIG, False)
    t0=time.time()
    while GPIO.input(ECHO)==0: t0=time.time()
    while GPIO.input(ECHO)==1: t1=time.time()
    return (t1-t0)*34300/2

def drive(l,r):
    left.forward(l) if l>=0 else left.backward(-l)
    right.forward(r) if r>=0 else right.backward(-r)

try:
    while True:
        d = distance_cm()
        if 0 < d < 12: drive(0,0); time.sleep(0.05); continue
        l = not L.value; c = not C.value; r = not R.value
        base, t = 0.6, 0.4
        if c and not l and not r: drive(base,base)
        elif l and c and not r:   drive(base-t, base+t)
        elif not l and c and r:   drive(base+t, base-t)
        elif l and not c and not r: drive(0, base)
        elif not l and not c and r: drive(base, 0)
        else: drive(0,0)
        time.sleep(0.02)
except KeyboardInterrupt:
    GPIO.cleanup()

โšก Skeleton (ESP32 โ€“ MicroPython)

# Mini-bot skeleton (MicroPython) - fill in motor pin details
from machine import Pin, PWM, time_pulse_us
from time import sleep, sleep_us

L=Pin(27, Pin.IN, Pin.PULL_UP); C=Pin(26, Pin.IN, Pin.PULL_UP); R=Pin(25, Pin.IN, Pin.PULL_UP)
TRIG=Pin(5, Pin.OUT); ECHO=Pin(18, Pin.IN)

# TODO: set your motor PWMs/pins
# ENA=PWM(Pin(...), freq=1000); IN1=Pin(...); IN2=Pin(...)
# ENB=PWM(Pin(...), freq=1000); IN3=Pin(...); IN4=Pin(...)

def distance_cm():
    TRIG.off(); sleep_us(2); TRIG.on(); sleep_us(10); TRIG.off()
    us = time_pulse_us(ECHO, 1, 30000)
    if us <= 0: return -1
    return (us*0.0343)/2

def drive(l,r): pass  # implement using your motor pins

while True:
    d = distance_cm()
    if 0 < d < 12: drive(0,0); sleep(0.05); continue
    l = not L.value(); c = not C.value(); r = not R.value()
    base, t = 0.6, 0.4
    if c and not l and not r: drive(base,base)
    elif l and c and not r:   drive(base-t, base+t)
    elif not l and c and r:   drive(base+t, base-t)
    elif l and not c and not r: drive(0, base)
    elif not l and not c and r: drive(base, 0)
    else: drive(0,0)
    sleep(0.02)

๐Ÿงช Test plan

  1. Place robot on the track. It should follow the line smoothly.
  2. Hold an object at 10 cm: robot stops.
  3. Remove object: robot resumes.

๐Ÿ—ฃ๏ธ Reflection

  • What tuning gave the smoothest follow?
  • How could we avoid an obstacle instead of stopping?

๐Ÿ–จ๏ธ Worksheet โ€” Mini-Bot Build Checklist

  • [ ] IR array mounted & aligned
  • [ ] Ultrasonic stable readings
  • [ ] Motors wired to correct sides
  • [ ] Power distribution safe & shared ground
RoboRider Labs โ€ข Classroom Lesson 6