Skip to content

Instantly share code, notes, and snippets.

@arduinothai
Created May 17, 2018 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arduinothai/de76d8234cdafba70a170dca2c620791 to your computer and use it in GitHub Desktop.
Save arduinothai/de76d8234cdafba70a170dca2c620791 to your computer and use it in GitHub Desktop.
#define CURRENT_SENSOR A0  // Define Analog input pin that sensor is attached
float amplitude_current;      // Float amplitude current
float effective_value;       // Float effective current
void setup(){
    Serial.begin(9600);
    pins_init();
}
void loop(){
    int sensor_max;
    sensor_max = getMaxValue();
    Serial.print("sensor_max = ");
    Serial.println(sensor_max);
    //the VCC on the Arduino interface of the sensor is 5v
    amplitude_current=(float)(sensor_max-512)/1024*5/185*1000000; // for 5A mode,you need to modify this with 20 A and 30A mode;
    effective_value=amplitude_current/1.414;
    //for minimum current=1/1024*5/185*1000000/1.414=18.7(mA)
    //Only sinusoidal alternating current
    Serial.println("The amplitude of the current is(in mA)");
    Serial.println(amplitude_current,1);
    //Only one number after the decimal poin
    Serial.println("The effective value of the current is(in mA)");
    Serial.println(effective_value,1);
}
void pins_init(){
  pinMode(CURRENT_SENSOR, INPUT);
}
/*Function: Sample for 1000ms and get the maximum value from the S pin*/
int getMaxValue()
{
    int sensorValue;    //value read from the sensor
    int sensorMax = 0;
    uint32_t start_time = millis();
    while((millis()-start_time) < 1000) //sample for 1000ms
    {
        sensorValue = analogRead(CURRENT_SENSOR);
        if (sensorValue > sensorMax)
        {
            /*record the maximum sensor value*/
           sensorMax = sensorValue;
        }
    }
    return sensorMax;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment