hi... I have developed a dimmer with arduino UNO and i required some guidance to interface with the ACS 712 current sensor to measure the alternating current and display it on LCD.
#include<LiquidCrystal.h>
#define pulse 5
#define SW 4
#define led 13
LiquidCrystal lcd(12,11,10,9,8,7);
const int sensorIn = A1;
int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
void setup(){
lcd.begin(16,2);
pinMode(2,INPUT);
digitalWrite(2,HIGH);
pinMode(pulse,OUTPUT); //pin 5
pinMode(SW, INPUT); //pin 4
digitalWrite(SW, HIGH);
pinMode(led,OUTPUT);
digitalWrite(led, LOW);
}
void loop(){
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707;
AmpsRMS = (VRMS * 1000)/mVperAmp;
lcd.setCursor(0,0);lcd.print("current"); lcd.setCursor(0,1);
lcd.print(AmpsRMS);
delay(1000); lcd.clear();
if(!digitalRead(SW))
{
attachInterrupt(0,startRoutine, FALLING);
digitalWrite(led, HIGH);
}
else if(digitalRead(SW))
{
detachInterrupt(0);
digitalWrite(led, LOW);
}
}
void startRoutine()
{
delayMicroseconds(analogRead(0)*8+1000);
digitalWrite(pulse, HIGH);
delayMicroseconds(200);
digitalWrite(pulse, LOW);
}
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1023; // store min value here
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the maximum sensor value*/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue - minValue) * 5.0)/1023.0;
return result;
}
The above is the code. Is there any problem with the code??
Large values passed to delayMicroseconds don't work (I think 8000 is the practical limit)
Delaying in an ISR with delay() will deadlock the processor completely, delayMicroseconds()
doesn't do that at least, but will break other stuff that relies on interrupts (Serial, millis()).
You shouldn't be detaching and reattaching interrupts like that, its a recipe for missed and spurious
interrupts due to race conditions.
Just set a volatile boolean flag to tell the ISR if its enabled or not:
void startRoutine()
{
if (! enabled)
return ;
.....
}
For other types of ISR you don't have an option for attaching/detaching anyway, so this is a more
general approach.
The dimmer coding is working and the only problem that occurred is that when interfacing with ACS-712 current sensor to measure AC current at the load (in this case the load is a 60W, 230V, 50Hz light bulb) it does not produce the required value
Any suggestion to interface with the current sensor with the dimmer?? Is there any configuration for the ACS-712 current sensor??
Currently, the largest value that will produce an accurate delay is 16383. This could change in future Arduino releases. For delays longer than a few thousand microseconds, you should use delay() instead.
thanks for all the reply...
I trying really hard with the current sensor coding but... i found out that the analog reading will only read only a specific value for a specific load at any level of voltage applied... With the load that used 60W, 230V, 50Hz bulb will only produce a value of 507, and 517 which converted to AC current will produce -0.22A and 0.27A. Is there any help of coding or reference to continuously read the gradually increased current reading?? Will "millis()" be affected or being affect the ISR sequences??
A 20A hall sensor isn't appropriate for measuring 0.2A... Hall sensors are by their nature noisy
and best for high currents (when the magnetic field they measure is much larger than the
earth's magnetic field and stray fields from nearby steel/iron objects).