minilab-pdu.ino 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. SFF Minilab PDU Project
  3. Current Sensing & Display Driver Board
  4. Author: tobychui
  5. */
  6. #define I_sense_A 32
  7. #define I_sense_B 33
  8. #define I_sense_C 34
  9. #define I_sense_D 35
  10. #define NUM_SAMPLES 10
  11. const float Rt = 5100.0; // top resistor (ohms)
  12. const float Rb = 10000.0; // bottom resistor (ohms)
  13. void setup() {
  14. Serial.begin(115200);
  15. //ADC_11db set ADC to ~0–3.3 V range
  16. analogSetPinAttenuation(I_sense_A, ADC_11db);
  17. analogSetPinAttenuation(I_sense_B, ADC_11db);
  18. analogSetPinAttenuation(I_sense_C, ADC_11db);
  19. analogSetPinAttenuation(I_sense_D, ADC_11db);
  20. }
  21. // get_current_sensor_voltage() gets the sensor raw output voltage from the given pin number
  22. // The sensor output is passed through a voltage divider of 5.1k - 10k that
  23. // convert the 5V signal output of ACS712 to 3.3v logic level readable by ESP32 3.3v ADC
  24. void get_current_sensor_voltage(int pin_number) {
  25. long sum_mV = 0;
  26. // take multiple samples
  27. for (int i = 0; i < NUM_SAMPLES; i++) {
  28. sum_mV += analogReadMilliVolts(pin_number);
  29. delayMicroseconds(200); // short delay to stabilize readings
  30. }
  31. // average the readings
  32. int avg_mV = sum_mV / NUM_SAMPLES;
  33. // Convert to float volts
  34. float vout = avg_mV / 1000.0f;
  35. // Reconstruct original pre-divider voltage
  36. float vin = vout * (Rt + Rb) / Rb;
  37. Serial.printf("Pin %d -> Vout=%.3f V Vin=%.3f V\n", pin_number, vout, vin);
  38. }
  39. void loop() {
  40. get_current_sensor_voltage(I_sense_A);
  41. get_current_sensor_voltage(I_sense_B);
  42. get_current_sensor_voltage(I_sense_C);
  43. get_current_sensor_voltage(I_sense_D);
  44. Serial.println("---");
  45. delay(3000);
  46. }