Writing on analog components

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 PWM values that it supports. Based on this information we will write a program to fade LEDs and control a micro servo motor.

Tip

Do not use pinMode() for a pin on which you are calling analogWrite(). It forces the pin to be GPIO mode only.

The function name seems like DAC functionality, but it just controls the PWM output. Though we are generating analog values inside the range, the signal is still digital. It is still capable of generating HIGH and LOW signals only. We are not controlling the voltage level. We are controlling the time digital signal is HIGH so that we generate any value as average value over the time. These are square digital waves. If you apply a low pass filter to them, you will get an analog sine wave.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.181.61