Introduction: Arduino - Motion Following Car

Hello, everyone!

Here I will be showing you how to build a motion following car. This project is based on two other similar ones and I recommend you check them both out to get a better idea of the layout for this project (https://www.instructables.com/id/Arduino-Ultimate-Obstacle-Avoiding-Robot/ and https://www.instructables.com/id/Motion-Following-Robot/).

Here is what you will need:

A body for the car. You can use a repurposed RC car, but I used this one: (https://www.amazon.ca/Car-Chassis-Kit-Battery-Encoder/dp/B01EIYJARW/ref=sr_1_1?ie=UTF8&qid=1492135756&sr=8-1&keywords=car+chassis). Whatever you choose, it should have a base, wheels, and two motors.

An Arduino Uno microcontroller chip

A motor driver chip. I used this one: https://www.amazon.ca/XD-56-HG7881-HG7881CP-motor...

Two 4 pin ultrasonic sensors

Two power sources - one for the motors and one for the Uno. I used 4 AA batteries for the motors and a 9V for the Uno

A mini breadboard

And a bunch of jumper wires to connect everything together (male-male as well as male-female)

Step 1: Wiring Everything Together

First thing to do is figure out where you want the parts to be placed on the car body and find a way of sticking them down (I just used double sided mounting tape). The ultrasonic sensors should be placed on the front of the car and the motor driver can be placed on top of the 9V battery to make it fit. Once you have everything in place you can start by attaching wires to the motors and ultrasonic sensors. I tried to use the same colour of wire for each part to make things a bit more clear. Ultrasonic sensor wires for both sensors are: echo - green and trig - yellow. Motor driver wires are: B-IA and A-IA - blue and B-IB and A-IB - purple. Red for positive and black for negative terminals were used as much as possible.

Once you have the components wired you can attach them to the Arduino and each other (via the breadboard). Here is how everything was attached to the Uno board:

Arduino pin ~6: purple to B-IB

pin 7: blue to B-IA

pin 8: blue to A-IA

pin ~9: purple to A-IB

pin ~10: green to left echo pin **This is left and right when viewed from the back looking forward

pin ~11: yellow to left trig pin

pin ~12: green to right echo pin

pin ~13: yellow to right trig pin

At this point you should have all the sensors wired to the Arduino, and now you just need the power and motors! I connected wires from the 5V and GND Arduino pins to separate lines of the breadboard, then attached the positive and negative leads from the ultrasonic sensors to the same lines on the breadboard. On the same lines I also attached the positive and negative leads from the 9V battery.

On the other side of the breadboard I attached the motor driver positive and negative leads to the positive and negative leads of the 4 AA batteries. I also added a wire from the negative line to a GND pin on the Arduino.

The last part is to attach the leads from the motor driver "Motor A" to the leads from the right motor and the leads from "Motor B" to the left motor and you should be done!

Step 2: Code

This code runs in the Arduino IDE and it shouldn't require any additional libraries. I have included the file or you can copy and paste it from below.

/*
Code for creating the Arduino following car.

*/

const int motorR_A = 8; // (pwm) pin 9 connected to pin A-IA

const int motorR_B = 9; // (pwm) pin 8 connected to pin A-IB

const int motorL_A = 7; // (pwm) pin 7 connected to pin B-IA

const int motorL_B = 6; // (pwm) pin 6 connected to pin B-IB

const int Lin = 10, Rin = 12, Lout = 11, Rout = 13; //Sets up the ultrasonic sensor input and output

long Rduration, Lduration, Rcms, Lcms; // Duration of ultrasonic pulse and resulting distance in cms

int threshold = 25;

boolean debug = true; // Starts the serial monitor

byte speed = 100; // change this (0-255) to control the speed of the motors

void setup()

{ if (debug)

{ Serial.begin(9600); }

pinMode(motorR_A, OUTPUT); // set pins to output

pinMode(motorR_B, OUTPUT);

pinMode(motorL_A, OUTPUT);

pinMode(motorL_B, OUTPUT);

pinMode(Rout, OUTPUT); // sets pins for ultrasonic sensors

pinMode(Rin, INPUT);

pinMode(Lout, OUTPUT);

pinMode(Lin, INPUT);

}

void loop()

{

digitalWrite(Rout, LOW); // Pulses for the right sensor.

digitalWrite(Rout, HIGH);

delayMicroseconds(2);

digitalWrite(Rout, LOW);

Rduration = pulseIn(Rin, HIGH);

digitalWrite(Lout, LOW); // Pulses for the left sensor

digitalWrite(Lout, HIGH);

delayMicroseconds(2);

digitalWrite(Lout, LOW);

Lduration = pulseIn(Lin, HIGH);

Rcms = microsecondsToCms(Rduration);

Lcms = microsecondsToCms(Lduration);

if (debug) {

Serial.print("Left: ");

Serial.print(Lcms);

Serial.println(" cms");

Serial.print("Right: ");

Serial.print(Rcms);

Serial.println(" cms"); }

follow(); }

long microsecondsToCms(long microseconds) {

// // Converts pulse time to a distance in cms

return microseconds / 2 / 29.1; }

void follow()

{

if (Lcms == threshold || Rcms == threshold) {

analogWrite(motorR_A, 0); // car stops if an object is too close

analogWrite(motorR_B, 0);

analogWrite(motorL_A, 0);

analogWrite(motorL_B, 0);

Serial.print(" Stopping!"); }

if (Lcms > threshold) {

left();

Serial.print(" Moving left!"); }

if (Rcms > threshold) {

right();

Serial.print(" Moving right!"); }

if (Lcms > threshold && Lcms < 60) { // Moves forward if the car is within this range

forward();

Serial.print(" Moving forward!"); }

if (Rcms > threshold && Rcms < 60) {

forward();

Serial.print(" Moving forward!"); } }

// methods for driving the motors in each direction. Backward isn't used here, but could be.

void forward() {

analogWrite(motorR_A, 0);

analogWrite(motorR_B, speed);

analogWrite(motorL_A, 0);

analogWrite(motorL_B, speed); }

void backward() {

analogWrite(motorR_A, speed);

analogWrite(motorR_B, 0);

analogWrite(motorL_A, speed);

analogWrite(motorL_B, 0); }

void left() {

analogWrite(motorR_A, speed);

analogWrite(motorR_B, 0);

analogWrite(motorL_A, 0);

analogWrite(motorL_B, speed); }

void right() {

analogWrite(motorR_A, 0);

analogWrite(motorR_B, speed);

analogWrite(motorL_A, speed);

analogWrite(motorL_B, 0); }

Step 3: Final Tests

Here is a video of my car running. The car should follow your hand, foot, etc. when it's placed in front of the sensors, in the closest direction (i.e. move right if the object is closest to the right sensor). If it isn't working as expected, you might have the motor leads positive and negative flipped so the motor is running in the opposite direction.

I hope this has been helpful and good luck building your own car!