Diseño de PCB Automotriz ESP32
Table
| Function | GPIO | Net |
|---|---|---|
| Speed potentiometer ADC | 34 | PIN_POT |
| LDR ADC | 35 | PIN_LDR |
| Oil level ADC | 33 | PIN_OIL |
| Rain sensor ADC | 32 | PIN_RAIN |
| DHT11 data | 27 | DHTPIN |
| OLED SDA | 21 | I2C_SDA |
| OLED SCL | 22 | I2C_SCL |
| Brake LED 1 | 14 | PIN_LED_FRENO_1 |
| Brake LED 2 | 12 | PIN_LED_FRENO_2 |
| Headlight LED | 13 | PIN_LED_FARO |
| Motor PWM | 25 | PIN_MOTOR_IA |
| Direction placeholder | 26 | PIN_MOTOR_IB |
Ini
[env:esp32dev] platform = espressif32 board = esp32dev framework = arduino monitor_speed = 115200 lib_deps = adafruit/Adafruit SSD1306@^2.5.10 adafruit/Adafruit GFX Library@^1.11.9 adafruit/DHT sensor library@^1.4.6
Cpp
#include <Arduino.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define PIN_POT_ADC 34 #define PIN_LDR_ADC 35 #define PIN_OIL_ADC 33 #define PIN_RAIN_ADC 32 #define DHTPIN_GPIO 27 #define PIN_LED_FRENO_1 14 #define PIN_LED_FRENO_2 12 #define PIN_LED_FARO 13 #define PIN_MOTOR_PWM 25 #define PIN_MOTOR_DIR 26 #define I2C_SDA_GPIO 21 #define I2C_SCL_GPIO 22 #define DHTTYPE DHT11 #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 DHT dht(DHTPIN_GPIO, DHTTYPE); Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); const int PWM_CH_MOTOR = 0; const int PWM_FREQ = 20000; const int PWM_RES_BITS = 8; unsigned long lastUpdate = 0; int readAdc12(int pin) { return analogRead(pin); } void setupOutputs() { pinMode(PIN_LED_FRENO_1, OUTPUT); pinMode(PIN_LED_FRENO_2, OUTPUT); pinMode(PIN_LED_FARO, OUTPUT); pinMode(PIN_MOTOR_DIR, OUTPUT); digitalWrite(PIN_MOTOR_DIR, LOW); ledcSetup(PWM_CH_MOTOR, PWM_FREQ, PWM_RES_BITS); ledcAttachPin(PIN_MOTOR_PWM, PWM_CH_MOTOR); ledcWrite(PWM_CH_MOTOR, 0); } void setupDisplay() { Wire.begin(I2C_SDA_GPIO, I2C_SCL_GPIO); if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println("OLED not found at 0x3C; continuing without display"); return; } display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); display.println("DASHBOARD IRS"); display.display(); } void setup() { Serial.begin(115200); delay(200); analogReadResolution(12); analogSetAttenuation(ADC_11db); setupOutputs(); dht.begin(); setupDisplay(); Serial.println("ESP32 Automotive Dashboard firmware started"); } void loop() { unsigned long now = millis(); if (now - lastUpdate < 500) return; lastUpdate = now; int pot = readAdc12(PIN_POT_ADC); int ldr = readAdc12(PIN_LDR_ADC); int oil = readAdc12(PIN_OIL_ADC); int rain = readAdc12(PIN_RAIN_ADC); float tempC = dht.readTemperature(); float speedPct = (pot / 4095.0f) * 100.0f; bool brake = speedPct > 70.0f; bool dark = ldr < 1800; digitalWrite(PIN_LED_FRENO_1, brake ? HIGH : LOW); digitalWrite(PIN_LED_FRENO_2, brake ? HIGH : LOW); digitalWrite(PIN_LED_FARO, dark ? HIGH : LOW); uint8_t motorDuty = map(pot, 0, 4095, 0, 255); ledcWrite(PWM_CH_MOTOR, motorDuty); Serial.printf("pot=%d speed=%.1f%% ldr=%d oil=%d rain=%d temp=%.1fC brake=%d faro=%d duty=%u\n", pot, speedPct, ldr, oil, rain, tempC, brake, dark, motorDuty); if (display.width() > 0) { display.clearDisplay(); display.setCursor(0, 0); display.println("DASHBOARD IRS"); display.printf("Vel: %.0f%%\n", speedPct); display.printf("Luz: %d\n", ldr); display.printf("Oil: %d Rain: %d\n", oil, rain); if (!isnan(tempC)) display.printf("Temp: %.1f C\n", tempC); display.printf("Brake:%s Faro:%s\n", brake ? "ON" : "OFF", dark ? "ON" : "OFF"); display.display(); } }
Bash
pio run pio run -t upload pio device monitor
Welcome 👋
Flux helps you build PCBs faster with an AI teammate!
Create your account to collaborate, stay updated, fork your own version, and get instant answers from our AI agent.