Hello there,
my name is Fabrizio. I had he opportunity to start learning arduino to make a tool actuated vacuum system for my garage.
Made lots of searches , bought the nano , rele and an acs712 current sensor.
I would like to switch on my vacuum as soon as the sensor see more than 1 amp on the wiring to my tool, then switch off the vacuum after 3 sec the tool is off.
Found some code to read ac current, works great! the serial monitor show the real current.
but seems impossible to close the rele and switch on the vacuum ( i use a contactor for that due to high amperage)
can you help me to find what i'm wrong?
//many thanks to Kraj Technocrates https://youtu.be/JOdcWmyKMQs
const int sensorIn = A0;
int mVperAmp = 66; // 185 or 100 per 20A
double Voltage = 0;
double VRMS = 0;
int AmpsRMS = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
if(AmpsRMS > 1 ){digitalWrite(7, HIGH);}
else {
delay(3000);
digitalWrite(7, LOW);
}
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707;
AmpsRMS = (VRMS * 1000)/mVperAmp;
Serial.println(AmpsRMS);
}
float getVPP()
{
float result;
int readValue;
int maxValue = 0;
int minValue = 1024;
uint32_t start_time = millis();
while((millis() - start_time) < 3000)
{
readValue = analogRead(sensorIn);
if (readValue > maxValue)
{
maxValue = readValue;
}
if (readValue < minValue)
{
minValue = readValue;
}
}
result = ((maxValue - minValue ) * 5.0)/1024.0;
return result;
}
Can you post a schematic of the project showing the driver circuit between the Arduino and contactor?
Hi, thanks. I will do it for sure.
But since i did some tests using basic code , and they where ok, i think the issue is inside the code
if(AmpsRMS > 1 ){digitalWrite(7, HIGH);}
OK, several things wrong here. Don't put all the code on one line. The { and } allow you to spread it out a little, like you did on the else side. Then once you've done that, hit the auto-format function on the Tools menu. It is very good at making your code more readable. If it appears to scramble your code then the code was wrong.
The biggest mistake I see is you've used a variable before calculating what should go in that variable. The usual pattern of usage is to read all the inputs, do the calculations and then set the outputs. It does work here because it remembers the value from the previous iteration but small changes elsewhere in the program may stop this from working. Then you post this snippet here and ask why it doesn't work and the problem is elsewhere.
Your function which reads the analog values is a little weird. Taking the average of the max and min over 3 seconds isn't really a good way to smooth out the small variations in analog readings. It also means that your program cannot respond to changes quickly. There's a minimum of 3 seconds delay built in there. In those 3 seconds, you are going to be making about 30,000 readings. That is a huge amount of data to turn into just two numbers. Basically you are throwing away 29,998 readings, so you probably didn't need to spend 3 seconds doing that.
Then there's the delay on switch-off which adds another 3 seconds delay.
How have you wired the relay? You know that most relays require more than 20mA which is the maximum any Arduino pin can provide safely?
Hey guys thanks a lot , I appreciate everything !!!
I just attached the wiring, hope it helps.
I understand your point on the 3 sec delay and 3 sec of the function. I'll try to make it better.
But so far, the relay is never on (the light is off). Even after 10 sec the tool is on and the ACS712 is sensing 5 amps.
I tried to check the realy using some simple coding and it works.
I changed the position of the IF , but nothing happened.
Followind other discussions, I tried to put "pinMode(7, OUTPUT); " into the setup , the relay is always on.
As soon i start the tool the relay goes off.
It works but the opposite i would like.
Here is the sketch:
const int sensorIn = A0;
int mVperAmp = 66; // 185 or 100 per 20A
double Voltage = 0;
double VRMS = 0;
int AmpsRMS = 0;
void setup() {
Serial.begin(9600);
pinMode(7, OUTPUT);
}
void loop()
{
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707;
AmpsRMS = (VRMS * 1000)/mVperAmp;
Serial.println(AmpsRMS);
if(AmpsRMS > 1 ){
digitalWrite(7, HIGH);
}
else {
digitalWrite(7, LOW);
}
}
float getVPP()
{
float result;
int readValue;
int maxValue = 0;
int minValue = 1024;
uint32_t start_time = millis();
while((millis() - start_time) < 3000)
{
readValue = analogRead(sensorIn);
if (readValue > maxValue)
{
maxValue = readValue;
}
if (readValue < minValue)
{
minValue = readValue;
}
}
result = ((maxValue - minValue ) * 5.0)/1024.0;
return result;
}
Well that's easy: just swap HIGH and LOW in the output.
it works!!!!
but i do not undestand why...
swapping high low i read a different instruction.
I know read :
if the amperage is minor of 1, (that means the tool is off) then close the relay (turn on the vacuum)
here is the final code for all the guys like me ( i know, in USA you can buy an IVac for 40 bucks, the arduino total cost is likely a bit more, but DIYers think in another way )
int mVperAmp = 66; // 185 or 100 per 20A
double Voltage = 0;
double VRMS = 0;
int AmpsRMS = 0;
void setup() {
Serial.begin(9600);
}
void loop()
{
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707;
AmpsRMS = (VRMS * 1000)/mVperAmp;
Serial.println(AmpsRMS);
pinMode(7, OUTPUT);
//if the tool is off the vacuum must be off too (no AMPS read -> the relay must be low )
if(AmpsRMS < 1 ){
digitalWrite(7, HIGH);
}
else {
delay (1500);
digitalWrite(7, LOW);
}
}
float getVPP()
{
float result;
int readValue;
int maxValue = 0;
int minValue = 1024;
uint32_t start_time = millis();
while((millis() - start_time) < 1500) //the bigger you can switch off and on the tool for a moment and have the vacuum always on, it is better for the motor of the vacuum.
{
readValue = analogRead(sensorIn);
if (readValue > maxValue)
{
maxValue = readValue;
}
if (readValue < minValue)
{
minValue = readValue;
}
}
result = ((maxValue - minValue ) * 5.0)/1024.0;
return result;
}
Some relay modules are active high, others active low. A high signal to a relay sometimes means it's on, sometimes that it's off, depending on the relay. It's as simple as that. In your case apparently the relay is on when the output signal is low.