Using 5v To switch 12v supply? (ATtiny in car use)

Hi guys, This is a tricky one that has me confused!

Basically I have used my arduino to program a little ATtiny85 that turns On/off Supply to a pin (LED's to be attached) With the function of being able to adjust the brightness by holding the button down (As explained in getting started with arduino book example 5).

I have it all working to the stage where I can turn on and off and vary brightness of some 3v led's without the use of resistors, BUT I designed the circuit for use in my car where I wanted to use 12v LED strips. The input voltage for the ATtiny is 3.2 - 5.5 V so I will have to use a voltage regulator to limit that (I forgot how to do this exactly to what I need! :P) . The problem arises where I am not sure if it is possible to effectively Upscale the voltage from the pin to 12v (Whilst still being able to adjust voltage to vary brightness) Or to use the ATtiny's output to effectively switch on and alter the 12v supply seperately.
I think it may be possible with some kind of transistor but this one truly has me confused!

That probably sounded like a load of nonsense so I professionally made up a BS8888 Standard Engineeirng drawing for you below :wink: (The wonders of paint! :P)

I would greatly appreciate any help you could give me... I'm truly stuck! :smiley:

Thankyou and happy modding!
¬Al

EDIT: I just noticed I forgot to connect the other pin of the Button to the +VCC on my expert diagram hahaha, assume it is connected :stuck_out_tongue:

First off, you should never be using an component LED without a resistor.

Yes, you can use a transistor to switch on and off the LED strip, but the strip would still be powered of the 12V, not the 5V supply of the MCU. The specific transistor and how to bias it would depend on the current the LED strip draws. So you need the specs of the particular LED strip.

For your regulator dilemma, look up the datasheet for a LM78L05.

You also need capacitors across the input and output of the regulator, that middle pin goes to ground.

Thanks for the replies,
The Forward Voltage is 12v
The Forward Current 20mA
More info here : http://www.phenoptix.com/index.php/led-tape/blue-12v-led-strip-5cm-smd-leds-flexible-tape.html
I believe That If I use an LM317 Voltage Regulator with Resistor values of R1= 330 Ohms And R2 = 1kOhm I can get a moderately accurate 5.04v Supply to the ATtiny.

As for the transistor, I'm still confused But I have been told I can use an NPN 2N3904 General Purpose tranistor with specific resistors to do this, not entirely sure how though as I effectively need two inputs (12v Supply for the LED Strips, and 0 - 5V Variable supply from the ATTiny ouput pin) with one output to the LED's that varies relatively to the ATTiny.
I know this is possible due to a transistor obviously being and acting as a transforming resistor; effectively raising the voltage but keeping the proportions.. A bit more help would be appreciated! :smiley:

And as for the Resistors on the LED's, the 12v Strips I am using have built in SMD resistors.

Driving an LED strip is just like driving a motor, or a relay. Look at the first two circuits here:-
http://www.thebox.myzen.co.uk/Workshop/Motors_1.html

If I use an LM317 Voltage Regulator

But if you used a fixed voltage regulator then you would not have to bother. There are hundreds to choose from here:-

effectively raising the voltage but keeping the proportions

No that is not how transistors work, see:-
http://www.kpsec.freeuk.com/trancirc.htm

and 0 - 5V Variable supply from the ATTiny ouput pin)

No you don't which is just as well as you can't get such a thing. Do you want to dim the LEDs?
If so they might respond to a PWM output, this is not a variable voltage output but a digital signal:-
http://www.thebox.myzen.co.uk/Tutorial/PWM.html

Have a look at the attached schematic. This should work for you.

Hey guys, Thanks for all the help...
BillO that looks like a KI cad shcematic!? :stuck_out_tongue:
I was hoping to get a couple of mini pcb's produced to make the finish more professional and tidy, I was wandering if you could send me the KI cad files for that schematic so I can convert it to a PCB layout?
That would be awesome! :smiley:
I have just ordered the specific components you stated, so I'll breadboard a circuit when I get them in the post and If it all works I'll send off for a pcb or two! (I could send you one if you like, as a thankyou for helping?)
Anyway... let me know! :smiley:
¬Al

It was drawn with DipTrace, but I only retained in JPEG form, sorry. It should take not take much time to enter it into some other application though.

If you're stuck and can wait till next week I can re-do it and even do a lay-out and produce the Gerber files. But I'm pretty busy until next Monday.

