Using ACS712 to control relay and servo motor

Hello, I need help to fix the code. The logic is if current is below 3Amps, the relay should close to run dc motor and also the servo motor simultaneously

#include <Servo.h>

Servo wiperServo;

const int currentPin = A0;    // ACS712 output
const int relayPin = 8;       // Relay control
float threshold = 3.0;        // 3A limit

void setup() {
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);
  wiperServo.attach(9);
  wiperServo.write(0);
}

void loop() {
  float current = 0;

  // Read and average current
  for (int i = 0; i < 1000; i++) {
    current += (0.044 * analogRead(currentPin) - 3.78) / 1000.0;
    delay(1);
  }

  Serial.print("Current: ");
  Serial.println(current);

  // When current is below threshold
  if (current < threshold) {
    digitalWrite(relayPin, HIGH);  // close relay → motor ON

    // wipe servo 3 times
    for (int i = 0; i < 3; i++) {
      wiperServo.write(90);
      delay(500);
      wiperServo.write(0);
      delay(500);
    }

    digitalWrite(relayPin, LOW);   // open relay → motor OFF
  }

  delay(1000);
}

So, what's not working? It looks like your code is doing exactly what you describe.

What does your Serial Monitor say? You have some Serial.print() statements that print out the current, is it below 3.0?

With running motors, there is always the possibility that you are not powering it correctly or supplying enough current to get the job done? How is this all wired up?

1 Like

It's not running in proteus as expected in the code

What values are you getting from the analog read?

:cry: I'm lost there. Proteus simulation is showing zero. I'm using proteus to simulate and Arduino IDE to write the code. I still have to do the actual testing on a board.

Did you tell Proteus to allow you to enter a value?

No, I did not. That's a new concept I need to learn

I don't know Proteus, but the relay trigger circuit looks quite weird...

Explain to me what you know I can try to troubleshoot using the concepts you know

Your image presents that you have relay trigger pin connected together with 5V to coil.

What's the correct connection,

The image I posted above should give you the idea...
NPN transistor drives the coil.