I can receive stable value from the PLC to change the motor speed but when I write it in an analogWrite() function to control the motor, this value become unstable.
When I delete this function, the value received returns unstable.
Thanks for your help.
const int inPin = A1;
const int outPin = 6;
int plcValue = 0;
int outputValue = 0;
int motorSpeed = 0;
void setup() {
// put your setup code here, to run once:
pinMode(outPin, OUTPUT); // Definir la pin en tant que sortie
pinMode(inPin, INPUT); // Definir la pin en tant que sortie
Serial.begin(9600); // Definir la vitesse en Baud (Données/sec)
}
void loop() {
// put your main code here, to run repeatedly:
motorSpeed = speedMotorControl();
}
int speedMotorControl() {
// read the analog in value:
plcValue = analogRead(inPin);
// map it to the range of the analog out:
outputValue = map(plcValue, 198, 990, 0, 255);
// print the results to the serial monitor:
Serial.print("plc = " );
Serial.print(plcValue);
Serial.print("\t output = ");
Serial.println(outputValue);
analogWrite(outPin, outputValue);
delay(100);
return outputValue;
}
The Arduino is connected to a PLC output module, wich provides a value between 1 and 5V.
This value is map (to be between 0 and 255) and send on the Arduino analog output pin to control the motor speed.
When I say "unstable" I mean that I do not receive the expected value or there is a lot of a wrong value.
Attached screenshots from the serial monitor (without and with the analogWrite() function). In this example, the expected received value from the PLC is 868.
It's a SIMATIC S7-300 Analog output module.
I work with an automation engineer.
I dont think the problem come from the wires because I can receive the expected value when I dont use the analogWrite() function
I tried my program with a potentiometer (instead of the PLC) and its works with the analogWrite() function
There is no obvious problem in the code.
The code works if you use a potentiometer instead of a PLC.
It has to be something to do with the PLC card you are using and the way things are wired, so you need to state what I/O card you are using and show how things are wired (both the inputs and the outputs).
You have said that you are receiving signal from PLC in the range: 1V to 5V.
Assuming UN0 (10-bit ADC), the range of numerical value returned by int plcValue = analogRead(A1) should be: 205 (1023/51) to 1023 (1023/55) and accordingly, the map() function would be:
GolamMostafa:
You have said that you are receiving signal from PLC in the range: 1V to 5V.
Assuming UN0 (10-bit ADC), the range of numerical value returned by int plcValue = analogRead(A1) should be: 205 (1023/51) to 1023 (1023/55) and accordingly, the map() function would be:
Try powering the motor from a separate power supply not from the arduino. The 5V from an arduino board is not suitable for driving high current devices like motors. I think what is happening is that excessive current to the motor is causing the Vcc rail on the to drop. The default setting for an arduino ADC is to use Vcc (+5V) as the analogue reference for the ADC. If the motor is pulling significant current and causing Vcc to drop then the analogRead() value will change.
E.G. if the controller is outputting 2.5v the with the Vcc rail at 5V the ADC will read 2.5/5 * 1024 = 512 but if the motor causes Vcc to drop to 4.5V then the ADC will read 2.5/4.5 * 1024 = 568.
In your case of trying the sketch with a pot across Vcc and Gnd you cancel out any variation in Vcc.
E.G. If the pot is set midway and with Vcc at 5V then the input to the ADC will be 2.5V and the ADC will read 512. If Vcc drops to 4.5V then the input to the ADC will be 2.25V and the output from the ADC will still be 512 (2.25/4.5 * 1024).
Remember when connecting a separate power supply to the motor to connect the 0V of the motor supply to the GND of the arduino.