Showing posts with label pwm. Show all posts
Showing posts with label pwm. Show all posts

Sunday, December 13, 2015

Christmas Lights and Their Control Signals

Merry Christmas.  I recently bought a set of LED Christmas lights for a display at work.  They were nothing special, just a cheap set of 4 colour lights that can flash and dim the lights in different patterns.  One thing that stood out was that the plug that connected the string of lights to the power supply only had two contacts.  So how is the controller able to control 4 different colours with only 2 contacts?

Lights
LED Party Lights
The first thing that came to mind was that there must be some sort of communication bus operating over the power rails controlling something like a WS2812 LED (I always come up with the most complicated solution first).  After watching the lights operate it became obvious what was happening.  The blue and green ones operate at the same time, and the orange and red ones are on at the same time as well.  So instead of controlling four different strands of lights the controller only needs to control two, red and orange, blue and green.  This can be easily done by connecting the red/orange and the blue/green LEDs with opposite polarity.  As the lights are LEDs and only conduct when voltage is applied with the correct polarity, applying a positive voltage to the connector, will light up half the lights, and applying a negative voltage will light up the other half.

I didn't have the time to test the set I bought for work, so I bought another smaller set of 100 lights from Bunnings for $10.

Lights
Lytworx LED Lights from Bunnings

Lights
Light String
The power supply is a small light isolated plug-pack that weighs only 51 grams.  I didn't want to open it as the case seems to be ultrasonically welded together and doing so would destroy it.  Given the weight, I'm almost certain that it's a switch-mode supply.  When a scope probe is placed near the case a 12.5 kHz switching signal can be detected with a 250 kHz ringing component.  The observed waveform and frequencies involved are commonly associated with switch-mode supplies.

Plug Pack
Power Supply
The supply has a button on the top that cycles the lights through different display modes.

