picture home | pixelblog | qt_tools

omino code blog

We need code. Lots of code.
entries for category "wbl weird blinking lights"

This post follows an earlier post.

More Measurements!

After a tantalizing near miss, I had to back up a bit to first principles in determining how to harness the mighty force of a $4 solar garden light to power a PIC chip in the evenings. But I really do want this to work… the idea of repurposing cheap consumer products to power my own gadgetry is just too appealing to pass up. It’s perfect for this use: pretty lights that activate automatically at night.

The solution, by the way, was to place a 5v zener diode across the output from the garden light, to protect the PIC circuit from overvoltage.

But because I had so much fun using an “oscilloscope” to understand the problem, I am compelled to here include and interpret the observations which led up to that solution.

Oscilloscope

Ah, the very word evokes sweet theremin tones. Traditionally, an oscilloscope was useful for observing periodic events and waveforms. Luckily, I have access to a modern oscilloscope which is digital and quite small and acts like a voltage-meter attached to a strip recorder. It was useful to understand the voltage swings of the solar garden light on a timescale of 10 or 15 seconds.

Run 1: Solar Garden Light With No Load

First experiment was to understand how the voltage multiplier responded with no LED or other load across its output terminals.

  • A: When the sun is out, the output is a steady 1v. Probably just the battery straight through, somehow. 1v is not enough to conduct through the LED.
  • B: Covering the solar cell for just a moment activates the voltage multiplier, and it charges up to 17 volts! With no load, it just sits there, discharging very slowly.
  • C: I touch the output terminals together for a moment, and it drops to zero, and then back to its default 1v.

Run 2: Solar Garden Light Attached to PIC Blinker

I knew that the solar garden light could power the blinker without necessarily destroying it. At least not ever time. So I risked it, to better understand what was going on voltage wise when it was running.

  • A: When the solar cell is covered, the voltage momentarily spikes to 7 or 8v…
  • B: …then settles down to 3 or 4v, very spiky, as the LEDs do a flickering on/off pattern.
  • C: All the LEDs go off for a little while, and the output of the solar garden light climbs up to 10v! The PIC chip specifies an absolute maximum of 7, but seems somehow to survive this.
  • D: The next pattern in the program begins: slow throbbing lights. The voltage drops back down to a reasonable 3 or 4v.

Run 3: Using LEDs To Limit The Voltage

Normally the output of the solar garden light is attached straight to a white 3v LED, and the voltage measured across the LED is, necessarily, always 3v. I reason thus: No matter what the voltage multiplier is doing, the 3v LED that came with it never gets fried, and runs at about 20mA. So, I continue to reason, although the voltage multiplier swings high with no load, pulling it down to 3v still only dissipates 20mA or so. That is: we can pull the voltage down by any means we like without fear of excessive current. So, what about using a pair of 3v LEDs to keep the voltage below 6v? (The PIC is rated up to 5.5v, but hey.)

  • A: A spike only to 6v. Safe and sane! The two blue LEDs flash briefly, as they are using any voltage over 6v. (Sort-of.)
  • B: Flickering LEDs pull the voltage down to 3 or 4v.
  • C: All LEDs go off… and again the voltage only rises to 6v; the two blue LEDs glow dimly.
  • D: LEDs come back on, voltage again hovers around 3 or 4v.

And The Solution Is: A Zener Diode

It turns out that a “zener diode” is a kind of diode which has, instead of the typical 0.7v drop, a calibrated drop of some other value. You can pick any value you like from a catalog. So, by placing a 5v zener diode across the output terminals it will dissipate (maybe) a few mA, and keep the power to the PIC chip down to a reasonable 5v. I got a tiny one rated for a half watt since the solar garden light never puts out much current.

The astute viewer (more astute than I) may notice the diode’s cathode marking is next to my scribbled + marking. Zener diodes are in some literal sense wired up “backwards” compared to normal diodes. The voltage rating is their “reverse bias breakdown voltage”. Ok, whatever. At least they use a slightly different schematic symbol, with a little jag on the cathode line.

And thus, I believe that this successfully harnesses the power of the cheap solar garden light!

Future Work

Future work includes: Optimizing the software to draw less power to maximize running time, by using fewer LEDs at a time. Analyzing other models of solar garden light; apparently some of them present a high-frequency pulsing across the output, which is fine for an LED but not so nice for a microcontroller.

Thanks
Thanks to Kerry V and Aaron F for their guidance and knowledge of the new electricity.

1 comments
Robert Soelberg // Mon 2007.06.25 02:222:22 am

David,

Your passion for unlocking the problems big and small continues unabated. I met you when you were writing software drivers for a graphics card maker and writing Ankh: 64 Rooms.

