rhandycan1

BATTERY MONITORING ARDUINO RX(2 WAY)

Dec 14th, 2025
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | Source Code | 0 0
  1. /* PROGRAM BY: RANDY N. CAÑADA,DEC 2025
  2. Arduino Uno wiring
  3. MOSI -> D11
  4. MISO -> D12
  5. SCK -> D13
  6. CSN -> D10
  7. CE -> D9
  8.  
  9. Arduino Uno (Receiver + INA219 Feedback Transmitter)
  10. */
  11.  
  12. #include <SPI.h>
  13. #include <nRF24L01.h>
  14. #include <RF24.h>
  15. #include <Wire.h>
  16. #include <Adafruit_INA219.h>
  17.  
  18. RF24 radio(9, 10); // CE, CSN
  19. const byte address[][6] = {"00001", "00002"}; // ESP32->Arduino, Arduino->ESP32
  20.  
  21. Adafruit_INA219 ina219;
  22. char receivedText[32];
  23. int LED = 7;
  24.  
  25. void setup() {
  26. Serial.begin(9600);
  27. pinMode(LED, OUTPUT);
  28.  
  29. radio.begin();
  30. radio.setPALevel(RF24_PA_MIN);
  31.  
  32. radio.openReadingPipe(1, address[0]); // Listen for ESP32 commands
  33. radio.openWritingPipe(address[1]); // Send feedback to ESP32
  34.  
  35. radio.startListening();
  36.  
  37. if (!ina219.begin()) {
  38. Serial.println("Failed to find INA219 chip");
  39. while (1);
  40. }
  41. ina219.setCalibration_32V_2A();
  42. Serial.println("INA219 ready!");
  43. }
  44.  
  45. void loop() {
  46. if (radio.available()) {
  47. radio.read(&receivedText, sizeof(receivedText));
  48. Serial.print("Received: ");
  49. Serial.println(receivedText);
  50.  
  51. if (strcmp(receivedText, "LED ON") == 0) digitalWrite(LED, HIGH);
  52. else if (strcmp(receivedText, "LED OFF") == 0) digitalWrite(LED, LOW);
  53.  
  54. // --- Measure battery ---
  55. float busVoltage = ina219.getBusVoltage_V();
  56. float current_mA = ina219.getCurrent_mA();
  57.  
  58. char vStr[10], iStr[10], feedback[32];
  59. dtostrf(busVoltage, 4, 2, vStr);
  60. dtostrf(current_mA, 6, 1, iStr);
  61. snprintf(feedback, sizeof(feedback), "V=%sV I=%smA", vStr, iStr);
  62.  
  63. // --- Send feedback ---
  64. radio.stopListening(); // Switch to TX
  65. delay(50); // Give ESP32 time to enter RX mode
  66. bool ok = radio.write(&feedback, sizeof(feedback));
  67. if (ok) {
  68. Serial.print("Sent feedback: ");
  69. Serial.println(feedback);
  70. } else {
  71. Serial.println("Feedback send failed.");
  72. }
  73. radio.startListening(); // Back to RX
  74. }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment