Sunday, November 20, 2016

Custom Turn Signal Retrofit - Part 2

So I previously defined the basic parameters of this project, but let's throw out a block diagram to make things clearer.
That's pretty much it. You may have noticed that there is no "input power". And you'd be correct. The input supply voltage comes from either the headlights being turned on or from the turn signal flashing. They powered the incandescent bulb, a little microcontroller and LED should be no problem.

It also may have occurred to you then that the mcu would be unpowered between signal flashes. Again you'd be correct. Every time the signal flashes, the micro will have to start up and figure out what's going on, and do the right thing. But not a problem really, it'll be instantaneous as far as anyone will be able to tell.

Since an LED is already picked out, the next course of action would be to figure out how to drive it. We know we need to achieve at least two brightness levels, and we also know we that we don't know what those brightnesses will be. We're gonna need a PWM. The other requirement is constant brightness over our expected voltage input (roughly 12 to 14 volts). This can be handled in hardware using a constant current source.
A simple but handy circuit. I've always that this was a clever design. We don't know what V+ will be at any given time and we cant change R2, so how do we regulate the current thru D1? With Q1. Since the base-emitter junction of Q1 is effectively a diode, we know that its going to be a pretty consistent 0.7V (approximately... whatever it's actual value is, it won't really vary with V+).
Q1 will regulate how current goes into the base of Q2 to ensure this. Now we just need to figure out what current we want. Determining the resistor then becomes: $R = \frac{0.7V}{I_{desired}}$. I am aiming for about 0.5A.... giving: $R = \frac{0.7V}{500mA} = 1.4\Omega$. We'll just round to $1.5\Omega$.

Another alternative would have been to use the built in ADC of the microcontroller and try to compensate the PWM duty cycle. But what a hassle, and we're going to be short on I/O as it is. There would need to be look up tables and scaling done to improve accuracy and more work than is worth doing. This method may ultimately be less parts too.

I'll quit here for now. As a head's up, the microcontroller to be used will be a PIC10F322. This heavy hitter has 6 pins, 64 bytes of RAM, and 512 words of program memory. Yup, this little guy barely has the RAM to store this sentence. Barely. And the text of this post probably wouldn't even fit into it's program memory. But we don't need to get that much done.

Tuesday, September 6, 2016

Custom Turn Signal Retrofit - Part 1

After an interesting winter with a mix of snow and temps on either side of freezing, water made its way into some of the light housings of my car... namely the front turn signals. The bulb sockets corroded and rusted to the bulbs, making things messy and more or less unusable. Everything is all just kinda shorted together. Now I can order new housings for around $70, but why not mod the ones I have? I can't ruin them any further. Let's throw in an ultra-bright LED.

Now this actually presents an interesting set of challenges. You can't simply throw an LED and resistor in there and watch it work (well, you actually kinda could, but that's no fun). But here are a few items to consider:

- The vehicle voltage can vary, especially if the engine is running or not. The battery will read a pretty steady 12V, but may sag under the load of you headlights an other accessories. Running, the system voltage is often around 14V. If using a current limiting resistor, you'll see some different intensities depending the actual voltage present. This is actually already an issue with the standard bulbs, but one that can be addressed.

- The current bulb is a typical 1157. It has two filaments; one to act as a running light when the headlights are turned on, and the other for the actual signaling. I'm only adding one LED element for both purposes.

- The 1157 has a low output filament which produces about 38 lumens at around 8W and a high output filament that'll do 402 lumens at 27W (see table). My particular LED is capable of outputting 900 lumens. But I'm not what exact current is gonna be needed to produce the right amount of brightness for either function. I'd like to have a little adjustment.

- The original bulb uses 27W for the turn signal. The signal timing relies on the 2 point something amps being drawn to toggle a bi-metallic switch.

- I want this project to be fun.