Your talents were so great, I was able to write a second game of 64 rooms.

Glad to see your are doing well.

Robert

oh, i dont know. what do you think?


Item 1. PIC12F509 is a fun/funny little chip.

The PIC12F509 truly embodies all that is great about our current state of technological development. It’s an 8-pin package that can store a kiloword of code and has a handful of bytes of RAM. It has 5 I/O pins and one input-only pin, and power and ground. Needs no other components; has a built-in 4MHz oscillator.

Costs about $1.

I mean really, if nothing else, it’s a fine replacement for your default 555 square wave generator, right? It costs about the same, and doesn’t need any external resistor or capacitor.

I jest of course, reminiscing about saving my pocket money in the late 70’s to buy a few more 7404’s at Radio Shack on the way home from school. Today those 5 I/O pins are about blinking the LEDs.

Oh, and the PIC12F509 has some of the requisite quirks that make it a joy to use. We don’t want things to be too easy, we want arbitrary challenges to sweeten the success.

  • Of the 1k program space, the CALL instruction can only access the first and third quadrants… and the third only if you are very careful.
  • Of the 1k program space, the GOTO (jump) instruction can only access the first 512 words unless you are very careful.
  • The RAM is bank switched in the most delightful way.
  • There’s a timer, but no interrupts! Ha!
  • The call stack is two, count ’em, two levels deep.

So this all means you have to be… how to say… “clever” in your programming, especially for a realtime LED blinker. Count those cycles and loops, friends. A joy.


[stop][go][-][+]

Item 2. Powering the LEDs from a cheap solar garden light.

Making blinking toys is fun, but there’s always the question of how best to power them.

Today at Home Depot I picked up a box of 10 solar-powered rechargeable garden lights. Each one has a white LED and a single 600 mAH NiCd rechargeable battery. $39.95 for the whole box and you can’t beat that.

I’ve had some success with other lamps, hacking them to power EL wire. (EL wire draws a lot of power, and they only last the first hour of evening, alas.) We got this latest box for our burning man camp, but also I had to try something.

Inside, the tiny circuit board apparently uses some kind of voltage multiplier circuit. One side has a diode and a capacitor or two, the other has a half dozen minuscule surface mount parts. A quick test confirms that the voltage across the LED is 3v. The PIC12F509 runs fine on 2v to 5v. Splendid!

Removing the LED and attaching two wires in its place seems like just the thing…


note — 3v label incorrect, see edit below

An LED usually draws about 20mA. The NiCd battery is rated 600mAH at 1.2v, which would be 240mAH at 3v, or 12 hours at 20mA on a full charge. Perfect for planet Earth: the average length of night turns out to be 12 hours. (Estimates based on optimistic assumptions: perfect charge, 100% efficient voltage-multiplier. Of course, you probably bought your solar garden light in the summer, when the nights are short. And in the winter, who’s checking?)

A PIC12F509 itself draws less than 1mA at 4Mhz, 3v. That means that, assuming our blinker averages less than 1 LED on at a time — a pretty safe average, if the blinking pattern uses partial-brightness — then the blinker should run all night. Joy.

Further experiments to follow.



Item 3. Edit. Later that same day. Whoops. Caution.

Ah. Well. Experiments followed, with cautionary results.

1. Measurement across output terminals under no-load conditions.
Sensor in sunlight (“lamp off”): 1.3v
Sensor in darkness (“lamp on”): 20.75v !

2. Measurement across output terminals while blinker operating: Varying from 3.5v to 4.5v, or so.

3. During the course of connecting/disconnecting the blinker from the output terminals, somehow I fried the PIC12F509. Replacing it with a fresh one worked fine. (They’re $1 each… I have a handful of these fine little computers.)

Hypotheses. The voltage multiplier is some kind of free-running unregulated bugger. With no load the voltage can climb pretty high. The device is designed for a particular load (a 3v 20mA white LED). In normal operation the device starts pumping its capacitor while there’s a load. I probably attached the blinker while the storage capacitor was peaked at 20v, letting it run through the load suddenly, and that fried the PIC chip.

Speculation. To avoid sending spikes through the replacement load, don’t connect it while the output terminals are live. Do it in the sunlight, or remove the battery first. And touch the leads together to discharge it. It may be prudent to program the blinker patterns such that there’s some LED load at all times. It may be prudent to seek more expert advice about voltage regulation and maintaining a load in some electricity-savvy manner. I’m just a software guy.

More to come.

4 comments
Yong // Mon 2007.06.18 09:099:09 am

David, you are a true geek. Thanks for sharing.

Kero Kero Keroppi // Mon 2007.06.18 17:495:49 pm

