Control of LEDs

Forums:

Hi there,

I'm planning to use an UltraBorg as it's easy to implement ultrasonic sensors for an RPI project. Could I also use the servo outputs to control the three channels of an RGB LED? The LEDs are Common Cathode and I plan to use just the signal pin from each o/p.

Will I need to change the PWM settings & if so to what?

Thanks for your help.
:)

piborg's picture

Yes, it would be possible to control the three channels of this LED from three of the servo outputs.

In normal use the PWM is limited to a range suitable for servos, so you will have to use slightly different calls to get the full PWM range from each channel like this:

# Import library functions we need 
import UltraBorg
import time

# PWM settings
PWM_MINIMUM = 0x0000			# Lowest allowed level (16 bit), 0 is a 0% PWM
PWM_MAXIMUM = 0xFFFF			# Highest allowed level (16 bit), 0xFFFF is a 100% PWM

# Start the UltraBorg
global UB
UB = UltraBorg.UltraBorg()      # Create a new UltraBorg object
UB.Init()                       # Set the board up (checks the board is connected)

# Function to set the LED colour using 0 to 1 for each channel
def SetLed(r, g, b):
	# Work out the PWM levels
	pwmR = (r * (PWM_MAXIMUM - PWM_MINIMUM)) + PWM_MINIMUM
	pwmG = (g * (PWM_MAXIMUM - PWM_MINIMUM)) + PWM_MINIMUM
	pwmB = (b * (PWM_MAXIMUM - PWM_MINIMUM)) + PWM_MINIMUM
	# Set the red level on servo #1
	UB.CalibrateServoPosition1(int(pwmR))
	# Set the green level on servo #2
	UB.CalibrateServoPosition2(int(pwmG))
	# Set the blue level on servo #3
	UB.CalibrateServoPosition3(int(pwmB))

# Quick example of pulsing the LED in white
while True:
	# 0 to 100 %
	for i in range(101):
		power = i / 100.0
		SetLed(power, power, power)
		time.sleep(0.1)
	# 100 to 0 %
	for i in range(100, -1, -1):
		power = i / 100.0
		SetLed(power, power, power)
		time.sleep(0.1)

The board already has 510 Ω series resistors between the 5V supply and the servo signal outputs, so the maximum current output from each channel is 9.8 mA. If the LED channels are rated for a lower current than this you may want to add additional series resistors to drop the maximum current. If the LED channels are rated for higher currents then it may still work, but the LED will not be able to reach full brightness.

That's great and many thanks.

Each channel of the LED has a current of 20mA and a forward voltage of 3.0..3.2V and I was planning to use 8 arranged like a Lego brick as this project is for children! 8 LEDs together would be 160mA and a 15ohmn resistor would be needed to drop the voltage to 3V... might need some sort of a driver circuit me thinks?

Or, just looked at the board, are these the 4 resitors alongside the servo connectors? If I was to power the board separately (not from the RPI), could I then replace the resistors with links? At my risk of course!

Thanks, :)

piborg's picture

Those four resistors next to each servo connector are the ones in series with the 5V, you could carefully replace them with a more appropriate value or just bridge across them to remove the resistance.

When powering the board separately remember to remove the "5V link" jumper in the centre of the UltraBorg to keep the two 5V power supplies isolated :)

I'll give it a go! :)

Subscribe to Comments for "Control of LEDs"