As always, seems like a good opportunity to use a microcontroller. What can't they do? With a really basic micro and a bit of circuitry we should be able to make something that operates over a varied input range, performs two output functions, and has adjustable brightness. And some sort of short circuit protection... cause that's part of the original problem.... water.

Until next time....

Tuesday, August 2, 2016

MIDI Keyboard Controller, Part 4

So, I was supposed to be continuing on with the MIDI keyboard, but I've had to press the big red button. Turns out I want this to be a USB keyboard now (still MIDI, but plug right into the PC). So that means a new microcontroller, somewhat new layout, using USB power, and a bunch of firmware redesign. I'm going to do a PCB spin this time around to hopefully speed up prototyping. Now its the USB-MIDI Keyboard Controller!

Monday, August 1, 2016

The Disalarmer! (Part 2)

I'm just going to dive right in. Be sure to have read Part 1, and maybe even have it open for reference. So continuing the discussion where we left off, I mentioned there is a potential issue of feeding 5V into the 3,3V power rail.
Now normally this wouldn't be a huge deal. Perhaps isn't even in this case. When one of the buttons is pushed, current flows into the 1K resistor, thru ESD1 and into the 3.3V power rail. So you'd expect that current (without D1 or R2) to to be something like:  $\frac{V_{B+}-V_{ESD1}-V_{CC}}{1K} = \frac{5V-.3V-3.3V}{1K} = 1.4mA$. Not a lot, right? But that 3.3V supply isn't magic. Let's take a look at the output of our regulator:
It only lets current into the circuit, it doesn't let any out. Those two resistors between the output and ground measure out to about $125K\Omega$. Without D1 & R2, I was finding the 3.3V rail would rise above 4V whenever a button was pressed. The only current paths were back into the regulator and whatever draw the microcontroller had.
The circuit without D1 & R2
So by adding a 3.3V zener and a small resistor (so it wouldn't really turn on in normal operation), I'm able to keep the voltage from rising too much. Interestingly though, the fact that the voltage was above the "safe" operating voltage may not have been too dangerous for the MCU. The 1K resistor between the button and the input limits any current into the circuit to 5mA, and that's if the microcontroller tried to short anything to ground.

Also, there was probably an easier solution had I not already soldered things and had better tools to undo my work: just use diodes on the outputs instead of  resistors (I was expecting active low, not high). They'd block the 5V from feeding into the 3.3V circuit and ultimately reduce idle current (the zener draws a couple mA all the time, so sleeping the micro doesn't even buy much). The only drawback might be the diode drop means you're only supply 2.6V to the input of the clock's processor, which is expecting 5V, and I don't know the logic type of those inputs.

But anyhow, that's not a super exciting discussion. It is however, all that's needed for this project to work. There's a little bit of firmware involved:

  •  It has to monitor the UART for a "snooze\r\n" or a "off\r\n" from the bluetooth module and act accordingly. Throw away all other messages.
  • It has a timer set for about 9 minutes (the sleep time of the clock is 8 minutes). This allows disabling the alarm between snoozes. Whenever the buzzer goes, the timer is reset.
  • After the timer is up, turn off the bluetooth and go to sleep.

That's almost the last thing. We need a phone app. While I've used the various incarnations of the Android studio or SDK's in the past, I actually did this one in Visual Studio. Nothing too fancy. A giant "snooze" button that's easy to hit. A smaller "off" button that requires a long press to activate, and a sort of "reset" button. The app stores the address of a paired bluetooth device so there is no need to fumble thru choices everytime you launch it. It recalls the stored device and opens a socket, no questions asked. Should the need to use a new paired address arise, then the "reset" button just clears it, and the app will then ask.

At this time, I've had this working in the alarm clock for a few months now, pretty much trouble free. Not the most important project I've ever done, or necessary, or all that worthwhile, but still a lot of fun. It was a small and compact project that spanned embedded to Android. Here's a little clip of it in action:
(the app loads slow because.... well I was filming, so something had to not work normally. The blinking on the alarm clock shows that the alarm is still active, after long pressing the "off" button, the blinking stops as the alarm is disabled)

