0

I have two pins with the same frequency (10Hz) and same duty cycle (50%) assigned to two different leds. How can I make the two leds blink alternatively? How can I put a delay of 50% in one of the pins?

Thanks in advance

import RPi.GPIO as GPIO
import time

L1 = 36
l2 = 37
frequency = 10

GPIO.setmode(GPIO.BOARD)
GPIO.setup(L1, GPIO.OUT)
GPIO.setup(L2, GPIO.OUT)

led1 = GPIO.PWM(L1, frequency)
led2 = GPIO.PWM(L2, frequency)
led1.start(50)
led2.start(50)

time.sleep(10)

led1.stop()
led2.stop()

GPIO.cleanup()
5
  • 1
    What you are asking for is not asynchronous blinking. The LEDs are still synchronous, just 180 degrees out of phase. – Elliot Alderson Jan 12 at 19:51
  • @ElliotAlderson: Correct, but a bit of a fine point methinks. – Seamus Jan 13 at 1:45
  • @Seamus You may think I am being pedantic, but engineers must be careful to communicate clearly. In some circumstances the difference between synchronous and asynchronous is the difference between easy and very difficult. – Elliot Alderson Jan 13 at 1:50
  • @ElliotAlderson: My point exactly - some circumstances. – Seamus Jan 13 at 1:57
  • @ElliotAlderson you're totally right. I'll edit that. Thanks for the repair. – Raquel Alves Jan 13 at 11:07
1

Using the lgpio Python module.

#!/usr/bin/env python
"""
http://abyz.me.uk/lg/py_lgpio.html
"""

import time
import lgpio as sbc

L1=16
L2=26

h = sbc.gpiochip_open(0)

sbc.gpio_claim_output(h, L1)
sbc.gpio_claim_output(h, L2)

sbc.tx_pwm(h, L1, 10, 50)
sbc.tx_pwm(h, L2, 10, 50, pulse_offset=50000)

time.sleep(20)

sbc.gpiochip_close(h)

enter image description here

6
  • I got an error saying 'module 'lgpio' has no attribute 'gpiochip_open'' ... Why does this happen? – Raquel Alves Jan 13 at 12:09
  • @RaquelAlves Impossible to say without knowing what you have done and seeing the code you are using. – joan Jan 13 at 12:17
  • I tried exactly the one you provided. The lgpio package was installed but it does not recognise any function. – Raquel Alves Jan 13 at 12:34
  • How did you install the package? What does pydoc lgpio reported in the FILE line? – joan Jan 13 at 12:42
  • I installed using pip2 install lgpio. It worked when I imported rgpio instead of lgpio. Don't know exactly why but it does. – Raquel Alves Jan 13 at 12:48
0
import time
import rgpio

L1=16
L2=26

sbc = rgpio.sbc()
h = sbc.gpiochip_open(0)

sbc.gpio_claim_output(h, L1)
sbc.gpio_claim_output(h, L2)

sbc.tx_pwm(h, L1, 10, 50)
sbc.tx_pwm(h, L2, 10, 50, pulse_offset=50000)

time.sleep(20)

sbc.gpiochip_close(h)
sbc.stop()

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.