简体   繁体   中英

raspberry pi PWM on GPIO pins

I'm trying to change the brightness of a led (as simulator for a motor) by means of PWM on a GPIO pin of my raspberry pi (3B). But whenever i try to run my code, it gives the error

'RPi.GPIO.PWM' Object has no attribute 'changeDutyCycle'

However, this is proven wrong by both the documentation and this code that gets an iteratable list of all methods of an object . I copy-pasted the method to be sure to have no spelling errors, but it still doesn't work.

The code i use is:

import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT)
p = GPIO.PWM(3, 50)
p.start(5)
time.sleep(0.5)
p.changeDutyCycle(95)
time.sleep(0.5)

The code crashes on line 9 (The second last line). The code above is only the relevant piece of the code so the resource cleanup is omitted.

I'm wondering what the error is caused by. Is it something i'm doing wrong in python, or should I look for the cause in one of my Raspberry Pi configurations?

Edit 1

Traceback, as kindly requested by jojo:

Traceback (most recent call last):
File "~/Documents/test.py", line 9, in <module>
p.changeDutyCycle(95)
AttributeError: 'RPi.GPIO.PWM' object has no attribute 'changeDutyCycle'

original answer from comments:

The function uses capital c, so p.ChangeDutyCycle(95) should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM