I was curious as to whether I could use the micro:bit to generate PWM [pulse-width modulation] signals. At first I thought

I'd have to find out how to control the processor's internal peripherals but it turned out that there was no need. Even

the block language has instructions to do PWM on the output pins, though it's disguised by calling it an 'analog write'



Here's a very simple program to set P0 to do PWM.





It works too. Here's what the oscilloscope sees as the waveform.





First problem, though, is that I asked for 256 microseconds as the period and it has given me 20mS. I'm obviously not too hot at this coding stuff.


Here's the single interval that the 'analog write' asks for in greater detail:






This is 23uS, which is about an 870th of the period. I suspect there's something here, too, that I'm not understanding.


Finally, here's the single interval with the oscilloscope's persistance set to infinity so that it accumulates multiple

pulses all overlaying each other.




You can immediately see why that's always a good test to do with any pulse generation.


Here the single pulse is most often 23uS, but just occasionally it extends to between 100uS and 120uS. That's very

characteristic of a software interrupt doing what interrupts do - interrupting.