I know that this is possible, i have done that with Python a year ago, and it was bad.... I have not tested it with an osciloscope but just with servo observations i can tell that this was bad... Maybe if you require lower frequency for a simple task it might work but otherwise, PWM generation with GPIO is a hard thing to do.
GPIO pin 18 - this pin is connected to the soundcard so it is possible to generate - as they say, a good quality PWM from this pin if you are not using 3.5mm audio jack, i have tested it and it did not have worked with my TowerPro SG90 servo so i have no clue if this is a myth or not.
During my Python adventure i was using WiringPi library, but there is a new one called RPi.GPIO and is advertised to be easier to implement than the WiringPi. The library description clearly states that:
Zitat:
Note that this module is unsuitable for real-time or timing critical applications.
so the issue still exists...
Here are some solutions to your specific problem:
A KIND OF SOLUTION:
In order to do PWM with CoDeSys you can write a python "daemon" script that monitors a file where the PWM CoDeSys variable is written on demand. I highly recommend locating that file in a ramdisk to avoid possible errors on SD card.
A PROPER COST EFFECTIVE WAY:
Another possibility - as i like to do it after my adventures with GPIO generated PWM, is to use an Arduino, you can buy el cehapo Arduino nano from Aliexpress for $2 shipping included, connected with USB it will be seen as an com port and will receive power (you can also use SPI or i2c). Arduino has a good PWM hardware generator and should be enough for your application. If you google enough you will find an Arduino code to control servo with PWM from com port - but com port in CoDeSys is another barrier...
I have seen a whole library with arduino code to use its full spectrum in CoDeSys, i can not find this now but it was on this forum.
A PROTIP FOR A SERVO:
If your servo is bad quality, and it jiggles even with stable PWM signal, you can turn off the signal, in Arduino you can do it by generating PWM from analog pins (analog pins in Arduino are always PWM's - easier to implement but you skip the things like frequency) and in order to stop the transmission, you just send the 0 or FALSE state to the port. In RaspberryPi with WiringPi you can just stop PWM with PWM_variable.stop() command.
DIMMING A LIGHT BULB:
With a proper capacitor you will be able to smooth a little bit your GPIO PWM signal so the light bulb will not flicker. I recommend you to try a PWM Python solution now and you will find if this is solution satisfies you enough to use it with a light bulb or you have to use an Adafruit or Arduino generator.
Your post is so inspiring that i will try to compare the PWM from a regular GPIO pin, pin 18 and from Arduino on an osciloscope, if i will find the time....
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi guys.
Has anyone had any experience with PWM on CODESYS for Raspberry Pi?
The Raspberry Pi has one hardware PWM on the gpio. Can we control this with CODESYS? I just want to dim a light bulb.
Regards.
UP !
I'm interesting too .
It will be hard even with CoDeSys...
I know that this is possible, i have done that with Python a year ago, and it was bad.... I have not tested it with an osciloscope but just with servo observations i can tell that this was bad... Maybe if you require lower frequency for a simple task it might work but otherwise, PWM generation with GPIO is a hard thing to do.
GPIO pin 18 - this pin is connected to the soundcard so it is possible to generate - as they say, a good quality PWM from this pin if you are not using 3.5mm audio jack, i have tested it and it did not have worked with my TowerPro SG90 servo so i have no clue if this is a myth or not.
During my Python adventure i was using WiringPi library, but there is a new one called RPi.GPIO and is advertised to be easier to implement than the WiringPi. The library description clearly states that:
so the issue still exists...
Here are some solutions to your specific problem:
A KIND OF SOLUTION:
In order to do PWM with CoDeSys you can write a python "daemon" script that monitors a file where the PWM CoDeSys variable is written on demand. I highly recommend locating that file in a ramdisk to avoid possible errors on SD card.
A PROPER COST EFFECTIVE WAY:
Another possibility - as i like to do it after my adventures with GPIO generated PWM, is to use an Arduino, you can buy el cehapo Arduino nano from Aliexpress for $2 shipping included, connected with USB it will be seen as an com port and will receive power (you can also use SPI or i2c). Arduino has a good PWM hardware generator and should be enough for your application. If you google enough you will find an Arduino code to control servo with PWM from com port - but com port in CoDeSys is another barrier...
I have seen a whole library with arduino code to use its full spectrum in CoDeSys, i can not find this now but it was on this forum.
A CODESYS WAY:
In an example below, CoDeSys have used an i2c Adafruit PWM controller and i also approve this solution.
https://www.youtube.com/watch?v=oDha-2y3pH4
A PROTIP FOR A SERVO:
If your servo is bad quality, and it jiggles even with stable PWM signal, you can turn off the signal, in Arduino you can do it by generating PWM from analog pins (analog pins in Arduino are always PWM's - easier to implement but you skip the things like frequency) and in order to stop the transmission, you just send the 0 or FALSE state to the port. In RaspberryPi with WiringPi you can just stop PWM with PWM_variable.stop() command.
DIMMING A LIGHT BULB:
With a proper capacitor you will be able to smooth a little bit your GPIO PWM signal so the light bulb will not flicker. I recommend you to try a PWM Python solution now and you will find if this is solution satisfies you enough to use it with a light bulb or you have to use an Adafruit or Arduino generator.
Your post is so inspiring that i will try to compare the PWM from a regular GPIO pin, pin 18 and from Arduino on an osciloscope, if i will find the time....