Arduino W5500 Ethernet NTP Time: Wiring, Code and Simulator
To build this Arduino W5500 Ethernet NTP Time project you need an Arduino Uno and a W5500 ethernet module. A UDP packet to an NTP server and the answer’s transmit timestamp: the real time, every ten seconds. Make the 6 connections in the wiring table, upload the Arduino C++ code, and compare the UTC time with your clock. The same packet works on any board with UDP.
Open this circuit in the free Arduino Uno simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | W5500 Ethernet module | Wired Ethernet on SPI: VCC 3.3 V, GND, CS to pin 10, SCK/MOSI/MISO to the board’s SPI pins. Ethernet.begin(mac) gets an address from the lab router by DHCP; EthernetClient fetches web pages, and an EthernetServer’s page opens from the Modules tab. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno 3.3V | W5500 Ethernet module VCC |
| 2 | Arduino Uno GND | W5500 Ethernet module GND |
| 3 | Arduino Uno pin 10 | W5500 Ethernet module CS |
| 4 | Arduino Uno pin 13 | W5500 Ethernet module SCK |
| 5 | Arduino Uno pin 11 | W5500 Ethernet module MOSI |
| 6 | Arduino Uno pin 12 | W5500 Ethernet module MISO |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add a W5500 ethernet module from the parts palette.
- Wire 1. Connect Arduino Uno 3.3V to W5500 Ethernet module VCC.
- Wire 2. Connect Arduino Uno GND to W5500 Ethernet module GND.
- Wire 3. Connect Arduino Uno pin 10 to W5500 Ethernet module CS.
- Wire 4. Connect Arduino Uno pin 13 to W5500 Ethernet module SCK.
- Wire 5. Connect Arduino Uno pin 11 to W5500 Ethernet module MOSI.
- Wire 6. Connect Arduino Uno pin 12 to W5500 Ethernet module MISO.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Compare the UTC time with your clock. The same packet works on any board with UDP.
Arduino C++ code
// W5500: the time from an NTP server over UDP (the Ethernet library's UdpNtpClient, shortened).
// CS -> 10, SCK -> 13, MOSI -> 11, MISO -> 12, VCC 3.3V, GND.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x02 };
EthernetUDP udp;
byte packet[48];
void ask() {
memset(packet, 0, 48);
packet[0] = 0b11100011; // LI, version, mode = client
packet[2] = 6;
packet[3] = 0xEC;
udp.beginPacket("pool.ntp.org", 123);
udp.write(packet, 48);
udp.endPacket();
}
void setup() {
Serial.begin(115200);
Ethernet.init(10);
if (Ethernet.begin(mac) == 0) { Serial.println(F("DHCP failed")); while (true) {} }
Serial.print(F("Address "));
Serial.println(Ethernet.localIP());
udp.begin(8888);
}
void loop() {
ask();
delay(1000);
if (udp.parsePacket()) {
udp.read(packet, 48);
unsigned long secs1900 = ((unsigned long)packet[40] << 24) | ((unsigned long)packet[41] << 16) | ((unsigned long)packet[42] << 8) | packet[43];
unsigned long epoch = secs1900 - 2208988800UL;
Serial.print(F("Unix time "));
Serial.print(epoch);
Serial.print(F(" UTC "));
Serial.print((epoch % 86400L) / 3600);
Serial.print(':');
if ((epoch % 3600) / 60 < 10) Serial.print('0');
Serial.print((epoch % 3600) / 60);
Serial.print(':');
if (epoch % 60 < 10) Serial.print('0');
Serial.println(epoch % 60);
}
delay(9000);
}
What to try
Compare the UTC time with your clock. The same packet works on any board with UDP.
Frequently asked questions
How do I wire the W5500 Ethernet NTP Time project on an Arduino Uno?
Make 6 connections: Arduino Uno 3.3V to W5500 Ethernet module VCC; Arduino Uno GND to W5500 Ethernet module GND; Arduino Uno pin 10 to W5500 Ethernet module CS; Arduino Uno pin 13 to W5500 Ethernet module SCK; Arduino Uno pin 11 to W5500 Ethernet module MOSI; Arduino Uno pin 12 to W5500 Ethernet module MISO.
What parts do I need for this Arduino project?
An Arduino Uno and a W5500 ethernet module.
Can I simulate this Arduino project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.
What does the code do?
A UDP packet to an NTP server and the answer’s transmit timestamp: the real time, every ten seconds.
How does an Arduino get the time with NTP?
It sends a 48-byte UDP packet to an NTP server on port 123; the answer’s transmit timestamp (bytes 40–43) is the seconds since 1900. Subtract 2208988800 to get Unix time.
More Arduino Uno projects
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator