trying to use GP11 as PWM

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Post Reply
windy54
Posts: 14
Joined: Thu May 28, 2015 8:01 pm

trying to use GP11 as PWM

Post by windy54 » Tue Apr 19, 2016 5:58 pm

I am trying to use GP11 as a PWM output to control a servo motor.

Based on the pinout and alternate functions table I should use alternate function 3.

So I have tried the following in the REPL

Code: Select all

>>> from machine import Pin
>>> g=Pin('GP11',mode=Pin.OUT, alt=3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid argument(s) value
>>> 
If i delete the ",alt=3" statement then the REPL is happy

Could someone please tell me what I am doing wrong, I have updated to the latest firmware on the WiPY

thanks

Steve

windy54
Posts: 14
Joined: Thu May 28, 2015 8:01 pm

Re: trying to use GP11 as PWM -Fixed

Post by windy54 » Tue Apr 19, 2016 6:54 pm

I think I need my own forum to post my questions, because as soon as I post a question I manage to fix it!

g=Pin('GP11',mode=Pin.OUT, alt=3) should be g=Pin('GP11',mode=Pin.ALT,alt=3) obvious once you know


so here is some simple code I have used to drive my servo between the zero and 180 position.

Code: Select all

from machine import Timer
from machine import Pin
import time

# set GP11 as an output and specify alternate function 3 for PWM
g=Pin('GP11',mode=Pin.ALT, alt=3)

# now initialse Timer 3 as PWM and it is associated with GP11 automatically
tim = Timer(3, mode=Timer.PWM, width=16)
# now set frequency and duty cycle
zero =200
one80=1200
step=10
cycle=zero
while True:
    tim_b = tim.channel(Timer.B, freq=50, duty_cycle=cycle)
    cycle+=step
    if cycle > one80:
       step=-step
       cycle=one80
       print("180")
    elif cycle < zero:
       step=-step
       cycle=zero
       print("0")
    time.sleep_ms(100)
    
not very elegant but it works.

I used ftp to copy it the the flash folder, then logged in via a telnet and used exec file("servo.py") to run it.

My servo is jerky around the 90 deg position, I need to try another servo or put a scope on the signal to see what is going on.

But, progress, that is a led and now a servo I have controlled, stepper motor next!

steve

Post Reply