# Manual and Automatic Traffic Lights

Bonus Track.
Pulse Width Modulation: a brief explanation.

Suppose you want to have more and more analog outputs, but having only fixed digital inputs. You're in trouble!

Digital must be a well-defined value without any kind of uncertainty, typical of analog sizes; what does it means?
Math constant $\pi$ has infinite decimal numbers (analog way), but usually they are truncated to the first 2, getting 3,14 (digital way). This could be a good example: we want all decimals from only 2 (just ideally).

So think that we want to switch some LEDs on and off but gradually (uh! deja-vu!).
In this article Arduino reads the analog input from the pot, and transforms it to an equivalent digital value on a 0-1023 scale, loosing many potential decimals (that is resolution): after that these values are rescaled (map function) because Arduino's PWM output goes from 0 to 255 (lesser resolution).

And now... what do LEDs see? which voltage?
They see only 5 or 0 volts; nothing else. Remember that Arduino's digital output pins are able to switch only between LOW and HIGH logical values, 0 and 5 on Arduino.
To get the fade effect we modulate/change the time that digital output is on one of those 2 states.
Doing it at a good speed avoids to make LEDs switch off during off period, making us think that a continuous voltage is powering them.

In our example pot modulates its resistance, so wiper has a proportional voltage on itself; after last scaling we have a value that indicates time we want output HIGH, referring to a basic time or better to the output frequency: 0 means 0V and 255 means 5V, so we go from a basic time-period all LOW to an all HIGH one, with other 254 possible values in the middle.
Default PWM frequency on Arduino is nearly 490 Hz, which a time-period of 2 milliseconds corresponds to.

Time the Output is HIGH, referring to time-period, is called Duty-Cycle.

[PWM is a powerful technique; it can be used to obtain signals generators]