Sunday, June 5, 2016

The Disalarmer! (Part 1)

Occasionally you get an idea that is way too much work and complication to justify going thru with it. This is one such idea, but a good learning experience. It should also lead to another, hopefully more worthwhile project.

I have an old Sony alarm clock from maybe junior high that I still use to this day as it has a nice loud and obnoxious alarm that is guaranteed to wake you up. But it has a drawback... it's loud and obnoxious. Due to it having a combo CD-player it actually has a decent pair of speakers and a some amplification. So, what is there to fix?

Well truly, not that often. Again, this idea is not worth the effort. I'm typically the person who hits the snooze a few times in the morning. On occasion, I'll get up with the intent to start my day, but ultimately end up on the couch.... And then the alarm goes off from the bedroom. What to do?

Let's Bluetooth the alarm clock.

The Plan

First we should define our main objectives:
  • Emulate the "snooze" button
  • Emulate the "off" button
  • The circuit should sleep while the alarm is not going off
  • The Bluetooth module should be easily removable, so that I can use it for other projects if I need it.
Like most things we'll probably need a microcontroller (I've chosen the PIC12F1572). Nothing fancy, just a few I/O. We're gonna want to tie into the physical buttons, and mimic a button press.We're also gonna want to listen to some signal indicating that the alarm is currently sounding.

Connection to the Clock

So where to start? Well, fortunately, there is a service manual, complete with schematics and drawings. Shown below is how the circuitry for the buttons on the alarm clock.

The button inputs to the processor for the alarm clock are pulled low thru 100K resistors to the ground. Pushing the button puts 5V across the 10K and 100K voltage divider (essentially 5V goes to the clock input).
$V_{IN} = V_{cc}\frac{R445}{R445+R456}=5V\frac{100K}{100K+10K}=4.55V$
The capacitor is just there for debouncing, it smooths out the transition from low to high and back down as the mechanical button is pushed. We can more or less ignore it for our intuitive analysis.
Controlling the input ourselves is as simple as emulating the button. Just make one of our microcontroller lines drive high.
A 1K resistor is added just to protect the output driver of the MCU. I actually should have done something different... but we'll get into that a little later.

The buzzer signal is also going to be an easy tie-in (although tough to solder to such a small area). I hooked it up to the scope to get a look at it, a series of 5V pulses at the frequency of the alarm. Perfect for triggering an interrupt on change pin. Instead of a 1K resistor like the buttons, I threw in a 10K. Shown below is the buzzer portion of the clock schematic, where I soldered in after R428. And that is all our signal connetions. 5V was picked up near the buttons above, and ground was everywhere.
Buzzer 5V square pulses

Buzzer has five quick beeps followed by a long pause.

Other bits

We still need to incorporate a Bluetooth serial module. The particular module I have is some kind of BC04 variant (same pinouts). All it really needs is power and serial connections. That's it. And in our case we only need to receive data from the Bluetooth module. On power up it goes right into pairing, and defaults to 9600 baud. You can literally just connect with your phone and start sending serial data. However, it does run off of 3.3V, and is the most power hungry part of the Disalarmer. We'll need a voltage regulator, and some way of switching power to it.

Before I show the finished circuit, I just wanted to bring up a minor hiccup. The microcontroller I had turns out to be a PIC12LF1572; that 'L' indicating low voltage. Not a huge deal as we already need a 3.3V regulator. But the 5V clock signals present a little bit of a challenge. But first, the schematic.
Switching the Bluetooth module on and off is as simple as a PNP transistor. Pull the base to ground and you've got power. Outputs to the buttons are just thru series resistors R4 & R7. Drive high to "push" the snooze or off buttons, and tri-state them otherwise.

You may also have noticed D1 and R2. These are my mediocre solution to the 5V input problem. But this is enough for now. We'll look at interfacing the 3.3V and 5V circuits in the next post.