Micro:bit - Basic PWM
Written by Harry Fairhead   
Tuesday, 05 April 2022
Article Index
Micro:bit - Basic PWM
PWM Functions
How Fast?

How Fast Can You Modulate?

In most cases the whole point is to vary the duty cycle or the period of the pulse train, for reasons that will be discussed later. This means that the next question is how fast can you change the characteristic of a PWM line? In other words, how fast can you change the duty cycle?

There is no easy way to give an exact answer and in most applications an exact answer isn't of much value. The reason is that for a PWM signal to convey information it generally has to deliver a number of complete cycles with a given duty cycle because of the way pulses are often averaged in applications.

We also have another problem - synchronization. There is no way to swap from one duty cycle to another exactly when a complete duty cycle has just finished. This means that there is going to be a glitch when you switch from one duty cycle to another. Of course, this glitch becomes less important as you slow the rate of duty cycle change and exactly what is usable depends on the application. For example if you try changing the duty cycle about every 100ms and the pulse width is 50ms then you are going to see roughly two to three pulses per duty cycle:

#include "MicroBit.h"
MicroBit uBit;
int main()
{
    volatile int i;
    uBit.init();
    uBit.io.P0.setAnalogValue(200);
    uBit.io.P0.setAnalogPeriodUs(50);
    for (;;)
    {
        for (i = 1; i < 100; i++){};
        uBit.io.P0.setAnalogValue(200);
        for (i = 1; i < 100; i++){};
        uBit.io.P0.setAnalogValue(800);
    }
}


This results in a fairly irregular pulse pattern:

pwm3

The timing of the change and the time it takes to make the change cause the glitches between duty cycles.


If you change the busy waits to n=1000 and generate duty cycle changes every millisecond or so then the pattern looks a lot better because you are going to see 19 to 20 pulses before each change, but the glitches are still there:

pwm4

Is there anything that can be done about the glitching? Yes and no. You can't do anything about the 40ms or so it takes to change from one duty cycle to another, but you can synchronize when it happens to the timer using the wait_us function. If you swap the busy waits for the 100µs delays to:

wait_us(100);

with the help of:

#define wait_us system_timer_wait_us

for the V2, you will find that you do consistently get three pulses of each duty cycle, but separated by a longer pulse.

What all this is really about is trying to lower your expectation of how sophisticated you can be in using PWM on the micro:bit. The fastest PWM repetition rate that you can use with the V1 is about 30ms and to minimize the glitches you need to leave the duty cycle stable for 10 to 20 pulses, i.e. about 200-600ms. In many applications this is very acceptable, but don't expect to use PWM to send coded data, and using it for waveform synthesis as a D to A converter is limited to around 4kHz.

In Book but not in this extract

  • Uses of PWM – Digital to Analog
  • Music
  • Controlling An LED
  • Changing the LED brightness
  • Controlling a Servo
  • Non-Inverting Drivers
  • What Else Can You Use PWM For?

 

Summary

  • PWM, Pulse Width Modulation, has a fixed repetition rate but a variable duty cycle, i.e. the amount of time the signal is high or low changes.

  • PWM can be generated by software simply by changing the state of a GPIO line correctly, but it can also be generated in hardware so relieving the processor of some work.

  • As well as being a way of signaling, PWM can also be used to vary the amount of power or voltage transferred. The higher the duty cycle, the more power/voltage.

  • Hardware PWM can generate high speed pulses, but how quickly you can change the duty cycle is still software-limited.

  • PWM can be used to implement a D to A converter simply by varying the duty cycle.

  • By varying the output of the D to A you can create music.

  • In the same way, by varying the duty cycle, you can dim an LED. As the brightness of an LED is not linear with applied voltage, you have to modify the output using a cubic law to get linear changes in brightness.

  • Servo motors set their position in response to the duty cycle of a PWM signal.

 

Micro:bit IoT In C Second Edition

By Harry Fairhead

microbite2360

Buy from Amazon.

Contents

  1. The Micro:bit Family
  2. Getting Started With C/C++ ***NEW!
  3. First Steps With The GPIO
  4. Fast Memory-Mapped GPIO
  5. Pulse Width Modulation, Servos and More
    Extract: Micro:bit - Basic PWM  
  6. I2C Bus
  7. I2C Measuring Temperature And Humidity
  8. Creating A Custom Protocol
  9. DS18B20 1-Wire Temperature Sensor
  10. SPI Bus
  11. A‑to‑D With The SPI Bus
  12. Serial Connections
  13. Getting On WiFi
  14. The LED Display
  15. Radio Sounds
    Extract: Morse Transmitter
  16. Appendix I The Mbed Online Compiler
  17. Appendix II Yotta And Offline Development

 <ASIN:1871962676>

<ASIN:B08XPRMK97>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Vint Cerf Awarded IEEE Medal of Honor
03/02/2023

Considered the highest honor in the field of Electrical Engineering, the IEEE Medal of Honor was inaugurated in 1917 and is awarded annually to a single recipient for exceptional contributions or [ ... ]



Conan C/C++ Package Manager 2.0 Now In Beta
17/01/2023

A pre-release beta of the Conan C/C++ Package Manager has been released on GitHub with new generators and a new graph model that provides better support for C and C++ binaries relationships, compilati [ ... ]


More News

picobook

 



 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:B08WTBNJ5Q>

<ASIN:B08NLX7773>



Last Updated ( Tuesday, 05 April 2022 )