Good and honorable stuff. Inspired by your effort, I’ve started writing up my own current project. Have look!

http://www.antfarm.org/blog/aaronf/

David Van Brink // Mon 2007.06.18 21:049:04 pm

Yay Aaron!
(And looks v. groovy so far.)

Kerry Veenstra // Mon 2007.06.18 22:3910:39 pm

Yes, I’ve seen that unregulated-supply thing, too. I have an old weather satellite receiver (which is sort of unnecessary now with the WWW). It wants 12 VDC. If you buy a Radio Shack 12V power supply, it will drive the receiver with about 16 V! If you buy a Radio Shack *Regulated* 12 VDC power supply, you’ll pay twice as much, and the receiver will be driven with 12 V, and one of channels will be not be fried.

oh, i dont know. what do you think?


David Van Brink // Thu 2007.06.7 09:58 // {wbl weird blinking lights}

MPLAB under Virtual PC and USB Serial

Keywords: MPLAB IDE Virtual PC USB Serial Cable Mac OS X PowerBook G4

Tedious “Human Interest” Introduction

In 1999, I purchased a “PicStart Plus” kit from MicroChip. This is a full development kit for their line of single-chip microcontrollers. Really quite amazing! Came with MPLAB, a Windows-based development tool suite (Windows 3.x era stuff, actually), and a serial-based programmer. They even threw in an 18-pin PIC16F84 chip. Just great stuff. With a protoboard and a few wires, you can write some assembly code and get the LEDs to count in binary after an hour’s play. Good clean fun.

So just recently, here in the year 2007, I’ve decided to return for a bit of LED blinking. Broke out the old programmer, and uncrated the old Windows 98 Vaio Ultralight with its studly 64MB and 233 MHz… and it worked… but oh, it was painful. Everything ran so slowly, and moving files on my network was… not transparent. I needed to upgrade to the present.

I do most of my tinkering on Mac OS, and to retain transparency with my revision control and network and such decided to pursue the Virtual PC route, since MPLAB has been updated quite a bit but is still Windows only.

Ran into a few snags. But it can be done!

How to Run MPLAB on a Macintosh

I shall here document the steps I took to get the latest version of MPLAB running on a 12″ PowerBook G4, under Virtual PC 7.0.2, using a USB Serial Cable.

On the Microchip forums and elsewhere there seems to be a fair amount of doubt regarding both Virtual PC and USB Serial cables in general. I assure you: it can be done!

Ingredients:

  • PowerBook G4 with Mac OS X 10.3 or later
  • Virtual PC 7.0.2 with Windows XP Home (about $200 from MacMall)
  • Any name or noname USB to Serial “cable” (they’re all based on the same Prolific 2303 chip) (about $15 from MacMall)
  • PicStart Plus programmer ($199 from DigiKey or Microchip)
  • PicStart Plus firmware upgrade (if you have an ancient 1999 version like I do; a new one will be up-to-date) ($30 from DigiKey or Microchip)

Step 1: Install Virtual PC & Windows XP
This was pretty annoying: the product activation codes were misprinted on the CD package! (I was home sick after surgery trying to do this install, and fortunately my smart girlfriend said, Try the other code first, instead. Wow. Doing anything with Windows really requires your full faculties.)

Other than that, no problem. Actually, it’s quite technically impressive, this virtual PC thing.

Step 2: Install USB Serial Drivers
You know, you can just plug any ol’ thing into a Mac and it works. But if you so much as glance at the USB port under Windows, you’d better be ready to click Next a lot.

Fire up Virtual Windows. Plug in the serial cable. New Hardware Discovered! says Windows, and “Show Me The Driver!” (It won’t find it by itself.) The “driver” disk that came with the USB-serial cable is a 3″ minidisc which cannot be used on the PowerBook’s slot loader. So I mounted it on my other Mac, used AFP to get it to show up on the PowerBook, and mapped it to drive M:\ under Virtual Windows. (Like I said, pretty slick virtuality, really.)

Driver installed fine. Under Windows System Properties/Device Manager, you can see the serial port like so:

I don’t know if this matters, but I double-clicked in the Device Manager and set the port to 57600 baud, 8 bits, 1 stop, no parity, no flow control.

Step 3: Install and Run MPLAB

Download the latest MPLAB from the Microchip web site. I got MPLAB 7.6. Under the menu Programmer:Settings… set it to COM3. The PicStart Plus wouldn’t work until I installed the new 40-pin upgrade — if your PicStart is newer than my 1999 edition, you might not have to do that. And after that, things worked great!

Conclusion

Ignore the naysayers, Serial USB and Virtual PC work just fine with MPLAB. On this 1.5Ghz PowerBook G4 performance is completely acceptable. Faster than the old Vaio.

