Kirk Fraser
Posts: 50
Joined: Thu Feb 13, 2014 6:52 am

LED Dim/Brite Advice Please!

Fri Dec 28, 2018 3:15 am

Here is code I got from YouTube to brighten and dim an LED on pin 7. Why doesn't it work when the 7 numbers are changed to 17 or other GPIO numbers? How can I get it to work on any GPIO pin? Thank you.

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(7, GPIO.OUT)
p = GPIO.PWM(7,50)
p.start(0)
try:
    while True:
        for i in range(100):
                p.ChangeDutyCycle(i)
                time.sleep(0.02)
        for i in range(100):
                p.ChangeDutyCycle(100-i)
                time.sleep(0.02)
except KeyboardInterrupt:
    pass
p.stop()

GPIO.cleanup()
Last edited by Kirk Fraser on Fri Dec 28, 2018 5:33 am, edited 2 times in total.

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: LED Dim/Brite Advice Please!

Fri Dec 28, 2018 4:20 am

Hello, you have set GPIO numbering to "GPIO.BOARD". On Pin 17 on the board, there is a 3.3V connection located.
Think there is a warning message printed, but as you set warning to false it is suppressed.
See https://pinout.xyz/# for the usage of the pins.
There is another level of complexity in pin usage: a few pins are used by the system (usually) and then can't be used by user gpio functions. An example are the serial lines pin8, 10 aka BCM14,15 which are attached to serial console. When you want to use these, you should switch off usage of serial in raspi-config.
Or the pins BCM0, BCM1 which are used to find whether a "hat" is attached to the pi.

Kirk Fraser
Posts: 50
Joined: Thu Feb 13, 2014 6:52 am

Re: LED Dim/Brite Advice Please!

Fri Dec 28, 2018 6:51 am

Thank you. Although I don't fully understand, is it correct to assume all the pins with () tags on https://pinout.xyz/ are used by RPi thus leaving only 10 true GPIO pins?

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: LED Dim/Brite Advice Please!

Fri Dec 28, 2018 9:35 am

Those secondary functions in brackets () are not always active by default. The TX,RX are active by default, but can be disabled in raspi-config. Think the only fixed function pins are BCM0,1 which are generally reserved for i2c communication with an EEPROM.
One example for general GPIO are the BCM7..11 where the SPI-functions can be activated in raspi-config. There are plenty of descriptions on the net available.

Return to “Python”