Finished product preview

This one was made by me (I removed the ultrasound module) :

Circuit diagram

Bluetooth APP-Controlled Dual Motor Device

material

  1. Arduino UNO (and what Teacher Bi Wants)
  2. L298N motor drive one (taobao 6 yuan)
  3. Three-wheeled car base a (Taobao 15 yuan or so)
  4. Copper column, screw, nut, Dupont line several
  5. Four batteries and one battery case
  6. One HC-06 Bluetooth module
  7. Bluetooth Serial Assistant APP
  8. One HC_SR04 ultrasonic module
  9. Differential motor two, generally buy chassis will bring, in order to prevent burning out suggest a spare
  10. Arduino development tools

Arduino UNO on-chip resources

The pins (A0 to A5) that support Analog (Analog) in the lower left corner are numbered 14 to 19.

L298N module PWM speed control

How to Understand PWM

PWM, English full name Pulse Width Modulation, Chinese Pulse Width Modulation. Pulse width modulation is a technique for converting analog signals into pulses.

Consider a scenario where a light connection is working properly in a circuit. Suddenly, unluckily, the switch in the circuit was falsely connected, causing the lights to flicker on and off. If the flicker frequency is fast enough, the human eye cannot distinguish the flicker of an electric light, which is the principle of movies and videos. But at one point, the brightness of the lamp will definitely decrease. When the proportion of the light time to the total time is large, the human eye perception of the light will become brighter, the greater the proportion of the dark time, then the human eye perception of the light will naturally become dark.

If you understand the above scenario, then you can understand the essence of PWM. PWM is a method of digital encoding analog signal level. The ratio of the time when the lamp is on to the total time mentioned in the above scenario is similar to the duty cycle concept in THE PWM. Duty cycle is the proportion of the total time spent on a pulse cycle. For example, a pulse train with pulse width of 0.001ms and signal period of 0.004ms has a duty cycle of 0.25.

If the peak voltage of the pulse (square wave) is 5V, the equivalent voltage value is 1.25V product of the peak voltage of the square wave and the duty ratio at the pulse duty ratio of 0.25

PS: I have spoken enough simple, if you do not understand, I suggest to go to make up for the lessons in advance. The next part is going to be even harder.

L298N module

L298 is a DUAL full-bridge DRIVER manufactured by ST Company. Operating voltage between 4.8-46V, output current up to 2A. Can drive two two phase motor, generally used to four wheel drive car, drive two wheel car, really have a kind of chicken how to kill with a knife feeling.

L298N module can be directly connected with the single chip microcomputer, it is very convenient to control.

L298N Logical function list

IN1 IN2 ENA state
X X 0 stop
1 0 1 clockwise
0 1 1 counterclockwise
1 1 0 stop

The logic of IN3, IN4, and ENB is the same as that in the preceding table.

HC_SR04 Ultrasonic module ranging

HC_SR04 working principle

Observe the above timing diagram. When a trigger signal of more than 10uS is provided, HC_SR04 will emit 8 cycle levels of 40kHz internally and detect the echo. Once the echo signal is detected, the output echo signal is stopped. So the pulse width of the echo signal is proportional to the distance. From this the distance can be calculated.

Take the speed of sound 343m/s (dry, room temperature 20 degrees), 1 microsecond distance is 0.0343 cm. Take the inverse, and you get 29.15 microseconds for every centimeter that sound travels.

And because the distance traveled from the sound to the echo received should be twice the distance, the actual distance of 1 cm should correspond to 58.3 microseconds, rounded 58.

So you can see code like this in your program:

int getDistance (a) {
  digitalWrite(ultrasonicOutputPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ultrasonicOutputPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultrasonicOutputPin, LOW);

  int distance = pulseIn(ultrasonicInputPin, HIGH);
  distance = distance / 58;
  return distance > 0 ? distance: 0;
}
Copy the code

In the preceding code, the ultrasonicOutputPin was connected to the Trig pins of the ultrasonic module, and the ultrasonicInputPin was connected to the Echo pins of the ultrasonic module.

Based on the working principle of ultrasonic module, ultrasonic ranging is suitable for hard and flat large objects, other scenes have poor performance.

Hc-06 (HC-05) Bluetooth module

Hc-06 and HC-05 modules are roughly the same. For Arduino users, they generally buy JY-MCU directly from Taobao, and I am no exception. Modules are similar to serial ports. The User Guide provides rich and detailed examples, which I will also briefly cover in the following articles.

Considering the expansibility of the car, for example, ESP8266 may be connected in the future, the hardware serial port of Arduino will not be used for the time being, and the soft serial port will be used for the connection between bluetooth module and board. The same soft serial port related content will be described below.

The reference code for soft serial port connection to bluetooth module is as follows:

#include <SoftwareSerial.h>

SoftwareSerial bluetoothSerial(11.12); // RX, TX

void setup(a) {
  // put your setup code here, to run once:
  Serial.begin(57600);
  bluetoothSerial.begin(57600);
   bluetoothSerial.print("AT");
  delay(1000);
  bluetoothSerial.println("AT+VERSION");
  delay(1000);
  bluetoothSerial.println("AT+BAUD7");
  delay(1000);
  bluetoothSerial.println("AT+NAMEarduino_car");
  delay(1000);
}
void bluetoothTaskCallback(a) {
  if (bluetoothSerial.available()) {
    int bluetoothCmd = (int)bluetoothSerial.read();
    Serial.print("bluttooth Command: ");
    Serial.println(bluetoothCmd);

    // TODO:Bluetooth incoming parameter verification
    motorCtrl(bluetoothCmd);
  }
  if (Serial.available()) {
    Serial.print("Serial available: "); Serial.println(Serial.read()); bluetoothSerial.write(Serial.read()); }}Copy the code