oh, i dont know. what do you think?


David Van Brink // Sat 2007.05.26 22:49 // {applet code wbl weird blinking lights}

The 1-bit DAC

This applet says it all… but there’s an explanation below, and even a bit of code.

Why? To blink the LED, of course.
If you’re like me you’ve often needed to use a single digital output pin on some chip or another to express an analog value. That’s because, if you’re like me, you’re a) too cheap to buy a DAC, b) too cheap AND lazy to rig one up out of multiple pins and resistors, and/or c) too clever by half.

Perhaps you want to fade an LED in and out from an FPGA or a PIC chip. Or you want to create NTSC-compatible video out of a single FPGA pin.

In general, you can just flick a pin high and low very fast to “simulate” some value between zero and one. Or, to be vaguely more specific, you flick the pin high and low much faster than the sort of signal you’re producing, and some other part of the system — your eyes or ears, the frequency response of the audio amplifier, or the RC filter you dutifully implemented — filters out those high frequencies.

Pulse Width Modulation, a Perennial Favorite
A typical approach is to use a “pulse width modulation”, in which you choose some basic sample-width, and raise your pin for a portion of the sample corresponding to the desired value. Suppose our sample-width is 8 clocks. We can represent the values between off and on with 8 intermediate values, with the following bitstreams:

Pulse Width Modulation
0.000 : 00000000 00000000 00000000...
0.125 : 10000000 10000000 10000000...
0.250 : 11000000 11000000 11000000...
0.375 : 11100000 11100000 11100000...
0.500 : 11110000 11110000 11110000...
0.625 : 11111000 11111000 11111000...
0.750 : 11111100 11111100 11111100...
0.875 : 11111110 11111110 11111110...
1.000 : 11111111 11111111 11111111...

The 1-bit DAC, a L337 Sekrit
A less-well-known approach is the so-called 1-bit DAC. (Also known as “sigma-delta modulation” and “pulse density modulation”.) It works by flickering the output pin as fast as possible to get, overall, the density of 1’s corresponding to the dac value. Here’s the bit patterns produced by a 1-bit DAC.

1-bit DAC
0.000 : 00000000 00000000 00000000...
0.125 : 00000001 00000001 00000001...
0.250 : 00010001 00010001 00010001...
0.375 : 00100101 00100101 00100101...
0.500 : 01010101 01010101 01010101...
0.625 : 01011011 01011011 01011011...
0.750 : 01110111 01110111 01110111...
0.875 : 01111111 01111111 01111111...
1.000 : 11111111 11111111 11111111...

The big difference is that the pin flickers faster. Why is this good? Well, in the case of, say, modulating the brightness of an LED it means the LED appears to flicker less!

Let’s show how it’s implemented — oh it’s so trivial! — and then talk a little bit more about why this technique is in some practical ways “better” than PWM.

How To Implement A 1-bit DAC

int dacRunningSum = 0;
int dacValue; // values are from 0 = off, to 8 = full on.
int getNextPinOutput() {
   int pinOutput = 0;
   dacRunningSum += dacValue;
   if(dacRunningSum >= 8) {
      pinOutput = 1;
      dacRunningSum -= 8;
   }
   return pinOutput;
}

Do you see how this can work? If your dacValue is 4, then exactly every other time through the running sum will exceed 8. And if it’s 5, a little more often; 3, a little less.

You can also see how easily this translates to checking the carry-bit in assembly language, or summing to a wider register in Verilog. I’ve used 8 levels in the examples, but 256 or even higher is a more common range.

Why It’s Better
For LED’s, which have essentially instant-on and instant-off times, flickering faster is just plain better. But why? It’s all about the frequency artifacts. The higher the frequency artifacts are, the further they are from the frequencies you’re actually interested in (such as the pulsing of an LED or an audible sine wave).

The applet above shows the high-frequency distributions for different bit patterns. The total amount of noise is about the same for both methods, but the distribution of noise favors higher frequences for the 1-bit DAC.

Applet Source Code
And, as customary, the source code to the Applet. I found an FFT on the web and used it as the basis for the frequency amplitudes display. (See frequencyAmplitudes() in Fft.java below.)

1 comments
Douglas // Sun 2007.06.10 07:517:51 am

Back in the early 90s when DACs were still expensive and you were lucky to get a CD player with an 8-bit DAC, Techniqs came out with their 1-bit DAC. I knew the basics of how it worked but your explanation fills in a lot of gaps. Thanks! Oh yeah, the CD player sounded great.

oh, i dont know. what do you think?



(c) 2003-2011 omino.com / contact poly@omino.com