Display NTP time with Arduino on ESP32-C3

Updated on 27 October 2023
dev board ESP32-C3-DevKitM-1
chip ESP32-C3-MINI-1-N4
features wifi ntp time ssid esp32c3
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Pre-requisites

Buy the components

Code

Download code ntp-time-arduino-esp32c3.ino
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

#include "Secret.h"
// char ssid[] = "secret";
// char pass[] = "secret";

// User defined parameters
const long timeZoneOffset = 28800;  // GMT+8 (8 hours * 60 minutes * 60 seconds)
#define START_TIME 9  // 9am or 0900h
#define END_TIME 21   // 9pm or 2100h

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
int currentHour;

void setup() {
  // (Optional) Press reset button
  // on the dev board to see these print statements

  Serial.begin(115200);
  while (!Serial) { }

  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);

  WiFi.useStaticBuffers(true);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("Connected to WiFi");

  timeClient.begin();
  timeClient.setTimeOffset(timeZoneOffset);
  currentHour = (timeClient.getHours() + (timeZoneOffset / 3600)) % 24;
}

void loop() {
  timeClient.update();
  Serial.println(timeClient.getFormattedTime());

  if (currentHour >= END_TIME || currentHour < START_TIME) {
    Serial.println("It's currently between 9 PM and 9 AM.");
  } else {
    Serial.println("It's currently outside the specified time range.");
  }

  delay(1000);
}

Makefile

BOARD?=esp32:esp32:esp32c3
PORT?=/dev/cu.usbserial-*
BUILD=build
## Pass in V=--verbose to output more information than default

.PHONY: help default lint compile upload clean

default: clean lint compile upload

help: ## Show help message
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m\033[0m\n"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

lint: ## Lint code using cpplint
	cpplint --extensions=ino --filter=-legal/copyright,-runtime/int *.ino

compile: ## Compile code and create the firmware binary
	arduino-cli compile $(V) --fqbn $(BOARD) --output-dir $(BUILD) ./

upload: ## Upload the firmware to the board
	arduino-cli upload $(V) --fqbn $(BOARD) --port $(PORT) --input-dir $(BUILD)

clean: ## Remove all built files
	rm -rf build

Prototype

A photo of the actual setup.

Display NTP time  with Arduino on ESP32-C3 prototype

Arduino IDE settings

Ensure the following IDE settings before flashing.

Display NTP time  with Arduino on ESP32-C3 Arduino IDE settings

Serial console

Serial output from the firmware.

Display NTP time  with Arduino on ESP32-C3 serial console

Description

Time example will print NTP time with timezone offset timeZoneOffset. It will also check if the time is within specified hours.

References