ESP32 Smart City Controller
esp32devmadhephaestus/ESP32ServoTable
| Function | GPIO | Direction | Schematic connection |
|---|---|---|---|
| Ultrasonic TRIG | 4 | Output | J2 pin 2 |
| Ultrasonic ECHO | 5 | Input | J2 pin 3 through R7/R8 divider |
| Servo PWM | 13 | Output | R9 to J3 Data |
| Microphone ADC | 34 | Input | J4 pin 2 |
| Sensitivity ADC | 35 | Input | J5 Output |
| LED 1 | 12 | Output | R1 to J6 P1; boot-strap pulldown R11 |
| LED 2 | 14 | Output | R2 to J7 P1 |
| LED 3 | 27 | Output | R3 to J8 P1 |
| LED 4 | 26 | Output | R4 to J9 P1 |
| LED 5 | 25 | Output | R5 to J10 P1 |
| LED 6 | 33 | Output | R6 to J11 P1 |
platformio.iniIni
[env:esp32dev] platform = espressif32 board = esp32dev framework = arduino monitor_speed = 115200 lib_deps = madhephaestus/ESP32Servo@^3.0.6 build_flags = -DCORE_DEBUG_LEVEL=1
src/main.cppCpp
#include <Arduino.h> #include <WiFi.h> #include <ESP32Servo.h> constexpr uint8_t PIN_US_TRIG = 4; constexpr uint8_t PIN_US_ECHO = 5; constexpr uint8_t PIN_SERVO = 13; constexpr uint8_t PIN_MIC = 34; constexpr uint8_t PIN_SENSITIVITY = 35; constexpr uint8_t LED_PINS[] = {12, 14, 27, 26, 25, 33}; constexpr size_t LED_COUNT = sizeof(LED_PINS) / sizeof(LED_PINS[0]); const char *WIFI_SSID = "YOUR_SSID"; const char *WIFI_PASSWORD = "YOUR_PASSWORD"; Servo cityServo; unsigned long lastSampleMs = 0; unsigned long lastWiFiRetryMs = 0; float readDistanceCm() { digitalWrite(PIN_US_TRIG, LOW); delayMicroseconds(2); digitalWrite(PIN_US_TRIG, HIGH); delayMicroseconds(10); digitalWrite(PIN_US_TRIG, LOW); const unsigned long pulseUs = pulseIn(PIN_US_ECHO, HIGH, 30000UL); if (pulseUs == 0) return NAN; return static_cast<float>(pulseUs) * 0.0343f * 0.5f; } void setBuildingLevel(uint8_t activeChannels) { if (activeChannels > LED_COUNT) activeChannels = LED_COUNT; for (size_t i = 0; i < LED_COUNT; ++i) { digitalWrite(LED_PINS[i], i < activeChannels ? HIGH : LOW); } } void connectWiFi() { if (strcmp(WIFI_SSID, "YOUR_SSID") == 0) { Serial.println("Wi-Fi credentials not configured; continuing offline."); return; } WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.print("Connecting to Wi-Fi"); for (int i = 0; i < 20 && WiFi.status() != WL_CONNECTED; ++i) { delay(500); Serial.print('.'); } if (WiFi.status() == WL_CONNECTED) { Serial.printf("\nWi-Fi connected: %s\n", WiFi.localIP().toString().c_str()); } else { Serial.println("\nWi-Fi unavailable; continuing offline."); } } void setup() { Serial.begin(115200); delay(200); pinMode(PIN_US_TRIG, OUTPUT); digitalWrite(PIN_US_TRIG, LOW); pinMode(PIN_US_ECHO, INPUT); for (uint8_t pin : LED_PINS) { pinMode(pin, OUTPUT); digitalWrite(pin, LOW); // GPIO12 must remain low during startup } analogReadResolution(12); analogSetPinAttenuation(PIN_MIC, ADC_11db); analogSetPinAttenuation(PIN_SENSITIVITY, ADC_11db); ESP32PWM::allocateTimer(0); cityServo.setPeriodHertz(50); cityServo.attach(PIN_SERVO, 500, 2400); cityServo.write(90); connectWiFi(); Serial.println("Smart City Controller initialized."); } void loop() { const unsigned long now = millis(); if (now - lastSampleMs >= 250) { lastSampleMs = now; const float distanceCm = readDistanceCm(); const int micRaw = analogRead(PIN_MIC); const int sensitivityRaw = analogRead(PIN_SENSITIVITY); const uint8_t activeBuildings = map(sensitivityRaw, 0, 4095, 0, LED_COUNT + 1); setBuildingLevel(activeBuildings); int servoAngle = 90; if (!isnan(distanceCm)) { servoAngle = constrain(map(static_cast<long>(distanceCm), 5, 100, 0, 180), 0, 180); cityServo.write(servoAngle); } Serial.printf("distance_cm=%s mic=%d sensitivity=%d leds=%u servo=%d wifi=%s\n", isnan(distanceCm) ? "timeout" : String(distanceCm, 1).c_str(), micRaw, sensitivityRaw, activeBuildings, servoAngle, WiFi.status() == WL_CONNECTED ? "connected" : "offline"); } if (WiFi.status() != WL_CONNECTED && strcmp(WIFI_SSID, "YOUR_SSID") != 0 && now - lastWiFiRetryMs >= 30000) { lastWiFiRetryMs = now; WiFi.disconnect(); connectWiFi(); } delay(1); }
Bash
pio run pio run -t upload pio device monitor
Platform & Toolchain
Pin Mapping
`platformio.ini`
`src/main.cpp`
Build & Flash