PWM

Exploration: PWM in Software – Part II

Posted in PWM, arduino, occam-pi on October 19th, 2009 by Radu – Be the first to comment

The last implementation proved to be very slow, so an obvious improvement was to eliminate the ticker; i.e. the dependence on the clock for synchronization. By removing the ticker process we ensure that PWM will run as fast as the virtual machine is. Here is the updated diagram:

PWM in Software (No Clock)

PWM in Software (No Clock)

When tested, this implementation runs quite a bit faster – the LED actually gives an impression of dimming. But upon plugging a speaker into the same pin we get a definite “buzzing” noice, when the desired effect is more like a nice sound. Hence, still not fast enough. It is therefore time to read the Arduino data sheet and find out how to do it in hardware.

Exploration: PWM in Software – Part I

Posted in PWM, arduino, occam-pi on October 19th, 2009 by Radu – Be the first to comment

In this post I will be referring to Pulse Width Modulation as PWM, which you can read about more here.

Since before I started working on this project I did not know anything about PWM, I did a bit of reading on-line and without getting into too much technical detail I started coding it as it first came to mind. The graph below is what I came up with.

PWM in Software

PWM in Software

The diagram abobe shows five parallel processes:

  • ticker controls the rate at which sine, and saw and control operate. It also dictates the rate at which control operates, although not necessary;
  • saw and sine are just two oscillating functions, the difference being that sine has a constant increment, while saw has its increment dependent on the level you want to do PWM; since they have different increment / decrement rates, they intersect at rates dependent on the level you pass to saw;
  • control reads the values from saw and sine and if they crossed sends a signal to switch;
  • switch upon receiving a signal flips the power on or off.

This implementation, even though very ugly, does what it is supposed to do, almost. By almost I mean it modulates the pulse, but the modulation is not fast enough to give an impression of dimming an LED. That is, the LED appears to just blink at a fast rate, but to actually achieve PWM as it is supposed to be, we would need a much faster virtual machine.