Arduino UNO R4 Ethernet
Hướng dẫn này sẽ chỉ cho bạn cách kết nối Arduino UNO R4 với internet hoặc mạng nội bộ của bạn bằng module Ethernet W5500. Chúng ta sẽ thảo luận các chi tiết sau:
- Cách kết nối Arduino UNO R4 với Module Ethernet W5500
- Cách lập trình Arduino UNO R4 để tạo HTTP Requests qua Ethernet
- Cách tạo một Web Server cơ bản trên Arduino UNO R4 qua Ethernet

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 (Thay thế) | ||
| 1 | × | Cáp USB Type-C | ||
| 1 | × | Module Ethernet W5500 | ||
| 1 | × | Cáp Ethernet | ||
| 1 | × | Dây nối Jumper | ||
| 1 | × | breadboard | ||
| 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 Ethernet W5500
Module Ethernet W5500 cung cấp hai giao diện:
- Giao diện Ethernet RJ45: Kết nối với router hoặc switch bằng cáp Ethernet.
- Giao diện SPI: Kết nối với bo mạch Arduino UNO R4 thông qua giao diện này. Nó bao gồm 10 chân:
- Chân NC: Để chân này không kết nối.
- Chân INT: Để chân này không kết nối.
- Chân RST: Đây là chân reset. Kết nối nó với chân EN trên Arduino UNO R4.
- Chân GND: Kết nối chân này với chân GND trên Arduino UNO R4.
- Chân 5V: Kết nối chân này với chân 5V trên Arduino UNO R4.
- Chân 3.3V: Để chân này không kết nối.
- Chân MISO: Kết nối chân này với chân SPI MISO trên Arduino UNO R4.
- Chân MOSI: Kết nối chân này với chân SPI MOSI trên Arduino UNO R4.
- Chân SCS: Kết nối chân này với chân SPI CS trên Arduino UNO R4.
- Chân SCLK: Kết nối chân này với chân SPI SCK trên Arduino UNO R4.

image source: diyables.io
Sơ đồ đấu dây giữa Arduino UNO R4 và module Ethernet W5500

This image is created using Fritzing. Click to enlarge image
Code Arduino UNO R4 cho Module Ethernet - Tạo HTTP request qua Ethernet
Code này hoạt động như một web client. Nó gửi các HTTP requests đến web server tại http://example.com/.
/*
* 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-ethernet-module
*/
#include <SPI.h>
#include <Ethernet.h>
// replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
int HTTP_PORT = 80;
String HTTP_METHOD = "GET"; // or POST
char HOST_NAME[] = "example.com";
String PATH_NAME = "/";
void setup() {
Serial.begin(9600);
delay(1000);
Serial.println("Arduino Uno R4 - Ethernet Tutorial");
// initialize the Ethernet shield using DHCP:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to obtaining an IP address");
// check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware)
Serial.println("Ethernet shield was not found");
// check for Ethernet cable
if (Ethernet.linkStatus() == LinkOFF)
Serial.println("Ethernet cable is not connected.");
while (true)
;
}
// connect to web server on port 80:
if (client.connect(HOST_NAME, HTTP_PORT)) {
// if connected:
Serial.println("Connected to server");
// make a HTTP request:
// send HTTP header
client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println(); // end HTTP header
while (client.connected()) {
if (client.available()) {
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
Serial.print(c);
}
}
// the server's disconnected, stop the client:
client.stop();
Serial.println();
Serial.println("disconnected");
} else { // if not connected:
Serial.println("connection failed");
}
}
void loop() {
}
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 tutorial về Arduino UNO R4 - Cài Đặt Phần Mềm.
- Kết nối module Ethernet với Arduino UNO R4 của bạn theo sơ đồ được cung cấp
- Kết nối module Ethernet với router hoặc switch của bạn bằng cáp Ethernet.
- Kết nối bo mạch Arduino Uno R4 với máy tính của bạn bằng cáp USB.
- Mở Arduino IDE trên máy tính của bạn.
- Chọn bo mạch Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 Minima) và cổng COM.
- Nhấp vào biểu tượng Libraries ở phía bên trái của Arduino IDE.
- Trong ô tìm kiếm, gõ "Ethernet" và tìm thư viện Ethernet bởi Various.
- Nhấn nút Install để thêm thư viện Ethernet.

- Mở Serial Monitor trong Arduino IDE.
- Sao chép code đã cho và dán vào Arduino IDE.
- Nhấp nút Upload trong Arduino IDE để gửi code đến Arduino Uno R4.
- Để xem kết quả, hãy quan sát Serial Monitor nơi kết quả sẽ được hiển thị như được chỉ ra.
COM6
Arduino UNO R4 - Ethernet Tutorial
Connected to server
HTTP/1.1 200 OK
Accept-Ranges: bytes
Age: 208425
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Fri, 12 Jul 2024 07:08:42 GMT
Etag: "3147526947"
Expires: Fri, 19 Jul 2024 07:08:42 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECAcc (lac/55B8)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256
Connection: close
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
disconnected
Autoscroll
Clear output
9600 baud
Newline
※ Lưu ý:
Nếu thiết bị khác trên cùng mạng chia sẻ địa chỉ MAC của bạn, có thể có vấn đề xảy ra.
Code Arduino UNO R4 cho Module Ethernet - Web Server
Code dưới đây biến Arduino UNO R4 thành một web server. Server này gửi một trang web đơn giản đến các trình duyệt internet.
/*
* 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-ethernet-module
*/
#include <SPI.h>
#include <Ethernet.h>
// replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80);
void setup() {
Serial.begin(9600);
delay(1000);
Serial.println("Arduino Uno R4 - Ethernet Tutorial");
// initialize the Ethernet shield using DHCP:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to obtaining an IP address");
// check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware)
Serial.println("Ethernet shield was not found");
// check for Ethernet cable
if (Ethernet.linkStatus() == LinkOFF)
Serial.println("Ethernet cable is not connected.");
while (true)
;
}
server.begin();
Serial.print("Arduino Uno R4 - Web Server IP Address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an HTTP request ends with a blank line
bool currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the HTTP request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard HTTP response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<body>");
client.println("<h1>Arduino Uno R4 - Web Server with Ethernet</h1>");
client.println("</body>");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
Các bước thực hiện
- Sao chép code trên và dán vào Arduino IDE.
- Nhấp nút Upload trong Arduino IDE để upload code lên Arduino Uno R4.
- Kiểm tra kết quả trên Serial Monitor, nó sẽ hiển thị như sau:
COM6
Arduino UNO R4 - Ethernet Tutorial
Arduino UNO R4 - Web Server IP Address: 192.168.0.2
Autoscroll
Clear output
9600 baud
Newline
- Sao chép địa chỉ IP được đưa ra ở trên và nhập vào thanh địa chỉ của trình duyệt web. Bạn sẽ thấy một trang web đơn giản được hiển thị bởi Arduino UNO R4.
