DUE Frequency

Frequency

Prerequisite

This page assumes you have completed the analog, sound, and servo lessons.

Freq()

This features allows for accurate control of special dedicated pin using internal special hardware. For example, the BrainPad Pulse has this feature available on either pin 0 and ‘p’. Note that the ‘p’ pin is the on-board piezo buzzer, see specific hardware pin-out for details on supported pin(s).

Freq() functions takes 4 arguments: pin, frequency, duration, and dutyCycle. Frequency is how many times the pin will cycle per second. Duty cycle is the percentage of the pin high to low state. A duty cycle of 0 will leave the pin inactive, 10 will make the pin active (high) 10% of the time, 90 will make the pin active 90% of the time, and so on. Duration is used to set a period for the pin to be active for. A duration of 1000 will output the specified PWM signal on the pin for 1 second. Setting duration to 0 is a special case and will make the pin active forever.

There are many ways to use PWM. One of the uses is to generate sounds by connecting the pin to a piezo. Connecting a speaker is also possible if using an amplifier circuit.

This example uses frequency to control the on-board buzzer of the BrainPad Pulse. Note that duty cycle is always 50 when generating sounds. Changing the duty cycle will reduce the “volume” of the sound but this is not typically a standard use. It is best to keep duty cycle at 50 and use the frequency to generate sounds.

@Loop
For x=1 to 1000
    Freq('p',x,200,50)
    Wait(200)
Next
For x=1000 to 1 step -1
    Freq('p',x,200,50)
    Wait(200)
Next
Goto Loop

Since Freq() is a non-blocking function, it will return immediately even if duration is set to a future time. Making a second call to Freq() will terminate any existing active frequency, despite the duration of previous calls argument.

In this example, 1Khz will be generated for only 2 seconds and not 5 seconds, because the second Freq() called cancels out the first one.

Freq('p',1000,5000,50)
Wait(2000)
Freq('p',0,0,0)

Another use of the buzzer is through fixing the frequency and changing the duty cycle. This is used to control energy levels, for example dimming lights and motor speed control. Note that motors and large lights draw a lot of current and need to be wired to the BrainPad through a special circuit. This will be detailed in other lessons. However, a small LED can be connected directly. This is an example that will activate the pin at 10% energy level.

Freq(0,1000,0,10)

Hardware vs Software Pin Control

Another use of PWM is to control servo motors. Servo motors need a pulse every 20ms (50Hz) and then the pulse width is used to determine the servo position where a typically 1ms pulse sets the servo at about 0 degrees and 2ms sets it at about 180 degrees. Can you guess what pulse width we need for 90 degrees? It is 1.5ms.

If PWM capable pin is needed to generate signal to control a servo then how come BrainPad boards can control servos on any pin? There are two ways to generating PWM (frequencies) on a pin. Some special pins can be controlled using hardware control through a special hardware that is built inside the chip. This is what Freq() uses. This is very accurate but is only available on special pins. Another way to control the pin is through Software control, which is not very accurate but can be done on any pin. AnalogOut(), Sound(), and Servo() are examples of functions/features that use software control. They are available on all pins but they are not very accurate. In contrast, Freq() offers much higher accuracy but it only works on specific pins.

The accuracy difference is invisible in most cases but can be slightly noticed in the case of servo control. The Servo() feature works on any pin as it is handled by software control but it only offers 20 steps though the whole 180 degree range. In this animated gif, the software controlled servo will jump in position, where the hardware controlled one move more smoothly. As you can tell, it is not too bad and works in most cases.

The needed 1ms to 2ms pulse can be generated by setting the duty cycle. We know the servo needs a pulse every 20ms (50Hz). Setting the duty cycle to 50% will get us a signal that is 10ms high and 10ms low. A 10% duty cycle will set the pin to be 2ms high and the rest is low. We know 2ms is what we need for servo to go to 180. For the 1ms needed to set the servo at 0 degree, we need a duty cycle of 5. But why 5? 5% of 20ms is 1ms!

Ok great! We now know we need duty cycle set between 5 and 10 (range 5) and this needs to be scaled to 0 to 180. The formula needed is duty=(5/180*deg)+5.

The d variable is used to hold the servo position and then Freq() is then set with the scaled value.

d=90
Freq(0,50,0,(5/180*d)+5)

While 1ms to 2ms is the standard, many hobby servos can have a different ranges. Some are 0.5ms to 2.5ms, that is a ratio of 2.5% to 12.5% off 20ms (range is 10ms).

d=90
Freq(0,50,0,(10/180*d)+2.5)

If not sure about your servo’s range, use the second formula and then try different values to determine the min and max possible degree.


BrainStorm

Compromises are made everywhere in computer systems. Speed vs accuracy, color depth vs memory utilization. And in this case, making a feature available on all pins with less accuracy vs being very accurate on a specific pin. Can you think of other similar cases? What about slow diesel trucks that can move very heavy weight vs a sports car that can accelerate 0 to 100 in 3 seconds?

Content Licensing
Newsletter

Twitter Feed
Hot off the press!
January 30, 2024
December 14, 2023
December 11, 2023
December 8, 2023
November 24, 2023