So far so good! But did you notice one thing? We have been using Raspberry Pi as a switch—simply switching the various components on and off. But what if we need to vary the intensity of the LEDs that we have just programmed? Is it possible? The answer is no. But we can still get it done somehow!
Let's see how. Computers work in binary which means that they can represent either 0
or 1
. This is because of the fact that the primary computing unit in any system is based on a transistor which can either be on or off representing 0
or 1
. So, if we see this technically, computers are only capable of switching due to the binary architecture. However, there is a trick. This trick is called pulse width modulation (PWM).
Now, before I explain any of it in detail, let's go ahead plug in an LED on pin number 18
, then copy this code into Raspberry Pi and run it:
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18,GPIO.OUT) ...