User manual

53
If the user has entered something else, the statement specified in
elseis executed. This section of a query is
always applied, if no other queries have delivered true results. In our case, the programme displays a
message on-screen.
As in previous experiments, the programme is terminated via
KeyboardInterrupt, that is, the user pressing
the key combination
[Ctrl]+[C]. The last line closes the GPIO ports used and thus switches off the LEDs.
6 Dimming the LED using pulse width modulation
LEDs are typical components of digital electronics to output signals. They can assume two different states, on
and off, 0 and 1 or
True und False. The same applies to those GPIO ports which are defined as digital
outputs. Theoretically dimming a LED seems to be impossible.
A little trick makes it possible to control the brightness of an LED on a digital GPIO port. If you let a LED flash
fast enough, then the human eye will not identify that as true flashing. The technique termed pulse width
modulation generates a pulsing signal that turns on and off at very short intervals. The voltage of the signal
remains always the same, only the ratio between level
False (0 V) and level True (+ 3.3V) is changed. The
duty cycle is the ratio of the length of the on-state to the total duration of a switching cycle.
Fig. 6.1: Left: Duty cycle 50 % – right: Duty cycle 20 %.
The smaller the duty ratio, the shorter is the lighting time of the LED within a switching cycle. Thus, the LED
seems darker than a permanently illuminated LED.
For the next experiment you will connect an LED via a series resistor to GPIO port 18.