12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- /*
- SFF Minilab PDU Project
- Current Sensing & Display Driver Board
- Author: tobychui
- */
- #define I_sense_A 32
- #define I_sense_B 33
- #define I_sense_C 34
- #define I_sense_D 35
- #define NUM_SAMPLES 10
- const float Rt = 5100.0; // top resistor (ohms)
- const float Rb = 10000.0; // bottom resistor (ohms)
- void setup() {
- Serial.begin(115200);
- //ADC_11db set ADC to ~0–3.3 V range
- analogSetPinAttenuation(I_sense_A, ADC_11db);
- analogSetPinAttenuation(I_sense_B, ADC_11db);
- analogSetPinAttenuation(I_sense_C, ADC_11db);
- analogSetPinAttenuation(I_sense_D, ADC_11db);
- }
- // get_current_sensor_voltage() gets the sensor raw output voltage from the given pin number
- // The sensor output is passed through a voltage divider of 5.1k - 10k that
- // convert the 5V signal output of ACS712 to 3.3v logic level readable by ESP32 3.3v ADC
- void get_current_sensor_voltage(int pin_number) {
- long sum_mV = 0;
- // take multiple samples
- for (int i = 0; i < NUM_SAMPLES; i++) {
- sum_mV += analogReadMilliVolts(pin_number);
- delayMicroseconds(200); // short delay to stabilize readings
- }
- // average the readings
- int avg_mV = sum_mV / NUM_SAMPLES;
- // Convert to float volts
- float vout = avg_mV / 1000.0f;
- // Reconstruct original pre-divider voltage
- float vin = vout * (Rt + Rb) / Rb;
- Serial.printf("Pin %d -> Vout=%.3f V Vin=%.3f V\n", pin_number, vout, vin);
- }
- void loop() {
- get_current_sensor_voltage(I_sense_A);
- get_current_sensor_voltage(I_sense_B);
- get_current_sensor_voltage(I_sense_C);
- get_current_sensor_voltage(I_sense_D);
- Serial.println("---");
- delay(3000);
- }
|