Reader small image

You're reading from  Practical Python Programming for IoT

Product typeBook
Published inNov 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838982461
Edition1st Edition
Languages
Right arrow

Controlling the MOSFET with Python

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...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Practical Python Programming for IoT
Published in: Nov 2020Publisher: PacktISBN-13: 9781838982461