[Tutor] Pi GPIO question was: Hi
Dennis Lee Bieber
wlfraed at ix.netcom.com
Sat Apr 9 16:14:51 EDT 2022
{purloining Mr. Gauld's renamed subject line to avoid too many split
threads}
On Sat, 9 Apr 2022 12:42:32 -0400, Chidinma Ufomadu <fufomadu894 at gmail.com>
declaimed the following:
>I need help with a project. I can't seem to be able to make the green led
>come on when the servo is rotating, the red led just stay on.
There isn't really any Python specific problem here... What device are
you using? Beaglebone Black, Raspberry-Pi (or one of the work-alikes --
Orange Pi, etc.), AdaFruit Metro running CircuitPython (the Metro are
Arduino compatible microcontrollers [no OS] with a CircuitPython
interpreter as the firmware, running scripts from flash). Note that
Adafruit provides a "blinka" library (which will run on Beagle and R-Pi) to
allow for a unified CircuitPython environment under those Linux boards.
Usenet comp.sys.raspberry-pi might be a better group for questions
involving GPIO and R-Pi's... Or AdaFruit's tutorials for CircuitPython.
from machine
>import Pin, PWM, ADC
What library is "machine" part of? A quick Google implies it is part of
MicroPython, which is not native to most (if not all) Linux-based
microcomputer boards.
>import utime
>Pot = ADC(0)# Pot at channel 0 for servo
Again -- what device? Raspberry-Pi boards don't have native ADC
(Beaglebone does have ADC); pretty much any microcontroller board (Arduino,
Metro, TIVA C) have native ADCs. One needs an external chip to obtain ADC
on an R-Pi. I'd have to study documentation some to determine if R-Pi has
hardware PWM -- I believe it is mostly software-based (tight loops counting
on/off durations).
>servo = PWM(Pin(0)) # GPIO pins
>servo.freq(50)
Are you actually controlling a servo?
>switch = (3, Pin.IN, Pin.PULL_UP) # PMW wave input
>
I'm not sure what you intend with this... If the /input/ really is a
PWM square wave it may be toggling too fast to really detect the on/off
intervals. It is more common for PWM inputs to be filtered via something
like a resistor/capacitor network to generate an average voltage, and then
use an ADC to read the voltage.
>L1 = Pin(1, Pin.OUT) #GPIO2 set as pin for RED LED
>L2 = Pin(2, Pin.OUT) #GPIO3 set as pin for Green LED
As Mr. Gauld mentions, those names "stink"... redLED/greenLED at least
are names that don't require one to look back in the code to determine what
is being controlled.
>
>ch1 = PWM(Pin(2)) # set ch1 for L1 to PWM using GP2
>ch1.freq(1000)
>ch2 = PWM(Pin(3)) # set ch2 for L2 to PWM using GP3
>ch2.freq(1000)
Here you seem to be replacing the prior "L1"/"L2" usage with connection
to the PWM output -- and again replacing "chX" with "channel_XXX" (where
XXX is RED or GREEN) would make the code easier to read. Or are they... You
set the LEDs on pins 1 and 2, but you are setting PWM on pins 2 and 3 --
but earlier you set pin 3 to be an input!
>while True: # Do forever
> duty = Pot.read_u16() # Copy pot Raw value to duty
> value = int(1350 + (Pot.read_u16() / 9.57))
> # adds the ruired offset to raw value of pot.
> servo.duty_u16(value) # final value needed a bit of weaking
So you are reading some (unknown) ADC device and using that to change
the duty-cycle of some unknown servo.
> print(value)
> utime.sleep(0.5)
> L1.value(1)
> L2.value(0)
What do you expect to see here? Those statements hard-code your RED LED
to ON, and the GREEN to OFF. I don't know how those conflict with your PWM
settings (which I'd presume defaulted to 50% duty cycle, and would have the
LEDs at half-brightness).
Confusingly, you have the ADC running a channel 0 (does that map to a
GPIO pin 0?), and you have the servo on a pin 0... Same pin? Knowing the
board being used, so one can look up the documentation of I/O pins is
greatly needed.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Tutor
mailing list