12v DC current sensor motor stop

The value that you read here with no current flowing through your sensor would be your offset.

The offset is the value that a sensor reports when nothing is happening....
According to the code that value is 512. Makes sense, that is exactly in the middle, so it can measure both positive and negative current.
Now: Does it really read 512 (value printed to Serial should be 0) if no current is flowing?
And: What does it read when a positive or negative current is flowing? If this number is significantly different from 512, we would know that the sensor is actually working... (step 1).

Use a 'slow' fuse (the ones used in cars are usually of 'slow' type). Those will allow a high rush in current for a short time even if it is much higher than the nominal value of the fuse.
A 'fast' fuse will burn out during the rush in...
I am not convinced the cheapy relay in the video will work voor long at this high current (and with an inductive load). A car relay would better.

Don't get your hand or fingers stuck with such a motor! It will continue running...

I will tell you a secret:
Highlight whatever you want.
Press CTRL-C
Move your mouse pointer to where you want it.
Press CTRL-V

Don't tell anyone else!

The wires on the motor in the video tell me that 5A is probably the max. continuous current of the motor. As said: rush in or stall current may be much larger!
At 40A those wires will get so hot that insulation may be damaged.

OK I'm back on deck.
Here is the code I have at the moment.

#define Relay A1
int analogPin = A0; // Current sensor output

long int sensorValue = 0; // variable to store the sensor value read

void setup() {
Serial.begin(9600); // setup serial
pinMode(Relay, OUTPUT);
}

void loop() {

sensorValue = analogRead(analogPin);

// wait 2 milliseconds before the next loop
delay(200);

Serial.println("ADC Value: ");
Serial.println(sensorValue);
// set value on which you want to switch off Motor. normally motor oprate between value 512 to 525.
// I set 530 value on which motor switch off for 5 seconds.
if(sensorValue > 515)
{
digitalWrite(Relay, LOW);
delay(5000);
}
else
{
digitalWrite(Relay, HIGH);
}

}

I tried to make a video for you of the ADC working but I seem to be unable to download it to here. so here is some photos from the video. There does not seem to be any change in the numbers except when the motor stops so do the numbers scrolling.

I guess that means the sensor is not working.
I have tried 2 different sensors and they are both the same.
Soooo These sensors are not suitable for this application.

Do you think I try a different brand of sensor or do you have another suggestion?

I think you meant to post your code like this:

#define Relay A1
int analogPin = A0; // Current sensor output

long int sensorValue = 0; // variable to store the sensor value read

void setup() {
  Serial.begin(9600); // setup serial
  pinMode(Relay, OUTPUT);
}

void loop() {

  sensorValue = analogRead(analogPin);

  // wait 2 milliseconds before the next loop
  delay(200);
  Serial.println("ADC Value: ");
  Serial.println(sensorValue);
  // set value on which you want to switch off Motor. normally motor oprate between value 512 to 525.
  // I set 530 value on which motor switch off for 5 seconds.
  if (sensorValue > 515)
  {
    digitalWrite(Relay, LOW);
    delay(5000);
  }
  else
  {
    digitalWrite(Relay, HIGH);
  }

}

(You have to use the code tags, just click on the appropriate button in the toolbar.)

I got this error. Bugger, got it wrong some how. I think I forgot to change the relay to 5v. I only have 1 pin for 5v and the the current sensor is on it. Is there another pin somewhere? Photo of my Uno below

