Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to Python USING PWM
#1
Hello All,

I have wracked my brain and the web attempting to solve this pwm problem. I am new to Python. I am trying to just run a PWM pin using:

my beaglebone black (Rev. C) which has Debian GNU/Linux 8.4 Jessie pre-installed on it. I just purchased it very recently.

The beagle board also has Python 2.7.9 installed on it. I am attempting to follow along with this very nice online tutorial by TopTechBoy on youtube (https://www.youtube.com/watch?v=vAR8v96J3FQ). This video was done in 2015 so I think perhaps a lot may have changed in the world of python and beaglebones to the point where if you run code on current boards, it wont work. All lessons up too now have worked. Unfortunately, when I run his code:

import Adafruit_BBIO.PWM as PWM
myPWM="P8_13"
PWM.start(myPWM, 0, 1000)
for I in range(0,5):
DC=int(raw_input("What Duty Cycle Would You Like"))
PWM.set_duty_cycle(myPWM, DC)
PWM.stop(myPWM)
PWM.cleanup()


on my board (using VNC Viewer or Putty) it craps out with a runtime error:
Traceback (most recent call last):
File "PWM.py" line 7, in <module>
PWM.set_duty_cycle(myPWM, DC)
RuntimeError: You must start() the PWM channel first

I have started my PWM. Also, I feel it also has something to do with the fact that the original input() function call I was also not able to use. I have to use raw_input() and then typecast the returned information. Can someone assist me with this confusion?

Many thanks in advance to solve this thank you!
Reply
#2
You should use the code tags when you upload code it would help with debugging your problem. This code:

import Adafruit_BBIO.PWM as PWM
myPWM="P8_13"
PWM.start(myPWM, 0, 1000)
for I in range(0,5):
DC=int(raw_input("What Duty Cycle Would You Like"))
PWM.set_duty_cycle(myPWM, DC)
PWM.stop(myPWM)
PWM.cleanup()
as posted has a loop that is not doing anything. Is this what you meant:

import Adafruit_BBIO.PWM as PWM
myPWM="P8_13"
PWM.start(myPWM, 0, 1000)
for I in range(0,5):
  DC=int(raw_input("What Duty Cycle Would You Like"))
  PWM.set_duty_cycle(myPWM, DC)
  PWM.stop(myPWM)
  PWM.cleanup()
In any case I can't tell what line is line 7. You should post the code in its entirety so that we can see where you have started your PWM and if it was done correctly. It is possible that there is a difference between raw_input() and input() but without context (ie show the code) its hard to debug.
Reply
#3
I am not sure what more context you need, 1) that's all the code there is. 2) I am new to python so I do not have more code, I am just trying to put a voltmeter on my pwm output signal. That's it.

You are certainly right about the indentation. That is how I have it in my code. I tried to figure out code tags for a good 15 minutes. I am sure there is a tutorial on here somewhere, I just need to find it. I am very mindful of those things but I need an answer to my question while I figure out how to use this forum.

Sorry about the tags, here is the code in tags (I think):

import Adafruit_BBIO.PWM as PWM
myPWM="P8_13"
PWM.start(myPWM, 0, 1000)

for i in range(0,5):
     DC=int(raw_input("What Duty Cycle Would You Like"))
     PWM.set_duty_cycle(myPWM, DC)

PWM.stop(myPWM)
PWM.cleanup()
It craps out with a runtime error:

Traceback (most recent call last):
File "PWM.py" line 7, in <module>
PWM.set_duty_cycle(myPWM, DC)
RuntimeError: You must start() the PWM channel first
Reply
#4
PWM is almost certainly a class so you have to use an instance of that class. From the docs https://sourceforge.net/p/raspberry-gpio.../wiki/PWM/

Quote:p = GPIO.PWM(channel, frequency)
To start PWM:

p.start(dc) # where dc is the duty cycle (0.0 <= dc <= 100.0)
Reply
#5
woooee, thank you for the suggestion, I tried many variations of that code without any luck. I am not sure, but that code is either for an earlier version of python or for a raspberry pi.
Reply
#6
I have solved the issue for any future reader of this thread. the trick is to flash the beagle bone with the latest Linux (I went from Debian 8.4 to Debian 9.4). Make sure to remove the SD card before it boots back up or you will end up re-flashing the drive again and again. When you log back in, you will not be root. I don't know how limiting this change is, but it is not affecting us in Python at all. We are using Putty to login to the beagle main website. u will have all PWM and GPIO functions!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020