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.
Related
I am trying to read the voltage from one of the analog input ports and my code is throwing an error. The error happens when calling ADC.setup() (Adafruit_BBIO). The error is as follows:
Traceback (most recent call last):
File "print_voltage", line 3, in
ADC.setup()
RuntimeError: Unable to set up ADC system. Possible causes are:
- A cape with a conflicting pin mapping is loaded
- A device tree object is loaded that uses the same name for a fragment: helper
I have tried reinstalling Adafruit_BBIO as mentioned here: Error using python Adafruit_BBIO GPIO and ADC and BeagleBone Black in ubuntu 14.04
Here is my code for the program.
import Adafruit_BBIO.ADC as ADC
from time import sleep
ADC.setup()
while i in range(0,100):
scale = ADC.read("P9_40")
print(scale*1.8)
sleep(.1)
I could not find the suitable code for this BBB Python as many source codes would express more on Raspberry Pi and Arduino. I am using VMware and Ubuntu_18 to run the linux terminal in order to run my BBB.
This are my starting few lines code to try test on QMC5883 magnetometer that I'm trying to translate Arduino into Python version.
import Adafruit_GPIO.I2C as I2C
import math
QMC5883 = I2C.Device(0x0D, 1)
QMC5883.write8(0x0b,0x01)
However, the error keep appearing as following especially the writebyte and readbyte on the terminal BBB
root#beaglebone:~/user_python# python compass1.py
Traceback (most recent call last):
File "compass1.py", line 5, in <module>
QMC5883.write8(0x0b,0x01)
File "build/bdist.linux-armv7l/egg/Adafruit_GPIO/I2C.py", line 116, in write8
File "build/bdist.linux-armv7l/egg/Adafruit_PureIO/smbus.py", line 256, in write_byte_data
IOError: [Errno 110] Connection timed out]
The link that I'm following that to call the function is from this Adafruit_GPIO/I2C.py
Even using the smbus library the error still the same
import smbus
Anyone here know how to solve this Errno110 time out connection?
I am looking forward for anyone to guide me throughout for BBB Python getting on QMC5883 magnetometer to function.
Finally is one month plus and I found the solution for Beaglebone Black. Make some changes in the library code if possible to change in the library itself. Change the bus number from 1 to 2.
This GitHub link might help you https://github.com/RigacciOrg/py-qmc5883l to get your bearing degree (yaw rotation) in no time.
I am trying to build the air conditioner control system, which will allow me to control the ac's using web app.
So far I have done same thing with Arduino and it worked for this particular air conditioner. I could send and receive signal so I don't think there is any problems with hardware.
So far irsend is not giving me any errors, but signal is not sent, although I tried some LED testing codes using python and it worked.
Here is the /etc/modules:
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
lirc_dev
lirc_rpi gpio_out_pin=22
Here is the /etc/lirc/hardware.conf:
########################################################
# /etc/lirc/hardware.conf
#
# Arguments which will be used when launching lircd
#
LIRCD_ARGS=""
#
# Don't start lircmd even if there seems to be a good config file
# START_LIRCMD=false
#
# Don't start irexec, even if a good config file seems to exist.
# START_IREXEC=false
#
# Try to load appropriate kernel modules
LOAD_MODULES="true"
# Run "lircd --driver=help" for a list of supported drivers.
DRIVER="default"
# usually /dev/lirc0 is the correct setting for systems using udev
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"
# Default configuration files for your hardware if any
#changed for true
LIRCD_CONF="/etc/lirc/lircd.conf"
LIRCMD_CONF=""
########################################################
And /boot/config.txt:
# For more options and information see
# http://www.raspberrypi.org/documentation/configuration/config-txt.md
# Some settings may impact device functionality. See link above for details
# Uncomment this to enable the lirc-rpi module
dtoverlay=lirc-rpi, gpio_out_pin=22
# Additional overlays and parameters are documented /boot/overlays/README
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
Can anyone have any idea as in why is signal is not sent? The connection seems to be correct, 22 gpio, but for python code used 15 to check if its working:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.OUT)
for i in range(0,10):
GPIO.output(15,True)
time.sleep(0.5)
GPIO.output(15,False)
time.sleep(0.5)
print "Done"
GPIO.cleanup()
P.S. I tried to change 22 to 15, didn't work out. :(
Found the error, Raspberry Pi was not reading my /etc/modules so when I saved the same data in /etc/modules-load.d/lirc_rpi.conf, it worked:
lirc_dev
lirc_rpi gpio_in_pin=22 gpio_out_pin=23
i2c-dev
I am playing around with simple GPIO commands on Raspberry Pi (B version), using built-in Python 3.2 in conjunction with RPi.GPIO 0.5.11. According to the related wiki, the header pin numbering convention can be set via GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM) and its status can be read with GPIO.getmode().
The wiki says that the getmode should return either GPIO.BOARD, GPIO.BCM or GPIO.UNKNOWN, however, using print GPIO.getmode() I get 10 for BOARD and 11 for BCM.
Why do I get a different-than-wiki result ? Is the wiki outdated or should I render a different command type ?
You're getting back exactly what the wiki tells you to expect. Consider:
>>> import RPi.GPIO as GPIO
>>> GPIO.BOARD
10
>>> GPIO.BCM
11
That said, you should always use the named constants (GPIO.BCM and GPIO.BOARD), never the literal integer values.
I have been trying to use the Python GPIO PWM to control a set of LEDs connected to my RPi. When I run the Python script, I get the following error:
Traceback (most recent call last):
File "cycle.py", line 12, in <module>
r = GPIO.PWM(f, RED)
RuntimeError: No access to /dev/mem. Try running as root!
I have tried running the script as root (both with sudo and with actually logging in as root). All of the other GPIO functions work correctly and I have tried doing an update and uninstalling/reinstalling python-rpi.gpio through apt. Here is the code I have been running.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
RED = 11
f = 100
r = GPIO.PWM(RED, f) <== Where it crashes
r.start(0)
try:
while 1:
for dc in range(0, 101, 5):
r.ChangeDutyCycle(dc)
time.sleep(0.1)
for dc in range(100, -1, 5):
r.ChangeDutyCycle(dc)
time.sleep(0.1)
except:
pass
r.stop()
GPIO.cleanup()
It is based off of the example found here, but there could still be bugs. I have been struggling with this for quite a bit now so any help provided would be greatly appreciated. Thanks!
The problem is with the code above is that I forgot to set RED to at output before trying to use it. The error message did not help resolve this problem. Next time, I need to remember to setup PWM pins as outputs before trying to use them.