|
| 1 | +/* |
| 2 | + Library for the Allegro MicroSystems ACS37800 power monitor IC |
| 3 | + By: Paul Clark |
| 4 | + SparkFun Electronics |
| 5 | + Date: December 4th, 2021 |
| 6 | + License: please see LICENSE.md for details |
| 7 | +
|
| 8 | + Feel like supporting our work? Buy a board from SparkFun! |
| 9 | + https://www.sparkfun.com/products/17873 |
| 10 | +*/ |
| 11 | + |
| 12 | +#include "SparkFun_ACS37800_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_ACS37800 |
| 13 | +#include <Wire.h> |
| 14 | + |
| 15 | +ACS37800 mySensor; //Create an object of the ACS37800 class |
| 16 | + |
| 17 | +void setup() |
| 18 | +{ |
| 19 | + Serial.begin(115200); |
| 20 | + Serial.println(F("ACS37800 Example")); |
| 21 | + |
| 22 | + Wire.begin(); |
| 23 | + |
| 24 | + //mySensor.enableDebugging(); // Uncomment this line to print useful debug messages to Serial |
| 25 | + |
| 26 | + //Initialize sensor using default I2C address |
| 27 | + if (mySensor.begin() == false) |
| 28 | + { |
| 29 | + Serial.print(F("ACS37800 not detected. Check connections and I2C address. Freezing...")); |
| 30 | + while (1) |
| 31 | + ; // Do nothing more |
| 32 | + } |
| 33 | + |
| 34 | + // From the ACS37800 datasheet: |
| 35 | + // CONFIGURING THE DEVICE FOR AC APPLICATIONS : DYNAMIC CALCULATION OF N |
| 36 | + // Set bypass_n_en = 0 (default). This setting enables the device to |
| 37 | + // dynamically calculate N based off the voltage zero crossings. |
| 38 | + mySensor.setBypassNenable(false, false); // Disable bypass_n in shadow memory and eeprom |
| 39 | + |
| 40 | + // We need to connect the LO pin to the 'low' side of the AC source. |
| 41 | + // So we need to set the divider resistance to 4M Ohms (instead of 2M). |
| 42 | + mySensor.setDividerRes(4000000); |
| 43 | +} |
| 44 | + |
| 45 | +void loop() |
| 46 | +{ |
| 47 | + float volts = 0.0; |
| 48 | + float amps = 0.0; |
| 49 | + |
| 50 | + mySensor.readRMS(&volts, &s); // Read the RMS voltage and current |
| 51 | + Serial.print(F("Volts: ")); |
| 52 | + Serial.print(volts, 2); |
| 53 | + Serial.print(F(" Amps: ")); |
| 54 | + Serial.println(amps, 2); |
| 55 | + |
| 56 | + delay(250); |
| 57 | +} |
0 commit comments