Run the code in the chapter07/transistor_test.py file, and the red LED will turn on then off, then fade in and out. Once you have confirmed that your circuit works, let's continue and look at the code:
# ...truncated ...
pi.set_PWM_range(GPIO_PIN, 100) # (1)
try:
pi.write(GPIO_PIN, pigpio.HIGH) # On. # (2)
print("On")
sleep(2)
pi.write(GPIO_PIN, pigpio.LOW) # Off.
print("Off")
sleep(2)
We are using PWM in this example. In line (1), we are telling PiGPIO that, for GPIO 21 (GPIO_PIN = 21), we want its duty cycle to be constrained to the value range 0 to 100 (rather than the default 0 to 255). This is an example of how we can change the granularity of duty cycle values in PiGPIO. We're using 0 to 100 just to make reporting easier because it maps into 0% to 100% for terminal output.Â
Next, in line (2), we simply turn the GPIO on and off for a duration to test the transistor...