PWM functionality allows you to create a square wave (fixed amplitude between 0 and 3.3V) with varying frequencies.
You are able to set the PWM pinmode from the Pin Configuration window. You are also able to set the default period of the signal.
From the Logic Maker you will have three options to choose from in the ACTION Gate to change the PWM pin.
SetPeriod
You able to set the period of the square wave by sending a value through to the ACTION Gate with SetPeriod selected.
If you send a value through to the ACTION Gate with SetPeriod selected, you will be able to set the period of your square wave. By default, SetPeriod reads values in microseconds.
SetDutyCycle
You are able to set the duty cycle by sending a value through to the ACTION Gate. By default, values must be between 0-100 and represents the percentage of time that the signal is set high. The diagram below shows an example signals with duty cycles of 50%, 80% and 30%.
SetPulseWidth
SetPulseWidth allows you to set the "ON" time of your square wave. It behaves the same as SetDutyCycle but allows you to send the time that the signal is on instead of a percentage of time. The value sent will be in microseconds. For example if the period of the waves below is 1000 microseconds, their respective SetPulseWidth values will be 500 microseconds, 800 microseconds and 300 microseconds.
NOTE: When you are using SetPulseWidth make sure you don't set the time longer than the period, otherwise it will constantly be on.
Example: Dimming LED
- Set the pin mode of IO0 to PWM with a period of 1000 microseconds.
- Set the pin mode of IO1 to Gnd
- Insert the long leg of the LED into IO0 and the short leg into IO1. Make sure the forward voltage of the LED is around 3V, otherwise you will need a resistor so that the LED doesn't burn out. (The LED used was 2.4V)
- Connect the following gates as per the image below.
PLAY -> CLOCK -> COUNTER BETWEEN -> ACTION - Type 50 into the CLOCK Gate and change the COUNTER BETWEEN Gate to count from 1-100
- Hit play and your LED should start getting brighter. It will cycle the duty cycle from 1-100%.
A flicker of 80Hz is the limit to what the human eye can see. The LED has a period of 1000 microseconds which translates to a frequency of 1000Hz. This frequency is much higher than what the human eye is able to see. Because of this you will perceive the LED to brighten since your eyes will see the accumulation of light (i.e. light intensity) rather than a flickering LED.
Try changing the period to below what the human eye can see. You will also need to change the clock time to be greater than the period of your wave to be able to see each change in duty cycle.
Try this method out with a RGB LED and flash through a rainbow of colours!