analogWrite() problem

Hi everybody,

I'm a beginner in Arduino and I try to control this motor 15. Octopus Motor Brick(EF04059) — ELECFREAKS WIKI with a Siemens PLC.

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

So the Arduino is reading and writing analog values to a Siemens PLC which in turn then controls a fan.
Why do you use the Arduino at all?

When you say "this value becomes unstable" what exactly do you mean, can you give some examples of the values.

PLCs use various analog I/O e.g 0-10V , 0-20mA, 4-20mA .

What exactly are you connecting the Arduino to and how, do you have the part numbers for the PLC cards you are using and how have you wired things.

Do you have filtering on the PWM output?

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.

Capture d’écran du 2019-10-01 14-43-56.png

Capture d’écran du 2019-10-01 14-44-27.png

Arduino reference;

If the analog input pin is not connected to anything, the value returned by

analogRead()

will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.).

Are things wired up properly?

(deleted)

It's weird but I think the problem come from the analogWrite() function because I can read the good values without it.

spycatcher2k:
Have you put a low pass filter on the PWM output to convert the ON-OFF PWM signal to an analog voltage?

I think you and TMFKAAWOL have hit the nail on the head.

Opps, changed my mind again :slight_smile:

He is reading a value from a PLC and outputting a value to a fan.
Why would the input from the PLC change unless there is a wiring issue?

spycatcher2k:
Have you put a low pass filter on the PWM output to convert the ON-OFF PWM signal to an analog voltage?

No I don't use any filter

I tried my program with a potentiometer (instead of the PLC) and its works with the analogWrite() function

Which Siemens module do you use? And how was it wired? (hand drawing will do, please no Fritzing mess)

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

Thanks fo your help

But which?

sorry for the lack of precision, its this one : 6ES7332-5HB01-0AB0 SIEMENS SIMATIC S7-300 SM 332 | Siemens | WIAutomation

nicope13:
No I don't use any filter

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:

byte outputValue = map(plcValue, 205, 1023, 0, 255);

and not --

byte outputValue = map(plcValue, 198, 990, 0, 255);

though it does not matter much; but, it seems to be not complying with data.

Here is a simple diagram of my setup

Thanks for your help

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:

byte outputValue = map(plcValue, 205, 1023, 0, 255);

and not --

byte outputValue = map(plcValue, 198, 990, 0, 255);

though it does not matter much; but, it seems to be not complying with data.

Yes you are right, I will change that

Thanks

Module

Hi

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.

Ian