Arduino UNO R4 GPS

Hướng dẫn này sẽ chỉ bạn cách sử dụng Arduino Uno R4 với module GPS. Cụ thể, chúng ta sẽ học cách tìm tọa độ GPS (kinh độ, vĩ độ, độ cao), tốc độ GPS (theo km/h), và thời gian hiện tại từ module GPS NEO-6M. Chúng ta cũng sẽ học cách tính khoảng cách từ vị trí GPS hiện tại đến tọa độ cụ thể, như tọa độ của London.

Arduino UNO R4 GPS module

Phần Cứng Cần Thiết

1×Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima
1×(Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi
1×Arduino UNO R4 Minima (Có thể thay thế)
1×Cáp USB Type-C
1×Module GPS NEO-6M
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Module GPS NEO-6M

Sơ Đồ Chân

Module GPS NEO-6M có 4 chân:

  • Chân VCC: Kết nối chân này với VCC (5V).
  • Chân GND: Kết nối chân này với GND (0V).
  • Chân TX: Được sử dụng cho giao tiếp serial. Kết nối với chân RX của Serial (hoặc SoftwareSerial) trên Arduino UNO R4.
  • Chân RX: Được sử dụng cho giao tiếp serial. Kết nối với chân TX của Serial (hoặc SoftwareSerial) trên Arduino UNO R4.
neo-6m GPS module sơ đồ chân

Sơ Đồ Kết Nối

Arduino UNO R4 GPS module sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Xin lưu ý rằng sơ đồ kết nối được hiển thị ở trên có thể hoạt động, nhưng không được khuyến nghị. Chân TX của Arduino UNO R4 phát ra tín hiệu 5V, trong khi chân RX của module GPS chỉ có thể xử lý 3.3V. Để an toàn, bạn nên sử dụng bộ chia điện áp giữa chân TX của Arduino UNO R4 và chân RX của module GPS. Cách thiết lập này được minh họa trong sơ đồ bên dưới.

Arduino UNO R4 GPS sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code Arduino UNO R4

Đọc tọa độ GPS, tốc độ (km/h), và thời gian

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino-uno-r4/arduino-uno-r4-gps */ #include <TinyGPS++.h> #include <SoftwareSerial.h> #define RX_PIN 4 // The Arduino UNO R4 pin connected to the TX of the GPS module #define TX_PIN 3 // The Arduino UNO R4 pin connected to the RX of the GPS module TinyGPSPlus gps; // the TinyGPS++ object SoftwareSerial gpsSerial(RX_PIN, TX_PIN); // the serial interface to the GPS module void setup() { Serial.begin(9600); gpsSerial.begin(9600); // Default baud of NEO-6M GPS module is 9600 Serial.println(F("Arduino - GPS module")); } void loop() { if (gpsSerial.available() > 0) { if (gps.encode(gpsSerial.read())) { if (gps.location.isValid()) { Serial.print(F("- latitude: ")); Serial.println(gps.location.lat()); Serial.print(F("- longitude: ")); Serial.println(gps.location.lng()); Serial.print(F("- altitude: ")); if (gps.altitude.isValid()) Serial.println(gps.altitude.meters()); else Serial.println(F("INVALID")); } else { Serial.println(F("- location: INVALID")); } Serial.print(F("- speed: ")); if (gps.speed.isValid()) { Serial.print(gps.speed.kmph()); Serial.println(F(" km/h")); } else { Serial.println(F("INVALID")); } Serial.print(F("- GPS date&time: ")); if (gps.date.isValid() && gps.time.isValid()) { Serial.print(gps.date.year()); Serial.print(F("-")); Serial.print(gps.date.month()); Serial.print(F("-")); Serial.print(gps.date.day()); Serial.print(F(" ")); Serial.print(gps.time.hour()); Serial.print(F(":")); Serial.print(gps.time.minute()); Serial.print(F(":")); Serial.println(gps.time.second()); } else { Serial.println(F("INVALID")); } Serial.println(); } } if (millis() > 5000 && gps.charsProcessed() < 10) Serial.println(F("No GPS data received: check wiring")); }

Các Bước Thực Hiện

Thực hiện theo các hướng dẫn sau từng bước:

  • Nếu đây là lần đầu tiên bạn sử dụng Arduino Uno R4 WiFi/Minima, hãy tham khảo hướng dẫn về Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Kết nối Arduino Uno R4 với module GPS theo sơ đồ được cung cấp.
  • Kết nối board Arduino Uno R4 với máy tính của bạn bằng cáp USB.
  • Khởi động Arduino IDE trên máy tính của bạn.
  • Chọn board Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 WiFi) và cổng COM.
  • Đi đến biểu tượng Libraries ở phía bên trái của Arduino IDE.
  • TinyGPSPlus trong hộp tìm kiếm, sau đó tìm thư viện TinyGPSPlus của Mikal Hart
  • Nhấp vào nút Install để thêm thư viện TinyGPSPlus.
Arduino UNO R4 tinyGPS++ thư viện
  • Sao chép code và mở nó trong Arduino IDE.
  • Nhấn nút Upload trong Arduino IDE để tải code lên Arduino UNO R4.
  • Kiểm tra kết quả trên Serial Monitor.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Tính khoảng cách từ vị trí hiện tại đến vị trí định trước

Code này tìm ra khoảng cách từ vị trí của bạn đến London (vĩ độ: 51.508131, kinh độ: -0.128002).

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino-uno-r4/arduino-uno-r4-gps */ #include <TinyGPS++.h> #include <SoftwareSerial.h> #define RX_PIN 4 // The Arduino UNO R4 pin connected to the TX of the GPS module #define TX_PIN 3 // The Arduino UNO R4 pin connected to the RX of the GPS module TinyGPSPlus gps; // the TinyGPS++ object SoftwareSerial gpsSerial(RX_PIN, TX_PIN); // the serial interface to the GPS module const double LONDON_LAT = 51.508131; const double LONDON_LON = -0.128002; void setup() { Serial.begin(9600); gpsSerial.begin(9600); // Default baud of NEO-6M GPS module is 9600 Serial.println(F("Arduino - GPS module")); } void loop() { if (gpsSerial.available() > 0) { if (gps.encode(gpsSerial.read())) { if (gps.location.isValid()) { double latitude = gps.location.lat(); double longitude = gps.location.lng(); unsigned long distanceKm = TinyGPSPlus::distanceBetween(latitude, longitude, LONDON_LAT, LONDON_LON) / 1000; Serial.print(F("- latitude: ")); Serial.println(latitude); Serial.print(F("- longitude: ")); Serial.println(longitude); Serial.print(F("- distance to London: ")); Serial.println(distanceKm); } else { Serial.println(F("- location: INVALID")); } Serial.println(); } } if (millis() > 5000 && gps.charsProcessed() < 10) Serial.println(F("No GPS data received: check wiring")); }

Các Bước Thực Hiện

  • Sao chép code và mở nó trong Arduino IDE.
  • Nhấn nút Upload trong Arduino IDE để gửi code đến Arduino UNO R4.
  • Kiểm tra kết quả trên Serial Monitor.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.

Bài hướng dẫn liên quan