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;
}