Distance Sensor Car With LEDs

by tanyapurba8 in Circuits > Arduino

55 Views, 0 Favorites, 0 Comments

Distance Sensor Car With LEDs

IMG_8048.jpeg

Hello, I'm Tanya, and I will be sharing my final project with you on this Instructable! I've built a distance sensor car equipped with LEDs. The main goal of this car is to move forward smoothly when the sensor doesn't detect anything. However, if an object is sensed within 5 inches or less, the car stops, reverses, turns left, and then continues forward.

To make things even more interesting, the LEDs switch back and forth as the car operates. When the car is operating forward without any obstacles, a bright green LED lights up and stays on. On the flip side, if an object is detected, the green LED turns off, and a red LED takes its place, and the car executes its necessary steps until the coast is clear for it to move forward again and turn back to green.

Supplies

Untitled319_20240121173413.png

Building the Car

IMG_7829.jpeg
IMG_7833.jpeg

The first step is to mount the Arduino onto the car chassis. To do this I used a brass spacer and used that to attach the Arduino to pre-drilled holes on the car chassis stand. To hold this in place use screws that come with the brass spacers. Next attach the breadboard to the chassis. This is highly recommended as it will keep the breadboard from moving around which can later impact your wires and disconnect them. To do this you can use tape or glue.

The last thing we have to attach on the base is the ultrasonic distance sensor. Since we need it at the front of the car to sense what's going on, we will have to hang it in the front. To do this I first completely stripped the silicon off the wires and fed it through the holes on the sensor. Do this on the other side as well. Then I put it through holes on the chassis and twisted it till it was secure.

Use the image above for help.

Building Part 1

IMG_5020.jpeg
IMG_7832.jpeg

**Use the diagram to help understand what each pin is

Start by grabbing the Arduino and connect the 5V pin to the bottom power rail. Then connect the GND (ground) pin to the bottom ground rail. Connect the top ground rail with the bottom ground rail with a black wire and do this with the power as well. Next, place the H-Bridge on the breadboard. Connect the "Enable 1&2'' pin to pin 9 on the Arduino. Then connect the "Input 1" pin to pin 3 on the Arduino. Connect the two "Ground" pins to the negative rail on the breadboard. Grab 1 DC motor and connect the negative side to the "Output 1" pin. Connect the positive side to the "Output 2" pin. Then connect the "Input 2" pin to pin 2 on the Arduino. Connect the "Power 2" pin to the top power rail. Moving onto the other side of the H-Bridge, connect the "Power 1" pin to the bottom power rail. Then connect the "Input 4" pin to pin 10 on the Arduino. Again, connect both "Ground" pins to the negative rail on the breadboard. Grab the second DC motor and connect the negative side to the "Output 4" pin. Connect the positive side to the "Output 3" pin. Then connect the "Input 3" pin to pin 12 on the Arduino. Lastly, connect the "Enable 3&4'' pin to pin 11 on the Arduino.

That's all for the h-bridge.

Take male to female wires and connect the female side (no exposed wire coming out of it) to the pins on the ultrasonic distance sensor and take the other side(male) and stick it in the breadboard. From there put new wires in front of it and this is what will go to the arduino. Connect the "VCC'' pin to the bottom power rail. Connect the "GND" pin to the negative rail on the breadboard. Next, connect the "TRIG'' pin to pin 4 on the breadboard.

Place the potentiometer on the breadboard. Connect the 1st pin to the bottom power rail. Connect the 3rd pin to the ground rail. Then connect the middle pin to pin A0 on the Arduino.

Lastly, place the LEDs on the breadboard. Connect the cathode to the ground rail through a 330Ω resistor for both of them. Then for the RED LED connect the anode to pin 8 on the Arduino. For the GREEN LED connect the anode to pin 7.

Use the image above for help.

Code: Declaring Int

IMG_5029.jpeg

To start off you have to declare all of your variables/components. By doing this you will be able to control the pins of the Arduino and much more. It also tells the arduino what pin is connected to what component and what to do with it. Copy the image above.

Code: Void Setup

IMG_5030.jpeg

Now you need to set them to input or output to actually connect the pin. You can do that by following this template

pinMode((Variable name),(Input or Output));

For example, since my LED is receiving instructions and a signal from the arduino and it outputs the instruction/signal (turning on) it would be seen as an output. Another example is a pushbutton. When you press the button it sends a single into the system, therefore it would be considered an input.

Code Final

IMG_5032.jpeg
IMG_5033.jpeg

Then add the code I have in the first image. This helps the motors run in the ways we want them to, such as forward, right and so on. The last part of the code is the most important part of the code. The void loop is a crucial part of Arduino programming, commonly used in Arduino sketches. In simple terms, the void loop is a function that runs continuously once the Arduino board is powered on or reset. It forms the core of your program's execution and is where you place the code that you want to run repeatedly. For us this would be measuring the inches, moving the car, and turning on the LED.

Here's a breakdown of our void loop:

  • The trigger pin (trig) is set to LOW, followed by a short delay.
  • The trigger pin is then set to HIGH and held for a brief period.
  • After triggering, the trigger pin is set back to LOW.
  • The duration of the pulse received by the echo pin (echo) is measured using pulseIn().
  • The duration is converted into inches using a formula (duration / 74 / 2).
  • The calculated distance in inches is printed to the Serial Monitor.
  • The motorEnable() function is called to enable the motor
  • If the measured distance is greater than 5 inches:
  • The motor moves forward.
  • The green LED is turned on, and the red LED is turned off.
  • If the measured distance is less than 5 inches:
  • The green LED is turned off, and the red LED is turned on.
  • The motor stops for a second, moves backward for a second, and then turns left for a second.

And that is it. The complete code is attached if you want to just download that and use it.

Downloads

Final

IMG_8047.jpeg

Congrats you made a car! Now the last step is to test it. Here is a demonstration of how it should work.

Downloads