/* ACS714 sur Arduino Uno */ #define RefVal 5.0 #define Pin A0 // Take the average of 500 times const int averageValue = 500; long int sensorValue = 0; float sensitivity = 1000.0 / 185.0; //1000mA per 185mV // Output voltage with no current: 2500mV float Vref = 2500; void setup() { Serial.begin(9600); } void loop() { // Read the value 500 times: for (int i = 0; i < averageValue; i++) { sensorValue += analogRead(Pin); // wait 2 milliseconds before the next loop delay(2); } sensorValue = sensorValue / averageValue; // The on-board ADC is 10-bits // Different power supply will lead to different reference sources // example: 2^10 = 1024 -> 5V / 1024 ~= 4.88mV // unitValue= 5.0 / 1024.0*1000 ; float unitValue = RefVal / 1024.0 * 1000; float voltage = unitValue * sensorValue; // When no load,Vref=initialValue Serial.print("initialValue: "); Serial.print(voltage); Serial.println("mV"); // Calculate the corresponding current float current = (voltage - Vref) * sensitivity; // Print display current (mA) Serial.print(current); Serial.println("mA"); Serial.print("\n"); // Reset the sensorValue for the next reading sensorValue = 0; // Read it once per second delay(1000); }