Created
October 17, 2013 10:49
-
-
Save locy/7022857 to your computer and use it in GitHub Desktop.
GY-712 電流傳感器, ACS712TELC-20A, ACS712TELC-30A, Arduino and ACS712 sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, can this sample code be used in DC current load? or in AC current load? thanks