This is an old revision of the document!


Adaptive Cruise Control Vehicle

Introduction

What it does: This project consists of a small autonomous vehicle capable of maintaining a constant safe distance from a target object or vehicle in front of it.

Its purpose: The main goal is to implement a real-time closed-loop control system (Adaptive Cruise Control) on an 8-bit microcontroller. It demonstrates bare-metal embedded programming by utilizing hardware interrupts, hardware timers, and custom serial protocols, completely avoiding high-level abstractions like Arduino libraries.

The starting idea: The idea is inspired by modern Advanced Driver Assistance Systems (ADAS) used in the automotive industry to prevent rear-end collisions and improve driving comfort.

Why it is useful: It serves as a highly practical demonstration of low-level hardware control, bridging the gap between theoretical micro-processor architecture and real-world physical applications.

General Description

The system uses an ultrasonic sensor mounted on the front to continuously measure the distance to the obstacle ahead.

Based on the calculated distance, a proportional control algorithm calculates the necessary speed adjustments to maintain a safe gap. The system smoothly interpolates the motor speed using hardware PWM based on the target's distance. If the target is too close (under 25 cm), the vehicle applies the brakes. If the target is within the tracking range (25 cm - 85 cm), it adjusts the speed dynamically to follow it.

Block Diagram:

Hardware Design

List of components:

  • Arduino UNO (ATmega328P Microcontroller) serving as the main processing unit.
  • L293D Motor Drive Shield (featuring the 74HC595 shift register).
  • 1x HC-SR04 Ultrasonic Sensor.
  • 2x DC TT Motors (Rear-wheel or 4-wheel drive configuration).
  • 2x 18650 Li-Ion Batteries (7.4V total) with holder.
  • Robot car chassis with wheels.
  • Dupont connecting wires.

Electrical Schematic:

Software Design

Development Environment:

  • VS Code with PlatformIO (using pure C and register-level programming).

Libraries and 3rd-party sources:

  • Standard AVR libc (<avr/io.h>, <avr/interrupt.h>, <util/delay.h>, <stdio.h>).
  • Zero high-level Arduino libraries (like `analogWrite`, `digitalWrite`, or `pulseIn`) were used.

Algorithms and Hardware Features utilized:

  • Bit-Banging for Shift Register: The L293D shield uses a 74HC595 shift register to control motor directions. A custom synchronous serial protocol was implemented (`shift_out_data`) to manually push bits to the latch, clock, and data pins using direct port manipulation (`PORTB`, `PORTD`).
  • Hardware Timers (Fast PWM): Timer2 was configured at the register level (`TCCR2A`, `TCCR2B`) to generate a pure hardware Fast PWM signal on pins OC2A and OC2B. This controls the motor speed without utilizing CPU cycles.
  • Pin Change Interrupts (PCINT) & Timer1: To avoid blocking the CPU while waiting for the ultrasonic echo, the Echo pin was mapped to a Pin Change Interrupt (`PCINT1_vect`). Timer1 counts the micro-seconds in the background, making the distance reading asynchronous and highly efficient.
  • Custom USART Stream: The USART module was initialized manually (`UBRR0`, `UCSR0`), and the standard output stream (`stdout`) was redirected to the serial port, allowing the use of `printf()` for clean, professional real-time debugging.

Obtained Results

The system reacts accurately and smoothly to dynamic environments.

  • The custom non-blocking ultrasonic reading accurately updates the distance without hanging the main loop.
  • The proportional mapping successfully overcomes the static friction of the DC motors by setting a calculated minimum PWM threshold (115), ensuring the car starts moving reliably even at low speeds.
  • The car smoothly decelerates as it approaches the 25 cm safety threshold and completely halts to prevent collisions.

Final Project Photo:

Conclusions

Implementing this project in a bare-metal C environment provided profound insights into the internal workings of the ATmega328P microcontroller. Stripping away the Arduino framework forced a deeper understanding of memory mapping, the crucial difference between polling and hardware interrupts, and the complexity of generating custom serial signals (bit-banging). The project successfully replicates an industrial ADAS concept on an 8-bit architecture.

Download

code

Journal

  • 09.05.2026: Chosen the project topic and completed the initial OCW documentation.
  • 17.05.2026: Assembled the hardware components, tested the L293D motor shield, and resolved DC motor wiring issues.
  • 19.05.2026: Fully migrated to VS Code / PlatformIO. Successfully implemented bare-metal code integrating PCINT for the HC-SR04 sensor, Timer2 Fast PWM for motor speed, and custom bit-banging for directional control. Tuned the PWM thresholds to overcome static friction.

Bibliography/Resources

Hardware Resources:

  • ATmega328P Datasheet (Microchip Technology)
  • HC-SR04 Ultrasonic Sensor Specifications
  • L293D Motor Shield Schematic & 74HC595 Datasheet

Software Resources:

  • AVR Libc Reference Manual
  • PlatformIO Documentation
pm/prj2026/andrei.batasev/dorian.gilca.1779691349.txt.gz · Last modified: 2026/05/25 09:42 by dorian.gilca
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0