/------轉載請附上本文鏈接 https://www.cnblogs.com/blogs-192-168-9-222/p/11506638.html -------啦啦啦我是快樂的分割線- ------------/
小車圖片😀ε=ε=ε=(~ ̄▽ ̄)~,其實很簡單一起來看看叭
廢話不多說。首先做這個智能小車所需要的東西有
硬件支持:
1.Arduino Uno 開發板
2.L298N電機驅動模塊
3.ESP8266芯片一塊
4.SG90舵機一個
5.HC-SR04超聲波模塊一個
6.一些杜邦線
7.7.4V(5600mA)鋰電池充
8.四個直流電機和一個四輪車底盤(某寶上十幾塊錢的就都有,只是要自己拼裝)
軟件支持:
1.Arduino IDE
2.Blynk App(手機安卓客戶端)
東西都備齊了就可以動手開始做啦。做這個東西從學習arduino到制作完成一共用了大約10天,中間還有七七八八的事情,總算制作完成了
L298N Arduino Uno ESP8266
HC-SR04超聲波模塊 SG90 舵機
------------------------------------------制作過程------------------------------------------------
一、對軟件Arduino IDE要下載一些外加庫和一些處理
1.文件-首選項-附加開發板管理器網址: http://arduino.esp8266.com/stable/package_esp8266com_index.json
2.下載Blynk庫,發現網上的文章都要到處找庫,其實你在下面的庫管理器里面可以下載最新的
然后在第三方庫示例里面可以看見Blynk庫
3.還有一些庫統一下載放在arduino里自帶的libraries文件夾里面
鏈接:https://pan.baidu.com/s/1mQo-x83PEcdNnmfdB6LGIA
提取碼:y1d0
二、然后硬件和編程方面
esp8266
esp8266和單片機連線
VCC-3.3V GND-GND TX和RX反接arduino的RX和TX,我接的是RX-A0 ,TX-A1
RST和IO可以置空
ESP8266默認波特率為115200,可以通過arduino軟件調試串口波特率為可以和arduino通信的9600波特率。
1 #include <SoftwareSerial.h> 2 3 SoftwareSerial mySerial(A1, A0); // RX, TX 4 5 6 void setup() { 7 8 // Open serial communications and wait for port to open: 9 10 Serial.begin(115200); 11 12 while (!Serial) { 13 14 ; // wait for serial port to connect. Needed for native USB port only 15 16 } 17 18 19 Serial.println("Goodnight moon!"); 20 21 22 // set the data rate for the SoftwareSerial port 23 24 mySerial.begin(115200); 25 26 mySerial.println("Hello, world?"); 27 28 } 29 30 31 32 void loop() { // run over and over 33 34 if (mySerial.available()) { 35 36 Serial.write(mySerial.read()); 37 38 } 39 40 if (Serial.available()) { 41 42 mySerial.write(Serial.read()); 43 44 } 45 46 }
上傳代碼,打開串口,調波特率為115200波特率,和換行格式 NL和CR
串口輸出
表明進入AT模式
再發送 AT+UART=9600,8,1,0,0 串口輸出OK ,就成功修改了esp8266的波特率為9600,可以讓esp8266和arduino uno通信啦。
調試好esp還有blynk呢
Blynk,先在華為商場或者別的,只要是安卓手機都行,下載Blynk APP
Blynk里面的物件需要能量,限2000,拖出來不用了的可以循環托回去,長按拖入那個三角循環浮標的,
不要Delete啊,我就Delete只有1400了,心疼(~﹃~)~zZ。
每建立一個工程blynk會發送郵箱給你,里面的token很重要,所以郵箱不要亂寫喲
然后是L298N
先看一下基礎教程:https://www.bilibili.com/video/av59280674?from=search&seid=424387283798668433
里面的使能是調速是通過自控使能引腳控制頻率調速
我們可以不拔掉跳帽,是使能引腳保持高電平,處於使能狀態,然后通過內置函數調PWM波
IN1,IN2,IN3,IN4分別接線 ~11,~6,~5,~3
7.4V(5600mA)鋰電池在+12V和GND處對驅動供電,在無電腦供電情況下,可以通過5V對Arduino供電
注意L298N的GND要和Arduino接地,不然沒在一個電路,電機動不了
接好線,可以先試一下通過blynk簡單控制電機
先不看模式轉換,上面的控件可以是電機前進,后退,左轉,右轉,調速
代碼如下:

