Help! Analogue sensor (ACS712) misread with relay output

Hi, I am having issues whereby a turning on a digital output (to a relay) is affecting the input analogue signal on the A0 pin.

It is for an automated power board that reads the current off one mains line (a power tool) and activates a relay (a vacuum) for dust collection. The project and code is somewhat based off this project
(https://www.instructables.com/Automatic-Load-Vacuum-Switch-With-ACS712-and-Ardui/)

The acs module is working and detects the voltage a current off the tool line appropriately, however as soon as the relay is activated the analogue signal from the ACS goes haywire. Subsequently the relay never turns off as it remains above the threshold to turn it on

Disconnecting the relay returns the analogue signal to normal working order

The analogue signal coming out of the A0 pin is as follows: (0-1024)
Relay off, no load: 806min - 811 max (0.71 watts)
Relay off with load from tool: 788-832 (160 watts)
Relay on - 480-830 (1425 watts)

Other points

Any ideas what is causing this ?!?!?!
(Sorry Im not great with schematics yet, but hopefully its a simple enough circuit for the pic to suffice)

Code:

#define inputPin A0       //ACS712 analog input pin
#define relayPin 5        //Control relay output pin GPIO 5 = D1 on esp8266

//const int sensorIn = A0;
int mVperAmp = 100; // use 185 for 5A, 100 for 20A Module and 66 for 30A Module

double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

unsigned long previousMillis = 0;
unsigned long currentMillis = 0;

//Turn off delay after tool stops 1000 = 1 second
const long interval = 5000;

void setup(){ 
  pinMode(A0, INPUT);
  pinMode(relayPin, OUTPUT);
  //digitalWrite(relayPin, LOW);
  Serial.begin(115200);
  delay(10);
  Serial.println(F("Init...."));
}

void loop(){
  Voltage = getVPP();
  VRMS = (Voltage/2.0) *0.707; // sq root
  AmpsRMS = (VRMS * 1000)/mVperAmp;
  float Wattage = (240*AmpsRMS)-20; //Observed 18-20 Watt when no load was connected, so substracting offset value to get real consumption.
  Serial.print(AmpsRMS);
  Serial.print(" AmpsRMS      ");
  Serial.print(Wattage); 
  Serial.println(" Watts ");

  if (Wattage > 50) {
  digitalWrite(relayPin, HIGH);
  Serial.println("Relay On");
  previousMillis = millis();
  } else {
  currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    digitalWrite(relayPin, LOW); }
  }
}

float getVPP()
{
  float result;
  
  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 1024;          // store min value here
  
   uint32_t start_time = millis();

   while((millis()-start_time) < 1000) //sample for 1 Sec
   {
       readValue = analogRead(inputPin);
       // 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)/1024.0;
      
   return result;
 }

what is the load the relay is switching ( resistive, inductive)? what current? AC or DC?
relays switching can cause noise on the power supply lines in particular if the load is inductive
take care with earthing - I tend to use screened cables between the relay and load

Well I haven't actually connected any load to the relay at this point (as in the picture), as I'm still prototyping

The plan would be to attach a vacuum, so would be inductive ac, but again I havent gotten to that point yet. Ill keep that in mind though

from the photo is it difficult to follow the circuit
could you upload a schematic in particular showing power supplies and grounding
if the load is AC consider using a non-invasive SCT-013 current clamp

1 Like

I believe you will be much happier if you use a solid state relay with zero cross for your AC, that will eliminate most of the electrical noise from switching. They can be gotten for not much more then the mechanical relay you are using. If you used some color scheme when wiring prototypes they would be much easier to follow. Default is Red for power Black for ground.

There is no analog signal coming out of the A0 pin it is an analog input. Something is not wired correctly but I cannot follow the picture to determine what. The reference for the A/D I beleive is the processor power VCC.

The ACS712 is a 5volt device, designed to be used with a ratiometric 5volt-logic processor. It is the wrong choice for a 3.3volt processor with absolute A/D. You will never get it 100% right.
The chip, and especially the board it's mounted on is not designed for 230volt AC either. Trace distance between mains voltage and Arduino ground is about 1mm... I consider that dangerous.
As @horace already said, use a (clamp-on) current transformer for these kind of projects.
Leo..

Thanks all for your help, much appreciated

It is interesting as there is a lot of documentation and and even guide videos online on using the same acs712 module with mains 240v power (seemingly without issues, although they may be "getting away with it" as some say). But deeper digging showed some agreement regarding the trace distance. Thanks for pointing that out - I think I will switch sensors as per your advice.

Do you think a wcs1800 module would be suitable? (I have also considered ACS758 which improves on the trace distance but the WCS1800 is probably better regardless)
WCS1800

As per my original problem - I will switch to a 5v arduino and see if thats resolves the issue.

I should mention that the only function I need is to determine whether the tool/power line is ON/OFF - I have no need for specific current level sensing. So if you know any other better option please let me know!

Thanks

What are the qualifications of the video author. What is your background in mains power and what training and certification have you completed to work with this. If somebody gets hurt who is responsible?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.