#define ACS712_ZERO_VOL 2.495 //2.495V. Set the macro value of the voltage output
//of the ACS712 when the measured current is zero.
#define ACS712_SENSITIVITY 0.185 //0.185mV is typical value
#define ADC_RESOLUTION (float)5/1024 // 5/1024 is eaque 0.0049V per unit
#define ACS712_VOUT A2
int samplesnum = 1000;
int caiji=A0;
float current;
void setup() {
pinMode(3,OUTPUT);
Serial.begin(9600);// put your setup code here, to run once:
}
void loop() {
if(analogRead(caiji)<1024){
digitalWrite(3,HIGH);
}else
float current_sum = 0;
for(int i = 0; i < samplesnum; i++)
{
current_sum += ((float)analogRead(ACS712_VOUT)*ADC_RESOLUTION - ACS712_ZERO_VOL)/ACS712_SENSITIVITY;
delay(1);
}
current = current_sum/samplesnum;
Serial.println("The measured current is ");
Serial.print(current,3);
Serial.println(" A");
delay(1000);digitalWrite(3,LOW);
delay(1000);
Serial.println(analogRead(caiji));// put your main code here, to run repeatedly:
}
想用这段程序检测电压电流等其达到临界值时断开电流。。但有些地方有错。。求指教。。
//of the ACS712 when the measured current is zero.
#define ACS712_SENSITIVITY 0.185 //0.185mV is typical value
#define ADC_RESOLUTION (float)5/1024 // 5/1024 is eaque 0.0049V per unit
#define ACS712_VOUT A2
int samplesnum = 1000;
int caiji=A0;
float current;
void setup() {
pinMode(3,OUTPUT);
Serial.begin(9600);// put your setup code here, to run once:
}
void loop() {
if(analogRead(caiji)<1024){
digitalWrite(3,HIGH);
}else
float current_sum = 0;
for(int i = 0; i < samplesnum; i++)
{
current_sum += ((float)analogRead(ACS712_VOUT)*ADC_RESOLUTION - ACS712_ZERO_VOL)/ACS712_SENSITIVITY;
delay(1);
}
current = current_sum/samplesnum;
Serial.println("The measured current is ");
Serial.print(current,3);
Serial.println(" A");
delay(1000);digitalWrite(3,LOW);
delay(1000);
Serial.println(analogRead(caiji));// put your main code here, to run repeatedly:
}
想用这段程序检测电压电流等其达到临界值时断开电流。。但有些地方有错。。求指教。。