BoneScript provides the function analogWrite()
to generate a PWM wave on any GPIO pin from the previous diagram. After a call to analogWrite()
, the pin will generate PWM waves of a specified duty cycle. Here is the prototype of the analogWrite()
function.
The code is as follows:
analogWrite(pin, value, [freq],[callback])
Where
pin
– BeagleBone pin identifier string
value
– duty cycle ratio of the PWM as value between 0
and 1
freq
– frequency of PWM in Hz
callback
– name of function which will be called automatically when analogWrite()
finishes.
Here, freq
and callback
parameters are optional. If freq
is not specified, the default value 2 kHz is used. The duty cycle ratio value is not taken as a percentage but as a ratio between 0 to 1. It is a ratio of time it was ON to period. We can get that by dividing the percentage value by 100. So, a 50% duty cycle will be 0.5 value. Sometimes, you need to read the datasheet of the analog output device to find out the correct...