Finally got the last of the components for this today, and I just built a breadboard circuit. It works... BUT It's working as if it's reading the reverse of the button, eg it thinks its being pressed when its not, and when its not being pressed it thinks it is!
Because I have it set to a momentary press to turn on/off, that bit is not affected due to a simple state change in the program, But what it does is it stays on and changes brightness as if you are holding the button! :S I think I have wired it up correctly... Here's a photo of my breadboard circuit... Can you spot anything i've done wrong? I have double checked! :L
And as regards to the circuit schematic, thanks for the offer that is very kind of you, but i'll have an attempt at it in KiCad then post it here for you guys to double check :stuck_out_tongue:

Cheers again,
Al

Almurray42:
Here's a photo of my breadboard circuit...

Where?

Oops! Forgot to paste the links! :L Sorry!

If I were you I'd ditch the transistor and use a relay. Relays are simpler to wire, they already come made for auto use, and they generally can handle more current than a transistor. That is unless you are looking to dim the leds with pwm, because relays aren't that fast at switching.

You'd need a transistor to drive the relay, so no, do not ditch the transistor. It's just fine.

In this circuit, the input at pin2 goes low when the button is pressed, and from what I can see that looks correct. Does the code expect the input to be active low or high?

BillO:
You'd need a transistor to drive the relay, so no, do not ditch the transistor. It's just fine.

In this circuit, the input at pin2 goes low when the button is pressed, and from what I can see that looks correct. Does the code expect the input to be active low or high?

Right, The code waits for the button input to be high, so I think that's where the problem could be... I can paste the code if you need?
And I really appreciate your help on this by the way! :smiley:

Sure, post the code and lets have a look.

// TURNS ON LED WHEN THE BUTTON IS PRESSED
// Includes Debouncing
// If button is held, Brightness changes

const int LEDS = 9; //Led pin
const int BUTTON = 7; // Input for button

int val = 0; //Stores value of button

int old_val = 0; //Stores previous value of button
int state = 0; // = LED off 1 = LED on

int brightness = 128; //Stores brightness
unsigned long startTime = 0; //When did we begin pressing?

void setup() {
  pinMode(LEDS,OUTPUT); //Sets Leds as output
  pinMode(BUTTON,INPUT); //Sets button as input
}

void loop() {
  val = digitalRead(BUTTON); // Reads button and stores
  if((val == HIGH) && (old_val == LOW)) {
    state = 1 - state; //Changes state of button
    
    startTime = millis();
    delay(10);
  }
  if ((val == HIGH) && (old_val == HIGH)) { //if the button is held for more than 500ms
  if (state == 1 && (millis() - startTime) > 500) {
    brightness++;
    delay(10);
    
    if (brightness > 255) {
      brightness = 0;
    }
  }
  }
  
  old_val = val; //Store new value
  
  if (state == 1) {
    analogWrite(LEDS, brightness); //Turn the LED ON at current brightness level
  } else {
    analogWrite(LEDS, 0); // Tun LED OFF
  }
}

Try this:

// TURNS ON LED WHEN THE BUTTON IS PRESSED
// Includes Debouncing
// If button is held, Brightness changes

const int LEDS = 9; //Led pin
const int BUTTON = 7; // Input for button

int val = 0; //Stores value of button

int old_val = HIGH; //Stores previous value of button
int state = 0; // = LED off 1 = LED on

int brightness = 128; //Stores brightness
unsigned long startTime = 0; //When did we begin pressing?

void setup() {
  pinMode(LEDS,OUTPUT); //Sets Leds as output
  pinMode(BUTTON,INPUT); //Sets button as input
}

void loop() {
  val = digitalRead(BUTTON); // Reads button and stores
  if((val == LOW) && (old_val == HIGH)) {
    state = 1 - state; //Changes state of button
    startTime = millis();
    delay(10);
  }

  if ((val == LOW) && (old_val == LOW)) { //if the button is held for more than 500ms
  if (state == 1 && (millis() - startTime) > 500) {
    brightness++;
    delay(10);
    
    if (brightness > 255) {
      brightness = 0;
    }
  }
  }
  
  old_val = val; //Store new value
  
  if (state == 1) {
    analogWrite(LEDS, brightness); //Turn the LED ON at current brightness level
  } else {
    analogWrite(LEDS, 0); // Tun LED OFF
  }
}

The posted circiut could supply several amps if you directly exchanged the '3904 for na IRFLXXX type Mosfet (the 'L' means Logic Level or proper operation from a 5V supply to switch a 12V load (most Mosfets aren't fully enhanced or turned on @ < + 8 Vgs. The Logic Level are fully turned on or enhanced @ ~ 2 - 3V Vgs. IMO and some more food for thought...

Doc

No Dice! :frowning:

Are you sure you have the pin designations right?