1

Sorry I am a beginner to both Raspberry Pi and python. I am write a simple python program to use the pulse width modulator, Here is the code.

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(12,GPIO.OUT)
pwm=GPIO.PWM(12,50)
pwm.start(0)
while True:
        for i in range(50):
                pwm.ChangeDutyCycle(i)
                time.sleep(0.05)
        for i in range(50,0,-1):
                pwm.ChangeDutyCycle(i)
                time.sleep(0.05)

I connected the led between 12 and ground with the suitable resistor. But when I execute, I don't get any error but it wont work.

2 Answers2

1

The code seems to work fine on my Rpi-3. So we cannot help you if we can't see the circuit design. I can make a guess that you probably used the normal numbering to connect the led, but used the BCM numbering in your program. So either change it to board numbering by GPIO.setmode(GPIO.BOARD) or refer the below chart for correct numbers. GPIO pin chart

deepakchethan
  • 5,240
  • 1
  • 23
  • 33
1

I got the problem. I need to use the proper numbering. The reason for having two types of numbering is,

The GPIO.BOARD option specifies that you are referring to the pins by the number of the pin the the plug - i.e the numbers printed on the board (e.g. P1).

The GPIO.BCM option means that you are referring to the pins by the "Broadcom SOC channel" number.

I hope this helps anyone.