Current detection sct013 >> Stepper Movment

Hi There,

I'm quite new to the hole arduino thing but managed to get almost everything working.

Brief explanation:

I have an Arduino Nano, with an sct013, when current is detected a stepper motor should “run”.

(The stepper has a lead screw attached which activates the limit switches)

Everything is working as it should if I "fake" the current detection with a normal switch. But when I attached the sct013 to all of it, the stepper barley turns.

I assume that has to do with the "sample size" or the fact that it reads the analog value??

I reduced the sample size from 10,100,500 to 1200 but with not much improvement.

I would be happy about any suggestions.

Here is my code:

#include <AccelStepper.h>
#include "EmonLib.h"
#define CURRENT_SENSOR_PIN A0

EnergyMonitor emon1;

// Define the connections for your stepper motor
const int stepPin = 3;
const int dirPin = 4;
const int enPin = 5;

// Define the connections for your limit switches
const int limitSwitch1 = 7;
const int limitSwitch2 = 8;   

const float currentThreshold = 2.0;

// Create an instance of the AccelStepper class
AccelStepper stepper(1, stepPin, dirPin);

void setup() {
  // Set the stepper motor as an output
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);

  // Set the limit switches as inputs with pull-up resistors
  pinMode(limitSwitch1, INPUT_PULLUP);
  pinMode(limitSwitch2, INPUT_PULLUP);

  // Set the motor's speed and acceleration
  stepper.setMaxSpeed(1500); // Adjust this to your motor's specs
  stepper.setAcceleration(500); // Adjust this to your motor's specs
  
  emon1.current(CURRENT_SENSOR_PIN, 111.1);  // Calibration factor may need adjustment
}

void loop() {
// Measure current using EmonLib
 double Irms = emon1.calcIrms(1480);  // 1480 = number of samples to take
}
  
// Check the state of the limit switches
  int switchState1 = digitalRead(limitSwitch1);
  int switchState2 = digitalRead(limitSwitch2);

  if (Irms > currentThreshold && (switchState2 == LOW)) {
    digitalWrite(enPin, LOW);
    stepper.setSpeed(1000); // Adjust speed as needed
    stepper.runSpeed();
    //Serial.println("on_1");
  }
  
  else if (Irms > currentThreshold && (switchState2 == HIGH)) {
    // stepper.setCurrentPosition(0);
    stepper.stop();
    digitalWrite(enPin, HIGH);
    //Serial.println("on_2");
  }

  else if (Irms < currentThreshold && (switchState1 == LOW)) {
    digitalWrite(enPin, LOW);
     stepper.setSpeed(-1000); // Adjust speed as needed
    stepper.runSpeed();
    //Serial.println("off_1");
  }

  else if (Irms < currentThreshold && (switchState1 == HIGH)) {
    stepper.setCurrentPosition(0);
    stepper.stop();
    digitalWrite(enPin, HIGH);
    //Serial.println("off_2");
  }
  
    else {
    stepper.setCurrentPosition(0);
    stepper.stop();
    digitalWrite(enPin, HIGH);
  }
  }

Please show us a schematic or at least a block diagram of how you have this whole thing set up and powered. How are you rectifying the AC from the current transformer so the Arduino can read the DC voltage?

Hi Paul,

I used this circuit for the sct013

and this one for the a4988 and the stepper, I have an external power supply with 12v to power the a4988

Hope that helps.

Please confirm that you have ONLY WIRE going through the current transformer opening.

yes, only the "hot" wire.

Then I suggest you write a program that deals ONLY with the current transformer so you can limit the code to that sensor ONLY! Then see if you can determine the error.

Ok, what I hear from that is that it should be possible to let the stepper run smothly even with constantly detecting current?

If there is nothing inherently wrong I'll try a bit further.

put some Serial.println() statements in your code to display the values of critical variables and show the flow of execution of the code
then if you have questions you can upload the serial Monitor output (as text not screen image)

Use emon1.calcVI(20,2000) to get the RMS current emon1.Irms

@horace will do that after i soldered everything up.

@jim-p
I'm not sure I understand what you mean, should i replace

emon1.calcIrms(1480)

With

emon1.calcVI(20,2000)

?

Thanks for your help

See this example:

I miss a rectifier diode in that current detection circuit. Now you're still getting AC to the Arduino and the capacitor, neither of which are particularly happy with negative voltages. Also the moment you take the sample has a big impact on the reading as you're getting a waveform: the positive half of the sine complete, the negative half clipped at -0.5V by the protection diodes on the Arduino input.

This schematic I've used before and works fine.
Tune R5 for sensitivity, I had a 1:1000 CT.
C5/R6 is the peak detection. Tune to suit your needs.
D3 is for protection.
R7 is for input protection.

@jim-p ill try this tonight (took me a little longer to solder everything together insted usibg a breadboard)

@wvmarle thanks i use a sct013 50A to 1V, would tge circuit be the same?

The example code in post #12 will not work correctly with the circuit shown in post #13
You should ask @wvmarle for his code if you use his circuit

Depending on the current the OP wants to sense, R5 may be much too high. CTs can generate rather high voltages on their secondary without a proper "burden". What is the max current to be sensed?

@jim-p I still use the circuit I have posted before but with your code I get 5 different readings but they don't change when I apply current.

in the test setup I only have 200-500 watt... in the real setup later it will be from

2kw/3amp up to
32kw/50amp

for me the accuracy totaly does not matter, the only thing is right now if the machine is on or not.

A quick Google search showed me that there are two versions of the sct013, one with voltage output and one with current output. If yours has indeed a voltage output, you can connect it to an analog input and just do an analogRead() to get a reading.

The voltage output being 0-1V, and an absolute value, means you should use the internal 1.1V reference for the ADC. Use

analogReference(INTERNAL);

in your setup() function.

Do some readings to see what value you get when the machine is on. Probably just testing for the reading to be >0 is enough, as with the machine off it should be zero, though I'd prefer to give it a little leeway for interference and consider the machine on when it's greater than say 10, or half of the reading when the machine is running.

Do mind that as it's a rectified AC signal reading the voltage output is likely to be unstable. As the voltage output is 0-1V you can't use a simple peak detection as my circuit, instead take samples for at least 20 ms (or 18 ms if you're on 60 Hz) and take the highest reading from those.

Come to think of it, maybe in my circuit it'd be better to place R5 behind D2 instead, for more accurate readings. But then I was also using it or simple on/of detection.

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