C:\Users\PC\AppData\Local\Temp.arduinoIDE-unsaved202441-4376-1o8du7j.v20n\sketch_may1b\sketch_may1b.ino: In function 'void loop()':
C:\Users\PC\AppData\Local\Temp.arduinoIDE-unsaved202441-4376-1o8du7j.v20n\sketch_may1b\sketch_may1b.ino:6:14: error: 'define' was not declared in this scope
void loop() {define Relay A1
^~~~~~
C:\Users\PC\AppData\Local\Temp.arduinoIDE-unsaved202441-4376-1o8du7j.v20n\sketch_may1b\sketch_may1b.ino:6:14: note: suggested alternative: 'rewind'
void loop() {define Relay A1
^~~~~~
rewind
C:\Users\PC\AppData\Local\Temp.arduinoIDE-unsaved202441-4376-1o8du7j.v20n\sketch_may1b\sketch_may1b.ino:11:14: error: a function-definition is not allowed here before '{' token
void setup() {
^
C:\Users\PC\AppData\Local\Temp.arduinoIDE-unsaved202441-4376-1o8du7j.v20n\sketch_may1b\sketch_may1b.ino:16:13: error: a function-definition is not allowed here before '{' token
void loop() {
^

exit status 1

Compilation error: 'define' was not declared in this scope

#define Relay A1  // not wrong but not preferred Arduino nomenclature or pin assignment
int analogPin = A0; // Current sensor output

long int sensorValue = 0; // an int would be fine here, more than a big enough container

void setup() {
  Serial.begin(9600); // setup serial
  pinMode(Relay, OUTPUT);
}

void loop() {

  sensorValue = analogRead(analogPin);

  // wait 2 milliseconds before the next loop
  delay(200); // this is 200 milliseconds, just saying
  Serial.println("ADC Value: ");
  Serial.println(sensorValue);
  // edited value for ease of observation
  // I set 530 value on which motor switch off for 5 seconds.
  // but I set it at 325 to see both conditions with nothing attached to Arduino
  if (sensorValue > 325)
  {
    digitalWrite(Relay, LOW);
    delay(5000);
  }
  else
  {
    digitalWrite(Relay, HIGH);
  }

}

So this code works as far as I can tell, with nothing hooked up to my trusty Genuino Uno R3. The "out of scope" error is often due to misplaced curly braces in setup() or loop(). Did you delete one by mistake? If you click just to the right of any curly brace, the IDE will show you with either text or a nifty box where its mate is. Great feature if you ask me.

Also, get the relay off A1. The analog pins can be used as digital outputs, but it not the usual way. Maybe someone else here can comment, but I think your reading an analog signal on A0 and switching an OUTPUT on A1 can be introducing noise on your relay pin.

I'll try again. I think I it would be best if I start a new sketch. How do I give it a name?

Got it!!

1 Like

What, the relay? Just name it something. Maybe

const int actuatorRelay = 7; // pin D7

If you mean the new sketch, just save as whatever you like. Make it meaningful in your growing sketchbook though, maybe "projectActuatorV1" and then in setup(), right after Serial.begin(9600);, it's a good idea to print what sketch is on the Arduino, with version if applicable, so maybe:

Serial.println("projectActuatorV1");
Serial.println(); // just a blank line
delay(1000); // give yourself a sec to notice

It's ALIVE??

images

Can you run a known current through your sensor?
Like from a 1.5V battery with 2 ohm in series?

I copy your code and place it after the brace on Void Loop. Is this correct? It states " // put your main code here, to run repeatedly:" Or should it go Void Setup? " // put your setup code here, to run once:"

You mean me? If so, here's your same code with the suggestions. Don't forget to change sensor threshold value to whatever you calibrate for.


const int analogPin = A0; // const because the pin assignment never changes
const int actuatorRelay = 7; // pin D7
int sensorValue = 0; // regular int because this value will change

void setup() {
  Serial.begin(9600);
  Serial.println("projectActuatorV1"); // make sure this always matches sketch name
  Serial.println(); // just a blank line
  delay(1000); // give yourself a sec to notice
  pinMode(actuatorRelay, OUTPUT);
}

void loop() {
  sensorValue = analogRead(analogPin);

  delay(200);
  Serial.print("ADC Value: ");
  Serial.println(sensorValue);

  if (sensorValue > 325)
  {
    digitalWrite(actuatorRelay, LOW);
    delay(5000);
  }
  else
  {
    digitalWrite(actuatorRelay, HIGH);
  }
}

Oh, and super important: if you got something working as intended. make a note at the top of the sketch, like a comment. Say something to yourself like

// DO NOT MODIFY - WORKS AS INTENDED

Then make a new sketch, copy/paste the good one, delete the comment I just suggested you use for the good one, and make experimental changes to the copy - NEVER make experimental changes to the good working one.

Trust me. Don't learn this lesson the hard way.

Did you see/try the code from post#31.
Leo..

I'm getting myself so confused. I seem to have so many codes and I can't get any to load. They keep coming up with an error

If I go back to this one:

void setup() {
// put your setup code here, to run once:

}

void loop() { sensorValue = analogRead(analogPin);

// wait 2 milliseconds before the next loop
delay(200); // this is 200 milliseconds, just saying
Serial.println("ADC Value: ");
Serial.println(sensorValue);
// edited value for ease of observation
// I set 530 value on which motor switch off for 5 seconds.
// but I set it at 325 to see both conditions with nothing attached to Arduino
if (sensorValue > 325)
{
digitalWrite(Relay, LOW);
delay(5000);
}
else
{
digitalWrite(Relay, HIGH);
}

}
// put your main code here, to run repeatedly:

}

It comes up with this error

C:\Users\PC\Documents\Arduino\Actuator_Stop\Actuator_Stop.ino: In function 'void loop()':
C:\Users\PC\Documents\Arduino\Actuator_Stop\Actuator_Stop.ino:6:15: error: 'sensorValue' was not declared in this scope
void loop() { sensorValue = analogRead(analogPin);
^~~~~~~~~~~
C:\Users\PC\Documents\Arduino\Actuator_Stop\Actuator_Stop.ino:6:40: error: 'analogPin' was not declared in this scope
void loop() { sensorValue = analogRead(analogPin);
^~~~~~~~~
C:\Users\PC\Documents\Arduino\Actuator_Stop\Actuator_Stop.ino:6:40: note: suggested alternative: 'analogWrite'
void loop() { sensorValue = analogRead(analogPin);
^~~~~~~~~
analogWrite
C:\Users\PC\Documents\Arduino\Actuator_Stop\Actuator_Stop.ino:17:18: error: 'Relay' was not declared in this scope
digitalWrite(Relay, LOW);
^~~~~
C:\Users\PC\Documents\Arduino\Actuator_Stop\Actuator_Stop.ino:17:18: note: suggested alternative: 'delay'
digitalWrite(Relay, LOW);
^~~~~
delay
C:\Users\PC\Documents\Arduino\Actuator_Stop\Actuator_Stop.ino:22:18: error: 'Relay' was not declared in this scope
digitalWrite(Relay, HIGH);
^~~~~
C:\Users\PC\Documents\Arduino\Actuator_Stop\Actuator_Stop.ino:22:18: note: suggested alternative: 'delay'
digitalWrite(Relay, HIGH);
^~~~~
delay
C:\Users\PC\Documents\Arduino\Actuator_Stop\Actuator_Stop.ino: At global scope:
C:\Users\PC\Documents\Arduino\Actuator_Stop\Actuator_Stop.ino:28:1: error: expected declaration before '}' token
}
^

exit status 1

Compilation error: 'sensorValue' was not declared in this scope

Where do I put the sensor value and what value should I put?