Connect to a WiFi network with Arduino on ESP32-C3

Updated on 27 October 2023
dev board ESP32-C3-DevKitM-1
chip ESP32-C3-MINI-1-N4
features wifi connect 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 connect-wifi-arduino-esp32c3.ino
#include "WiFi.h"
#include "Secret.h"

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");
  printWifiStatus();
}

void loop() {}

void printWifiStatus() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

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.

Connect to a WiFi network with Arduino on ESP32-C3 prototype

Arduino IDE settings

Ensure the following IDE settings before flashing.

Connect to a WiFi network with Arduino on ESP32-C3 Arduino IDE settings

Serial console

Serial output from the firmware.

Connect to a WiFi network with Arduino on ESP32-C3 serial console

Description

Create a file Secret.h in the same folder with the code.

$ tree
.
├── Secret.h
└── connect-wifi-arduino-esp32c3.ino

Include the actual Wifi SSID name and password in the file.

char ssid[] = "secret";
char pass[] = "secret";

Compile and upload the firmware to connect to the WiFi.

References