1 #define BLYNK_PRINT Serial 2 3 4 #include <ESP8266_Lib.h> 5 #include <BlynkSimpleShieldEsp8266.h> 6 7 // You should get Auth Token in the Blynk App. 8 // Go to the Project Settings (nut icon). 9 char auth[] = "YourAuthToken"; 10 11 // Your WiFi credentials. 12 // Set password to "" for open networks. 13 char ssid[] = "YourNetworkName"; 14 char pass[] = "YourPassword"; 15 16 // Hardware Serial on Mega, Leonardo, Micro... 17 #define EspSerial Serial1 18 19 // or Software Serial on Uno, Nano... 20 #include <SoftwareSerial.h> 21 SoftwareSerial EspSerial(A1, A0); // RX, TX 22 23 // Your ESP8266 baud rate: 24 #define ESP8266_BAUD 9600 25 ESP8266 wifi(&EspSerial); 26 27 void setup() 28 { 29 // Debug console 30 Serial.begin(9600); 31 32 delay(10); 33 34 // Set ESP8266 baud rate 35 EspSerial.begin(ESP8266_BAUD); 36 delay(10); 37 38 Blynk.begin(auth, wifi, ssid, pass); 39 } 40 41 void loop() 42 { 43 Blynk.run(); 44 }
YourAuthTokenW改為郵件里的Token,YourNetworkName,YourPassword改為你的8266可以連的WIFI的名字和密碼
只要是可以連的WIFI就行,手機熱點也行,讓8266聯網,然后和blynk雲端服務器與Arduino通信實現聯網控制功能
SG90舵機
舵機的橙、紅、棕三色線分別代表PWM輸入信號線,正極,負極
將超聲波模塊和舵機轉頭放在一起
超聲波模塊 Trig-12,Echo-~9
小車總代碼:
1 #define BLYNK_PRINT Serial 2 //#include <avr/wdt.h> 3 //#include<VoiceRecognition.h> 4 5 #define motor_in1 11//將電機驅動IN1接到A0端口 6 #define motor_in2 6 7 #define motor_in3 5 8 #define motor_in4 3 9 #define trig 12 10 #define echo 9 11 12 #include <ESP8266_Lib.h> 13 #include <BlynkSimpleShieldEsp8266.h> 14 #include <Servo.h> 15 //VoiceRecognition Voice; 16 char auth[] = "o370IDZiW12AbhEiQiquapDaJynIE40_"; 17 unsigned int right,left; 18 char ssid[] = "OPPO A9x"; 19 char pass[] = "12345678"; 20 int key=7; 21 int sound=8; 22 unsigned int cm; 23 Servo s; 24 // Hardware Serial on Mega, Leonardo, Micro... 25 26 #define EspSerial Serial1 27 #include <SoftwareSerial.h> 28 29 SoftwareSerial EspSerial(A1, A0); // RX, TX 30 31 // Your ESP8266 baud rate: 32 #define ESP8266_BAUD 9600 33 34 ESP8266 wifi(&EspSerial); 35 36 void setup() 37 { 38 // put your setup code here, to run once: 39 Serial.begin(9600); 40 delay(10); 41 42 EspSerial.begin(ESP8266_BAUD); 43 44 delay(10); 45 46 Blynk.begin(auth, wifi, ssid, pass); 47 pinMode(motor_in1,OUTPUT); 48 pinMode(motor_in2,OUTPUT); 49 pinMode(motor_in3,OUTPUT); 50 pinMode(motor_in4,OUTPUT); 51 pinMode(trig,OUTPUT); 52 pinMode(echo,INPUT); 53 s.attach(2); 54 s.write(90); 55 tone(12,800,500); 56 digitalWrite(motor_in1,LOW); 57 digitalWrite(motor_in2,LOW); 58 digitalWrite(motor_in3,LOW); 59 digitalWrite(motor_in4,LOW); 60 } 61 void range() 62 { 63 digitalWrite(trig,LOW); 64 delayMicroseconds(2); 65 digitalWrite(trig,HIGH); 66 delayMicroseconds(10); 67 digitalWrite(trig,LOW); 68 int temp = float(pulseIn(echo,HIGH)); 69 cm = (temp*17)/1000; 70 71 Serial.print("Echo ="); 72 Serial.print(temp);//串口輸出等待時間的原始數據 73 Serial.print(" | | Distance = "); 74 Serial.print(cm);//串口輸出距離換算成cm的結果 75 Serial.println("cm"); 76 delay(100); 77 78 if(cm<30) 79 { 80 tone(12,800,50); 81 delay(50); 82 } 83 } 84 void Leftrange() 85 { 86 digitalWrite(trig,LOW); 87 delayMicroseconds(2); 88 digitalWrite(trig,HIGH); 89 delayMicroseconds(10); 90 digitalWrite(trig,LOW); 91 int temp = float(pulseIn(echo,HIGH)); 92 cm = (temp*17)/1000; 93 94 Serial.print("Echo ="); 95 Serial.print(temp);//串口輸出等待時間的原始數據 96 Serial.print(" | | LeftDistance = "); 97 Serial.print(cm);//串口輸出距離換算成cm的結果 98 Serial.println("cm"); 99 delay(100); 100 101 if(cm<30) 102 { 103 tone(12,800,50); 104 delay(50); 105 } 106 } 107 void Rightrange() 108 { 109 digitalWrite(trig,LOW); 110 delayMicroseconds(2); 111 digitalWrite(trig,HIGH); 112 delayMicroseconds(10); 113 digitalWrite(trig,LOW); 114 int temp = float(pulseIn(echo,HIGH)); 115 cm = (temp*17)/1000; 116 117 Serial.print("Echo ="); 118 Serial.print(temp);//串口輸出等待時間的原始數據 119 Serial.print(" | | RightDistance = "); 120 Serial.print(cm);//串口輸出距離換算成cm的結果 121 Serial.println("cm"); 122 delay(100); 123 if(cm<30) 124 { 125 tone(12,800,50); 126 delay(50); 127 } 128 } 129 void turn() 130 { 131 stopit(); 132 delay(1000); 133 s.write(170); 134 delay(500); 135 Leftrange(); 136 left=cm; 137 s.write(90); 138 delay(500); 139 s.write(10); 140 delay(500); 141 Rightrange(); 142 right=cm; 143 s.write(90); 144 delay(500); 145 if(right>=left) 146 { 147 moveRight(); 148 delay(600); 149 moveForward(); 150 151 } 152 else 153 { 154 moveLeft(); 155 delay(600); 156 moveForward(); 157 158 } 159 } 160 161 void loop() { 162 // put your main code here, to run repeatedly: 163 Blynk.run(); 164 while(1) 165 { 166 Blynk.run(); 167 int val=digitalRead(key); 168 while(val==LOW) 169 { 170 Blynk.run(); 171 val=digitalRead(key); 172 } 173 while(1) 174 { 175 Blynk.run(); 176 val=digitalRead(key); 177 if(val==LOW) 178 { 179 stopit(); 180 break; 181 } 182 range(); 183 while(cm>30) 184 { 185 Blynk.run(); 186 val=digitalRead(key); 187 if(val==LOW) 188 { 189 stopit(); 190 break; 191 } 192 moveForward(); 193 range(); 194 if(cm<10) 195 break; 196 } 197 Blynk.run(); 198 val=digitalRead(key); 199 if(val==LOW) 200 { 201 stopit(); 202 break; 203 } 204 turn(); 205 } 206 } 207 } 208 209 void stopit() 210 { 211 digitalWrite(motor_in1,LOW); 212 digitalWrite(motor_in2,LOW); 213 digitalWrite(motor_in3,LOW); 214 digitalWrite(motor_in4,LOW); 215 } 216 void moveForward() 217 { 218 digitalWrite(motor_in1,HIGH); 219 digitalWrite(motor_in2,LOW); 220 analogWrite(motor_in1,150); 221 analogWrite(motor_in2,0); //調PWM波 222 digitalWrite(motor_in3,HIGH); 223 digitalWrite(motor_in4,LOW); 224 analogWrite(motor_in3,150); 225 analogWrite(motor_in4,0); 226 } 227 void moveRight() 228 { 229 digitalWrite(motor_in2,HIGH); 230 digitalWrite(motor_in1,LOW); 231 digitalWrite(motor_in4,LOW); 232 digitalWrite(motor_in3,LOW); 233 } 234 void moveLeft() 235 { 236 digitalWrite(motor_in1,LOW); 237 digitalWrite(motor_in2,LOW); 238 digitalWrite(motor_in3,HIGH); 239 digitalWrite(motor_in4,LOW); 240 } 241 242 void moveback() 243 { 244 digitalWrite(motor_in4,HIGH); 245 digitalWrite(motor_in3,LOW); 246 digitalWrite(motor_in2,HIGH); 247 digitalWrite(motor_in1,LOW); 248 249 }
避障代碼是我自己寫的,邏輯上有點,但是不是很嚴謹,有時會有一點問題,有更好代碼的伙伴們歡迎指教
有問題請大家指教(●ˇ∀ˇ●)~~~~~~~~