As an Amazon Associate we earn from qualifying purchases.
An automatic brake system slows or stops a vehicle when something is in its path, without waiting for the driver to react. This project is a small working model of that idea. An ultrasonic sensor measures the distance to an obstacle, a PIC microcontroller reads it, and the motor speed drops as the obstacle gets closer.
It is a learning prototype, not a replacement for the safety systems fitted to production cars. But it shows the same basic loop those systems use: sense, decide, act.
How an automatic brake system works
Any automatic braking setup has three parts:
- A sensor that detects what is ahead. Production vehicles generally use radar, cameras or lidar. This project uses an ultrasonic distance sensor.
- A controller that turns the reading into a decision. Here that is a PIC microcontroller running a short program.
- An actuator that slows the vehicle. In a real car this is the brake system. In this model it is the drive motors, controlled with PWM.
The closer the obstacle, the less speed the controller allows. Below a set distance, it raises an alert and the vehicle is held back.
What this project does
The program runs in a continuous loop and does the following on each pass:
- Sends a short trigger pulse to the ultrasonic sensor.
- Measures how long the echo pin stays high, which is proportional to distance.
- Shows the distance on a 16×2 LCD.
- If the distance is under 30 cm, prints “OBSTACLE CLOSER” and switches the output pins to their alert state.
- Sets the PWM duty cycle for both motor channels based on the distance, so the vehicle slows as it gets nearer.
What you need
- A PIC microcontroller with PWM outputs and a suitable clock circuit
- An ultrasonic distance sensor with trigger and echo pins
- A 16×2 character LCD
- A motor driver and two DC motors for the drive wheels
- A buzzer or indicator for the close-obstacle output
- A regulated power supply and a chassis to mount everything
The code is written for the mikroC compiler. Check the pin names in the program against your own wiring before you build.
Circuit
The diagram below shows how the sensor, LCD, motor driver and microcontroller are connected. The trigger pin goes to PORTB bit 2 and the echo pin to PORTB bit 3. The LCD uses PORTD, and the PWM outputs feed the motor driver.
Program
int c1=0,speed;
char distance[8];
void usinit(void);
void uscheck(void);
// LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
void InitMain() {
PWM1_Init(5000);
PWM2_Init(5000);
}
void main(){
trisc.f3=0; trisc.f0=0;
portc.f0=0; portc.f3=0;
InitMain();
PWM1_Start();
PWM2_Start();
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
usinit();
adcon1=7;
trise.f2=0;
trisc.f3=0; trisc.f0=0;
portc.f0=0; portc.f3=0;
trisb.f7=0; portb.f7=0;
while(1)
{
uscheck();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(2,1,"DISTANCE="); Lcd_Out_cp(distance);
if(c1<30){porte.f2=1;portb.f7=0; Lcd_Out(1,1,"OBSTACLE CLOSER");} else { porte.f2=0; portb.f7=1; }
speed=c1*4;
if (speed>255) speed=255;
PWM1_Set_Duty(speed); PWM2_Set_Duty(speed);
}
}
void uscheck(void)
{
portb.f2=1 ;
delay_us(10);
portb.f2=0 ;
while(!portb.f3);
c1=0;
while(portb.f3) {c1++; delay_us(58); } // 1CM ==58US
IntToStr(c1, distance);
delay_ms(200);
}
void usinit(void)
{
trisb.f3=1; //echoPin
trisb.f2=0; //trigPin
}
How the code works
Measuring distance
The uscheck function sends a 10 microsecond pulse on the trigger pin. It then waits for the echo pin to go high and counts how long it stays high. Sound travels out and back, so about 58 microseconds of echo time equals 1 cm of distance. The counter c1 therefore holds the distance in centimetres. The timing is approximate, because the loop itself takes a little extra time, so calibrate it against a ruler.
Warning and output pins
If c1 is below 30, the program prints “OBSTACLE CLOSER” on the first LCD line and sets one output pin high while pulling the other low. Otherwise the two pins swap states. Use these outputs for a buzzer, an indicator or a motor driver enable line, depending on your wiring.
Speed control with PWM
The speed value is the distance multiplied by 4 and capped at 255, which is the maximum 8-bit PWM duty. Both PWM channels are set to this value, so the motors run slower as the obstacle approaches and at full duty once it is far enough away. The PWM frequency is set to 5 kHz. Speed control from a sensor reading is the same idea used in an automatic chaser with speed control.
Testing the project
- Power the board with the motors disconnected and confirm the LCD shows a changing distance as you move your hand in front of the sensor.
- Check the readings against a ruler at a few distances and adjust the timing if they are off.
- Connect the motor driver and run the vehicle on a bench with the wheels lifted.
- Test on the floor with a flat, solid obstacle such as a box or book, approaching head-on.
Limitations and ideas for improvement
This design is simple, so it has limits worth knowing about:
- Ultrasonic sensors work best on flat, hard surfaces facing the sensor. Soft or angled objects can reflect poorly.
- Because speed is tied directly to distance, the motors slow down gradually but do not hard-stop at a set point. You can add a cut-off so the duty drops to zero below a minimum distance.
- The 200 ms delay in each reading limits how quickly the system reacts. Shorten it for faster response, leaving enough time for echoes to die out.
- A single front sensor has a narrow field of view. Several sensors give wider coverage.
The same sensing approach is useful elsewhere. An automatic car parking robot needs distance sensing to avoid collisions, and an automatic railway gate uses sensors to trigger an action without human input. If you want to detect sudden movement or impact rather than distance, try combining this project with an accelerometer and Arduino approach.
FAQ
What is an automatic brake system?
It is a system that detects an obstacle ahead and slows or stops the vehicle without driver input. Production systems typically use radar, cameras or both. This project is a simplified model using an ultrasonic sensor.
Why use an ultrasonic sensor?
Ultrasonic sensors are cheap, easy to connect and give a direct distance reading at short range. That makes them a good fit for a small demonstration vehicle.
Can this be fitted to a real car?
No. The circuit is a prototype for learning and small robot vehicles. Real vehicles need sensors and controllers designed and tested for safety, with fail-safe behaviour.
How do I change the distance at which it reacts?
Change the number 30 in the if statement. It is the trigger distance in centimetres. You can also change the multiplier of 4 in the speed calculation to make the vehicle slow down earlier or later.