1

I am a beginner in Python and in the Raspberry field. I want to write a code to drive 3 leds with a PWM signal. The three leds must not emit at the same time and I want them to emit pulses trains. In order to do that, I triggered the driving signals for the three leds on the rising edge of a fourth PWM signal name "clock".

Firstly I write a code with the RPi library and it worked quite well. But for a frequency above 1 kHz, the Raspberry doesn't generate a clean signal. This is why I want to use the pigpio library.

Thus, I modified my previous code to adapt it to the pigpio library. The code is the following :

import pigpio
import time


pi = pigpio.pi()

clock=11

led_1=35

led_2=32

led_3=33



pi.set_mode(12, pigpio.OUTPUT)

pi.set_mode(clock, pigpio.INPUT)

pi.set_mode(led_1, pigpio.OUTPUT)

pi.set_mode(led_2, pigpio.OUTPUT)

pi.set_mode(led_3, pigpio.OUTPUT)


pi.set_PWM_frequency(clock, 10)

pi.set_PWM_dutycycle(clock, 128)

pi.set_PWM_frequency(led_1, 2000)

pi.set_PWM_frequency(led_2, 10)

pi.set_PWM_frequency(led_3, 10)



pi.wait_for_edge(clock)

pi.set_PWM_dutycycle(led_1, 0)

pi.set_PWM_dutycycle(led_2, 0)

pi.set_PWM_dutycycle(led_3, 0)


while(1) :

        pi.wait_for_edge(clock)
        pi.set_PWM_dutycycle(led_1, 25)
        pi.wait_for_edge(clock,FALLING_EDGE)
        pi.set_PWM_dutycycle(led_1, 0)

        pi.wait_for_edge(clock)
        pi.set_PWM_dutycycle(led_2, 25)
        pi.wait_for_edge(clock,FALLING_EDGE)
        pi.set_PWM_dutycycle(led_2, 0)


        pi.wait_for_edge(clock)
        pi.set_PWM_dutycycle(led_2, 25)
        pi.wait_for_edge(clock,FALLING_EDGE)
        pi.set_PWM_dutycycle(led_2, 0)


        pi.wait_for_edge(clock)
        pi.set_PWM_dutycycle(led_3, 25)
        pi.wait_for_edge(clock,FALLING_EDGE)
        pi.set_PWM_dutycycle(led_3, 0)

When I run the program with sudo, it returns to me this error message :

Traceback (most recent call last):

  File "led_train_2.3.py", line 16, in <module>
    pi.set_mode(led_1, pigpio.OUTPUT)

  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 1340, in set_mode

    return _u2i(_pigpio_command(self.sl, _PI_CMD_MODES, gpio, mode))
  File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 975, in _u2i
    raise error(error_text(v))

pigpio.error: 'no permission to update GPIO

I have read many topics about this type of error but I didn't find a solution which worked for me. Please, if you could explain it to me in the simplest way I would very grateful.

Lionel.

bumped to the homepage by Community yesterday

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

0

pigpio only accepts Broadcom GPIO numbers.

You will get an error message 'no permission to update GPIO' if you attempt to use a GPIO which is not present on the expansion header of your Pi.

On recent Pis with the 40 pin expansion header GPIO numbered 2-27 are available for use. GPIO 0 and 1 are also available for use in certain circumstances.

Also see https://pinout.xyz/

  • In addition to this: looks to me that @Lionel is using physical pin numbers. Gpiozero uses BCM/Broadcom (the 'GPIO' numbers in pinout) – Dirk May 7 at 16:35
0

with the library RPi, I chose to use the BCM GPIO numbers. Now I am using the pigpio library I thought I had to use the broadcom GPIO numbers but in fact the BCM GPIO numbers works and it solves my previous issue.

0

I eventually managed to generate my PWM trains triggered by a clock signal by combining the RPi and the pigpio library. My PWM trains are driving three differents leds which are thus emitting one after another.

In fact, I used the RPi library to generate the low frequency clock signal and to use the wait_for_edge function. Then I used the pigpio library to generate the higher frequency leds signal. See the code below :

import pigpio
import RPi.GPIO as GPIO
import time



GPIO.setmode(GPIO.BCM)

pi = pigpio.pi()

clock=17
led_1=19
led_2=12
led_3=13


GPIO.setup(18, GPIO.OUT)
GPIO.setup(clock, GPIO.IN)
GPIO.setup(led_1, GPIO.OUT)
GPIO.setup(led_2, GPIO.OUT)
GPIO.setup(led_3,GPIO.OUT)
GPIO.setup(14, GPIO.OUT)

clock_pwm = GPIO.PWM(18, 5)
clock_pwm.start(50)

pi.set_PWM_frequency(led_1,2000)
pi.set_PWM_frequency(led_2,2000)
pi.set_PWM_frequency(led_3,2000)
pi.set_PWM_frequency(14,2000)

GPIO.wait_for_edge(clock,GPIO.RISING)
pi.set_PWM_dutycycle(led_1,   0)
pi.set_PWM_dutycycle(led_2,   0)
pi.set_PWM_dutycycle(led_3,   0)
pi.set_PWM_dutycycle(14,   0)




while(1) :
    GPIO.wait_for_edge(clock,GPIO.RISING)
    pi.set_PWM_dutycycle(led_1,   25)
    GPIO.wait_for_edge(clock,GPIO.FALLING)
    pi.set_PWM_dutycycle(led_1,   0)

    GPIO.wait_for_edge(clock,GPIO.RISING)
    pi.set_PWM_dutycycle(led_2,   25)
    GPIO.wait_for_edge(clock,GPIO.FALLING)
    pi.set_PWM_dutycycle(led_2,   0)

    GPIO.wait_for_edge(clock,GPIO.RISING)
    pi.set_PWM_dutycycle(led_3,   25)
    GPIO.wait_for_edge(clock,GPIO.FALLING)
    pi.set_PWM_dutycycle(led_3,   0)

    GPIO.wait_for_edge(clock,GPIO.RISING)
    pi.set_PWM_dutycycle(14,   25)
    GPIO.wait_for_edge(clock,GPIO.FALLING)
    pi.set_PWM_dutycycle(14,   0)

I am still not fully satisfied because, when the rising edge is detected with the wait_for_edge function, the pwm signals which drive my leds, are starting with a random delay I would like them to start at a fixed delay. Any idea how I can do this ?

Thanks,

Lionel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.