How to make blinking Led's on 16-channel servo hat

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
User avatar
Rich_art
 
Posts: 4
Joined: Mon Apr 11, 2022 1:47 pm

How to make blinking Led's on 16-channel servo hat

Post by Rich_art »

Hi Guys,

I am totally new here. So I hope to find some help with my project.
I have an adafruit 16-channel servo hat for raspberry pi with 2 connected servo's and 2 led's. My 2 servo's are working great. My next step is to make the 2 connected led's blink. Now there 'on' for a few milliseconds and then turned 'off'. Can any body tell me what is the best way (code) to make the led's blink?

My setup:
Raspberry pi 4b+
Adafruit 16-channel servo hat
2x SG90 micro servo's (on channel 0 and 1)
2 led's connected on 1 channel on the adafruit hat (on channel 2).

Thanks in advance!!

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: How to make blinking Led's on 16-channel servo hat

Post by mikeysklar »

You can used PWM to control the LEDs. This section of the guide will be quite helpful:

https://learn.adafruit.com/adafruit-16- ... -3013782-7

Code: Select all

# Increase brightness:
for i in range(0xffff):
    led_channel.duty_cycle = i

# Decrease brightness:
for i in range(0xffff, 0, -1):
    led_channel.duty_cycle = i

User avatar
Rich_art
 
Posts: 4
Joined: Mon Apr 11, 2022 1:47 pm

Re: How to make blinking Led's on 16-channel servo hat

Post by Rich_art »

Hi Mike, thanks for your quick reply.
I had read this part of the documentation but I do not understand the numbering between the parentheses.
So 'for i in range(0xfff, 0, -1):'
Where is the (0) and the (-1) for? How do I set the time of 'blinking' of the led's?

Thanks in advance!
Rich

User avatar
Rich_art
 
Posts: 4
Joined: Mon Apr 11, 2022 1:47 pm

Re: How to make blinking Led's on 16-channel servo hat

Post by Rich_art »

Ah... I did find a workaround. :-)
For now this is it for me:

Code: Select all

led_channel = hat.channel[2]
led_channel.duty_cycle = 0xffff
time.sleep(0.1)
led_channel.duty_cycle = 0
time.sleep(0.1)
led_channel.duty_cycle = 0xffff
time.sleep(0.1)
led_channel.duty_cycle = 0
time.sleep(0.1)
led_channel.duty_cycle = 0xffff
time.sleep(0.1)
led_channel.duty_cycle = 0
time.sleep(0.1)

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: How to make blinking Led's on 16-channel servo hat

Post by mikeysklar »

The loop code I had linked you to sets the duty_cycle in a range from 0-65535.

The first loop counts up from 0 (0xffff) or 65,535 the next loop goes back down to zero.

Python uses the 0 and -1 as loop controls to specific where to stop and how much to decrement by.

User avatar
Rich_art
 
Posts: 4
Joined: Mon Apr 11, 2022 1:47 pm

Re: How to make blinking Led's on 16-channel servo hat

Post by Rich_art »

After puzzling for a while I made it the following:

Code: Select all

for i in range(20):
    led_channel = hat.channels[2]
    led_channel.duty_cycle = 0xffff   #led on
    time.sleep(0.1)
    led_channel.duty_cycle = 0   #led off
    time.sleep(0.1)
Here I find some extra info: https://www.w3schools.com/python/python_for_loops.asp

Thanks!!

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: How to make blinking Led's on 16-channel servo hat

Post by mikeysklar »

You got it!

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”