"This article will introduce you how to make Arduino-based remote control, the car is controlled by using Bluetooth applications with automatic brake functions, and the car will stop when the car encounters obstacles to avoid crash.
The car uses the ultrasonic sensor mounted on the server to detect whether there is an object in front, while the car will travel backwards. The car drives the gear motor through the L293D motor driver. And the car does not have any steering mechanism, so it performs left and right turn by friction, that is, the right wheel forward rotation and the left rotation (vice versa) to complete the turn. In this way, the car can also turn on the spot without moving or retreat.
Be
Hardware components:
Four wheel drive smart car chassis
Arduino Uno
L293D motor driver shroud
SG90 micro servo motor
Ultrasonic sensor-hc-sr04
HC-05 Bluetooth module
Battery casing
Battery
Design steps:
1, welding
Gear motor:
L293D motor shroud
Pins 2 and 13 are not used, so the pins of the ultrasonic sensor are welded on this pin; the transmission and reception of the Bluetooth module also requires pin 0 and 1, and the lead welding is received on the power pins of the shielded bottom.
2, assembled:
2 motors
Nylon wheel sticks in front with double-sided tape
Use the nylon tie to secure Arduino on the car and use double-sided tape to stick the battery in front of Arduino.
Add a switch on the battery of the battery
The 18650 battery is only 3.7V, and the power is not sufficient to power the car, so there is a 4X AA battery holder (each 1.5V), so the total voltage is 8.9V.
The ultrasonic sensor will be mounted on top of the server, using cardboard and tape to create a bracket for ultrasonic sensors
Use double-sided tape to install the servers on the side of the battery
Ultrasonic sensor will be installed on the top of the servers using double-sided tape
Ultimate design
detail:
Install the motor shroud to Arduino
Screw the motor into the terminal pair
Connect the servo to the servo 1 pin on the motor shield, the brown line here should be connected to the negative terminal
Insert the cable into the power terminal from the battery. (Make sure the positive electrode of the battery is connected to the positive electrode, the negative electrode is connected to the ground).
Connect the ultrasonic sensor to the cable and connect VCC and ground wire to 5V and ground pins. Connect the return pin to the pin 13, connect the trigger pin to the pin 2.
Connect the HC05 Bluetooth module to the cable and connect TXD and RXD to pin 0 and 1, then connect it. Connect the VCC and GND to the servo 2 pin on the motor shield.
3, programming:
The program requires two libraries, AFMotor.h is used to control motor cover, servo.h is used to control motor
When the button is pressed, the Arduino Bluetooth app sends a command / character as an input.
Use the Switch statement to press when pressing the button assignment in the Arduino Bluetooth application to make details.
Object sensing is performed by HC-SR04 ultrasonic sensor. The sensor emits ultrasonic waves through air, and if there is an object, it will rebound back to the module. In order to produce ultrasonic waves, the trigger pin is required to be high, lasting 10 microseconds, will transmit an 8-cycle sound wave at 40 kHz. When the sensor detects the reflected ultrasonic waves, the returning needle will become higher.
Using the Pulsein (Echo, HIGH) function, the waiting echo pin is higher, and the timeout as the parameter as a PULSEIN function is added, and set to the longest time waiting to return the signal. In this way, the time required to transmit and receive signals can be obtained by ultrasonic sensors. Since the ultrasonic wave is propagated in a sound speed, the distance from the sensor to the object can be calculated by dividing the signal returned at 2 and multiplying the sound speed in the air.
A function named getDistance is created, which returns the distance between the ultrasonic sensor and the object. This feature whenever the car moves forward or backward, when the distance returned by the function is less than the maximum distance I set, it will automatically stop the car. When the car is moving forward, the sensor will face the front; and when the car is running, the sensor will rearward. The direction of the sensor faces will be controlled by the server because the sensor will be mounted on the top of the server.
Code:
// Christopher Sutjiono
#include
// Import the library to control motor mask
#include // Import Library Control Servo
AF_DCMOTOR LEFTBACK (1);
AF_DCMOTOR RIGHTBACK (4);
Servo Servolook; // Create an object to control the servo
CHAR Command; / / Global variables for storing BT applications
Int motorspeed = 100; // speed variable (0-255)
BYTE TRIG = 2; // Assign Ultrasonic Sensor Pins
BYTE ECHO = 13;
Byte stopdist = 50; // From the object to the stop distance (cm)
FLOAT TIMEOUT = 2 * (MaxDist + 10) / 100/340 * 1000000; // Waiting for the longest time for returning signal
Void setup () {
Serial.Begin (9600); // Setting the Bluetooth module baud rate
Stopmove (); // Make sure all motors stop running
Servolook.attach (10); // Assigning servo sales
Servolook.write (180);
PINMODE (TRIG, OUTPUT); / / Specify Ultrasonic Sensor Pin Mode
PinMode (Echo, Input);
}
void loop ()
{
IF (serial.available ()> 0)
{
Command = serial.read (); // Store input to variables
Switch (command) {// will enter CASE used as Switch statement
Case 'f':
Servolook.write (180); // Set the servo to your forward
IF (getDistance ()> = stopdist) // If there is no object within the parking distance, move forward
{
Drive ();
}
Else Stopmove (); // If there is an object in the parking distance, parking
Break;
Case 'b':
Servolook.write (0); // Set the servo to see
IF (getDistance ()> = stopdist) // There is no object in the parking distance, move backward
{
Reverse (); // If there is an object in the parking distance, parking
}
Else stopmove ();
Break;
Case 'L': TurnLeft (); Break;
Case 'R': TurnRight (); Break;
Case 'g': driveleft (); break;
Case 'I': driveright (); Break;
Case 'h': Reverseleft (); Break;
Case 'J': REVERSERIGHT (); BREAK;
Case 's': stopmove (); Break;
CASE '0': motorspeed = 0; Break;
Case '1': motorSpeed = 25; Break;
Case '2': motorSpeed = 50; Break;
Case '3': motorSpeed = 75; Break;
Case '4': motorspeed = 100; Break;
Case '5': motorSpeed = 125; Break;
Case '6': motorSpeed = 150; Break;
Case '7': motorSpeed = 175; Break;
Case '8': motorSpeed = 200; Break;
Case '9': motorSpeed = 225; Break;
Case 'Q': motorspeed = 250; Break;
}
}
}
Void Drive ()
{
LEFTBACK.SETSPEED (Motorspeed); // Defines the maximum speed
LEFTBACK.RUN (Forward); // Clockwise rotary motor
Rightback.setSpeed (Motorspeed); // Defines the maximum speed
Rightback.run (forward); // clockwise rotating motor
}
void reverse ()
{
LEFTBACK.SETSPEED (Motorspeed); // Defines the maximum speed
Leftback.run (backward); // Rotate the motor counterclockwise
Rightback.setSpeed (Motorspeed); // Defines the maximum speed
Rightback.run (backward); // Rotate the motor counterclockwise
}
Void Turnft ()
{
LEFTBACK.SETSPEED (Motorspeed); // Defines the maximum speed
Leftback.run (backward); // Rotate the motor counterclockwise
Rightback.setSpeed (Motorspeed); // Defines the maximum speed
Rightback.run (forward); // clockwise rotating motor
}
Void TurnRight ()
{
LEFTBACK.SETSPEED (Motorspeed); // Defines the maximum speed
LEFTBACK.RUN (Forward); // Clockwise rotary motor
Rightback.setSpeed (Motorspeed); // Defines the maximum speed
Rightback.run (backward); // Rotate the motor counterclockwise
}
Void Driveright ()
{
LEFTBACK.SETSPEED (Motorspeed); // Defines the maximum speed
LEFTBACK.RUN (Forward); // Clockwise rotary motor
Rightback.setSpeed (Motorspeed / 4); // Defines the maximum speed
Rightback.run (forward); // clockwise rotating motor
}
Void Reverseeright ()
{
LEFTBACK.SETSPEED (Motorspeed / 4); // Defines the maximum speed
Leftback.run (backward); // Rotate the motor counterclockwise
Rightback.SetSpeed (Motorspeed);// Define the maximum speed
Rightback.run (backward); // Rotate the motor counterclockwise
}
void driveleft ()
{
LEFTBACK.SETSPEED (Motorspeed / 4); // Defines the maximum speed
LEFTBACK.RUN (Forward); // Clockwise rotary motor
Rightback.setSpeed (Motorspeed); // Defines the maximum speed
Rightback.run (forward); // clockwise rotating motor
}
Void Reverseleft ()
{
LEFTBACK.SETSPEED (Motorspeed); // Defines the maximum speed
Leftback.run (backward); // Clockwise rotating motor
Rightback.setSpeed (Motorspeed / 4); // Defines the maximum speed
Rightback.run (backward); // Rotate the motor counterclockwise
}
Void stopmove () // Sets all motors to stop
{
LEFTBACK.SETSPEED (0); // Define the minimum speed
Leftback.run (Release); // Clockwise rotating motor
Rightback.setSpeed (0); // Define the minimum speed
Rightback.run (Release); // Stop the motor when the button is released
}
INT getDistance () // Gets the distance between ultrasonic sensors and objects
{
Unsigned long pulsetime; // Create a variable to store the pulse travel time
INT distance; // Create variables to store the calculated distance
DigitalWrite (TRIG, HIGH); // Generates a pulse of 10 microseconds
DelayMICROSECONDS (10);
DigitalWrite (TRIG, LOW);
Pulsetime = pulsein (echo, high, timeout); // Measure the time of pulse returns
Distance = (float) Pulsetime * 340/2/10000; / / Calculate the target distance according to pulse time
Return distance;
"
Our other product: