Projects / Side distance lights for a model car

Side distance lights for a model car

Beginner1.3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Wire the sensor

    VCC to 5V, GND, Trig to D7, Echo to D6.

  2. NANO14.3 mA

    Wire the LEDs

    D3, D4 and D5 each through 220 ohms to the long leg, short legs to GND.

  3. NANO

    Upload the sketch

    Open the Serial Monitor at 9600 baud.

  4. NANO

    Hold the sensor 60 cm from a wall

    Green lights.

  5. NANO14.4 mA

    Move to 30 cm

    Yellow lights.

  6. NANO

    Move to 15 cm

    Red lights.

  7. NANO

    Mount it on the side of the model

    Drive it slowly along a wall and watch the lights.

In short

How do you build a side distance lights for a model car?

An HC-SR04 on the side of a model car lights a green, yellow or red LED as a wall or kerb gets closer, which teaches thresholds and filtering. It is a model for learning and not a driving aid. You need Arduino Nano, HC-SR04 Ultrasonic Sensor, LED 5 mm red, LED 5 mm yellow, LED 5 mm green, Resistor 220 Ω × 3, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 80 minutes.

Level
Beginner
Time
About 80 min
Parts
8

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
HC-SR04 VCCNano 5VPower at 5 V
HC-SR04 GNDNano GNDCommon ground
HC-SR04 TrigNano D7Start a ping
HC-SR04 EchoNano D6Echo (5 V is fine on a Nano)
Green LED long legNano D3 through a 220 ohm resistorFar
Yellow LED long legNano D4 through a 220 ohm resistorGetting close
Red LED long legNano D5 through a 220 ohm resistorVery close
All LED short legsNano GNDCommon ground

Code

The sensor sends a short burst of ultrasound and the sketch times the echo to work out the distance. One reading can jump, so the sketch takes three readings a few milliseconds apart and uses their average. The green LED shows the car is clear, yellow means the wall is closer than forty centimetres, and red means closer than twenty. Change the two numbers at the top to suit your model. Soft surfaces and sharp angles bounce the sound away and fool the sensor, so test against a flat wall. It is a teaching model and must not be used as a parking or blind spot aid on any real vehicle, bicycle or wheelchair.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const uint8_t TRIG = 7;
const uint8_t ECHO = 6;
const uint8_t GREEN = 3;
const uint8_t YELLOW = 4;
const uint8_t RED = 5;

const float YELLOW_CM = 40;     // closer than this: yellow
const float RED_CM = 20;        // closer than this: red

float readCm() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(3);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long us = pulseIn(ECHO, HIGH, 25000);     // give up after about 4 m
  if (us == 0) return 400;                          // no echo: treat as far
  return us * 0.0343 / 2.0;
}

float average3() {
  float a = readCm(); delay(15);
  float b = readCm(); delay(15);
  float c = readCm();
  return (a + b + c) / 3.0;
}

void show(float cm) {
  digitalWrite(GREEN, cm >= YELLOW_CM);
  digitalWrite(YELLOW, cm < YELLOW_CM && cm >= RED_CM);
  digitalWrite(RED, cm < RED_CM);
}

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(RED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  float cm = average3();
  Serial.println(cm);
  show(cm);
  delay(60);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

79.95
80.07
80.02
80.04
79.91
79.90

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Far or no echoGreen
About 30 cm from a wallYellow
About 15 cm from a wallRed
Soft cloth in frontMay read far, so test on a hard wall
Edit YELLOW_CM and RED_CMLights change at your numbers

If it does not work

Lights flicker

Increase the number of averaged readings or aim at a flat wall.

Always green

Check Trig and Echo are not swapped and the sensor has 5 V.

Always red

Move the sensor away from the car body.

One LED never lights

Check its direction and resistor.

Serial shows 400

No echo is coming back, so check the wiring.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the side distance lights for a model car?

Arduino Nano, HC-SR04 Ultrasonic Sensor, LED 5 mm red, LED 5 mm yellow, LED 5 mm green, Resistor 220 Ω × 3, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the side distance lights for a model car take to build?

About 80 minutes for a beginner maker, in 7 steps.

How do I know the side distance lights for a model car works?

Far or no echo: Green. About 30 cm from a wall: Yellow. About 15 cm from a wall: Red.

Lights flicker: what should I check?

Increase the number of averaged readings or aim at a flat wall.

Always green: what should I check?

Check Trig and Echo are not swapped and the sensor has 5 V.

Always red: what should I check?

Move the sensor away from the car body.

One LED never lights: what should I check?

Check its direction and resistor.