rhandycan1

ESP32 TX Voltage Monitoring RC Car Preparation

Jan 4th, 2026
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.11 KB | Source Code | 0 0
  1. /* Program by: RANDY N. CAÑADA, DEC. 2025
  2. ESP32 wiring (VSPI example)
  3. MOSI -> GPIO23
  4. MISO -> GPIO19
  5. SCK  -> GPIO18
  6. CSN  -> GPIO5
  7. CE   -> GPIO17
  8.  
  9. ESP32 (Transmitter + Feedback Receiver)
  10.  
  11. OLED 1306 CONNECTIONS:
  12. -can be operated in 3.3vdc / 5 vdc
  13. sda=21,scl=22
  14. */
  15.  
  16. #include <SPI.h>
  17. #include <nRF24L01.h>
  18. #include <RF24.h>
  19. #include <Wire.h>
  20. #include <Adafruit_GFX.h>
  21. #include <Adafruit_SSD1306.h>
  22.  
  23. // --- RF24 setup ---
  24. RF24 radio(17, 5); // CE, CSN
  25. const byte address[][6] = {"00001", "00002"}; // ESP32->Arduino, Arduino->ESP32
  26.  
  27. int buttonPin = 13;
  28.  
  29. // --- OLED setup ---
  30. #define SCREEN_WIDTH 128
  31. #define SCREEN_HEIGHT 64
  32. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  33.  
  34. // --- Battery thresholds ---
  35. const float VBAT_MIN = 7.0;   // cutoff for 2S Li-ion
  36. const float VBAT_MAX = 8.4;   // full charge for 2S Li-ion
  37.  
  38. void setup() {
  39.   Serial.begin(9600);
  40.   pinMode(buttonPin, INPUT_PULLUP);
  41.  
  42.   // --- Radio init ---
  43.   radio.begin();
  44.   radio.setPALevel(RF24_PA_MIN);
  45.   radio.openWritingPipe(address[0]);   // Send commands to Arduino
  46.   radio.openReadingPipe(1, address[1]); // Listen for feedback
  47.  
  48.   // --- OLED init ---
  49.   if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  50.     Serial.println("SSD1306 allocation failed");
  51.     while (1);
  52.   }
  53.   display.clearDisplay();
  54.   display.setTextSize(3);
  55.   display.setTextColor(SSD1306_WHITE);
  56.   display.setCursor(20,0);
  57.   display.println("Remote");
  58.   display.setCursor(20,35);
  59.   display.println("Ready!");
  60.   display.display();
  61.   delay(1500);
  62. }
  63.  
  64. void loop() {
  65.   // --- Send command ---
  66.   radio.stopListening();
  67.   if (digitalRead(buttonPin) == LOW) {
  68.     const char text[] = "LED ON";
  69.     radio.write(&text, sizeof(text));
  70.     Serial.println("Sent: LED ON");
  71.   } else {
  72.     const char text[] = "LED OFF";
  73.     radio.write(&text, sizeof(text));
  74.     Serial.println("Sent: LED OFF");
  75.   }
  76.  
  77.   // --- Switch to RX mode and wait for feedback ---
  78.   radio.startListening();
  79.   unsigned long start_time = millis();
  80.   while (!radio.available()) {
  81.     if (millis() - start_time > 500) {
  82.       Serial.println("No feedback received.");
  83.       display.clearDisplay();
  84.       display.setTextSize(2);
  85.       display.setCursor(0,0);
  86.       display.println("OFFLINE!");
  87.       display.display();
  88.       return;
  89.     }
  90.   }
  91.  
  92.   char feedback[32] = "";
  93.   radio.read(&feedback, sizeof(feedback));
  94.   Serial.print("Battery feedback: ");
  95.   Serial.println(feedback);
  96.  
  97.   // --- Parse feedback (format: "V=1.45V I=1.4mA") ---
  98.   float busVoltage = 0.0, current_mA = 0.0;
  99.   sscanf(feedback, "V=%fV I=%fmA", &busVoltage, &current_mA);
  100.  
  101.   // Calculate power and battery percentage
  102.   float power_W = busVoltage * (current_mA / 1000.0);
  103.   int pct = batteryPercent(busVoltage);
  104.  
  105.   // --- Serial Monitor output ---
  106.   Serial.print("V: "); Serial.print(busVoltage, 2); Serial.println("V");
  107.   Serial.print("I: "); Serial.print(current_mA, 1); Serial.println(" mA");
  108.   Serial.print("P: "); Serial.print(power_W, 1); Serial.println(" W");
  109.   Serial.print("Batt: "); Serial.print(pct); Serial.println("%");
  110.   Serial.println("-----------------------------");
  111.  
  112.   // --- OLED output ---
  113.   display.clearDisplay();
  114.   display.setCursor(0,0);
  115.   display.setTextSize(2);
  116.   display.print("V: "); display.print(busVoltage, 2); display.println("V");
  117.   display.print("I: "); display.print(current_mA, 1); display.println(" mA");
  118.   display.print("P: "); display.print(power_W, 1); display.println(" W");
  119.  
  120.   // --- Graphical Battery Bar ---
  121.   int barX = 0;
  122.   int barY = 48;
  123.   int barWidth = 100;
  124.   int barHeight = 12;
  125.  
  126.   // Outline
  127.   display.drawRect(barX, barY, barWidth, barHeight, SSD1306_WHITE);
  128.  
  129.   // Fill based on pct
  130.   int fillWidth = map(pct, 0, 100, 0, barWidth - 2);
  131.   display.fillRect(barX + 1, barY + 1, fillWidth, barHeight - 2, SSD1306_WHITE);
  132.  
  133.   // Percentage text
  134.   display.setCursor(104, 48);
  135.   display.setTextSize(1);
  136.   display.print(pct);
  137.   display.print("%");
  138.  
  139.   // --- Battery warnings ---
  140.   if (pct >= 15 && pct <= 20) {
  141.     display.clearDisplay();
  142.     display.setTextSize(2);
  143.     display.setCursor(28,0);
  144.     display.print("Charge");
  145.     display.setCursor(42,20);
  146.     display.print("Your");
  147.     display.setCursor(28,40);
  148.     display.print("Battery");
  149.     Serial.println("⚠️ Battery Low!");
  150.   }
  151.   else if (pct <= 5) {
  152.     display.clearDisplay();
  153.     display.setTextSize(2);
  154.     display.setCursor(52,1);
  155.     display.print("LOW");
  156.     display.setCursor(28,32);
  157.     display.print("BATTERY!");
  158.     Serial.println("❌ Battery Depleted!");
  159.   }
  160.  
  161.   display.display();
  162.   delay(1000);
  163. }
  164.  
  165. // ------------------------------------------------------
  166. // REALISTIC NON-LINEAR BATTERY PERCENTAGE FUNCTION
  167. // For 2S Li-ion (7.0V empty, 8.4V full)
  168. // ------------------------------------------------------
  169. int batteryPercent(float v) {
  170.   if (v >= 8.40) return 100;
  171.   if (v >= 8.30) return 90;
  172.   if (v >= 8.20) return 80;
  173.   if (v >= 8.10) return 70;
  174.   if (v >= 8.00) return 60;
  175.   if (v >= 7.90) return 50;
  176.   if (v >= 7.80) return 40;
  177.   if (v >= 7.60) return 30;
  178.   if (v >= 7.40) return 20;
  179.   if (v >= 7.20) return 10;
  180.   if (v >= 7.00) return 5;
  181.   return 0;
  182. }
  183.  
Tags: ina219 l298n
Advertisement
Add Comment
Please, Sign In to add comment