Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://gist.github.com/locy/7022857
- //https://gist.githubusercontent.com/locy/7022857/raw/1a3ff02c1ad74ddd1f4765555a64532e356b704e/ACS712.ino
- int VQ;
- int ACSPin = 0;
- void setup() {
- Serial.begin(115200);
- VQ = determineVQ(ACSPin); //Quiscent output voltage - the average voltage ACS712 shows with no load (0 A)
- delay(1000);
- }
- void loop() {
- Serial.print("ACS712@A2:");Serial.print(readCurrent(ACSPin),3);Serial.println(" mA");
- delay(150);
- }
- int determineVQ(int PIN) {
- Serial.print("estimating avg. quiscent voltage:");
- long VQ = 0;
- //read 5000 samples to stabilise value
- for (int i=0; i<5000; i++) {
- VQ += analogRead(PIN);
- delay(1);//depends on sampling (on filter capacitor), can be 1/80000 (80kHz) max.
- }
- VQ /= 5000;
- Serial.print(map(VQ, 0, 1023, 0, 5000));Serial.println(" mV");
- return int(VQ);
- }
- float readCurrent(int PIN) {
- int current = 0;
- int sensitivity = 185.0;//change this to 100 for ACS712-20A or to 66 for ACS712-30A
- //read 5 samples to stabilise value
- for (int i=0; i<5; i++) {
- current += analogRead(PIN) - VQ;
- delay(1);
- }
- current = map(current/5, 0, 1023, 0, 5000);
- return float(current)/sensitivity;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement