<> one , Required components

arduino nano One (Uno too big , So we chose the smaller one Nano)
L298N One motor drive module
Two DC reduction motors
A universal wheel
Acrylic board
Two tires
TCRT5000 Four tracking sensors
( Quick jump to three , Connection part )

<> two , Detailed introduction of components

(1)arduino nano

Here's one Nano Picture of , It's rather obscure , No problem , I don't understand . In this project, you just need to know GDN(Ground) It's grounded ,3v3 and 5v Is the output voltage ( to Nano Board power on and energy consumption ). The pin of the curve in the figure (D3,D5,D6,D9,D10,D11) Available PWM output .( Generally speaking : These pins can output any value between low voltage and high voltage , The other pins either output high voltage , Or output low voltage )

(2)L298N Motor drive module
It looks like this ( Picture from tb)
Pay attention to the area circled in red : It says yes ENA,IN1,IN2,IN3,IN4,ENB.ENA amount to EN1,EN2 Main switch of the power supply ,ENB corresponding IN3,IN4 Main switch of the power supply . example : Give Way ENA High level ,IN1,IN2 The signal given by foot position is the signal , if ENA Low level ,IN1,IN2 No matter what signal is given, these two pins are low level .(
notes : It can only be understood in this way ,ENA and ENB It has a special name called enable )
(3) DC reduction motor , Universal wheel , Acrylic board

I use it like this ( Picture from tb), Of course, there are many DC motors for you to choose from , The graphic should be the cheapest .
(4)TCRT5000 Tracking sensor

Notice that there are four pins in the figure , It will be used in this project A0,VCC,GND, Three pins ,VCC and GND It is positive and negative , For power supply ,A0 To output the detected information .(D0 Not used , You don't have to take any line ).

<> three , Connection

TCRT5000 The tracking sensor is placed in the front of the car , Numbered from left to right Lb,La,Ra,Rb.Lb,La,Ra,Rb Of vcc even arduino Plank 5V,GND even arduino Plank GND, his A0 The feet are connected separately arduino Of A0,A1,A2,A3 Foot position . This can be seen clearly in the program .

L298N The connection of motor drive module is also expressed in the program , example ,ENA=12 representative ENA Connect to arduino Of 12 Foot position .OUT1, And OUT2 Connect the upper and lower ends of the motor respectively .( Even if the order is different, the program will be written differently ). Here you can try it yourself .

<> four , Program code

Before uploading the program, first select the development board as Arduino Nano, The following processor options also need to be changed , The general situation is choice ATmega328P(Old
Bootloader), If it's a new version Nano Development board to choose the other two try .
int ENA=12,IN1=9,IN2=6,IN3=5,IN4=3,ENB=2; int La,Ra,Lb,Rb; void setup() {
//Serial.begin(9600); pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(5,OUTPUT);
pinMode(6,OUTPUT); pinMode(9,OUTPUT); pinMode(12,OUTPUT); } void loop() {
digitalWrite(ENA,HIGH); digitalWrite(ENB,HIGH); La=analogRead(A0); Ra=analogRead
(A1); Rb=analogRead(A2); Lb=analogRead(A3); /*Serial.print(Lb);
Serial.print(";"); Serial.print(La); Serial.print(";"); Serial.print(Ra);
Serial.print(";"); Serial.println(Rb); delay(300);*/ if(La<=300 && Ra<=320)
// No black line was detected straight(); if(La>=Ra && Lb<=300 && Rb<=300)// Black line detected on left side , There's no one on the right , So turn left
straightturnleft(); if(La<=Ra && Lb<=300 && Rb<=300)// Black line detected on right side , There's no one on the left , So turn right
straightturnright(); if(La>=300 && Ra>=300)// Black lines were detected in all cases straight(); if(Rb>=300)// Big right turn
turnright(); if(Lb>=300)// Big left turn turnleft(); } void straight() { analogWrite(IN1,0)
;// Zuo Zheng analogWrite(IN2,60); analogWrite(IN3,60);// Right Zheng analogWrite(IN4,0); } void
straightturnleft() { analogWrite(IN1,0);// Zuo Zheng analogWrite(IN2,60); analogWrite(
IN3,100);// Right Zheng analogWrite(IN4,0); } void straightturnright() { analogWrite(IN1,0
);// Zuo Zheng analogWrite(IN2,100); analogWrite(IN3,60);// Right Zheng analogWrite(IN4,0); } void
turnright() { analogWrite(IN1,0);// Zuo Zheng analogWrite(IN2,150); analogWrite(IN3,60);
// Right Zheng analogWrite(IN4,0); } void turnleft() { analogWrite(IN1,0);// Zuo Zheng analogWrite
(IN2,60); analogWrite(IN3,150);// Right Zheng analogWrite(IN4,0); }
<> five , Some notes

The number in the code block 300 and 320 It's a relative value I measured, which can accurately distinguish whether the black line is detected or not , Not necessarily very accurate , Interested students can also use the serial monitor to test their own .
Re enable the lines commented out in the code to monitor the serial port , After importing the program, click the small magnifying glass in the upper right corner to view the serial port .(Serial The first few lines )
if(La<=300 && Ra<=320)// No black line was detected straight(); if(La>=Ra && Lb<=300 && Rb<=300)
// Black line detected on left side , There's no one on the right , So turn left straightturnleft(); if(La<=Ra && Lb<=300 && Rb<=300)
// Black line detected on right side , There's no one on the left , So turn right straightturnright(); if(La>=300 && Ra>=300)// Black lines were detected in all cases straight
(); if(Rb>=300)// Big right turn turnright(); if(Lb>=300)// Big left turn turnleft();

It's for use arduino My first project , If there's no accident , It should be the last one . I'm studying recently python, I hope you will not forget your original intention , Keep going . Busy with study , Update the fate . last arduino It is a relatively low-end language in the industry , It is popular among high school students , Let's have a little understanding , You don't have to go into this very deeply . It's a good choice to take part in the competition , But try not to be in the arduino Too much time on the Internet .

Technology