Plug Pack
Button to change between modes is shown
Something to note that will become important later is that the output voltage of the supply is 31 volts.  How do you power 50 (half at a time of 100) LEDs from 31 volts?  The only other bit of interesting information here is the maximum power of 4 Watts.  That's similar to a USB charger.  Although you could power the lights from a 5 Volt 1 Amp source, without a boost converter the size of the cabling would have to increase to reduce losses.  I do however wonder what will happen if the USB power delivery specification becomes ubiquitous.  It can supply 20 Volts at up to 5 Amps.  Will Christmas lights of the future come with a power supply?  You might just get a string of LEDs with a small in-line button to switch between modes and a USB connector.  What's more likely is a string of lights with a USB connector and bluetooth connectivity (it's trending towards free) to control something like a string of WS2812 lights.  This means that the manufacturer doesn't have to worry about providing a power supply.

Specs
Power Supply Specs
There are 8 different light patterns.  Combination, In Waves, Sequential, Slo Glo, Chasing/Flash, Slow Fade, Twinkle/Flash, and Steady On.  For patterns that require all the lights to be on at once, the voltage alternates between positive and negative at a rate of about 140 Hz.  This means the lights aren't on all the time but the changes are too fast for the eye to notice.  To dim the lights you just need to reduce the fraction of the time that they're on.  If you're unsure of how this works read up on pulse width modulation to catch up.

Light Programs
Different Light Patterns
The string of lights is connected to the power supply via a simple barrel jack.

plug
2 Pin connector
To see if the lights you have use pulse width modulation, wave the lights around quickly.  You should see a dashed line of lights similar to the pattern below.  By comparing the length of the blue streaks to the spaces in between them you can see that the light is on for about 40% of the time.

Lights
Longish exposure showing PWM of LED lights
To see how the 31 Volt supply was connected to the lights I sat down and traced out the wiring.  It turns out that there are 10 strands of lights connected in series.  Each strand contains 10 lights in parallel.  This means each strand is supplied with 3.1 Volts.  This is an interesting point.  All the LEDs seem to be supplied with the same voltage.  The problem with this is that red/orange LEDs only need about 1.8 volts while blue/green ones need closer to 3 volts.  I'm not sure how the red ones are running on 3.1 Volts.  Do they have an integrated resistor or diode on board?  I have a feeling they may all be white LEDs with tinted dies.  When the lights are off and viewed at the correct angle the colour of the light is still visible.  That shouldn't happen.  LEDs don't emit a specific colour of light because they are that colour, the emit coloured light because of the properties and geometry of the semiconductors used.

Below is a small scale representation of the lights containing 2 strands of 4 lights.  The circuit on the left is the easiest way to understand the electrical layout.  This circuit can be rearranged to form a single long string of Christmas lights.  As seen in the image below, one wire runs the length of the string as a current return path, there are also 2 wires that almost run the whole length as well, however between strands there is only one wire.  In the lights I bought, this occurs every tenth light.

Schematic
How the Christmas lights are wired
There you go.  The designers have used a simple arrangement for maximum effect.  By taking advantage of the unidirectional nature of the LEDs and controlling them with PWM, they've created a simple enjoyable product.  To see how the electrical signals correlate with the light patterns, I taped the set of lights around my scope and probed the signal.  Enjoy!


For completeness I've included the labels on the light string as well.


Specs
Light specifications
.
Specs
Light Specifications
.

Thursday, October 8, 2015

Generating 2 Level Harmonic Elimination PWM with Python

A couple years ago I wrote some code to generate Harmonic Elimination PWM (HEPWM) waveforms in octave, which is kind of like Matlab.  Recently someone asked if I could rewrite it in Python so it'd be easier to use and understand (fair call, it was pretty archaic).  I jumped at the chance as I'd been meaning to do it but just didn't have a strong reason to.  While I was at it I added a feature that may make it more useful, but more on that later.  To understand everything I recommend reading my previous HEPWM posts.

I should take a step back and first explain what HEPWM waveforms are.  If you're reading this I'm going to assume that you know what a PWM wave is and that you know the square nature of periodic PWM waves generate unwanted harmonics.  HEPWM is an extension of that idea, but the timing of the transitions in the wave are calculated to cancel or reduce specified harmonics.  The even harmonics are eliminated by default by making the wave half wave symmetric.  You don't have to make the wave quarter wave symmetric, that's a bit of a maths trick, but by doing that you halve the number of equations and unknowns that you need to solve for.  (If I remember correctly, solving the entire set of equations allows you to control the phase of the harmonics as well.)  That sounds really complicated, but the image below may help.

The upper waveform is typical of what you'd see on the output of an unfiltered inverter.  In this case the period of the wave is 20 ms, which means the fundamental frequency is 50 Hz.  The HEPWM calculations however have allowed the creation of a wave with the 3rd, 5th, 7th, and 9th harmonics set to zero and the fundamental frequency set to 0.6 (It looks like 0.3, but you have to remember that the positive and negative frequencies add in this case)  As we're controlling 5 harmonics (1, 3, 5, 7, 9) there needs to be 5 transitions in each quarter of the waveform.

Waveform
3 Level HE-PWM
The HEPWM waveform is constructed by starting with the base waveform below.  Multiple versions of this waveform are alternatively added and subtracted with the alpha values increasing each time (Alpha values are always between 0 and T/4)

Waveform
Quarter Wave Symmetric Waveform
Start with the definition for the Fourier series below.  It can then be applied to the above waveform.

Equation
Fourier Series
If you do the math X[0] = 0, B[k]=0 and A[k]=0 for even positive values of k.  For positive odd values of k, A[k] is shown below.

Equation
Fourier Series Coefficient for QWS waveform
This is the magnitude of a particular harmonic, k, for a particular value of alpha.  The total value for each harmonic can be found by adding all the components from each value of alpha (remember alternating alpha values cause the sign to change).  It helps to note the relation between the period and angular frequency.  Combining this with the above equation a system of equations can be generated and solved.  I go into more detail in previous posts, but this is needed to give context to the next addition.

Equation
Angular Frequency
The 3 level waveform in the first image is easy to generate with a H bridge topology, but what if you want to accomplish a similar effect with a microcontroller with only 2 output levels?  It seems that you can just shift the first half of the waveform up by adding the waveform below, but by doing that you are adding the Fourier series of the below wave to the Fourier series of the waveform calculated above.  This will invalidate the magnitudes of the harmonics you are trying to target.  I reused some images from a previous post that used a period of T but it makes more sense to use a generalised period of 2 pi.

Waveform
Translated Square Wave
All is not lost though, calculating the Fourier series of translated square wave above gives the result below.  There's nothing we can do about the DC offset, but the summation term can be easily added to the  calculated harmonics of the original waveform because it only has a sine term.  All we need to do is tweak the code the generates the set of simultaneous equations to be solved and we can now move to a 2 level system.

Equation
Fourier series of Translated Square Wave
 For clarity, a two level system with the 3rd, 5th, 7th, and 9th harmonics nulled and the main harmonic set to 0.3 is shown.  The main difference between the spectrum of this waveform and the one above is the DC offset.  That's what coupling capacitors are for.

Waveform
2 Level HEPWM
Below is the python code that does this little trick.  When the twoLevels variable is set to 0, the code runs as usual and generates the transition points for a 3 level waveform.  When it's set to 1, the adjustment term that describes the Fourier series of the square wave is included in the calculations.  (What the hell was I thinking with that comment "the location of the switching location")

Python Code
Calculating the Harmonic Magnitude in Python

Caveats, Warnings and Errata

This code isn't meant to hold your hand.  It's a demonstration and will probably have errors.  If you find any let me know.  It also assumes that your waveforms have perfect 0 time transitions with no overshoot or similar real world effects.  I recommend using it as a starting point.  Calculate your transition points and do a localised search with those values while running simulations closer to the real world.

I did my best to document things, but it's hard to comment symbolic math equations.  I may look into that.

Get the code!