A simple test code for the Saleng-ACS712 module. Connect the terminal blocks of the Saleng ACS712 in series with the circuit you want to test. Simply upload and open the serial monitor with a baud rate of 9600. Connects are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/*************************************************************************** Saleng ACS712 Description: A simple test code for the Saleng-ACS712 module. Simply upload and open the serial monitor with a baud rate of 9600. Connect as follows: Saleng Uno Saleng-ACS712 5V + / V GND - / G A0 OUT / S This software is free provided that this notice is not removed and proper attribution is accorded to Layad Circuits and its Author(s). Layad Circuits invests resources in producing free software. By purchasing Layad Circuits' products or utilizing its services, you support the continuing development of free software for all. Author(s): CDMalecdan for Layad Circuits Electronics Engineering Revision: 1.0 - 2018/03/08 - initial creation Layad Circuits Electronics Engineering Supplies and Services B314 Lopez Bldg., Session Rd. cor. Assumption Rd., Baguio City, Philippines www.facebook.com/layadcircuits www.layadcircuits.com general: info@layadcircuits.com sales: sales@layadcircuits.com mobile: +63-916-442-8565 ***************************************************************************/ const byte SENSORPIN = A0; const unsigned int MIN_RAW_VALUE = 322; const unsigned int MAX_RAW_VALUE = 700; unsigned int rawValue; //reading from ADC int currentMA; //computed, in mA char str[17]="";//temporary buffer void setup() { Serial.begin(9600); } void loop() { //read ADC and take average rawValue=0; for(byte i=0;i<5;i++) { rawValue += analogRead(SENSORPIN); } rawValue /= 5; // limit values if(rawValue<=MIN_RAW_VALUE) rawValue = MIN_RAW_VALUE; if(rawValue>=700) rawValue = MAX_RAW_VALUE; // compute equivalent current currentMA = map(rawValue,MIN_RAW_VALUE,MAX_RAW_VALUE,-5000,5000); // display results in serial monitor Serial.print("Raw="); Serial.print(rawValue); Serial.print(" Calculated="); Serial.print(currentMA); Serial.println(" mA"); delay(1000); } |
The complete code may be downloaded from our github page.
See the Saleng-ACS712 user guide here.