How to do it...

  1. Create the ledtest.py script as follows:
#!/usr/bin/python3 
#ledtest.py 
import time 
import RPi.GPIO as GPIO 
# RGB LED module 
#HARDWARE SETUP 
# GPIO 
# 2[======XRG=B==]26[=======]40 
# 1[=============]25[=======]39 
# X=GND R=Red G=Green B=Blue  
#Setup Active States 
#Common Cathode RGB-LED (Cathode=Active Low) 
RGB_ENABLE = 1; RGB_DISABLE = 0 
 
#LED CONFIG - Set GPIO Ports 
RGB_RED = 16; RGB_GREEN = 18; RGB_BLUE = 22 
RGB = [RGB_RED,RGB_GREEN,RGB_BLUE] 
 
def led_setup(): 
  #Setup the wiring 
  GPIO.setmode(GPIO.BOARD) 
  #Setup Ports 
  for val in RGB: 
    GPIO.setup(val,GPIO.OUT) 
 
def main(): 
  led_setup() 
  for val in RGB: 
    GPIO.output(val,RGB_ENABLE) 
    print("LED ON") 
    time.sleep(5) 
    GPIO.output(val,RGB_DISABLE) 
    print("LED OFF") 
 
try: 
  main() 
finally: 
  GPIO.cleanup() 
  print("Closed Everything. END") 
#End
  1. The RPi.GPIO library will require sudo permissions to access the GPIO pin hardware, so you will need to run the script using the following command:
sudo python3 ledtest.py  

When you run the script, you should see the red, green, and blue parts of the LED (or each LED, if you're using separate ones) light up in turn. If not, double-check your wiring or confirm that the LED is working by temporarily connecting the red, green, or blue wire to the 3V3 pin (pin 1 of the GPIO header).

The sudo command is required for most hardware-related scripts because it isn't normal for users to directly control hardware at such a low level. For example, setting or clearing a control pin that is part of the SD card controller could corrupt data being written to it. Therefore, for security purposes, superuser permissions are required to stop programs from using hardware by accident (or with malicious intent).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.172.93