analogRead() give variable value

Hi everybody,

I use a PLC, connected to my arduino, to control a motor rotation speed ( connected to my arduino too).

The PLC give an output between 1-5 V. I checked with a multimeter and the voltage is stable.

For a 100 % rotation speed command control I have 5 V (stable).
For a 0 % rotation speed command control I have 1 V (stable).
etc ...

But when I read this input on my arduino with the analogRead () function, the value isnt stable. I read that this function map input voltages into integer values between 0 and 1023.

For a 1V input I read around 195, and for 5V, around 990.

Thanks for your help.

Here is my code and schema of my setup.

/*
  Description

*/

const int analogInPinMotor = A1;  // Analog input pin that the motor control from PLC is attached to

const int analogOutPinMotor = 6; // Analog output pin that value to control the motor is generate from


int sensorValue = 0;        // Value read from the analogInPinMotor
int outputValue = 0;        // Value mapped (0-255) from the analogInPinMotor
int motorSpeed = 0;         // Motor speed (0-225)
int speedValue = 0;         // Motor speed (0-100)


int temperatureCalculationParameter [2] = {0,0};


void setup() {

  //Define pin as input
  pinMode(analogInPinMotor, INPUT);

  // Define pin as output
  pinMode(analogOutPinMotor, OUTPUT);

  //Define Baud speed
  Serial.begin(9600);
}

void loop() {
  temperatureCalculationParameter[0] = speedMotorControl();
}

/*
Motor speed control from PLC command on input pin A0
*/
int speedMotorControl() {
  
  // read the analog in value:
  sensorValue = analogRead(analogInPinMotor);
  
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 195, 990, 0, 255);

  if (outputValue > 255) {
    outputValue = 255;
  }
  
  if (outputValue < 0) {
    outputValue = 0;
  }


  /* print the results to the serial monitor:
    Serial.print("plc = " );
    Serial.print(sensorValue);
    Serial.print("\t output = ");
    Serial.println(outputValue);   */

  analogWrite(analogOutPinMotor, outputValue);
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(100);
  return outputValue;
}

Your schematic has the PLC output being connected to Arduino pin A1, but your code is reading A0. IS there a typo somewhere?

Driving a motor from the Arduino is not going to let that Arduino live a long life - get a different power supply for it.

wildbill:
Your schematic has the PLC output being connected to Arduino pin A1, but your code is reading A0. IS there a typo somewhere?

Driving a motor from the Arduino is not going to let that Arduino live a long life - get a different power supply for it.

Sorry It was a mistake in my schema, I modified the code.

I use an arduino for the power supply just for my test, but thanks for your advice. I will change that

If you measure the voltage between Ground and the Aref pin do you get the exact same 5.00V as between Ground and the A1 pin?

Maybe I dit not express myself well, the multimeter is wired to the PLC.

Thanks to that I know the output voltage value from the PLC is stable (and consistent).
Relative to the desired rotation speed value :

-5V for 100% rotation speed
-1V for 0% rotation speed
etc.

I attached my setup schema with the multimeter added.

johnwasser:
If you measure the voltage between Ground and the Aref pin do you get the exact same 5.00V as between Ground and the A1 pin?

You are right, I just measured the voltage between Ground and the Aref, I measured between 4.86 ans 4.87 V.
Same between the 5V output pin of the arduino and the ground.

Maybe the power supplying received by the arduino isnt enough ?

As long as you know what you're getting, does it matter? You're mapping it anyway to 0 to 255. As long as it's consistent, you're good I think.

wildbill:
As long as you know what you're getting, does it matter? You're mapping it anyway to 0 to 255. As long as it's consistent, you're good I think.

That's tthe problem, the value read by the arduino isnt consistent but I checked with a multimeter and the value send by the PLC is consistent.

What does the Arduino read and how does that compare to what your voltmeter says?