In the code above, the Receiving pin (RX) of the Arduino soft serial port is 11, which is connected to the sending pin (TX) of the Bluetooth module. The sending pin (TX) is 12, with the Bluetooth module’s receiving pin (RX).

During the initialization process of the board, some Settings are made on the Bluetooth module using the AT instruction:

  • AT+BAUD7Set the baud rate of the bluetooth module to 57600bps (the highest rate for the Arduino soft serial port).The communication can be normal only when the baud rate on both sides is set to the same.
  • AT+NAMEarduino_carSet the name of Bluetooth to “Arduino_car”, so that we can use bluetooth debugging assistant to connect.

Software analog serial port

In addition to hardware serial ports (D0 and D1 ports on the board), The SoftwareSerial class library is also provided by Arduino, which allows users to programmatically simulate other digital pins (D*) into serial communication pins.

Software analog serial port is called soft serial port, and the use of hardware serial port is basically the same. See Arduino Reference SoftwareSerial Libray for a detailed description of the class library.

As noted in Reference, the soft serial port has the following set of limitations:

The library has the following known limitations:

  • If using multiple software serial ports, only one can receive data at a time.
  • Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
  • Not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
  • On Arduino or Genuino 101 the current maximum RX speed is 57600bps
  • On Arduino or Genuino 101 RX doesn’t work on Pin 13

If your project requires simultaneous data flows, see Paul Stoffregen’s AltSoftSerial library. AltSoftSerial overcomes a number of other issues with the core SoftwareSerial, but has it’s own limitations. Refer to the AltSoftSerial site for more information.

The translation reads as follows:

This library has the following known limitations:

  • If multiple soft serial ports are used, only one data can be received at a time, and the serial ports interfere with each other.
  • Because not all pins on the Mega and Mega2560 boards support changing interrupts, only the following pins can be used as receiving pins (RX) for soft serial ports: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
  • On Arduino and Genuino 101, the maximum reception rate is 57,600 BPS.
  • On Arduino and Genuino 101, the receiving pin (RX) cannot be pin 13.

If you need to synchronize data streams in your project, see Paul Stoffregen’s AltSoftSerial library. AltSoftSerial also addresses some of the other problems in SoftwareSerial, but it has its own limitations. Please refer to the AltSoftSerial website for more information.

Bluetooth Serial Assistant

For convenience, I choose to use Android phone as bluetooth host (Master) and Bluetooth serial module as slave (Salve). Leaders and followers are better, but masters and Salves are suspected of violating human rights.

The setting process of Bluetooth serial Assistant is quite tedious. I referred to the Arduino smart car — Bluetooth Car written by CSDN blogger “Music Illiterate Appreciation” in the setting process.

Through the Bluetooth serial assistant of the mobile phone, a series of instructions can be given to the car, the contents of which are as follows:

instruction meaning note
01 stop
02 forward
03 back
04 Turn left
05 Turn right
06 Control of motor speed

The specific control functions are a bit complicated, so consider a separate article later.

Task scheduling

At present, the car supports bluetooth remote control, speed regulation, and placed in the direction of the forward ultrasonic module to detect the distance, to prevent collisions. The two tasks can be placed in void loop() and controlled by delay(). This may seem like a good solution (albeit a low one), but if you have too many tasks, it’s not enough.

In order to better coordinate the two tasks and ensure the orderly operation of the two tasks, scheduler is introduced. The scheduler can execute tasks on a specific cycle.

Code used in the scheduler making Repo:github.com/arkhipenko/…

Call reference code:

#include <TaskScheduler.h>

Scheduler runner;

void ultrasonicTaskCallback(a);
void bluetoothTaskCallback(a);

Task bluetoothTask(150,TASK_FOREVER,&bluetoothTaskCallback, &runner, true);
Task ultrasonicTask(500,TASK_FOREVER,&ultrasonicTaskCallback, &runner,true);

void loop(a) {
  runner.execute();
}
Copy the code

conclusion

A large part of the content involved in the article is the basic knowledge of engineering students, such as PWM, serial communication, ultrasonic ranging, motor control and so on. However, in the process of writing, I found it difficult to explain how to make an Arduino car and make it run in just a few thousand words. Because these things look simple, but the range of knowledge involved is quite wide.

In the process of the experiment, if you have any questions, you can contact me through the public account or the comment section.

The complete code

Get the public number dialog box to reply “Arduino car” to get the complete source code + circuit diagram

conclusion

A large part of the content involved in the article is the basic knowledge of engineering students, such as PWM, serial communication, ultrasonic ranging, motor control and so on. However, in the process of writing, I found it difficult to explain how to make an Arduino car and make it run in just a few thousand words. Because these things look simple, but the range of knowledge involved is quite wide.

In the process of the experiment, if you have any questions, you can contact me through the public account or the comment section.

Long press two seconds to identify the TWO-DIMENSIONAL code concerns ▼

“Public” dialog box, reply keyword “bytedance internal push” to obtain bytedance internal push code